Skip to content

Commit 0a67174

Browse files
committed
Adding /ps-delete command
1 parent d7d019e commit 0a67174

File tree

4 files changed

+163
-11
lines changed

4 files changed

+163
-11
lines changed

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

Lines changed: 160 additions & 8 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') || 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'))
1111
outputs:
1212
ref: ${{steps.prechecks.outputs.ref}}
1313
eyes: ${{steps.prechecks.outputs.eyes}}
@@ -348,7 +348,7 @@ jobs:
348348
ref: REF
349349
})
350350
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.'
352352
core.setOutput('error', message)
353353
throw new Error(message)
354354
}
@@ -556,8 +556,6 @@ jobs:
556556
throw new Error(message)
557557
}
558558
559-
core.info("DB branch name: " + BRANCH_NAME)
560-
core.setOutput('BRANCH_NAME', BRANCH_NAME)
561559
core.info("DDL statements: " + DDL_STATEMENTS)
562560
core.setOutput('DDL_STATEMENTS', DDL_STATEMENTS)
563561
@@ -574,7 +572,7 @@ jobs:
574572
if (error.status !== 404) {
575573
throw error
576574
} 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.'
578576
core.setOutput('error', message)
579577
throw new Error(message)
580578
}
@@ -583,7 +581,7 @@ jobs:
583581
const log_url = `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${process.env.GITHUB_RUN_ID}`
584582
585583
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 ...
587585
588586
You can watch the progress and authorize access [here](${log_url}).
589587
`;
@@ -609,7 +607,6 @@ jobs:
609607
ORG_NAME: ${{secrets.ORG_NAME}}
610608
GITHUB_USER: ${{github.actor}}
611609
DDL_STATEMENTS: ${{ steps.validate_params.outputs.DDL_STATEMENTS }}
612-
BRANCH_NAME: ${{ steps.validate_params.outputs.BRANCH_NAME }}
613610
run: . .pscale/ps-env-${REF}.sh && ./update-db-branch.sh "$BRANCH_NAME" "$DDL_STATEMENTS" "$DEPLOY_REQUEST_NUMBER"
614611

615612
- name: Database branch and DR update succeeded
@@ -770,7 +767,7 @@ jobs:
770767
if (error.status !== 404) {
771768
throw error
772769
} 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.'
774771
core.setOutput('error', message)
775772
throw new Error(message)
776773
}
@@ -881,6 +878,161 @@ jobs:
881878
})
882879
883880
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+
8841036
await github.reactions.createForIssueComment({
8851037
...context.repo,
8861038
comment_id: ${{github.event.comment.id}},

create-branch-connection-string.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ function create-branch-connection-string {
22
local DB_NAME=$1
33
local BRANCH_NAME=$2
44
local ORG_NAME=$3
5-
local CREDS=$4
5+
local CREDS=${4,,}
66
local secretshare=$5
77

88
# delete password if it already existed

ps-create-helper-functions.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function create-diff-for-ci {
8080
local BRANCH_DIFF="Diff could not be generated for deploy request $deploy_request"
8181

8282
# updating schema for branch
83-
if [ -n "$refresh_schmema" ]; then
83+
if [ -n "$refresh_schema" ]; then
8484
pscale branch refresh-schema "$DB_NAME" "$BRANCH_NAME" --org "$ORG_NAME"
8585
fi
8686

wait-for-branch-readiness.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function wait_for_branch_readiness {
22
local retries=$1
33
local db=$2
4-
local branch=$3
4+
local branch=${3,,}
55
local org=$4
66

77
# check whether fifth parameter is set, otherwise use default value

0 commit comments

Comments
 (0)