|
| 1 | +name: traceability-trigger |
| 2 | + |
| 3 | +permissions: |
| 4 | + checks: write |
| 5 | + pull-requests: read |
| 6 | + |
| 7 | +on: |
| 8 | + issue_comment: |
| 9 | + types: |
| 10 | + - created |
| 11 | + - edited |
| 12 | + - deleted |
| 13 | + |
| 14 | +jobs: |
| 15 | + traceability-trigger: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + if: github.event.issue.pull_request |
| 18 | + steps: |
| 19 | + - name: "Get Pull Request for Comment" |
| 20 | + uses: actions/github-script@v6 |
| 21 | + id: get-commit |
| 22 | + with: |
| 23 | + script: | |
| 24 | + const request = { |
| 25 | + owner: context.repo.owner, |
| 26 | + repo: context.repo.repo, |
| 27 | + pull_number: context.issue.number |
| 28 | + } |
| 29 | + core.info(`Getting PR #${request.pull_number} from ${request.owner}/${request.repo}`) |
| 30 | + try { |
| 31 | + const result = await github.rest.pulls.get(request) |
| 32 | + core.debug(result) |
| 33 | + return result.data |
| 34 | + } catch (err) { |
| 35 | + core.setFailed(`Request failed with error ${err}`) |
| 36 | + } |
| 37 | + - name: "Get Check Run For Pull Request" |
| 38 | + uses: actions/github-script@v6 |
| 39 | + id: get-check-runs |
| 40 | + with: |
| 41 | + script: | |
| 42 | + const request = { |
| 43 | + owner: context.repo.owner, |
| 44 | + repo: context.repo.repo, |
| 45 | + ref: `${{ fromJSON(steps.get-commit.outputs.result).head.sha }}`, |
| 46 | + check_name: 'traceability', |
| 47 | + filter: 'latest' |
| 48 | + } |
| 49 | + core.info(`Getting check-runs for ${request.ref} from ${request.owner}/${request.repo}`) |
| 50 | + try { |
| 51 | + const result = await github.rest.checks.listForRef(request) |
| 52 | + core.debug(result) |
| 53 | + if (result.data.check_runs.length > 0) { |
| 54 | + return { |
| 55 | + runs: JSON.stringify(result.data.check_runs), |
| 56 | + continue: true |
| 57 | + } |
| 58 | + } |
| 59 | + else { |
| 60 | + return { |
| 61 | + continue: false |
| 62 | + } |
| 63 | + } |
| 64 | + } catch (err) { |
| 65 | + core.setFailed(`Request failed with error ${err}`) |
| 66 | + } |
| 67 | + - name: "Cancel Check Runs" |
| 68 | + uses: actions/github-script@v6 |
| 69 | + id: cancel-check-run |
| 70 | + if: ${{ fromJSON(steps.get-check-runs.outputs.result).continue }} |
| 71 | + with: |
| 72 | + script: | |
| 73 | + const checkRuns = ${{ fromJSON(steps.get-check-runs.outputs.result).runs }} |
| 74 | + for (const checkRun of checkRuns) { |
| 75 | + const checkRunId = checkRun.id |
| 76 | + const checkRunStatus = checkRun.status |
| 77 | + const request = { |
| 78 | + owner: context.repo.owner, |
| 79 | + repo: context.repo.repo, |
| 80 | + check_run_id: checkRunId, |
| 81 | + conclusion: 'cancelled', |
| 82 | + output: { |
| 83 | + title: 'Cancelled.', |
| 84 | + summary: 'Check Run cancelled by "traceability-trigger.yaml".', |
| 85 | + } |
| 86 | + } |
| 87 | + if (checkRunStatus !== 'completed') { |
| 88 | + core.info(`Cancelling check-run ${checkRunId} from ${request.owner}/${request.repo}`) |
| 89 | + try { |
| 90 | + const result = await github.rest.checks.update(request) |
| 91 | + core.debug(result) |
| 92 | + } catch (err) { |
| 93 | + core.setFailed(`Request failed with error ${err}`) |
| 94 | + } |
| 95 | + } else { |
| 96 | + core.info(`No need to cancel check-run ${checkRunId}`) |
| 97 | + } |
| 98 | + } |
| 99 | + - name: "Re-request Check Suite" |
| 100 | + uses: actions/github-script@v6 |
| 101 | + id: rerequest-check-suite |
| 102 | + if: ${{ fromJSON(steps.get-check-runs.outputs.result).continue }} |
| 103 | + with: |
| 104 | + script: | |
| 105 | + const checkRuns = ${{ fromJSON(steps.get-check-runs.outputs.result).runs }} |
| 106 | + for (const checkRun of checkRuns) { |
| 107 | + const checkSuiteId = checkRun.check_suite.id |
| 108 | + const request = { |
| 109 | + owner: context.repo.owner, |
| 110 | + repo: context.repo.repo, |
| 111 | + check_suite_id: checkSuiteId |
| 112 | + } |
| 113 | + try { |
| 114 | + core.info(`Re-requesting check-suite ${request.check_suite_id} from ${request.owner}/${request.repo}`) |
| 115 | + const result = await github.rest.checks.rerequestSuite(request) |
| 116 | + core.debug(result) |
| 117 | + } catch (err) { |
| 118 | + // we cancelled any running check-suite in the step before this one, so if we get an error here saying |
| 119 | + // a check-suite is already re-running it means something else triggered it to rerun between this step |
| 120 | + // and the last. That is what we wanted to do anyways, so we can ignore these errors. |
| 121 | + if (err.response.data.message === 'This check suite is already re-running.') { |
| 122 | + return |
| 123 | + } |
| 124 | + core.setFailed(`Request failed with error ${err}`) |
| 125 | + } |
| 126 | + } |
0 commit comments