Skip to content

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mattzcarey committed Nov 27, 2023
1 parent 73e6809 commit 06aaee6
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 52 deletions.
17 changes: 0 additions & 17 deletions services/core/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,6 @@ module.exports = {
},
],
"prefer-const": "error",
"import/order": [
"error",
{
groups: [
["external", "builtin"],
"unknown",
"internal",
["parent", "sibling", "index"],
],
alphabetize: {
order: "asc",
caseInsensitive: false,
},
"newlines-between": "always",
pathGroupsExcludedImportTypes: ["builtin"],
},
],
"padding-line-between-statements": [
"error",
{
Expand Down
12 changes: 2 additions & 10 deletions services/core/functions/webhook/src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { Context, Probot } from "probot";

import { loadChat } from "./chat/loadChat";
import { getFilesWithChanges } from "./review/changes/getFilesWithChanges";
import { collectAllReviews } from "./review/comment/collectAllReviews";
import { filterReviews } from "./review/comment/filterReviews";
import { postReviews } from "./review/comment/postReviews";
import { review } from "./pr";

export const app = (app: Probot): void => {
app.on(
Expand All @@ -16,12 +13,7 @@ export const app = (app: Probot): void => {
async (context: Context<"pull_request">): Promise<void> => {
const chat = await loadChat(context);

const { files, commits } = await getFilesWithChanges(context);
const allReviews = await collectAllReviews(files, chat);

const topReviews = filterReviews(allReviews);

await postReviews(context, topReviews, commits);
await review(context, chat);

console.info(
`Successfully reviewed PR #${context.pullRequest().pull_number}`
Expand Down
19 changes: 19 additions & 0 deletions services/core/functions/webhook/src/pr/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Context } from "probot";

import { Chat } from "../chat/chat";
import { getFilesWithChanges } from "./review/changes/getFilesWithChanges";
import { collectAllReviews } from "./review/collectAllReviews";
import { filterReviews } from "./review/filterReviews";
import { postReviews } from "./review/postReviews";

export const review = async (
context: Context<"pull_request">,
chat: Chat
): Promise<void> => {
const { files, commits } = await getFilesWithChanges(context);
const allReviews = await collectAllReviews(files, chat);

const topReviews = filterReviews(allReviews);

await postReviews(context, topReviews, commits);
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { extname } from "path";

import { excludedKeywords, supportedFiles } from "../../constants";
import { ChangedFile } from "../../types";
import { excludedKeywords, supportedFiles } from "../../../constants";
import { ChangedFile } from "../../../types";

export const filterFiles = (
changedFiles: string[],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Context } from "probot";

import { ChangedFile, Commit } from "../../types";
import { ChangedFile, Commit } from "../../../types";
import { filterFiles } from "./filterFiles";

// This function retrieves files with changes for a given pull request
Expand All @@ -10,8 +10,6 @@ export const getFilesWithChanges = async (
const { owner, repo } = context.repo();
const pullRequest = context.payload.pull_request;

console.debug(`Fetching files with changes for PR #${pullRequest.number}`);

// Fetch comparison data for the entire pull request
const comparisonData = await fetchComparisonData(
context,
Expand All @@ -22,7 +20,7 @@ export const getFilesWithChanges = async (
);

if (!comparisonData.files) {
throw new Error(`No files to review ${JSON.stringify(comparisonData)}`);
throw new Error("No files to review");
}

let changedFilesInLastCommit: string[] = [];
Expand All @@ -47,10 +45,6 @@ export const getFilesWithChanges = async (
);
changedFilesInLastCommit =
lastCommitData.files?.map((file) => file.filename) || [];

console.debug(
`Files changed in last commit: ${changedFilesInLastCommit.toString()}`
);
}

const filteredFiles = filterFiles(
Expand All @@ -59,7 +53,7 @@ export const getFilesWithChanges = async (
);

if (!filteredFiles.length) {
throw new Error("No files to review: all files hav been filtered out.");
throw new Error("No files to review: all files are filtered out.");
}

return { files: filteredFiles, commits: comparisonData.commits };
Expand All @@ -79,14 +73,7 @@ const fetchComparisonData = async (
head,
});

//for each file check that the patch is not empty, if it is remove the file from the list
data.files = data.files?.filter((file) => file.patch !== undefined);

if (!data.files) {
throw new Error("No files to review");
}

return { files: data.files as ChangedFile[], commits: data.commits };
return { files: data.files, commits: data.commits };
};

const isSynchronizeAction = (context: Context<"pull_request">): boolean =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ import { Chat } from "../../chat/chat";
import { ReviewFile } from "../../types";

export const collectAllReviews = async (
files: { patch: string; filename: string }[],
files: { patch?: string; filename: string }[],
chat: Chat
): Promise<ReviewFile[]> => {
let allReviews: ReviewFile[] = [];
for (const file of files) {
const patch: string = file.patch;
if (!patch) {
if (!file.patch) {
continue;
}

try {
const reviewArray = await chat.getReview(patch);
const reviewArray = await chat.getReview(file.patch);

console.debug(`Reviewing ${file.filename}`);

Expand All @@ -24,7 +23,7 @@ export const collectAllReviews = async (
// Add the filename and patch to the review data
reviewArray.forEach((review) => {
review.filename = file.filename;
review.patch = patch;
review.patch = file.patch;
});

allReviews = allReviews.concat(reviewArray);
Expand Down
2 changes: 1 addition & 1 deletion services/core/functions/webhook/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface ChangedFile {
blob_url: string;
raw_url: string;
contents_url: string;
patch: string;
patch?: string;
previous_filename?: string | undefined;
}

Expand Down

0 comments on commit 06aaee6

Please sign in to comment.