|
| 1 | +name: CI Mode Command |
| 2 | + |
| 3 | +on: |
| 4 | + issue_comment: |
| 5 | + types: [created] |
| 6 | + |
| 7 | +permissions: |
| 8 | + actions: write |
| 9 | + contents: read |
| 10 | + issues: write |
| 11 | + pull-requests: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + set_ci_mode: |
| 15 | + if: ${{ github.event.issue.pull_request && startsWith(github.event.comment.body, '/ci ') }} |
| 16 | + runs-on: ubuntu-latest |
| 17 | + steps: |
| 18 | + - name: Parse command and authorize |
| 19 | + id: parse |
| 20 | + uses: actions/github-script@v7 |
| 21 | + with: |
| 22 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 23 | + script: | |
| 24 | + const body = (context.payload.comment.body || "").trim(); |
| 25 | + const match = body.match(/^\/ci\s+(small|medium|large)\s*$/i); |
| 26 | + if (!match) { |
| 27 | + core.setOutput("valid", "false"); |
| 28 | + core.setOutput("allowed", "false"); |
| 29 | + core.setOutput("reason", "invalid"); |
| 30 | + core.setOutput("message", "Invalid command. Use `/ci small`, `/ci medium`, or `/ci large`."); |
| 31 | + return; |
| 32 | + } |
| 33 | +
|
| 34 | + const mode = match[1].toLowerCase(); |
| 35 | + const assoc = (context.payload.comment.author_association || "").toUpperCase(); |
| 36 | + const isRepoCollaborator = ["OWNER", "MEMBER", "COLLABORATOR"].includes(assoc); |
| 37 | + const isMaintainer = ["OWNER", "MEMBER"].includes(assoc); |
| 38 | +
|
| 39 | + if (!isRepoCollaborator) { |
| 40 | + core.setOutput("valid", "true"); |
| 41 | + core.setOutput("allowed", "false"); |
| 42 | + core.setOutput("reason", "permission"); |
| 43 | + core.setOutput("message", "You are not authorized to set CI mode on this repository."); |
| 44 | + core.setOutput("mode", mode); |
| 45 | + return; |
| 46 | + } |
| 47 | +
|
| 48 | + if (mode === "large" && !isMaintainer) { |
| 49 | + core.setOutput("valid", "true"); |
| 50 | + core.setOutput("allowed", "false"); |
| 51 | + core.setOutput("reason", "large-restricted"); |
| 52 | + core.setOutput("message", "Only repository maintainers can run `/ci large`."); |
| 53 | + core.setOutput("mode", mode); |
| 54 | + return; |
| 55 | + } |
| 56 | +
|
| 57 | + core.setOutput("valid", "true"); |
| 58 | + core.setOutput("allowed", "true"); |
| 59 | + core.setOutput("mode", mode); |
| 60 | +
|
| 61 | + - name: Respond to invalid or unauthorized command |
| 62 | + if: ${{ steps.parse.outputs.allowed != 'true' }} |
| 63 | + uses: actions/github-script@v7 |
| 64 | + with: |
| 65 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 66 | + script: | |
| 67 | + const message = ${{ toJSON(steps.parse.outputs.message) }}; |
| 68 | + await github.rest.issues.createComment({ |
| 69 | + owner: context.repo.owner, |
| 70 | + repo: context.repo.repo, |
| 71 | + issue_number: context.issue.number, |
| 72 | + body: message |
| 73 | + }); |
| 74 | +
|
| 75 | + - name: Set CI mode label and dispatch CI |
| 76 | + if: ${{ steps.parse.outputs.allowed == 'true' }} |
| 77 | + uses: actions/github-script@v7 |
| 78 | + with: |
| 79 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 80 | + script: | |
| 81 | + const mode = `${{ steps.parse.outputs.mode }}`; |
| 82 | + const desired = `ci:${mode}`; |
| 83 | + const { owner, repo } = context.repo; |
| 84 | + const issue_number = context.issue.number; |
| 85 | + const repoFullName = `${owner}/${repo}`.toLowerCase(); |
| 86 | +
|
| 87 | + const { data: pr } = await github.rest.pulls.get({ |
| 88 | + owner, |
| 89 | + repo, |
| 90 | + pull_number: issue_number |
| 91 | + }); |
| 92 | +
|
| 93 | + const { data: existing } = await github.rest.issues.listLabelsOnIssue({ |
| 94 | + owner, |
| 95 | + repo, |
| 96 | + issue_number |
| 97 | + }); |
| 98 | +
|
| 99 | + const keep = existing |
| 100 | + .map((l) => l.name) |
| 101 | + .filter((name) => !/^ci:(small|medium|large)$/.test(name)); |
| 102 | + keep.push(desired); |
| 103 | +
|
| 104 | + await github.rest.issues.setLabels({ |
| 105 | + owner, |
| 106 | + repo, |
| 107 | + issue_number, |
| 108 | + labels: keep |
| 109 | + }); |
| 110 | +
|
| 111 | + const headRepoFullName = (pr.head.repo?.full_name || "").toLowerCase(); |
| 112 | + const dispatchRef = headRepoFullName === repoFullName ? pr.head.ref : "main"; |
| 113 | +
|
| 114 | + await github.rest.actions.createWorkflowDispatch({ |
| 115 | + owner, |
| 116 | + repo, |
| 117 | + workflow_id: "dbt_ci_modes.yml", |
| 118 | + ref: dispatchRef, |
| 119 | + inputs: { |
| 120 | + pr_number: String(issue_number), |
| 121 | + mode |
| 122 | + } |
| 123 | + }); |
| 124 | +
|
| 125 | + await github.rest.issues.createComment({ |
| 126 | + owner, |
| 127 | + repo, |
| 128 | + issue_number, |
| 129 | + body: `CI mode set to \`${mode}\` via \`${desired}\`. Triggered Tuva CI now (ref: \`${dispatchRef}\`).` |
| 130 | + }); |
0 commit comments