Skip to content

Commit 68d9ecd

Browse files
committed
delete-on-pr-close
1 parent 393ac6c commit 68d9ecd

File tree

3 files changed

+80
-38
lines changed

3 files changed

+80
-38
lines changed

.github/workflows/add-comment-on-pr-creation.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
permissions:
1111
pull-requests: write
1212
steps:
13-
- name: Add comment
13+
name: Add GitHub Comment for review app instructions
1414
uses: actions/github-script@v7
1515
with:
1616
script: |
@@ -21,7 +21,7 @@ jobs:
2121
body: [
2222
"Hi 👋 Here are the commands available for this PR:",
2323
"",
24-
"- `/deploy`: Deploy your changes to a review environment",
24+
"- `/deploy-review-app`: Deploy your changes to a review environment",
2525
"- `/delete-review-app`: Clean up the review environment when you're done",
2626
"- `/help`: Show detailed information about all commands",
2727
"",

.github/workflows/delete-review-app.yml

+78-16
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,68 @@ jobs:
4848
- name: Setup Environment
4949
uses: ./.github/actions/setup-environment
5050

51+
- name: Set shared functions
52+
id: shared-functions
53+
uses: actions/github-script@v7
54+
with:
55+
script: |
56+
core.exportVariable('GET_CONSOLE_LINK', `
57+
function getConsoleLink(prNumber) {
58+
return ' [Control Plane Console for Review App with PR #' + prNumber + '](' +
59+
'https://console.cpln.io/org/' + process.env.CPLN_ORG + '/workloads/' + process.env.APP_NAME + ')';
60+
}
61+
`);
62+
63+
- name: Initialize Delete
64+
id: init-delete
65+
uses: actions/github-script@v7
66+
with:
67+
script: |
68+
eval(process.env.GET_CONSOLE_LINK);
69+
70+
async function getWorkflowUrl(runId) {
71+
// Get the current job ID
72+
const jobs = await github.rest.actions.listJobsForWorkflowRun({
73+
owner: context.repo.owner,
74+
repo: context.repo.repo,
75+
run_id: runId
76+
});
77+
78+
const currentJob = jobs.data.jobs.find(job => job.status === 'in_progress');
79+
const jobId = currentJob?.id;
80+
81+
if (!jobId) {
82+
console.log('Warning: Could not find current job ID');
83+
return `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${runId}`;
84+
}
85+
86+
return `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${runId}/job/${jobId}`;
87+
}
88+
89+
const workflowUrl = await getWorkflowUrl(context.runId);
90+
91+
const comment = await github.rest.issues.createComment({
92+
issue_number: process.env.PR_NUMBER,
93+
owner: context.repo.owner,
94+
repo: context.repo.repo,
95+
body: [
96+
' Starting app deletion...',
97+
'',
98+
' [View Delete Logs](' + workflowUrl + ')',
99+
'',
100+
getConsoleLink(process.env.PR_NUMBER)
101+
].join('\n')
102+
});
103+
104+
return {
105+
commentId: comment.data.id,
106+
workflowUrl
107+
};
108+
109+
- name: Set workflow URL
110+
run: |
111+
echo "WORKFLOW_URL=${{ fromJSON(steps.init-delete.outputs.result).workflowUrl }}" >> $GITHUB_ENV
112+
51113
- name: Create Initial Delete Comment
52114
id: init-delete
53115
uses: actions/github-script@v7
@@ -81,28 +143,28 @@ jobs:
81143
uses: actions/github-script@v7
82144
with:
83145
script: |
146+
eval(process.env.GET_CONSOLE_LINK);
147+
84148
const success = '${{ job.status }}' === 'success';
85149
const prNumber = process.env.PR_NUMBER;
86-
const cpConsoleUrl = `https://console.cpln.io/org/${process.env.CPLN_ORG}/workloads/${process.env.APP_NAME}`;
87150
88-
let message;
89-
if (success) {
90-
message = '✅ Review app for PR #' + prNumber + ' was successfully deleted';
91-
if ('${{ github.event_name }}' === 'pull_request') {
92-
const merged = '${{ github.event.pull_request.merged }}' === 'true';
93-
message += merged ? ' after merge' : ' after PR was closed';
94-
}
95-
} else {
96-
message = [
97-
'❌ Review app for PR #' + prNumber + ' failed to be deleted',
98-
'',
99-
'🎮 [Control Plane Console for Review App with PR #' + prNumber + '](' + cpConsoleUrl + ')'
100-
].join('\n');
101-
}
151+
const successMessage = [
152+
'✅ Review app for PR #' + prNumber + ' was successfully deleted',
153+
'',
154+
' [View Completed Delete Logs](' + process.env.WORKFLOW_URL + ')'
155+
].join('\n');
156+
157+
const failureMessage = [
158+
'❌ Review app for PR #' + prNumber + ' failed to be deleted',
159+
'',
160+
' [View Delete Logs with Errors](' + process.env.WORKFLOW_URL + ')',
161+
'',
162+
getConsoleLink(prNumber)
163+
].join('\n');
102164
103165
await github.rest.issues.updateComment({
104166
owner: context.repo.owner,
105167
repo: context.repo.repo,
106168
comment_id: ${{ fromJSON(steps.init-delete.outputs.result).commentId }},
107-
body: message
169+
body: success ? successMessage : failureMessage
108170
});

.github/workflows/help-command.yml

-20
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,7 @@ permissions:
1414
pull-requests: write
1515

1616
jobs:
17-
debug-trigger:
18-
if: always()
19-
runs-on: ubuntu-latest
20-
steps:
21-
- name: Debug Trigger Conditions
22-
env:
23-
EVENT_NAME: ${{ github.event_name }}
24-
IS_PR: ${{ toJSON(github.event.issue.pull_request) }}
25-
COMMENT: ${{ github.event.comment.body }}
26-
ISSUE_NUMBER: ${{ github.event.issue.number }}
27-
run: |
28-
echo "Debug information for help command:"
29-
echo "Event name: $EVENT_NAME"
30-
echo "Is PR (raw): $IS_PR"
31-
echo "Comment body: $COMMENT"
32-
echo "Issue number: $ISSUE_NUMBER"
33-
echo "Raw event payload:"
34-
echo '${{ toJSON(github.event) }}'
35-
3617
show-help:
37-
needs: debug-trigger
3818
if: |
3919
github.event_name == 'issue_comment' &&
4020
github.event.issue.pull_request &&

0 commit comments

Comments
 (0)