Skip to content

Commit b58710b

Browse files
committed
/ps-update command
1 parent ed138a9 commit b58710b

File tree

3 files changed

+224
-3
lines changed

3 files changed

+224
-3
lines changed

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

Lines changed: 202 additions & 1 deletion
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'))
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'))
1111
outputs:
1212
ref: ${{steps.prechecks.outputs.ref}}
1313
eyes: ${{steps.prechecks.outputs.eyes}}
@@ -515,3 +515,204 @@ jobs:
515515
comment_id: ${{github.event.comment.id}},
516516
reaction_id: ${{needs.prechecks.outputs.eyes}}
517517
})
518+
519+
act-on-ps-update-request:
520+
name: "/ps-update - click here ..."
521+
if: startsWith(github.event.comment.body, '/ps-update')
522+
needs: [prechecks]
523+
runs-on: ubuntu-latest
524+
steps:
525+
- name: Validating command and associated PS env
526+
id: validate_params
527+
env:
528+
REF: ${{ needs.prechecks.outputs.ref }}
529+
comment: ${{ github.event.comment.body }}
530+
uses: actions/github-script@v3
531+
with:
532+
github-token: ${{ secrets.GITHUB_TOKEN }}
533+
script: |
534+
const { REF, comment } = process.env;
535+
536+
// check if comment starts with '/ps-update' and is only followed by whitespaces
537+
const regexCommandWithoutParameters = /^\/ps-update\s*$/
538+
539+
// check if comment starts with '/ps-update' and is followed by a valid DDL
540+
const regexCommandWithDDL = /^\/ps-update\s+"([^"]*)"\s*$/
541+
542+
BRANCH_NAME = REF
543+
DDL_STATEMENTS = ""
544+
DDL_UPDATED = ""
545+
546+
// check which of the two regexes above matches the comment, override BRANCH_NAME and DDL_STATEMENTS if present, error if none of the above matches
547+
if (regexCommandWithoutParameters.test(comment)) {
548+
console.log("/ps-update command without parameters")
549+
} else if (regexCommandWithDDL.test(comment)) {
550+
console.log("/ps-update command with DDL")
551+
DDL_STATEMENTS = comment.match(regexCommandWithDDL)[1]
552+
DDL_UPDATED = "and database schema"
553+
} else {
554+
message = 'Invalid command, please use \`/ps-update`, or \`/ps-update "<DDL>"\`.'
555+
core.setOutput('error', message)
556+
throw new Error(message)
557+
}
558+
559+
core.info("DB branch name: " + BRANCH_NAME)
560+
core.setOutput('BRANCH_NAME', BRANCH_NAME)
561+
core.info("DDL statements: " + DDL_STATEMENTS)
562+
core.setOutput('DDL_STATEMENTS', DDL_STATEMENTS)
563+
564+
const ps_env_name = '.pscale/ps-env-' + REF + ".sh"
565+
// check whether file ps_env_name exists in the repo and given ref
566+
try {
567+
const response = await github.repos.getContent({
568+
...context.repo,
569+
path: ps_env_name,
570+
ref: REF
571+
})
572+
} catch (error) {
573+
// check error code
574+
if (error.status !== 404) {
575+
throw error
576+
} else {
577+
message = 'Script ' + ps_env_name + ' does not exists, please use \`/ps-create <branch> "<DDL>"\` to create a DB branch first.'
578+
core.setOutput('error', message)
579+
throw new Error(message)
580+
}
581+
}
582+
583+
const log_url = `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${process.env.GITHUB_RUN_ID}`
584+
585+
const commentBody = `\
586+
👋 __${context.actor}__, updating information ${DDL_UPDATED} of DB branch __${BRANCH_NAME}__ and Git branch __${REF}__ now ...
587+
588+
You can watch the progress and authorize access [here](${log_url}).
589+
`;
590+
591+
await github.issues.createComment({
592+
...context.repo,
593+
issue_number: context.issue.number,
594+
body: commentBody
595+
})
596+
597+
- name: Checkout
598+
uses: actions/checkout@v2
599+
with:
600+
ref: ${{ needs.prechecks.outputs.ref }}
601+
602+
- name: Update DB branch (info) - if asked, please click on displayed link to authenticate
603+
id: update-db-branch
604+
timeout-minutes: 3
605+
env:
606+
PLANETSCALE_SERVICE_TOKEN_NAME: ${{secrets.PLANETSCALE_SERVICE_TOKEN_NAME}}
607+
PLANETSCALE_SERVICE_TOKEN: ${{secrets.PLANETSCALE_SERVICE_TOKEN}}
608+
REF: ${{ needs.prechecks.outputs.ref }}
609+
ORG_NAME: ${{secrets.ORG_NAME}}
610+
GITHUB_USER: ${{github.actor}}
611+
DDL_STATEMENTS: ${{ steps.validate_params.outputs.DDL_STATEMENTS }}
612+
BRANCH_NAME: ${{ steps.validate_params.outputs.BRANCH_NAME }}
613+
run: . .pscale/ps-env-${REF}.sh && ./update-db-branch.sh "$BRANCH_NAME" "$DDL_STATEMENTS" "$DEPLOY_REQUEST_NUMBER"
614+
615+
- name: Database branch and DR update succeeded
616+
id: ps-update-succeeded
617+
if: success()
618+
uses: actions/github-script@v3
619+
env:
620+
DEPLOY_REQUEST_URL: ${{ steps.update-db-branch.outputs.DEPLOY_REQUEST_URL }}
621+
BRANCH_NAME: ${{ steps.update-db-branch.outputs.BRANCH_NAME }}
622+
BRANCH_URL: ${{ steps.update-db-branch.outputs.BRANCH_URL }}
623+
DB_NAME: ${{ steps.update-db-branch.outputs.DB_NAME }}
624+
ORG_NAME: ${{ steps.update-db-branch.ORG_NAME }}
625+
DDL_STATEMENTS: ${{ steps.validate_params.outputs.DDL_STATEMENTS }}
626+
BRANCH_DIFF: ${{ steps.update-db-branch.outputs.BRANCH_DIFF }}
627+
with:
628+
github-token: ${{ secrets.GITHUB_TOKEN }}
629+
script: |
630+
const { DEPLOY_REQUEST_URL, BRANCH_NAME, BRANCH_URL, DB_NAME, ORG_NAME, DDL_STATEMENTS, BRANCH_DIFF } = process.env;
631+
const log_url = `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${process.env.GITHUB_RUN_ID}`
632+
633+
const commentBody = `\
634+
### Updated database branch (info) successfully :tada:
635+
636+
* :seedling: __DB-Branch__: [${BRANCH_NAME}](${BRANCH_URL})
637+
* :train2: [Deploy request](${DEPLOY_REQUEST_URL})
638+
639+
<details>
640+
<summary>:scroll: Added DDL-Statements:</summary>
641+
642+
\`\`\`
643+
${DDL_STATEMENTS}
644+
\`\`\`
645+
646+
</details>
647+
648+
<details>
649+
<summary>📖 Calculated schema changes:</summary>
650+
651+
\`\`\`
652+
${BRANCH_DIFF}
653+
\`\`\`
654+
655+
</details>
656+
657+
`;
658+
659+
github.issues.createComment({
660+
...context.repo,
661+
issue_number: ${{ github.event.issue.number }},
662+
body: commentBody
663+
});
664+
665+
await github.reactions.createForIssueComment({
666+
...context.repo,
667+
comment_id: ${{github.event.comment.id}},
668+
content: '+1'
669+
})
670+
671+
await github.reactions.deleteForIssueComment({
672+
...context.repo,
673+
comment_id: ${{github.event.comment.id}},
674+
reaction_id: ${{needs.prechecks.outputs.eyes}}
675+
})
676+
677+
- name: /ps-update failed
678+
id: ps-update-failed
679+
if: cancelled() || failure()
680+
uses: actions/github-script@v3
681+
env:
682+
REF: ${{ needs.prechecks.outputs.ref }}
683+
message: ${{steps.validate_params.outputs.error}}
684+
DEPLOY_REQUEST_URL: ${{ steps.update-db-branch.outputs.DEPLOY_REQUEST_URL }}
685+
BRANCH_NAME: ${{ steps.update-db-branch.outputs.BRANCH_NAME }}
686+
BRANCH_URL: ${{ steps.update-db-branch.outputs.BRANCH_URL }}
687+
DB_NAME: ${{ steps.update-db-branch.outputs.DB_NAME }}
688+
ORG_NAME: ${{ steps.update-db-branch.ORG_NAME }}
689+
with:
690+
github-token: ${{ secrets.GITHUB_TOKEN }}
691+
script: |
692+
const { REF, message, DEPLOY_REQUEST_URL, BRANCH_NAME, BRANCH_URL, DB_NAME, ORG_NAME } = process.env;
693+
const log_url = `${process.env.GITHUB_SERVER_URL}/${context.repo.owner}/${context.repo.repo}/actions/runs/${process.env.GITHUB_RUN_ID}`
694+
695+
if (message === null || message === '') {
696+
errorMessage = `Updating failed for branch __${REF}__ :cry:. [View error logs](${log_url}).`
697+
} else {
698+
errorMessage = message
699+
}
700+
701+
github.issues.createComment({
702+
...context.repo,
703+
issue_number: ${{ github.event.issue.number }},
704+
body: errorMessage
705+
})
706+
707+
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+
})

