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

delete-on-pr-close #617

Merged
merged 8 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
24 changes: 17 additions & 7 deletions .github/workflows/add-comment-on-pr-creation.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
name: Add helper Comment on PR creation
name: Add Comment on PR Creation

on:
pull_request:
types: [opened]

jobs:
comment-on-pr:
add-comment:
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- name: Add GitHub Comment for review app instructions
- name: Add comment
uses: actions/github-script@v7
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
body: "Hi 👋 To deploy a review app, please comment `/deploy-review-app`"
})
issue_number: context.payload.pull_request.number,
body: [
"Hi 👋 Here are the commands available for this PR:",
"",
"- `/deploy`: 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",
"",
"Use `/help` to see full documentation, including configuration options."
].join("\n")
});
63 changes: 31 additions & 32 deletions .github/workflows/delete-review-app.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: Delete Review App

on:
pull_request:
types: [closed]
issue_comment:
types: [created]

Expand All @@ -13,33 +15,17 @@ permissions:
env:
CPLN_ORG: ${{ secrets.CPLN_ORG }}
CPLN_TOKEN: ${{ secrets.CPLN_TOKEN }}
APP_NAME: qa-react-webpack-rails-tutorial-pr-${{ github.event.issue.number }}
PR_NUMBER: ${{ github.event.issue.number }}
APP_NAME: qa-react-webpack-rails-tutorial-pr-${{ github.event.pull_request.number || github.event.issue.number }}
PR_NUMBER: ${{ github.event.pull_request.number || github.event.issue.number }}

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 }}
run: |
echo "Debug information for delete-review-app command:"
echo "Event name: $EVENT_NAME"
echo "Is PR (raw): $IS_PR"
echo "Comment body: $COMMENT"
echo "Raw event payload:"
echo '${{ toJSON(github.event) }}'

Process-Delete-Command:
needs: debug-trigger
if: |
github.event_name == 'issue_comment' &&
github.event.issue.pull_request &&
github.event.comment.body == '/delete-review-app'
(github.event_name == 'issue_comment' &&
github.event.issue.pull_request &&
github.event.comment.body == '/delete-review-app') ||
(github.event_name == 'pull_request' &&
github.event.action == 'closed')
runs-on: ubuntu-latest

steps:
Expand All @@ -55,7 +41,7 @@ jobs:
done

if [ ${#missing_secrets[@]} -ne 0 ]; then
echo " Required secrets are not set: ${missing_secrets[*]}"
echo "Required secrets are not set: ${missing_secrets[*]}"
exit 1
fi

Expand All @@ -67,11 +53,17 @@ jobs:
uses: actions/github-script@v7
with:
script: |
let message = '🗑️ Starting app deletion';
if ('${{ github.event_name }}' === 'pull_request') {
const merged = '${{ github.event.pull_request.merged }}' === 'true';
message += merged ? ' (PR merged)' : ' (PR closed)';
}

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...'
body: message
});
return { commentId: comment.data.id };

Expand All @@ -93,13 +85,20 @@ jobs:
const prNumber = process.env.PR_NUMBER;
const cpConsoleUrl = `https://console.cpln.io/org/${process.env.CPLN_ORG}/workloads/${process.env.APP_NAME}`;

const message = success
? ' Review app for PR #' + prNumber + ' was successfully deleted'
: [
' Review app for PR #' + prNumber + ' failed to be deleted',
'',
'[Control Plane Console for Review App with PR #' + prNumber + '](' + cpConsoleUrl + ')'
].join('\n');
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');
}

await github.rest.issues.updateComment({
owner: context.repo.owner,
Expand Down
Loading
Loading