Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
delete-on-pr-close
  • Loading branch information
justin808 committed Jan 26, 2025
commit 68d9ecd440cbcdd8e61c74d25651b38a974cb844
4 changes: 2 additions & 2 deletions .github/workflows/add-comment-on-pr-creation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
permissions:
pull-requests: write
steps:
- name: Add comment
name: Add GitHub Comment for review app instructions
uses: actions/github-script@v7
with:
script: |
Expand All @@ -21,7 +21,7 @@ jobs:
body: [
"Hi 👋 Here are the commands available for this PR:",
"",
"- `/deploy`: Deploy your changes to a review environment",
"- `/deploy-review-app`: Deploy your changes to a review environment",
"- `/delete-review-app`: Clean up the review environment when you're done",
"- `/help`: Show detailed information about all commands",
"",
Expand Down
94 changes: 78 additions & 16 deletions .github/workflows/delete-review-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,68 @@ jobs:
- name: Setup Environment
uses: ./.github/actions/setup-environment

- name: Set shared functions
id: shared-functions
uses: actions/github-script@v7
with:
script: |
core.exportVariable('GET_CONSOLE_LINK', `
function getConsoleLink(prNumber) {
return ' [Control Plane Console for Review App with PR #' + prNumber + '](' +
'https://console.cpln.io/org/' + process.env.CPLN_ORG + '/workloads/' + process.env.APP_NAME + ')';
}
`);

- name: Initialize Delete
id: init-delete
uses: actions/github-script@v7
with:
script: |
eval(process.env.GET_CONSOLE_LINK);

async function getWorkflowUrl(runId) {
// Get the current job ID
const jobs = await github.rest.actions.listJobsForWorkflowRun({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: runId
});

const currentJob = jobs.data.jobs.find(job => job.status === 'in_progress');
const jobId = currentJob?.id;

if (!jobId) {
console.log('Warning: Could not find current job ID');
return `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${runId}`;
}

return `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${runId}/job/${jobId}`;
}

const workflowUrl = await getWorkflowUrl(context.runId);

const comment = await github.rest.issues.createComment({
issue_number: process.env.PR_NUMBER,
owner: context.repo.owner,
repo: context.repo.repo,
body: [
' Starting app deletion...',
'',
' [View Delete Logs](' + workflowUrl + ')',
'',
getConsoleLink(process.env.PR_NUMBER)
].join('\n')
});

return {
commentId: comment.data.id,
workflowUrl
};

- name: Set workflow URL
run: |
echo "WORKFLOW_URL=${{ fromJSON(steps.init-delete.outputs.result).workflowUrl }}" >> $GITHUB_ENV

- name: Create Initial Delete Comment
id: init-delete
uses: actions/github-script@v7
Expand Down Expand Up @@ -81,28 +143,28 @@ jobs:
uses: actions/github-script@v7
with:
script: |
eval(process.env.GET_CONSOLE_LINK);

const success = '${{ job.status }}' === 'success';
const prNumber = process.env.PR_NUMBER;
const cpConsoleUrl = `https://console.cpln.io/org/${process.env.CPLN_ORG}/workloads/${process.env.APP_NAME}`;

let message;
if (success) {
message = '✅ Review app for PR #' + prNumber + ' was successfully deleted';
if ('${{ github.event_name }}' === 'pull_request') {
const merged = '${{ github.event.pull_request.merged }}' === 'true';
message += merged ? ' after merge' : ' after PR was closed';
}
} else {
message = [
'❌ Review app for PR #' + prNumber + ' failed to be deleted',
'',
'🎮 [Control Plane Console for Review App with PR #' + prNumber + '](' + cpConsoleUrl + ')'
].join('\n');
}
const successMessage = [
'✅ Review app for PR #' + prNumber + ' was successfully deleted',
'',
' [View Completed Delete Logs](' + process.env.WORKFLOW_URL + ')'
].join('\n');

const failureMessage = [
'❌ Review app for PR #' + prNumber + ' failed to be deleted',
'',
' [View Delete Logs with Errors](' + process.env.WORKFLOW_URL + ')',
'',
getConsoleLink(prNumber)
].join('\n');

await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: ${{ fromJSON(steps.init-delete.outputs.result).commentId }},
body: message
body: success ? successMessage : failureMessage
});
20 changes: 0 additions & 20 deletions .github/workflows/help-command.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,7 @@ permissions:
pull-requests: write

jobs:
debug-trigger:
if: always()
runs-on: ubuntu-latest
steps:
- name: Debug Trigger Conditions
env:
EVENT_NAME: ${{ github.event_name }}
IS_PR: ${{ toJSON(github.event.issue.pull_request) }}
COMMENT: ${{ github.event.comment.body }}
ISSUE_NUMBER: ${{ github.event.issue.number }}
run: |
echo "Debug information for help command:"
echo "Event name: $EVENT_NAME"
echo "Is PR (raw): $IS_PR"
echo "Comment body: $COMMENT"
echo "Issue number: $ISSUE_NUMBER"
echo "Raw event payload:"
echo '${{ toJSON(github.event) }}'

show-help:
needs: debug-trigger
if: |
github.event_name == 'issue_comment' &&
github.event.issue.pull_request &&
Expand Down
Loading