ps-create-helper-functions.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,14 @@ function create-diff-for-ci {
7777
local deploy_request="https://app.planetscale.com/${ORG_NAME}/${DB_NAME}/deploy-requests/${deploy_request_number}"
7878
local BRANCH_DIFF="Diff could not be generated for deploy request $deploy_request"
7979

80-
local raw_output=`pscale deploy-request diff "$DB_NAME" "$deploy_request_number" --org "$ORG_NAME" --format=json | jq .[].raw`
80+
# read shell output line by line and assign to variable
81+
while read -r line; do
82+
raw_output="$raw_output\n$line"
83+
done < <(pscale deploy-request diff "$DB_NAME" "$deploy_request_number" --org "$ORG_NAME" --format=json | jq .[].raw)
84+
8185

8286
if [ $? -ne 0 ]; then
83-
BRANCH_DIFF="$BRANCH_DIFF : $raw_output"
87+
BRANCH_DIFF="$BRANCH_DIFF : ${raw_output}"
8488
else
8589
BRANCH_DIFF=$raw_output
8690
fi

update-db-branch.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
. use-pscale-docker-image.sh
4+
5+
. authenticate-ps.sh
6+
7+
BRANCH_NAME="$1"
8+
DDL_STATEMENTS="$2"
9+
DEPLOY_REQUEST_NUMBER="$3"
10+
11+
12+
. set-db-and-org-and-branch-name.sh
13+
14+
. ps-create-helper-functions.sh
15+
create-schema-change "$DB_NAME" "$BRANCH_NAME" "$ORG_NAME" "$DDL_STATEMENTS"
16+
create-diff-for-ci "$DB_NAME" "$ORG_NAME" "$DEPLOY_REQUEST_NUMBER"

0 commit comments

Comments
 (0)