Skip to content

Commit

Permalink
Only show app diffs related to current PR (#50)
Browse files Browse the repository at this point in the history
* filter by file changes

* remove comments
  • Loading branch information
aaron-weisberg-qz authored Aug 21, 2024
1 parent 00ffb25 commit 5df4531
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
42 changes: 41 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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++) {
Expand Down
44 changes: 42 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ async function getApps(): Promise<App[]> {
} 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 (
Expand All @@ -116,6 +116,13 @@ async function getApps(): Promise<App[]> {
) && targetPrimary
);
});

const changedFiles = await getChangedFiles();
console.log(`Changed files: ${changedFiles.join(', ')}`);
const appsAffected = repoApps.filter(app => {
return partOfApp(changedFiles, app)
});
return appsAffected;
}

interface Diff {
Expand Down Expand Up @@ -221,6 +228,39 @@ _Updated at ${new Date().toLocaleString('en-US', { timeZone: 'America/Los_Angele
}
}

async function getChangedFiles(): Promise<string[]> {
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<T>(
array: T[],
callback: (item: T, i: number, arr: T[]) => Promise<void>
Expand Down

0 comments on commit 5df4531

Please sign in to comment.