traceability-trigger #31
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: traceability-trigger | |
permissions: | |
checks: write | |
pull-requests: read | |
on: | |
issue_comment: | |
types: | |
- created | |
- edited | |
- deleted | |
jobs: | |
traceability-trigger: | |
runs-on: ubuntu-latest | |
if: github.event.issue.pull_request | |
steps: | |
- name: "Get Pull Request for Comment" | |
uses: actions/github-script@v6 | |
id: get-commit | |
with: | |
script: | | |
const request = { | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
pull_number: context.issue.number | |
} | |
core.info(`Getting PR #${request.pull_number} from ${request.owner}/${request.repo}`) | |
try { | |
const result = await github.rest.pulls.get(request) | |
core.debug(result) | |
return result.data | |
} catch (err) { | |
core.setFailed(`Request failed with error ${err}`) | |
} | |
- name: "Get Check Run For Pull Request" | |
uses: actions/github-script@v6 | |
id: get-check-runs | |
with: | |
script: | | |
const request = { | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
ref: `${{ fromJSON(steps.get-commit.outputs.result).head.sha }}`, | |
check_name: 'traceability', | |
filter: 'latest' | |
} | |
core.info(`Getting check-runs for ${request.ref} from ${request.owner}/${request.repo}`) | |
try { | |
const result = await github.rest.checks.listForRef(request) | |
core.debug(result) | |
if (result.data.check_runs.length > 0) { | |
return { | |
runs: JSON.stringify(result.data.check_runs), | |
continue: true | |
} | |
} | |
else { | |
return { | |
continue: false | |
} | |
} | |
} catch (err) { | |
core.setFailed(`Request failed with error ${err}`) | |
} | |
- name: "Cancel Check Runs" | |
uses: actions/github-script@v6 | |
id: cancel-check-run | |
if: ${{ fromJSON(steps.get-check-runs.outputs.result).continue }} | |
with: | |
script: | | |
const checkRuns = ${{ fromJSON(steps.get-check-runs.outputs.result).runs }} | |
for (const checkRun of checkRuns) { | |
const checkRunId = checkRun.id | |
const checkRunStatus = checkRun.status | |
const request = { | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
check_run_id: checkRunId, | |
conclusion: 'cancelled', | |
output: { | |
title: 'Cancelled.', | |
summary: 'Check Run cancelled by "traceability-trigger.yaml".', | |
} | |
} | |
if (checkRunStatus !== 'completed') { | |
core.info(`Cancelling check-run ${checkRunId} from ${request.owner}/${request.repo}`) | |
try { | |
const result = await github.rest.checks.update(request) | |
core.debug(result) | |
} catch (err) { | |
core.setFailed(`Request failed with error ${err}`) | |
} | |
} else { | |
core.info(`No need to cancel check-run ${checkRunId}`) | |
} | |
} | |
- name: "Re-request Check Suite" | |
uses: actions/github-script@v6 | |
id: rerequest-check-suite | |
if: ${{ fromJSON(steps.get-check-runs.outputs.result).continue }} | |
with: | |
script: | | |
const checkRuns = ${{ fromJSON(steps.get-check-runs.outputs.result).runs }} | |
for (const checkRun of checkRuns) { | |
const checkSuiteId = checkRun.check_suite.id | |
const request = { | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
check_suite_id: checkSuiteId | |
} | |
try { | |
core.info(`Re-requesting check-suite ${request.check_suite_id} from ${request.owner}/${request.repo}`) | |
const result = await github.rest.checks.rerequestSuite(request) | |
core.debug(result) | |
} catch (err) { | |
// we cancelled any running check-suite in the step before this one, so if we get an error here saying | |
// a check-suite is already re-running it means something else triggered it to rerun between this step | |
// and the last. That is what we wanted to do anyways, so we can ignore these errors. | |
if (err.response.data.message === 'This check suite is already re-running.') { | |
return | |
} | |
core.setFailed(`Request failed with error ${err}`) | |
} | |
} |