7
7
jobs :
8
8
prechecks :
9
9
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') || startsWith(github.event.comment.body, '/ps-approve'))
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') || startsWith(github.event.comment.body, '/ps-delete') )
11
11
outputs :
12
12
ref : ${{steps.prechecks.outputs.ref}}
13
13
eyes : ${{steps.prechecks.outputs.eyes}}
@@ -348,7 +348,7 @@ jobs:
348
348
ref: REF
349
349
})
350
350
if (response.data.content) {
351
- message = 'Script ' + ps_env_name + ' already exists, please use \`/ps-update "<DDL>"\` to update existing env.'
351
+ message = 'Script \` ' + ps_env_name + '\` already exists, please use \`/ps-update "<DDL>"\` to update existing env.'
352
352
core.setOutput('error', message)
353
353
throw new Error(message)
354
354
}
@@ -556,8 +556,6 @@ jobs:
556
556
throw new Error(message)
557
557
}
558
558
559
- core.info("DB branch name: " + BRANCH_NAME)
560
- core.setOutput('BRANCH_NAME', BRANCH_NAME)
561
559
core.info("DDL statements: " + DDL_STATEMENTS)
562
560
core.setOutput('DDL_STATEMENTS', DDL_STATEMENTS)
563
561
@@ -574,7 +572,7 @@ jobs:
574
572
if (error.status !== 404) {
575
573
throw error
576
574
} else {
577
- message = 'Script ' + ps_env_name + ' does not exists, please use \`/ps-create <branch> "<DDL>"\` to create a DB branch first.'
575
+ message = 'Script \` ' + ps_env_name + '\` does not exists, please use \`/ps-create <branch> "<DDL>"\` to create a DB branch first.'
578
576
core.setOutput('error', message)
579
577
throw new Error(message)
580
578
}
@@ -583,7 +581,7 @@ jobs:
583
581
const log_url = `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${process.env.GITHUB_RUN_ID}`
584
582
585
583
const commentBody = `\
586
- 👋 __${context.actor}__, updating information ${DDL_UPDATED} of DB branch __${BRANCH_NAME}__ and Git branch __${REF}__ now ...
584
+ 👋 __${context.actor}__, updating information ${DDL_UPDATED} of Git branch __${REF}__ now ...
587
585
588
586
You can watch the progress and authorize access [here](${log_url}).
589
587
`;
@@ -609,7 +607,6 @@ jobs:
609
607
ORG_NAME : ${{secrets.ORG_NAME}}
610
608
GITHUB_USER : ${{github.actor}}
611
609
DDL_STATEMENTS : ${{ steps.validate_params.outputs.DDL_STATEMENTS }}
612
- BRANCH_NAME : ${{ steps.validate_params.outputs.BRANCH_NAME }}
613
610
run : . .pscale/ps-env-${REF}.sh && ./update-db-branch.sh "$BRANCH_NAME" "$DDL_STATEMENTS" "$DEPLOY_REQUEST_NUMBER"
614
611
615
612
- name : Database branch and DR update succeeded
@@ -770,7 +767,7 @@ jobs:
770
767
if (error.status !== 404) {
771
768
throw error
772
769
} else {
773
- message = 'Script ' + ps_env_name + ' does not exists, please use \`/ps-create <branch> "<DDL>"\` to create a DB branch first.'
770
+ message = 'Script \` ' + ps_env_name + '\` does not exists, please use \`/ps-create <branch> "<DDL>"\` to create a DB branch first.'
774
771
core.setOutput('error', message)
775
772
throw new Error(message)
776
773
}
@@ -881,6 +878,161 @@ jobs:
881
878
})
882
879
883
880
881
+ await github.reactions.createForIssueComment({
882
+ ...context.repo,
883
+ comment_id: ${{github.event.comment.id}},
884
+ content: '-1'
885
+ })
886
+
887
+ await github.reactions.deleteForIssueComment({
888
+ ...context.repo,
889
+ comment_id: ${{github.event.comment.id}},
890
+ reaction_id: ${{needs.prechecks.outputs.eyes}}
891
+ })
892
+ act-on-ps-delete-request :
893
+ name : " /ps-delete - click here ..."
894
+ if : startsWith(github.event.comment.body, '/ps-delete')
895
+ needs : [prechecks]
896
+ runs-on : ubuntu-latest
897
+ steps :
898
+ - name : Validating command and associated PS env
899
+ id : validate_params
900
+ env :
901
+ REF : ${{ needs.prechecks.outputs.ref }}
902
+ comment : ${{ github.event.comment.body }}
903
+ uses : actions/github-script@v3
904
+ with :
905
+ github-token : ${{ secrets.GITHUB_TOKEN }}
906
+ script : |
907
+ const { REF, comment } = process.env;
908
+
909
+ // check if comment starts with '/ps-delete' and is only followed by whitespaces
910
+ const regexCommandWithoutParameters = /^\/ps-delete\s*$/
911
+
912
+ // check if the regexp above matches the comment, override BRANCH_NAME and DDL_STATEMENTS if present, error if none of the above matches
913
+ if (regexCommandWithoutParameters.test(comment)) {
914
+ console.log("/ps-delete command without parameters")
915
+ } else {
916
+ message = 'Invalid command, please use \`/ps-delete\`.'
917
+ core.setOutput('error', message)
918
+ throw new Error(message)
919
+ }
920
+
921
+ const ps_env_name = '.pscale/ps-env-' + REF + ".sh"
922
+ // check whether file ps_env_name exists in the repo and given ref
923
+ try {
924
+ const response = await github.repos.getContent({
925
+ ...context.repo,
926
+ path: ps_env_name,
927
+ ref: REF
928
+ })
929
+ } catch (error) {
930
+ // check error code
931
+ if (error.status !== 404) {
932
+ throw error
933
+ } else {
934
+ message = 'Script \`' + ps_env_name + '\` does not exists, could not identify associated branch.'
935
+ core.setOutput('error', message)
936
+ throw new Error(message)
937
+ }
938
+ }
939
+
940
+ const log_url = `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${process.env.GITHUB_RUN_ID}`
941
+
942
+ const commentBody = `\
943
+ 👋 __${context.actor}__, deleting DB branch associated with Git branch __${REF}__ now ...
944
+
945
+ You can watch the progress and authorize access [here](${log_url}).
946
+ `;
947
+
948
+ await github.issues.createComment({
949
+ ...context.repo,
950
+ issue_number: context.issue.number,
951
+ body: commentBody
952
+ })
953
+
954
+ - name : Checkout
955
+ uses : actions/checkout@v2
956
+ with :
957
+ ref : ${{ needs.prechecks.outputs.ref }}
958
+
959
+ - name : Delete DB branch - if asked, please click on displayed link to authenticate
960
+ id : delete-db-branch
961
+ timeout-minutes : 3
962
+ env :
963
+ PLANETSCALE_SERVICE_TOKEN_NAME : ${{secrets.PLANETSCALE_SERVICE_TOKEN_NAME}}
964
+ PLANETSCALE_SERVICE_TOKEN : ${{secrets.PLANETSCALE_SERVICE_TOKEN}}
965
+ REF : ${{ needs.prechecks.outputs.ref }}
966
+ ORG_NAME : ${{secrets.ORG_NAME}}
967
+ GITHUB_USER : ${{github.actor}}
968
+ run : . .pscale/ps-env-${REF}.sh && ./delete-db-branch.sh "$BRANCH_NAME"
969
+
970
+ - name : Database branch deleted
971
+ id : ps-delete-succeeded
972
+ if : success()
973
+ uses : actions/github-script@v3
974
+ env :
975
+ BRANCH_NAME : ${{ steps.delete-db-branch.outputs.BRANCH_NAME }}
976
+ with :
977
+ github-token : ${{ secrets.GITHUB_TOKEN }}
978
+ script : |
979
+ const { BRANCH_NAME } = process.env;
980
+ const log_url = `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${process.env.GITHUB_RUN_ID}`
981
+
982
+ const commentBody = `\
983
+ ### Deleted database branch successfully :tada:
984
+
985
+ * :seedling: __DB-Branch__: ${BRANCH_NAME}
986
+ `;
987
+
988
+ github.issues.createComment({
989
+ ...context.repo,
990
+ issue_number: ${{ github.event.issue.number }},
991
+ body: commentBody
992
+ });
993
+
994
+ await github.reactions.createForIssueComment({
995
+ ...context.repo,
996
+ comment_id: ${{github.event.comment.id}},
997
+ content: '+1'
998
+ })
999
+
1000
+ await github.reactions.deleteForIssueComment({
1001
+ ...context.repo,
1002
+ comment_id: ${{github.event.comment.id}},
1003
+ reaction_id: ${{needs.prechecks.outputs.eyes}}
1004
+ })
1005
+
1006
+ - name : /ps-delete failed
1007
+ id : ps-delete-failed
1008
+ if : cancelled() || failure()
1009
+ uses : actions/github-script@v3
1010
+ env :
1011
+ REF : ${{ needs.prechecks.outputs.ref }}
1012
+ message : ${{steps.validate_params.outputs.error}}
1013
+ BRANCH_NAME : ${{ steps.delete-db-branch.outputs.BRANCH_NAME }}
1014
+ BRANCH_URL : ${{ steps.delete-db-branch.outputs.BRANCH_URL }}
1015
+ DB_NAME : ${{ steps.delete-db-branch.outputs.DB_NAME }}
1016
+ ORG_NAME : ${{ steps.delete-db-branch.ORG_NAME }}
1017
+ with :
1018
+ github-token : ${{ secrets.GITHUB_TOKEN }}
1019
+ script : |
1020
+ const { REF, message, BRANCH_NAME, BRANCH_URL, DB_NAME, ORG_NAME } = process.env;
1021
+ const log_url = `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${process.env.GITHUB_RUN_ID}`
1022
+
1023
+ if (message === null || message === '') {
1024
+ errorMessage = `Deletion failed for DB branch associated with Git branch __${REF}__ :cry:. [View error logs](${log_url}).`
1025
+ } else {
1026
+ errorMessage = message
1027
+ }
1028
+
1029
+ github.issues.createComment({
1030
+ ...context.repo,
1031
+ issue_number: ${{ github.event.issue.number }},
1032
+ body: errorMessage
1033
+ })
1034
+
1035
+
884
1036
await github.reactions.createForIssueComment({
885
1037
...context.repo,
886
1038
comment_id: ${{github.event.comment.id}},
0 commit comments