-
Notifications
You must be signed in to change notification settings - Fork 29.4k
198 lines (183 loc) · 8.73 KB
/
Copy pathnotify_test_workflow.yml
File metadata and controls
198 lines (183 loc) · 8.73 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
# Intentionally has a general name.
# because the test status check created in GitHub Actions
# currently randomly picks any associated workflow.
# So, the name was changed to make sense in that context too.
# See also https://github.qkg1.topmunity/t/specify-check-suite-when-creating-a-checkrun/118380/10
name: On pull request update
on:
pull_request_target:
types: [opened, reopened, synchronize]
jobs:
notify:
name: Notify test workflow
runs-on: ubuntu-slim
permissions:
actions: read
checks: write
steps:
- name: "Notify test workflow"
uses: actions/github-script@v9
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const endpoint = 'GET /repos/:owner/:repo/actions/workflows/:id/runs?&branch=:branch'
const check_run_endpoint = 'GET /repos/:owner/:repo/commits/:ref/check-runs?per_page=100'
// TODO: Should use pull_request.user and pull_request.user.repos_url?
// If a different person creates a commit to another forked repo,
// it wouldn't be able to detect.
const params = {
owner: context.payload.pull_request.head.repo.owner.login,
repo: context.payload.pull_request.head.repo.name,
id: 'build_main.yml',
branch: context.payload.pull_request.head.ref,
}
const check_run_params = {
owner: context.payload.pull_request.head.repo.owner.login,
repo: context.payload.pull_request.head.repo.name,
ref: context.payload.pull_request.head.ref,
}
console.log('Ref: ' + context.payload.pull_request.head.ref)
console.log('SHA: ' + context.payload.pull_request.head.sha)
const name = 'Build'
const head_sha = context.payload.pull_request.head.sha
let status = 'queued'
// Wait 3 seconds to make sure the fork repository triggered a workflow.
await new Promise(r => setTimeout(r, 3000))
// The workflow run for this exact SHA may not be registered yet, and the
// listing endpoint orders by most recent, so blindly taking the first run
// can return a stale run from a previous push. Re-query a few times looking
// for the run whose head_sha matches this PR's head SHA before giving up.
let matched_run
let any_runs = false
let retryCount = 0
while (retryCount < 3) {
let runs
try {
runs = await github.request(endpoint, params)
} catch (error) {
console.error(error)
// Assume that runs were not found.
}
if (runs && runs.data.workflow_runs.length > 0) {
any_runs = true
matched_run = runs.data.workflow_runs.find(r => r.head_sha == head_sha)
if (matched_run) {
break
}
}
retryCount++
if (retryCount < 3) {
await new Promise(resolve => setTimeout(resolve, 3000))
}
}
// If we saw runs but none matched the PR head SHA, a newer commit was pushed
// and a fresh notify run will handle it; nothing useful to report for this SHA.
if (any_runs && !matched_run) {
throw new Error('There was a new unsynced commit pushed. Please retrigger the workflow.');
}
if (!matched_run) {
status = 'completed'
const conclusion = 'action_required'
github.rest.checks.create({
owner: context.repo.owner,
repo: context.repo.repo,
name: name,
head_sha: head_sha,
status: status,
conclusion: conclusion,
output: {
title: 'Workflow run detection failed',
summary: `
Unable to detect the workflow run for testing the changes in your PR.
1. If you did not enable GitHub Actions in your forked repository, please enable it by clicking the button as shown in the image below. See also [Managing Github Actions Settings for a repository](https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/enabling-features-for-your-repository/managing-github-actions-settings-for-a-repository) for more details.
2. It is possible your branch is based on the old \`master\` branch in Apache Spark, please sync your branch to the latest master branch. For example as below:
\`\`\`bash
git fetch upstream
git rebase upstream/master
git push origin YOUR_BRANCH --force
\`\`\``,
images: [
{
alt: 'enabling workflows button',
image_url: 'https://raw.githubusercontent.com/apache/spark/master/.github/workflows/images/workflow-enable-button.png'
}
]
}
})
} else {
const run_id = matched_run.id
const actions_url = 'https://github.com/'
+ context.payload.pull_request.head.repo.full_name
+ '/actions/runs/'
+ run_id
console.log('Actions URL: ' + actions_url)
// Here we get the check run ID to provide a Check run view instead of the
// Actions view, see also SPARK-37879. The check run may not have materialized
// yet (it is created later than the workflow run, especially when the matrix
// is queued), so this is best-effort: if it cannot be found, we fall back to
// the Actions run URL rather than failing and leaving the PR with no Build
// check for the scheduled updater to sync.
let retryCount = 0;
let check_run_head;
while (retryCount < 3) {
const check_runs = await github.request(check_run_endpoint, check_run_params);
check_run_head = check_runs.data.check_runs.find(
r => r.name === "Run / Check changes" && r.head_sha == head_sha);
if (check_run_head) {
break;
}
retryCount++;
if (retryCount < 3) {
await new Promise(resolve => setTimeout(resolve, 3000));
}
}
let summary_url = actions_url
if (check_run_head) {
summary_url = 'https://github.com/'
+ context.payload.pull_request.head.repo.full_name
+ '/runs/'
+ check_run_head.id
console.log('Check run URL: ' + summary_url)
} else {
console.log('Check run not found; falling back to Actions URL: ' + actions_url)
}
github.rest.checks.create({
owner: context.repo.owner,
repo: context.repo.repo,
name: name,
head_sha: head_sha,
status: status,
output: {
title: 'Test results',
summary: '[See test results](' + summary_url + ')\n\n'
+ 'If the tests fail for reasons unrelated to this pull request, '
+ 'please rerun the workflow in your forked repository.\n'
+ 'If the failures are related to this pull request, '
+ 'please investigate them and push follow-up changes.',
text: JSON.stringify({
owner: context.payload.pull_request.head.repo.owner.login,
repo: context.payload.pull_request.head.repo.name,
run_id: run_id
})
},
details_url: actions_url,
})
}