From 06aaee661d029b56caf1d2b75243ee5b7ea6f2e3 Mon Sep 17 00:00:00 2001 From: Matt <77928207+mattzcarey@users.noreply.github.com> Date: Mon, 27 Nov 2023 15:03:55 +0200 Subject: [PATCH] chore: refactor --- services/core/.eslintrc.js | 17 --------------- services/core/functions/webhook/src/bot.ts | 12 ++--------- .../core/functions/webhook/src/pr/index.ts | 19 +++++++++++++++++ .../{ => pr}/review/changes/filterFiles.ts | 4 ++-- .../review/changes/getFilesWithChanges.ts | 21 ++++--------------- .../review}/collectAllReviews.ts | 9 ++++---- .../comment => pr/review}/filterReviews.ts | 0 .../comment => pr/review}/findLineInPatch.ts | 0 .../review}/formatReviewComment.ts | 0 .../comment => pr/review}/postReviews.ts | 0 services/core/functions/webhook/src/types.ts | 2 +- 11 files changed, 32 insertions(+), 52 deletions(-) create mode 100644 services/core/functions/webhook/src/pr/index.ts rename services/core/functions/webhook/src/{ => pr}/review/changes/filterFiles.ts (75%) rename services/core/functions/webhook/src/{ => pr}/review/changes/getFilesWithChanges.ts (76%) rename services/core/functions/webhook/src/{review/comment => pr/review}/collectAllReviews.ts (78%) rename services/core/functions/webhook/src/{review/comment => pr/review}/filterReviews.ts (100%) rename services/core/functions/webhook/src/{review/comment => pr/review}/findLineInPatch.ts (100%) rename services/core/functions/webhook/src/{review/comment => pr/review}/formatReviewComment.ts (100%) rename services/core/functions/webhook/src/{review/comment => pr/review}/postReviews.ts (100%) diff --git a/services/core/.eslintrc.js b/services/core/.eslintrc.js index 988d5630..8650114f 100644 --- a/services/core/.eslintrc.js +++ b/services/core/.eslintrc.js @@ -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", { diff --git a/services/core/functions/webhook/src/bot.ts b/services/core/functions/webhook/src/bot.ts index 015b9371..0a4d8ab5 100644 --- a/services/core/functions/webhook/src/bot.ts +++ b/services/core/functions/webhook/src/bot.ts @@ -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( @@ -16,12 +13,7 @@ export const app = (app: Probot): void => { async (context: Context<"pull_request">): Promise => { 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}` diff --git a/services/core/functions/webhook/src/pr/index.ts b/services/core/functions/webhook/src/pr/index.ts new file mode 100644 index 00000000..2e50c517 --- /dev/null +++ b/services/core/functions/webhook/src/pr/index.ts @@ -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 => { + const { files, commits } = await getFilesWithChanges(context); + const allReviews = await collectAllReviews(files, chat); + + const topReviews = filterReviews(allReviews); + + await postReviews(context, topReviews, commits); +}; diff --git a/services/core/functions/webhook/src/review/changes/filterFiles.ts b/services/core/functions/webhook/src/pr/review/changes/filterFiles.ts similarity index 75% rename from services/core/functions/webhook/src/review/changes/filterFiles.ts rename to services/core/functions/webhook/src/pr/review/changes/filterFiles.ts index db6abfd5..033e5956 100644 --- a/services/core/functions/webhook/src/review/changes/filterFiles.ts +++ b/services/core/functions/webhook/src/pr/review/changes/filterFiles.ts @@ -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[], diff --git a/services/core/functions/webhook/src/review/changes/getFilesWithChanges.ts b/services/core/functions/webhook/src/pr/review/changes/getFilesWithChanges.ts similarity index 76% rename from services/core/functions/webhook/src/review/changes/getFilesWithChanges.ts rename to services/core/functions/webhook/src/pr/review/changes/getFilesWithChanges.ts index 44103d33..207e6b02 100644 --- a/services/core/functions/webhook/src/review/changes/getFilesWithChanges.ts +++ b/services/core/functions/webhook/src/pr/review/changes/getFilesWithChanges.ts @@ -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 @@ -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, @@ -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[] = []; @@ -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( @@ -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 }; @@ -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 => diff --git a/services/core/functions/webhook/src/review/comment/collectAllReviews.ts b/services/core/functions/webhook/src/pr/review/collectAllReviews.ts similarity index 78% rename from services/core/functions/webhook/src/review/comment/collectAllReviews.ts rename to services/core/functions/webhook/src/pr/review/collectAllReviews.ts index a01d87a8..4eb56970 100644 --- a/services/core/functions/webhook/src/review/comment/collectAllReviews.ts +++ b/services/core/functions/webhook/src/pr/review/collectAllReviews.ts @@ -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 => { 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}`); @@ -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); diff --git a/services/core/functions/webhook/src/review/comment/filterReviews.ts b/services/core/functions/webhook/src/pr/review/filterReviews.ts similarity index 100% rename from services/core/functions/webhook/src/review/comment/filterReviews.ts rename to services/core/functions/webhook/src/pr/review/filterReviews.ts diff --git a/services/core/functions/webhook/src/review/comment/findLineInPatch.ts b/services/core/functions/webhook/src/pr/review/findLineInPatch.ts similarity index 100% rename from services/core/functions/webhook/src/review/comment/findLineInPatch.ts rename to services/core/functions/webhook/src/pr/review/findLineInPatch.ts diff --git a/services/core/functions/webhook/src/review/comment/formatReviewComment.ts b/services/core/functions/webhook/src/pr/review/formatReviewComment.ts similarity index 100% rename from services/core/functions/webhook/src/review/comment/formatReviewComment.ts rename to services/core/functions/webhook/src/pr/review/formatReviewComment.ts diff --git a/services/core/functions/webhook/src/review/comment/postReviews.ts b/services/core/functions/webhook/src/pr/review/postReviews.ts similarity index 100% rename from services/core/functions/webhook/src/review/comment/postReviews.ts rename to services/core/functions/webhook/src/pr/review/postReviews.ts diff --git a/services/core/functions/webhook/src/types.ts b/services/core/functions/webhook/src/types.ts index 25c7d053..e89c72cd 100644 --- a/services/core/functions/webhook/src/types.ts +++ b/services/core/functions/webhook/src/types.ts @@ -15,7 +15,7 @@ export interface ChangedFile { blob_url: string; raw_url: string; contents_url: string; - patch: string; + patch?: string; previous_filename?: string | undefined; }