-
Notifications
You must be signed in to change notification settings - Fork 30
126 lines (123 loc) · 4.72 KB
/
traceability-comments-trigger.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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}`)
}
}