From 5df453135440faf3ae11de14351ad92a391fa004 Mon Sep 17 00:00:00 2001 From: Aaron Weisberg <144726311+aaron-weisberg-qz@users.noreply.github.com> Date: Wed, 21 Aug 2024 11:57:44 -0700 Subject: [PATCH] Only show app diffs related to current PR (#50) * filter by file changes * remove comments --- dist/index.js | 42 +++++++++++++++++++++++++++++++++++++++++- src/main.ts | 44 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 83 insertions(+), 3 deletions(-) diff --git a/dist/index.js b/dist/index.js index d5c85aa..0402410 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1762,11 +1762,18 @@ function getApps() { catch (e) { core.error(e); } - return responseJson.items.filter(app => { + const apps = responseJson.items; + const repoApps = apps.filter(app => { const targetRevision = app.spec.source.targetRevision; const targetPrimary = targetRevision === 'master' || targetRevision === 'main' || !targetRevision; return (app.spec.source.repoURL.includes(`${github.context.repo.owner}/${github.context.repo.repo}`) && targetPrimary); }); + const changedFiles = yield getChangedFiles(); + console.log(`Changed files: ${changedFiles.join(', ')}`); + const appsAffected = repoApps.filter(app => { + return partOfApp(changedFiles, app); + }); + return appsAffected; }); } function postDiffComment(diffs) { @@ -1855,6 +1862,39 @@ _Updated at ${new Date().toLocaleString('en-US', { timeZone: 'America/Los_Angele } }); } +function getChangedFiles() { + return __awaiter(this, void 0, void 0, function* () { + const { owner, repo } = github.context.repo; + const pull_number = github.context.issue.number; + const listFilesResponse = yield octokit.rest.pulls.listFiles({ + owner, + repo, + pull_number + }); + const changedFiles = listFilesResponse.data.map(file => file.filename); + return changedFiles; + }); +} +function partOfApp(changedFiles, app) { + const sourcePath = path.normalize(app.spec.source.path); + const appPath = getFirstTwoDirectories(sourcePath); + return changedFiles.some(file => { + const normalizedFilePath = path.normalize(file); + return normalizedFilePath.startsWith(appPath); + }); +} +function getFirstTwoDirectories(filePath) { + // Normalize the path to handle any inconsistencies + const normalizedPath = path.normalize(filePath); + // Split the path into parts based on the OS-specific separator + const parts = normalizedPath.split(path.sep).filter(Boolean); // filter(Boolean) removes empty strings + // Check if the path has at least two segments + if (parts.length < 2) { + return parts.join(path.sep); // Return the entire path if less than two directories + } + // Join the first two segments + return parts.slice(0, 2).join(path.sep); +} function asyncForEach(array, callback) { return __awaiter(this, void 0, void 0, function* () { for (let index = 0; index < array.length; index++) { diff --git a/src/main.ts b/src/main.ts index 28fd028..b184021 100644 --- a/src/main.ts +++ b/src/main.ts @@ -106,8 +106,8 @@ async function getApps(): Promise { } catch (e) { core.error(e); } - - return (responseJson.items as App[]).filter(app => { + const apps = responseJson.items as App[] + const repoApps = apps.filter(app => { const targetRevision = app.spec.source.targetRevision const targetPrimary = targetRevision === 'master' || targetRevision === 'main' || !targetRevision return ( @@ -116,6 +116,13 @@ async function getApps(): Promise { ) && targetPrimary ); }); + + const changedFiles = await getChangedFiles(); + console.log(`Changed files: ${changedFiles.join(', ')}`); + const appsAffected = repoApps.filter(app => { + return partOfApp(changedFiles, app) + }); + return appsAffected; } interface Diff { @@ -221,6 +228,39 @@ _Updated at ${new Date().toLocaleString('en-US', { timeZone: 'America/Los_Angele } } +async function getChangedFiles(): Promise { + const { owner, repo } = github.context.repo; + const pull_number = github.context.issue.number; + + const listFilesResponse = await octokit.rest.pulls.listFiles({ + owner, + repo, + pull_number + }); + + const changedFiles = listFilesResponse.data.map(file => file.filename); + return changedFiles; +} + +function partOfApp(changedFiles: string[], app: App): boolean { + const sourcePath = path.normalize(app.spec.source.path); + const appPath = getFirstTwoDirectories(sourcePath); + + return changedFiles.some(file => { + const normalizedFilePath = path.normalize(file); + return normalizedFilePath.startsWith(appPath); + }); +} + +function getFirstTwoDirectories(filePath: string): string { + const normalizedPath = path.normalize(filePath); + const parts = normalizedPath.split(path.sep).filter(Boolean); // filter(Boolean) removes empty strings + if (parts.length < 2) { + return parts.join(path.sep); // Return the entire path if less than two directories + } + return parts.slice(0, 2).join(path.sep); +} + async function asyncForEach( array: T[], callback: (item: T, i: number, arr: T[]) => Promise