Skip to content

Commit

Permalink
fix: types
Browse files Browse the repository at this point in the history
  • Loading branch information
mattzcarey committed Dec 12, 2023
1 parent fe58cbe commit fb9bce6
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 62 deletions.
36 changes: 20 additions & 16 deletions services/core/functions/webhook/src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Context, Probot } from "probot";

import { loadChat } from "./controllers/chat/loadChat";
import { review } from "./pr/review";
import { loadPR, loadQA } from "./controllers/chat/load";
import { qa } from "./pr/qa";
import { review } from "./pr/review";

export const app = (app: Probot): void => {
app.on(
Expand All @@ -11,8 +11,14 @@ export const app = (app: Probot): void => {
"pull_request.synchronize",
"pull_request.reopened",
],
async (context: Context<"pull_request">): Promise<void> => {
const chat = await loadChat(context);
async (
context: Context<
| "pull_request.opened"
| "pull_request.synchronize"
| "pull_request.reopened"
>
): Promise<void> => {
const chat = await loadPR(context);

await review(context, chat);

Expand All @@ -23,18 +29,16 @@ export const app = (app: Probot): void => {
);

//chat with bot about PR
app.on([
"pull_request_review_thread",
], async (context: Context<"pull_request_review_thread">): Promise<void> => {
const chat = await loadChat(context);

await qa(context, chat);

console.info(
`Successfully answered question in PR #${context.payload.pull_request.number}`
);


app.on(
["pull_request_review_thread"],
async (context: Context<"pull_request_review_thread">): Promise<void> => {
const chat = await loadQA(context);

await qa(context, chat);

console.info(
`Successfully answered question in PR #${context.payload.pull_request.number}`
);
}
);
};
52 changes: 52 additions & 0 deletions services/core/functions/webhook/src/controllers/chat/load.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { Context } from "probot";

import { AIModel } from "../ai";
import { Chat } from "./chat";
import { getAPIKeyFromGH } from "./variables/apiKey";
import { getOptionalVariablesFromGH } from "./variables/optional";

export const loadPR = async (
context: Context<
"pull_request.opened" | "pull_request.synchronize" | "pull_request.reopened"
>
): Promise<Chat> => {
// Get variables from GitHub
// @ts-ignore
const apiKey = await getAPIKeyFromGH(context);
const optionalVariables = await getOptionalVariablesFromGH(
["OPENAI_MODEL_NAME", "TEMPERATURE"],
context
);

const ai = new AIModel({
modelName: optionalVariables.OPENAI_MODEL_NAME ?? "gpt-4-1106-preview",
apiKey,
temperature: optionalVariables.temperature
? parseFloat(optionalVariables.temperature)
: 0,
});

return new Chat(ai);
};

export const loadQA = async (
context: Context<"pull_request_review_thread">
): Promise<Chat> => {
// Get variables from GitHub
const apiKey = await getAPIKeyFromGH(context);
const optionalVariables = await getOptionalVariablesFromGH(
["OPENAI_MODEL_NAME", "TEMPERATURE"],
context
);

const ai = new AIModel({
modelName: optionalVariables.OPENAI_MODEL_NAME ?? "gpt-4-1106-preview",
apiKey,
temperature: optionalVariables.temperature
? parseFloat(optionalVariables.temperature)
: 0,
});

return new Chat(ai);
};
36 changes: 0 additions & 36 deletions services/core/functions/webhook/src/controllers/chat/loadChat.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { Context } from "probot";

export const getAPIKeyFromGH = async (context: Context): Promise<string> => {
export const getAPIKeyFromGH = async (
context: Context<
| "pull_request.opened"
| "pull_request.synchronize"
| "pull_request.reopened"
| "pull_request_review_thread"
>
): Promise<string> => {
const repo = context.repo();

try {
Expand All @@ -23,9 +30,7 @@ export const getAPIKeyFromGH = async (context: Context): Promise<string> => {
repo: repo.repo,
owner: repo.owner,
issue_number: context.pullRequest().pull_number,
body: `@${
repo.owner as string
} I can't access your OPENAI_API_KEY. This is set in your GitHub repository at Settings/Actions/Repository Variables/Secrets. Please contact the repository owner to set this up.`,
body: `@${repo.owner} I can't access your OPENAI_API_KEY. This is set in your GitHub repository at Settings/Actions/Repository Variables/Secrets. Please contact the repository owner to set this up.`,
});

throw new Error("Error fetching OPENAI_API_KEY");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ interface GHVariables {
}
export const getOptionalVariablesFromGH = async (
variables: string[],
context: Context
context: Context<
| "pull_request.opened"
| "pull_request.synchronize"
| "pull_request.reopened"
| "pull_request_review_thread"
>
): Promise<GHVariables> => {
const repo = context.repo();
const variablesData: GHVariables = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { filterFiles } from "./filterFiles";

// This function retrieves files with changes for a given pull request
export const getFilesWithChanges = async (
context: Context<"pull_request">
context: Context<
"pull_request.opened" | "pull_request.synchronize" | "pull_request.reopened"
>
): Promise<{ files: ChangedFile[]; commits: Commit[] }> => {
const { owner, repo } = context.repo();
const pullRequest = context.payload.pull_request;
Expand Down Expand Up @@ -60,7 +62,9 @@ export const getFilesWithChanges = async (
};

const fetchComparisonData = async (
context: Context<"pull_request">,
context: Context<
"pull_request.opened" | "pull_request.synchronize" | "pull_request.reopened"
>,
owner: string,
repo: string,
base: string,
Expand All @@ -76,7 +80,10 @@ const fetchComparisonData = async (
return { files: data.files, commits: data.commits };
};

const isSynchronizeAction = (context: Context<"pull_request">): boolean =>
context.payload.action === "synchronize";
const isSynchronizeAction = (
context: Context<
"pull_request.opened" | "pull_request.synchronize" | "pull_request.reopened"
>
): boolean => context.payload.action === "synchronize";

const hasMultipleCommits = (commits: Commit[]): boolean => commits.length > 1;
4 changes: 3 additions & 1 deletion services/core/functions/webhook/src/pr/review/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import { filterReviews } from "./filterReviews";
import { postReviews } from "./postReviews";

export const review = async (
context: Context<"pull_request">,
context: Context<
"pull_request.opened" | "pull_request.synchronize" | "pull_request.reopened"
>,
chat: Chat
): Promise<void> => {
const { files, commits } = await getFilesWithChanges(context);
Expand Down

0 comments on commit fb9bce6

Please sign in to comment.