Skip to content

Commit

Permalink
check if there's a diff (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron-weisberg-qz authored Aug 19, 2024
1 parent c5cb57a commit 00ffb25
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
Binary file removed bin/argo
Binary file not shown.
24 changes: 22 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1780,8 +1780,13 @@ function postDiffComment(diffs) {
const sha = (_b = (_a = github.context.payload.pull_request) === null || _a === void 0 ? void 0 : _a.head) === null || _b === void 0 ? void 0 : _b.sha;
const commitLink = `https://github.com/${owner}/${repo}/pull/${github.context.issue.number}/commits/${sha}`;
const shortCommitSha = String(sha).substr(0, 7);
// const filteredDiffs = diffs
const filteredDiffs = diffs.map(diff => {
diff.diff = filterDiff(diff.diff);
return diff;
}).filter(d => d.diff !== '');
const prefixHeader = `## ArgoCD Diff on ${ENV}`;
const diffOutput = diffs.map(({ app, diff, error }) => `
const diffOutput = filteredDiffs.map(({ app, diff, error }) => `
App: [\`${app.metadata.name}\`](${protocol}://${ARGOCD_SERVER_URL}/applications/${app.metadata.name})
YAML generation: ${error ? ' Error 🛑' : 'Success 🟢'}
App sync status: ${app.status.sync.status === 'Synced' ? 'Synced ✅' : 'Out of Sync ⚠️ '}
Expand Down Expand Up @@ -1840,7 +1845,7 @@ _Updated at ${new Date().toLocaleString('en-US', { timeZone: 'America/Los_Angele
}
}
// Only post a new comment when there are changes
if (diffs.length) {
if (filteredDiffs.length) {
octokit.rest.issues.createComment({
issue_number: github.context.issue.number,
owner,
Expand Down Expand Up @@ -1895,6 +1900,21 @@ function run() {
}
});
}
function filterDiff(diffText) {
// Split the diff text into sections based on the headers
const sections = diffText.split(/(?=^===== )/m);
const filteredSection = sections.map(section => {
var removedLabels = section.replace(/<\s+argocd\.argoproj\.io\/instance:.*\n---\n>\s+argocd\.argoproj\.io\/instance:.*\n?/g, '').trim();
var removedLabels = removedLabels.replace(/<\s+app.kubernetes.io\/part-of:.*\n?/g, '').trim();
return removedLabels;
}).filter(section => section.trim() !== '');
const removeEmptyHeaders = filteredSection.filter(entry => {
// Remove empty strings and sections that are just headers with line numbers
return !entry.match(/^===== .*\/.* ======\n\d+(,\d+)?c\d+(,\d+)?$/);
});
// Join the filtered sections back together
return removeEmptyHeaders.join('\n').trim();
}
run().catch(e => core.setFailed(e.message));


Expand Down
29 changes: 27 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,14 @@ async function postDiffComment(diffs: Diff[]): Promise<void> {
const commitLink = `https://github.com/${owner}/${repo}/pull/${github.context.issue.number}/commits/${sha}`;
const shortCommitSha = String(sha).substr(0, 7);

// const filteredDiffs = diffs
const filteredDiffs = diffs.map(diff => {
diff.diff = filterDiff(diff.diff);
return diff;
}).filter(d => d.diff !== '');

const prefixHeader = `## ArgoCD Diff on ${ENV}`
const diffOutput = diffs.map(
const diffOutput = filteredDiffs.map(
({ app, diff, error }) => `
App: [\`${app.metadata.name}\`](${protocol}://${ARGOCD_SERVER_URL}/applications/${app.metadata.name})
YAML generation: ${error ? ' Error 🛑' : 'Success 🟢'}
Expand Down Expand Up @@ -205,7 +211,7 @@ _Updated at ${new Date().toLocaleString('en-US', { timeZone: 'America/Los_Angele
}

// Only post a new comment when there are changes
if (diffs.length) {
if (filteredDiffs.length) {
octokit.rest.issues.createComment({
issue_number: github.context.issue.number,
owner,
Expand Down Expand Up @@ -261,4 +267,23 @@ async function run(): Promise<void> {
}
}

function filterDiff(diffText: string) {
// Split the diff text into sections based on the headers
const sections = diffText.split(/(?=^===== )/m);

const filteredSection = sections.map(section => {
var removedLabels = section.replace(/<\s+argocd\.argoproj\.io\/instance:.*\n---\n>\s+argocd\.argoproj\.io\/instance:.*\n?/g, '').trim();
var removedLabels = removedLabels.replace(/<\s+app.kubernetes.io\/part-of:.*\n?/g, '').trim();
return removedLabels;
}).filter(section => section.trim() !== '');

const removeEmptyHeaders = filteredSection.filter(entry => {
// Remove empty strings and sections that are just headers with line numbers
return !entry.match(/^===== .*\/.* ======\n\d+(,\d+)?c\d+(,\d+)?$/);
});

// Join the filtered sections back together
return removeEmptyHeaders.join('\n').trim();
}

run().catch(e => core.setFailed(e.message));

0 comments on commit 00ffb25

Please sign in to comment.