Skip to content

Commit

Permalink
filter by file changes
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron-weisberg-qz committed Aug 19, 2024
1 parent 00ffb25 commit 1870d30
Show file tree
Hide file tree
Showing 2 changed files with 90 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,19 @@ 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 changedInThisPR = repoApps.filter(app => {
const appPath = app.spec.source.path;
return isInArgoCDSourceRepo(changedFiles, appPath);
});
return changedInThisPR;
});
}
function postDiffComment(diffs) {
Expand Down Expand Up @@ -1855,6 +1863,38 @@ _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 isInArgoCDSourceRepo(changedFiles, argoCdRepoPath) {
const appPath = getFirstTwoDirectories(path.normalize(argoCdRepoPath));
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
51 changes: 49 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,14 @@ async function getApps(): Promise<App[]> {
) && targetPrimary
);
});

const changedFiles = await getChangedFiles();
console.log(`Changed files: ${changedFiles.join(', ')}`);
const changedInThisPR = repoApps.filter(app => {
const appPath = app.spec.source.path
return isInArgoCDSourceRepo(changedFiles, appPath)
});
return changedInThisPR;
}

interface Diff {
Expand Down Expand Up @@ -221,6 +229,45 @@ _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 isInArgoCDSourceRepo(changedFiles: string[], argoCdRepoPath: string): boolean {
const appPath = getFirstTwoDirectories(path.normalize(argoCdRepoPath));

return changedFiles.some(file => {
const normalizedFilePath = path.normalize(file);
return normalizedFilePath.startsWith(appPath);
});
}

function getFirstTwoDirectories(filePath: string): string {
// 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);
}

async function asyncForEach<T>(
array: T[],
callback: (item: T, i: number, arr: T[]) => Promise<void>
Expand Down

0 comments on commit 1870d30

Please sign in to comment.