Skip to content

Commit

Permalink
Update action (#41)
Browse files Browse the repository at this point in the history
* feat: allow default revision

* remove deleteComments func

* remove debugging statements

---------

Co-authored-by: Aaron Weisberg <[email protected]>
  • Loading branch information
aaron-weisberg-qz and aweis89 authored Dec 8, 2023
1 parent 0eabe5d commit 0c37713
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
25 changes: 21 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1763,18 +1763,20 @@ function getApps() {
core.error(e);
}
return responseJson.items.filter(app => {
const targetPrimary = app.spec.source.targetRevision === 'master' || app.spec.source.targetRevision === 'main';
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);
});
});
}
function postDiffComment(diffs) {
var _a, _b;
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
const { owner, repo } = github.context.repo;
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 prefixHeader = `## ArgoCD Diff on ${ENV}`;
const diffOutput = diffs.map(({ app, diff, error }) => `
App: [\`${app.metadata.name}\`](https://${ARGOCD_SERVER_URL}/applications/${app.metadata.name})
YAML generation: ${error ? ' Error 🛑' : 'Success 🟢'}
Expand Down Expand Up @@ -1807,7 +1809,7 @@ ${diff}
---
`);
const output = scrubSecrets(`
## ArgoCD Diff on ${ENV} for commit [\`${shortCommitSha}\`](${commitLink})
${prefixHeader} for commit [\`${shortCommitSha}\`](${commitLink})
_Updated at ${new Date().toLocaleString('en-US', { timeZone: 'America/Los_Angeles' })} PT_
${diffOutput.join('\n')}
Expand All @@ -1817,6 +1819,22 @@ _Updated at ${new Date().toLocaleString('en-US', { timeZone: 'America/Los_Angele
| ⚠️ | The app is out-of-sync in ArgoCD, and the diffs you see include those changes plus any from this PR. |
| 🛑 | There was an error generating the ArgoCD diffs due to changes in this PR. |
`);
const commentsResponse = yield octokit.rest.issues.listComments({
issue_number: github.context.issue.number,
owner,
repo
});
// Delete stale comments
for (const comment of commentsResponse.data) {
if ((_c = comment.body) === null || _c === void 0 ? void 0 : _c.includes(prefixHeader)) {
core.info(`deleting comment ${comment.id}`);
octokit.rest.issues.deleteComment({
owner,
repo,
comment_id: comment.id,
});
}
}
// Only post a new comment when there are changes
if (diffs.length) {
octokit.rest.issues.createComment({
Expand Down Expand Up @@ -1873,7 +1891,6 @@ function run() {
}
});
}
run().catch(e => core.setFailed(e.message));


/***/ }),
Expand Down
26 changes: 22 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ async function getApps(): Promise<App[]> {
}

return (responseJson.items as App[]).filter(app => {
const targetPrimary = app.spec.source.targetRevision === 'master' || app.spec.source.targetRevision === 'main'
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}`
Expand All @@ -129,6 +130,7 @@ 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 prefixHeader = `## ArgoCD Diff on ${ENV}`
const diffOutput = diffs.map(
({ app, diff, error }) => `
App: [\`${app.metadata.name}\`](https://${ARGOCD_SERVER_URL}/applications/${app.metadata.name})
Expand Down Expand Up @@ -168,7 +170,7 @@ ${diff}
);

const output = scrubSecrets(`
## ArgoCD Diff on ${ENV} for commit [\`${shortCommitSha}\`](${commitLink})
${prefixHeader} for commit [\`${shortCommitSha}\`](${commitLink})
_Updated at ${new Date().toLocaleString('en-US', { timeZone: 'America/Los_Angeles' })} PT_
${diffOutput.join('\n')}
Expand All @@ -179,6 +181,24 @@ _Updated at ${new Date().toLocaleString('en-US', { timeZone: 'America/Los_Angele
| 🛑 | There was an error generating the ArgoCD diffs due to changes in this PR. |
`);

const commentsResponse = await octokit.rest.issues.listComments({
issue_number: github.context.issue.number,
owner,
repo
});

// Delete stale comments
for (const comment of commentsResponse.data) {
if (comment.body?.includes(prefixHeader)) {
core.info(`deleting comment ${comment.id}`)
octokit.rest.issues.deleteComment({
owner,
repo,
comment_id: comment.id,
});
}
}

// Only post a new comment when there are changes
if (diffs.length) {
octokit.rest.issues.createComment({
Expand Down Expand Up @@ -235,5 +255,3 @@ async function run(): Promise<void> {
core.setFailed(`ArgoCD diff failed: Encountered ${diffsWithErrors.length} errors`);
}
}

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

0 comments on commit 0c37713

Please sign in to comment.