-
-
Notifications
You must be signed in to change notification settings - Fork 8
350 lines (294 loc) · 13 KB
/
sync-rest.yml
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
name: Sync Rest
on:
workflow_dispatch:
inputs:
repos:
description: 'List of repos to rerun the syncing workflow for, formatted as JSON array, e.g. ["exercism/configlet"]'
required: true
default: ""
repository_dispatch:
types: [appends_update, repo_update]
push:
branches:
- main
paths-ignore:
- README.md
- LICENSE
- .github/labels.yml
- .github/workflows/sync-labels.yml
env:
BOT_USERNAME: "exercism-bot"
GIT_USERNAME: "Exercism Bot"
GIT_EMAIL: "[email protected]"
jobs:
open-tracking-issue:
name: Open tracking issue
runs-on: ubuntu-20.04
outputs:
issue-url: ${{ steps.open-tracking-issue.outputs.result }}
steps:
- name: Open tracking issue
id: open-tracking-issue
uses: actions/github-script@98814c53be79b1d30f795b907e553d8679345975
with:
result-encoding: string
script: |
const title = `[Tracking] ${context.workflow} - ${context.eventName} - ${context.actor} - ${context.sha}`
const label = '👁 tracking issue'
const existingIssue = (await github.paginate(github.rest.issues.listForRepo, {
owner: context.repo.owner,
repo: context.repo.repo,
labels: label,
state: 'open',
})).find((issue) => issue.title === title)
if (existingIssue) {
console.log(`::notice::Existing issue found: ${existingIssue.html_url}`)
return existingIssue.html_url
}
const issue = (await github.rest.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: title,
body: `Workflow run: https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
labels: [label]
})).data
return issue.html_url
fetch-repos:
name: Fetch & fork target repos
runs-on: ubuntu-20.04
outputs:
repos: ${{ steps.fetch-repos.outputs.result }}
steps:
- name: Fetch list of target repos
id: fetch-repos
uses: actions/github-script@98814c53be79b1d30f795b907e553d8679345975
with:
script: |
if (context.eventName === 'repository_dispatch') {
return context.payload.client_payload.repos
} else if (context.eventName === 'workflow_dispatch') {
return JSON.parse(context.payload.inputs.repos)
} else {
const allRepos = (await github.paginate(github.rest.search.repos, {
q: 'org:exercism+is:public+archived:false'
})).flatMap(({ full_name }) => [full_name])
const toolingRepos = (await github.paginate(github.rest.search.repos, {
q: 'org:exercism+topic:exercism-tooling+is:public+archived:false'
})).flatMap(({ full_name }) => [full_name])
const trackRepos = (await github.paginate(github.rest.search.repos, {
q: 'org:exercism+topic:exercism-track+is:public+archived:false'
})).flatMap(({ full_name }) => [full_name])
return allRepos.filter(r => !toolingRepos.includes(r) && !trackRepos.includes(r))
}
- name: Determine target repos needing to be forked
id: required-forks
uses: actions/github-script@98814c53be79b1d30f795b907e553d8679345975
with:
github-token: ${{ secrets.BOT_PERSONAL_ACCESS_TOKEN }}
script: |
const botRepos = await github.paginate(github.rest.repos.listForAuthenticatedUser, {
affiliation: 'owner'
})
// Replacing the bot username with exercism is slightly hacky, but necessary
// for the comparison below. Unfortunately, GitHub's API response does not seem
// to contain any reference to the upstream repo of the fork.
// TODO: The GraphQL API does have this info. Look into changing to that.
const botForks = botRepos.flatMap(({ full_name, fork }) => fork ? [full_name.replace(process.env.BOT_USERNAME, 'exercism')] : [])
const repos = ${{ steps.fetch-repos.outputs.result }}
return repos.filter(repo => !botForks.includes(repo))
- name: Fork repos that haven't been forked yet
if: steps.required-forks.outputs.result != '[]'
uses: actions/github-script@98814c53be79b1d30f795b907e553d8679345975
with:
github-token: ${{ secrets.BOT_PERSONAL_ACCESS_TOKEN }}
script: |
const reposFullNames = ${{ steps.required-forks.outputs.result }}
const repos = reposFullNames.map((fullName) => fullName.split('/')[1])
for (let repo of repos) {
await github.rest.repos.createFork({
owner: 'exercism',
repo
})
// Sleep a bit to avoid rate-limiting
await new Promise(x => setTimeout(x, 2000))
}
// Forking repos happens asynchronously on GitHub, therefore
// we need to sleep a bit to ensure it has been created
await new Promise(x => setTimeout(x, 60000))
abort-timer:
needs: [fetch-repos, open-tracking-issue]
runs-on: ubuntu-20.04
name: ⚠ 5 MINUTES TO ABORT
environment: abort-timer
steps:
- run: exit 0
push-to-repos:
needs: [fetch-repos, abort-timer, open-tracking-issue]
name: Push to ${{ matrix.repo }}
runs-on: ubuntu-20.04
timeout-minutes: 30
# Launch one job per track.
# This is probably less efficient than running everything in one job
# and manually cloning and checking out the repos. However, launching a job
# lets us use actions like actions/checkout.
# It also gives us a pretty job overview that makes it easy to spot issues with
# particular tracks.
strategy:
fail-fast: false
max-parallel: 1
matrix:
repo: ${{ fromJson(needs.fetch-repos.outputs.repos) }}
steps:
- name: Check if pull request has already been opened
id: pr-already-exists
uses: actions/github-script@98814c53be79b1d30f795b907e553d8679345975
env:
TARGET_REPO_FULLNAME: ${{ matrix.repo }}
TRACKING_ISSUE_URL: ${{ needs.open-tracking-issue.outputs.issue-url }}
with:
github-token: ${{ secrets.BOT_PERSONAL_ACCESS_TOKEN }}
script: |
const repo = process.env.TARGET_REPO_FULLNAME.split('/')[1]
const existingPr = (
await github.paginate(github.rest.issues.listForRepo, {
owner: "exercism",
repo: repo,
state: 'open',
})
).find((pr) =>
pr.body && pr.body.includes(process.env.TRACKING_ISSUE_URL)
)
if (existingPr) {
console.log(`::notice::Existing pull request found: ${existingPr.html_url}`)
return true
}
return false
############################################################
# ONLY RUN THE REST OF THE SCRIPT IF THE PR DOES NOT EXIST #
# All following steps must have: #
# if: steps.pr-already-exists.outputs.result == 'false' #
###########################################################
- name: Checkout main repo
if: steps.pr-already-exists.outputs.result == 'false'
uses: actions/checkout@24cb9080177205b6e8c946b17badbe402adc938f
with:
path: main
- name: Checkout target repo
if: steps.pr-already-exists.outputs.result == 'false'
uses: actions/checkout@24cb9080177205b6e8c946b17badbe402adc938f
with:
repository: ${{ matrix.repo }}
token: ${{ secrets.BOT_PERSONAL_ACCESS_TOKEN }}
path: track-repo
fetch-depth: 0
- name: Checkout new branch on track repo
if: steps.pr-already-exists.outputs.result == 'false'
id: branch
run: |
cd track-repo
# github.run_id is a unique number for each run within a repository.
# WARNING: the run_id will NOT be updated if a workflow is re-run
branch="🤖/org-wide-files/${{ github.run_id }}"
git checkout -b "$branch"
echo "name=$branch" >> $GITHUB_OUTPUT
- name: Copy globally synced files to target repo
if: steps.pr-already-exists.outputs.result == 'false'
run: |
cp -a main/global-files/. track-repo/
- name: Determine if repo is a tooling repo
if: steps.pr-already-exists.outputs.result == 'false'
id: is-tooling-repo
uses: actions/github-script@98814c53be79b1d30f795b907e553d8679345975
env:
TARGET_REPO_FULLNAME: ${{ matrix.repo }}
with:
github-token: ${{ secrets.BOT_PERSONAL_ACCESS_TOKEN }}
script: |
const repoTopics = await github.paginate(github.rest.repos.getAllTopics, {
owner: 'exercism',
repo: process.env.TARGET_REPO_FULLNAME.split('/')[1]
})
return repoTopics[0].names.includes('exercism-tooling')
- name: Copy tooling-only synced files to target repo
if: steps.is-tooling-repo.outputs.result == 'true'
run: |
cp -a main/tooling-files/. track-repo/
- name: Determine if repo is a track repo
if: steps.pr-already-exists.outputs.result == 'false'
id: is-track-repo
uses: actions/github-script@98814c53be79b1d30f795b907e553d8679345975
env:
TARGET_REPO_FULLNAME: ${{ matrix.repo }}
with:
github-token: ${{ secrets.BOT_PERSONAL_ACCESS_TOKEN }}
script: |
const repoTopics = await github.paginate(github.rest.repos.getAllTopics, {
owner: 'exercism',
repo: process.env.TARGET_REPO_FULLNAME.split('/')[1]
})
return repoTopics[0].names.includes('exercism-track')
- name: Copy tracks-only synced files to target repo
if: steps.is-track-repo.outputs.result == 'true'
run: |
cp -a main/tracks-files/. track-repo/
- name: Apply transformations based on track config
if: steps.pr-already-exists.outputs.result == 'false'
env:
TRACK: ${{ matrix.repo }}
run: julia --color=yes main/scripts/apply-track-config.jl
- name: Check for changes
if: steps.pr-already-exists.outputs.result == 'false'
id: changes
run: |
cd track-repo
if [ -z "$(git status --porcelain)" ]; then
echo "No files have changed."
exit 0
fi
echo "changes=true" >> $GITHUB_OUTPUT
########################################################
# ONLY RUN THE REST OF THE SCRIPT IF THERE ARE CHANGES #
# All following steps must have: #
# if: steps.changes.outputs.changes == 'true' #
########################################################
- name: Configure the git user
if: steps.changes.outputs.changes == 'true'
run: |
git config --global user.email "$GIT_EMAIL"
git config --global user.name "$GIT_USERNAME"
- name: Commit and push changes
if: steps.changes.outputs.changes == 'true'
env:
TARGET_REPO_FULLNAME: ${{ matrix.repo }}
BOT_PERSONAL_ACCESS_TOKEN: ${{ secrets.BOT_PERSONAL_ACCESS_TOKEN }}
run: |
cd track-repo
# Show diff for debugging
git diff
git add .
# The commit message assumes that one push maps to exactly one commit
# If a push contains several commits, the commit message will only link to the last one
git commit -m "🤖 Sync org-wide files to upstream repo" -m "More info: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/commit/$GITHUB_SHA"
# Push commit
target_repo="${TARGET_REPO_FULLNAME/exercism/$BOT_USERNAME}"
git push --force "$GITHUB_SERVER_URL/$target_repo.git"
- name: Open pull request on track repo
if: steps.changes.outputs.changes == 'true'
uses: actions/github-script@98814c53be79b1d30f795b907e553d8679345975
env:
TARGET_REPO_FULLNAME: ${{ matrix.repo }}
BRANCH_NAME: ${{ steps.branch.outputs.name }}
TRACKING_ISSUE_URL: ${{ needs.open-tracking-issue.outputs.issue-url }}
with:
github-token: ${{ secrets.BOT_PERSONAL_ACCESS_TOKEN }}
script: |
const repo = process.env.TARGET_REPO_FULLNAME.split('/')[1]
github.rest.pulls.create({
owner: 'exercism',
repo,
title: '🤖 Sync org-wide files to upstream repo',
head: `exercism-bot:${process.env.BRANCH_NAME}`,
base: 'main',
body: `ℹ More info: ${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/commit/${process.env.GITHUB_SHA}\n👁 Tracking issue: ${process.env.TRACKING_ISSUE_URL}`,
maintainer_can_modify: false
})