Skip to content

Commit 67bec58

Browse files
committed
Add /ps-approve command
1 parent 5b49f0d commit 67bec58

File tree

2 files changed

+192
-3
lines changed

2 files changed

+192
-3
lines changed

.github/workflows/issue-ops-ps-commands.yml

Lines changed: 179 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
jobs:
88
prechecks:
99
name: Permission pre-check
10-
if: github.event.issue.pull_request != null && (startsWith(github.event.comment.body, '/ps-merge') || startsWith(github.event.comment.body, '/ps-create') || startsWith(github.event.comment.body, '/ps-update'))
10+
if: github.event.issue.pull_request != null && (startsWith(github.event.comment.body, '/ps-merge') || startsWith(github.event.comment.body, '/ps-create') || startsWith(github.event.comment.body, '/ps-update') || startsWith(github.event.comment.body, '/ps-approve'))
1111
outputs:
1212
ref: ${{steps.prechecks.outputs.ref}}
1313
eyes: ${{steps.prechecks.outputs.eyes}}
@@ -329,7 +329,7 @@ jobs:
329329
BRANCH_NAME = comment.match(regexCommandWithBranchAndParameters)[1]
330330
DDL_STATEMENTS = comment.match(regexCommandWithBranchAndParameters)[2]
331331
} else {
332-
message = 'Invalid command, please use \`/ps-create`, \`/ps-create "<DDL>"\`, or \`/ps-create <branch> "<DDL>"\`.'
332+
message = 'Invalid command, please use \`/ps-create\`, \`/ps-create "<DDL>"\`, or \`/ps-create <branch> "<DDL>"\`.'
333333
core.setOutput('error', message)
334334
throw new Error(message)
335335
}
@@ -551,7 +551,7 @@ jobs:
551551
DDL_STATEMENTS = comment.match(regexCommandWithDDL)[1]
552552
DDL_UPDATED = "and database schema"
553553
} else {
554-
message = 'Invalid command, please use \`/ps-update`, or \`/ps-update "<DDL>"\`.'
554+
message = 'Invalid command, please use \`/ps-update\`, or \`/ps-update "<DDL>"\`.'
555555
core.setOutput('error', message)
556556
throw new Error(message)
557557
}
@@ -705,6 +705,182 @@ jobs:
705705
})
706706
707707
708+
await github.reactions.createForIssueComment({
709+
...context.repo,
710+
comment_id: ${{github.event.comment.id}},
711+
content: '-1'
712+
})
713+
714+
await github.reactions.deleteForIssueComment({
715+
...context.repo,
716+
comment_id: ${{github.event.comment.id}},
717+
reaction_id: ${{needs.prechecks.outputs.eyes}}
718+
})
719+
720+
act-on-ps-approve-request:
721+
name: "/ps-approve - click here ..."
722+
if: startsWith(github.event.comment.body, '/ps-approve')
723+
needs: [prechecks]
724+
runs-on: ubuntu-latest
725+
steps:
726+
- name: Validating command and associated PS env
727+
id: validate_params
728+
env:
729+
REF: ${{ needs.prechecks.outputs.ref }}
730+
comment: ${{ github.event.comment.body }}
731+
uses: actions/github-script@v3
732+
with:
733+
github-token: ${{ secrets.GITHUB_TOKEN }}
734+
script: |
735+
const { REF, comment } = process.env;
736+
737+
reviewComment = "LGTM"
738+
739+
// check if comment starts with '/ps-approve' and is only followed by whitespaces
740+
const regexCommandWithoutParameters = /^\/ps-approve\s*$/
741+
742+
// check if comment starts with '/ps-approve' and is followed by a comment
743+
const regexCommandWithParameters = /^\/ps-approve\s+(.*)$/
744+
745+
// check which of the two regexes above matches the comment
746+
if (regexCommandWithoutParameters.test(comment)) {
747+
console.log("/ps-approve command without parameters")
748+
} else if (regexCommandWithParameters.test(comment)) {
749+
console.log("/ps-approve command with comment")
750+
reviewComment = comment.match(regexCommandWithParameters)[1]
751+
}
752+
else {
753+
message = 'Invalid command, please use \`/ps-approve\` or \`/ps-approve <comment>\` .'
754+
core.setOutput('error', message)
755+
throw new Error(message)
756+
}
757+
758+
core.setOutput('reviewComment', reviewComment)
759+
760+
const ps_env_name = '.pscale/ps-env-' + REF + ".sh"
761+
// check whether file ps_env_name exists in the repo and given ref
762+
try {
763+
const response = await github.repos.getContent({
764+
...context.repo,
765+
path: ps_env_name,
766+
ref: REF
767+
})
768+
} catch (error) {
769+
// check error code
770+
if (error.status !== 404) {
771+
throw error
772+
} else {
773+
message = 'Script ' + ps_env_name + ' does not exists, please use \`/ps-create <branch> "<DDL>"\` to create a DB branch first.'
774+
core.setOutput('error', message)
775+
throw new Error(message)
776+
}
777+
}
778+
779+
const log_url = `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${process.env.GITHUB_RUN_ID}`
780+
781+
const commentBody = `\
782+
👋 __${context.actor}__, approving DB changes associated with Git branch __${REF}__ now ...
783+
784+
You can watch the progress and authorize access [here](${log_url}).
785+
`;
786+
787+
await github.issues.createComment({
788+
...context.repo,
789+
issue_number: context.issue.number,
790+
body: commentBody
791+
})
792+
793+
- name: Checkout
794+
uses: actions/checkout@v2
795+
with:
796+
ref: ${{ needs.prechecks.outputs.ref }}
797+
798+
- name: Approve deploy request - if asked, please click on displayed link to authenticate
799+
id: approve-deploy-request
800+
timeout-minutes: 3
801+
env:
802+
PLANETSCALE_SERVICE_TOKEN_NAME: ${{secrets.PLANETSCALE_SERVICE_TOKEN_NAME}}
803+
PLANETSCALE_SERVICE_TOKEN: ${{secrets.PLANETSCALE_SERVICE_TOKEN}}
804+
REF: ${{ needs.prechecks.outputs.ref }}
805+
ORG_NAME: ${{secrets.ORG_NAME}}
806+
GITHUB_USER: ${{github.actor}}
807+
COMMENT: ${{steps.validate_params.outputs.reviewComment}}
808+
run: . .pscale/ps-env-${REF}.sh && ./approve-deploy-request.sh "$DEPLOY_REQUEST_NUMBER" "$COMMENT"
809+
810+
- name: Approved deploy request
811+
id: ps-approve-succeeded
812+
if: success()
813+
uses: actions/github-script@v3
814+
env:
815+
DEPLOY_REQUEST_URL: ${{ steps.approve-deploy-request.outputs.DEPLOY_REQUEST_URL }}
816+
BRANCH_NAME: ${{ steps.approve-deploy-request.outputs.BRANCH_NAME }}
817+
BRANCH_URL: ${{ steps.approve-deploy-request.outputs.BRANCH_URL }}
818+
DB_NAME: ${{ steps.approve-deploy-request.outputs.DB_NAME }}
819+
ORG_NAME: ${{ steps.approve-deploy-request.ORG_NAME }}
820+
821+
with:
822+
github-token: ${{ secrets.GITHUB_TOKEN }}
823+
script: |
824+
const { DEPLOY_REQUEST_URL, BRANCH_NAME, BRANCH_URL, DB_NAME, ORG_NAME, DDL_STATEMENTS, BRANCH_DIFF } = process.env;
825+
const log_url = `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${process.env.GITHUB_RUN_ID}`
826+
827+
const commentBody = `\
828+
### :+1: Approved deploy request :ship:
829+
830+
* :seedling: __DB-Branch__: [${BRANCH_NAME}](${BRANCH_URL})
831+
* :train2: [Deploy request](${DEPLOY_REQUEST_URL})
832+
833+
`;
834+
835+
github.issues.createComment({
836+
...context.repo,
837+
issue_number: ${{ github.event.issue.number }},
838+
body: commentBody
839+
});
840+
841+
await github.reactions.createForIssueComment({
842+
...context.repo,
843+
comment_id: ${{github.event.comment.id}},
844+
content: '+1'
845+
})
846+
847+
await github.reactions.deleteForIssueComment({
848+
...context.repo,
849+
comment_id: ${{github.event.comment.id}},
850+
reaction_id: ${{needs.prechecks.outputs.eyes}}
851+
})
852+
853+
- name: /ps-approve failed
854+
id: ps-approve-failed
855+
if: cancelled() || failure()
856+
uses: actions/github-script@v3
857+
env:
858+
REF: ${{ needs.prechecks.outputs.ref }}
859+
message: ${{steps.validate_params.outputs.error}}
860+
DEPLOY_REQUEST_URL: ${{ steps.update-db-branch.outputs.DEPLOY_REQUEST_URL }}
861+
BRANCH_NAME: ${{ steps.update-db-branch.outputs.BRANCH_NAME }}
862+
BRANCH_URL: ${{ steps.update-db-branch.outputs.BRANCH_URL }}
863+
DB_NAME: ${{ steps.update-db-branch.outputs.DB_NAME }}
864+
ORG_NAME: ${{ steps.update-db-branch.ORG_NAME }}
865+
with:
866+
github-token: ${{ secrets.GITHUB_TOKEN }}
867+
script: |
868+
const { REF, message, DEPLOY_REQUEST_URL, BRANCH_NAME, BRANCH_URL, DB_NAME, ORG_NAME } = process.env;
869+
const log_url = `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${process.env.GITHUB_RUN_ID}`
870+
871+
if (message === null || message === '') {
872+
errorMessage = `Approving database changes for Git branch __${REF}__ failed :cry:. [View error logs](${log_url}).`
873+
} else {
874+
errorMessage = message
875+
}
876+
877+
github.issues.createComment({
878+
...context.repo,
879+
issue_number: ${{ github.event.issue.number }},
880+
body: errorMessage
881+
})
882+
883+
708884
await github.reactions.createForIssueComment({
709885
...context.repo,
710886
comment_id: ${{github.event.comment.id}},

approve-deploy-request.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
. use-pscale-docker-image.sh
4+
5+
. authenticate-ps.sh
6+
7+
DEPLOY_REQUEST_NUMBER=$1
8+
COMMENT=$2
9+
10+
. set-db-and-org-and-branch-name.sh
11+
pscale deploy-request review "$DB_NAME" "$DEPLOY_REQUEST_NUMBER" --approve --comment "$COMMENT" --org "$ORG_NAME"
12+
13+

0 commit comments

Comments
 (0)