Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update action #41

Merged
merged 3 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
Loading