duel 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/docs/ci.md CHANGED
@@ -61,23 +61,33 @@ jobs:
61
61
  with:
62
62
  fetch-depth: 0 # the baseline comes from history, tags included
63
63
  persist-credentials: false
64
- # (And whatever else your tests need set up: packages, services.)
65
- - uses: ruby/setup-ruby@v1
66
- with:
67
- ruby-version: "3.4" # (or none, with a .ruby-version)
68
- bundler-cache: true
69
64
  # The labels of the pull request (or for a push, of the one that
70
65
  # merged it), for c.accept_label in .duel.rb, as a JSON array. They're
71
- # read as the job runs, so after adding the label, run it again.
66
+ # read as the job runs, and adding the label (or taking it off) runs
67
+ # the job again: see the Perf comment workflow.
68
+ # On a schedule, the last commit since the last release that merged a
69
+ # pull request with the label (ACCEPT_LABEL, as .duel.rb names it):
70
+ # what that slowed down was accepted, so it's compared with instead.
72
71
  - name: Find the pull request's labels
73
72
  id: labels
74
- if: github.event_name == 'pull_request' || github.event_name == 'merge_group' || github.event_name == 'push'
73
+ if: github.event_name == 'pull_request' || github.event_name == 'merge_group' || github.event_name == 'push' || github.event_name == 'schedule'
75
74
  continue-on-error: true # (without them, nothing's accepted)
76
75
  env:
77
76
  GH_TOKEN: ${{ github.token }}
78
77
  PR: ${{ github.event.pull_request.number }}
79
78
  QUEUED: ${{ github.event.merge_group.head_ref }}
79
+ ACCEPT_LABEL: 'duel: accept'
80
+ RELEASE_TAGS: "v*"
80
81
  run: |
82
+ if [ "$GITHUB_EVENT_NAME" = schedule ]; then
83
+ tag=$(git describe --tags --abbrev=0 --match "$RELEASE_TAGS" HEAD^ 2>/dev/null) || exit 0
84
+ since=$(TZ=UTC git log -1 --date=format-local:%Y-%m-%dT%H:%M:%SZ --format=%cd "$tag")
85
+ merged=$(gh pr list --repo "$GITHUB_REPOSITORY" --state merged --label "$ACCEPT_LABEL" --search "merged:>=$since" --limit 1000 \
86
+ --json mergeCommit --jq '.[].mergeCommit.oid')
87
+ accepted=$(git rev-list "$tag..HEAD" | grep -Fx -m 1 -f <(echo "$merged")) || accepted=
88
+ echo "accepted=$accepted" >> "$GITHUB_OUTPUT"
89
+ exit 0
90
+ fi
81
91
  # (In a merge queue, the last pull request queued, which the queue's
82
92
  # branch names: gh-readonly-queue/main/pr-123-<sha>.)
83
93
  if [ -n "$QUEUED" ]; then
@@ -90,6 +100,11 @@ jobs:
90
100
  labels=$(gh api "repos/$GITHUB_REPOSITORY/commits/$GITHUB_SHA/pulls" --jq '[.[0].labels[]?.name]')
91
101
  fi
92
102
  echo "labels=$labels" >> "$GITHUB_OUTPUT"
103
+ # (And whatever else your tests need set up: packages, services.)
104
+ - uses: ruby/setup-ruby@v1
105
+ with:
106
+ ruby-version: "3.4" # (or none, with a .ruby-version)
107
+ bundler-cache: true
93
108
  # Against the branch it targets for a pull request, the queue's base
94
109
  # for a merge queue, what was there before for a push (or as
95
110
  # c.push_base in .duel.rb says, the last commit Duel passed), and the
@@ -105,6 +120,7 @@ jobs:
105
120
  # with itself, which checks the setup.)
106
121
  DUEL_FORCE: ${{ github.event_name == 'workflow_dispatch' && '1' || '' }}
107
122
  DUEL_LABELS: ${{ steps.labels.outputs.labels }}
123
+ DUEL_ACCEPTED: ${{ steps.labels.outputs.accepted }}
108
124
  run: bundle exec duel --markdown "$RUNNER_TEMP/duel/duel.md"
109
125
  # For a pull request, what Duel said, for the Perf comment workflow,
110
126
  # which can't see this job's outputs.
@@ -126,148 +142,6 @@ jobs:
126
142
  path: ${{ runner.temp }}/duel/
127
143
  if-no-files-found: ignore # none when Duel didn't get to compare
128
144
  retention-days: 1
129
-
130
- # The report, as a new comment on a pushed commit, which notifies its
131
- # author, when Duel says to, as for a pull request (see the Perf comment
132
- # workflow). It does only where a regression in a push passes, with
133
- # c.fail_on = :pull_requests in .duel.rb, so the comment says what the
134
- # check doesn't. Its last comment is the last one on any commit. This
135
- # job checks nothing out and runs none of the code.
136
- commit-comment:
137
- name: Comment on the commit
138
- needs: duel
139
- if: ${{ !cancelled() && github.event_name == 'push' && (needs.duel.outputs.comment == 'post' || needs.duel.outputs.comment == 'change') }}
140
- runs-on: ubuntu-latest
141
- permissions:
142
- contents: write # to comment on the commit with the report
143
- steps:
144
- - name: Get the report
145
- id: report
146
- continue-on-error: true # there's none when the Duel job failed before comparing
147
- uses: actions/download-artifact@v8
148
- with:
149
- name: duel-report
150
- path: ${{ runner.temp }}/report
151
- - name: Comment on the commit
152
- if: steps.report.outcome == 'success'
153
- env:
154
- GH_TOKEN: ${{ github.token }}
155
- COMMENT: ${{ needs.duel.outputs.comment }}
156
- STATUS: ${{ needs.duel.outputs.status }}
157
- run: |
158
- marker="<!-- duel"
159
- last=$(gh api --paginate "repos/$GITHUB_REPOSITORY/comments" \
160
- --jq ".[] | select(.user.login == \"github-actions[bot]\") | .body | split(\"\n\")[0] | rtrimstr(\"\r\") | select(startswith(\"$marker status=\"))" | tail -n 1)
161
- if [ "$COMMENT" = post ] || { [ -n "$last" ] && [ "$last" != "$marker status=$STATUS -->" ]; }; then
162
- { echo "$marker status=$STATUS -->"; cat "$RUNNER_TEMP/report/duel.md"; } > "$RUNNER_TEMP/comment.md"
163
- gh api --method POST "repos/$GITHUB_REPOSITORY/commits/$GITHUB_SHA/comments" -F body=@"$RUNNER_TEMP/comment.md" > /dev/null
164
- fi
165
-
166
- # With c.push_base = :last_passed in .duel.rb, a push is compared with
167
- # the last commit Duel passed, which a branch marks (each branch pushed
168
- # has its own), so slowdowns too small for any one push to fail add up
169
- # until they fail one. This job moves the branch to a push that passed
170
- # (or makes it), but never back: where a newer push's run moved it
171
- # first, it stays. It checks nothing out and runs none of the code. To
172
- # accept a regression, move the branch yourself (for main, git push
173
- # origin main:duel/passed/main).
174
- passed:
175
- name: Mark the commit passed
176
- needs: duel
177
- if: ${{ !cancelled() && github.event_name == 'push' && needs.duel.outputs.passed == 'true' }}
178
- runs-on: ubuntu-latest
179
- permissions:
180
- contents: write # to move the branch
181
- steps:
182
- - name: Move the branch to the commit
183
- env:
184
- GH_TOKEN: ${{ github.token }}
185
- BRANCH: duel/passed/${{ github.ref_name }}
186
- run: |
187
- # (Git can't make a branch where another's name is a folder of its
188
- # name: a branch named duel blocks duel/passed/main. If one does,
189
- # this names it.)
190
- blocked() {
191
- prefix=$BRANCH
192
- while [ "${prefix%/*}" != "$prefix" ]; do
193
- prefix=${prefix%/*}
194
- if gh api "repos/$GITHUB_REPOSITORY/git/ref/heads/$prefix" > /dev/null 2>&1; then
195
- echo "::error::The branch $prefix keeps Duel from making $BRANCH, as Git can't have both. Delete it (git push origin --delete $prefix), or rename it."
196
- fi
197
- done
198
- exit 1
199
- }
200
- # (Forward: GitHub moves it only where it's behind the push.)
201
- gh api --method PATCH "repos/$GITHUB_REPOSITORY/git/refs/heads/$BRANCH" -f sha="$GITHUB_SHA" > /dev/null 2>&1 && exit 0
202
- at=$(gh api "repos/$GITHUB_REPOSITORY/git/ref/heads/$BRANCH" --jq .object.sha 2>/dev/null) || at=
203
- if [ -z "$at" ]; then
204
- gh api --method POST "repos/$GITHUB_REPOSITORY/git/refs" -f ref="refs/heads/$BRANCH" -f sha="$GITHUB_SHA" > /dev/null || blocked
205
- exit 0
206
- fi
207
- compared=$(gh api "repos/$GITHUB_REPOSITORY/compare/$GITHUB_SHA...$at" --jq .status 2>&1) || case "$compared" in
208
- # (GitHub can't compare commits with no history in common.)
209
- *"No common ancestor"*) compared=unrelated ;;
210
- esac
211
- case "$compared" in
212
- # (A newer push's run moved it, or this one's did, run again.)
213
- ahead | identical) ;;
214
- # (History rewritten, a force push's, say, or replaced: moved anyway.)
215
- diverged | behind | unrelated) gh api --method PATCH "repos/$GITHUB_REPOSITORY/git/refs/heads/$BRANCH" -f sha="$GITHUB_SHA" -F force=true > /dev/null ;;
216
- *) echo "::error::Couldn't compare $BRANCH with this push: $compared" && exit 1 ;;
217
- esac
218
-
219
- # Duel's badge, for the README: green (perf guarded) or red (perf
220
- # regressed), as the last push to the default branch that Duel compared
221
- # left it. It's red where the Duel job failed or found a regression,
222
- # even one that passed (with c.fail_on = :pull_requests in .duel.rb). A
223
- # push with nothing to compare leaves it as it was, and so does a run
224
- # for an older push than the badge's. This job writes the badge, alone,
225
- # to a branch of its own, which the README shows:
226
- #
227
- # [![Duel](https://raw.githubusercontent.com/OWNER/REPO/duel/badge/badge.svg)](https://github.com/sferik/duel-ruby)
228
- #
229
- # It checks nothing out and runs none of the code.
230
- badge:
231
- name: Update the badge
232
- needs: duel
233
- if: ${{ !cancelled() && github.event_name == 'push' && github.ref_name == github.event.repository.default_branch && (needs.duel.result == 'failure' || needs.duel.outputs.status == 'passed' || needs.duel.outputs.status == 'failed') }}
234
- runs-on: ubuntu-latest
235
- permissions:
236
- contents: write # to move the badge's branch
237
- steps:
238
- - name: Update the badge
239
- env:
240
- GH_TOKEN: ${{ github.token }}
241
- BRANCH: duel/badge
242
- RESULT: ${{ needs.duel.result }}
243
- STATUS: ${{ needs.duel.outputs.status }}
244
- BADGES: https://raw.githubusercontent.com/sferik/duel-ruby/main/assets
245
- run: |
246
- # (Git can't make a branch where another's name is a folder of its
247
- # name: a branch named duel blocks duel/passed/main. If one does,
248
- # this names it.)
249
- blocked() {
250
- prefix=$BRANCH
251
- while [ "${prefix%/*}" != "$prefix" ]; do
252
- prefix=${prefix%/*}
253
- if gh api "repos/$GITHUB_REPOSITORY/git/ref/heads/$prefix" > /dev/null 2>&1; then
254
- echo "::error::The branch $prefix keeps Duel from making $BRANCH, as Git can't have both. Delete it (git push origin --delete $prefix), or rename it."
255
- fi
256
- done
257
- exit 1
258
- }
259
- # (Where the badge is a newer push's, that push's run has the say.
260
- # Its commit names the push.)
261
- last=$(gh api "repos/$GITHUB_REPOSITORY/commits/$BRANCH" --jq .commit.message 2>/dev/null | sed -n 's/^Duel.s badge, for \([0-9a-f]*\)$/\1/p') || true
262
- if [ -n "$last" ] && [ "$(gh api "repos/$GITHUB_REPOSITORY/compare/$GITHUB_SHA...$last" --jq .status 2>/dev/null)" = ahead ]; then exit 0; fi
263
- badge="badge.svg"
264
- if [ "$RESULT" = failure ] || [ "$STATUS" = failed ]; then badge="badge-regressed.svg"; fi
265
- curl -fsSL "$BADGES/$badge" -o "$RUNNER_TEMP/badge.svg"
266
- tree=$(jq -n --rawfile svg "$RUNNER_TEMP/badge.svg" '{tree: [{path: "badge.svg", mode: "100644", type: "blob", content: $svg}]}' |
267
- gh api --method POST "repos/$GITHUB_REPOSITORY/git/trees" --input - --jq .sha)
268
- commit=$(gh api --method POST "repos/$GITHUB_REPOSITORY/git/commits" -f message="Duel's badge, for $GITHUB_SHA" -f tree="$tree" --jq .sha)
269
- gh api --method PATCH "repos/$GITHUB_REPOSITORY/git/refs/heads/$BRANCH" -f sha="$commit" -F force=true > /dev/null 2>&1 ||
270
- gh api --method POST "repos/$GITHUB_REPOSITORY/git/refs" -f ref="refs/heads/$BRANCH" -f sha="$commit" > /dev/null || blocked
271
145
  ```
272
146
 
273
147
  A newer push to a pull request cancels the pull request's run, if it's
@@ -291,8 +165,11 @@ The workflow compares the following:
291
165
  the last commit that Duel passed. If that commit isn't in the history,
292
166
  such as after a force push or for a branch's first push, Duel reports
293
167
  that there's nothing to compare against, and passes.
294
- - On a schedule, the last release. Until you tag your first release,
295
- Duel reports that there's nothing to compare against, and passes.
168
+ - On a schedule, the last release, or with
169
+ [`c.accept_label`](#accept-a-regression-with-a-label), the last commit
170
+ since then that merged a pull request with the label. Until you tag
171
+ your first release, Duel reports that there's nothing to compare
172
+ against, and passes.
296
173
  - When you run the workflow manually, your code against itself. This
297
174
  checks that the benchmarks run on the CI machines.
298
175
 
@@ -355,9 +232,12 @@ the pull request's code. The pull request's code ran in the Duel job,
355
232
  and could have written anything into the artifact, so the workflow
356
233
  trusts only what Duel could have said there: a number that names the
357
234
  pull request whose head the run compared, and a `comment` and a
358
- `status` that Duel uses. The report's Markdown is the comment's body,
359
- which can only say something on the pull request that the pull request
360
- could say itself. When the Duel job fails before it compares, there's
235
+ `status` that Duel uses. For a pull request from a branch of your
236
+ repository, whose author can push to it anyway, the report's Markdown
237
+ is the comment's body. For one from a fork, the report is shown as text,
238
+ in a code block, since the fork's code wrote it: there, posted by
239
+ `github-actions[bot]`, it can't mention anyone, link anywhere or show an
240
+ image. A report too long for a comment is cut short. When the Duel job fails before it compares, there's
361
241
  no report, and no comment. GitHub runs a `workflow_run` workflow only
362
242
  from your default branch, so the pull request that adds the workflow
363
243
  gets no comment.
@@ -375,12 +255,24 @@ name: Perf comment
375
255
  # the pull request's code, and of what the Duel job kept (where that code
376
256
  # ran), it trusts only what that job could have said: the pull request's
377
257
  # number, if it names the pull request the run compared, and the comment
378
- # and the status, if they're ones Duel says. (The workflow must be on the
379
- # default branch before it runs at all.)
258
+ # and the status, if they're ones Duel says. A fork's report, which the
259
+ # fork's code wrote, is shown as text.
260
+ #
261
+ # And when the label that accepts a regression (c.accept_label in
262
+ # .duel.rb) is added to a pull request, or taken off, it runs the Duel
263
+ # job again, which reads the labels as it runs: a run that failed, once
264
+ # the label's added, or one that passed, once it's taken off.
265
+ #
266
+ # (The workflow must be on the default branch before it runs at all.)
380
267
  on:
381
268
  workflow_run:
382
269
  workflows: ["Perf"]
383
270
  types: [completed]
271
+ # (This repository's own workflow, as for the comment, with a token
272
+ # that can run a workflow again: it checks nothing out, and runs none
273
+ # of the pull request's code.)
274
+ pull_request_target:
275
+ types: [labeled, unlabeled]
384
276
 
385
277
  permissions: {}
386
278
 
@@ -413,16 +305,62 @@ jobs:
413
305
  COMMENT=$(said comment 'post|change')
414
306
  STATUS=$(said status 'passed|failed|neutral')
415
307
  [ -n "$PR" ] && [ -n "$COMMENT" ] && [ -n "$STATUS" ] || exit 0
416
- [ "$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR" --jq .head.sha)" = "$HEAD_SHA" ] || exit 0
308
+ # (The pull request's head commit, and the repository it's from.)
309
+ head=$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR" --jq '"\(.head.sha) \(.head.repo.full_name)"')
310
+ [ "${head%% *}" = "$HEAD_SHA" ] || exit 0
311
+ # The report, as Markdown for a branch of this repository (whose
312
+ # author can push here anyway), but for a fork's, as text: its run
313
+ # ran the fork's code, which could have written anything in it. In
314
+ # a code block, fenced with more backticks than any run of them in
315
+ # the report, it can't mention anyone, link anywhere or show an
316
+ # image. It's cut short where a comment couldn't hold it, and loses
317
+ # any NUL bytes, which GitHub wouldn't take, and grep reads it as
318
+ # text whatever's in it, so it finds every run of backticks.
319
+ report() {
320
+ if [ "${head#* }" = "$GITHUB_REPOSITORY" ]; then cat "$RUNNER_TEMP/report/duel.md" && return; fi
321
+ text="$RUNNER_TEMP/report.txt"
322
+ LC_ALL=C tr -d '\000' < "$RUNNER_TEMP/report/duel.md" | head -c 60000 > "$text"
323
+ fence=$(LC_ALL=C grep -ao '``*' "$text" | awk '{ if (length > n) n = length } END { n = (n < 3) ? 3 : n + 1; while (n-- > 0) printf "`" }')
324
+ printf 'From a fork, so shown as text:\n\n%s\n' "$fence"
325
+ cat "$text"
326
+ printf '\n%s\n' "$fence"
327
+ }
417
328
  marker="<!-- duel"
418
329
  # The first line of the last comment Duel posted here: its marker,
419
330
  # with its status (none in Duel 0.1.0's, which counts as another).
420
331
  last=$(gh api --paginate "repos/$GITHUB_REPOSITORY/issues/$PR/comments" \
421
332
  --jq ".[] | select(.user.login == \"github-actions[bot]\") | .body | split(\"\n\")[0] | rtrimstr(\"\r\") | select(startswith(\"$marker status=\") or . == \"$marker -->\")" | tail -n 1)
422
333
  if [ "$COMMENT" = post ] || { [ -n "$last" ] && [ "$last" != "$marker status=$STATUS -->" ]; }; then
423
- { echo "$marker status=$STATUS -->"; cat "$RUNNER_TEMP/report/duel.md"; } > "$RUNNER_TEMP/comment.md"
334
+ { echo "$marker status=$STATUS -->"; report; } > "$RUNNER_TEMP/comment.md"
424
335
  gh api --method POST "repos/$GITHUB_REPOSITORY/issues/$PR/comments" -F body=@"$RUNNER_TEMP/comment.md" > /dev/null
425
336
  fi
337
+
338
+ again:
339
+ name: Run Duel again
340
+ if: >-
341
+ github.event_name == 'pull_request_target' && github.event.label.name == 'duel: accept'
342
+ runs-on: ubuntu-latest
343
+ permissions:
344
+ actions: write # to run the Perf workflow's jobs again
345
+ steps:
346
+ - name: Run Duel again
347
+ env:
348
+ GH_TOKEN: ${{ github.token }}
349
+ GH_REPO: ${{ github.repository }}
350
+ HEAD_SHA: ${{ github.event.pull_request.head.sha }}
351
+ ACTION: ${{ github.event.action }}
352
+ run: |
353
+ # The newest Perf run of the pull request's head commit, once it's
354
+ # done (one that's running may have read the labels already).
355
+ id=$(gh run list --workflow "Perf" --event pull_request --commit "$HEAD_SHA" --limit 1 \
356
+ --json databaseId --jq '.[0].databaseId // empty')
357
+ [ -n "$id" ] || exit 0
358
+ gh run watch "$id" --interval 30 > /dev/null || true
359
+ conclusion=$(gh run view "$id" --json conclusion --jq .conclusion)
360
+ case "$ACTION:$conclusion" in
361
+ labeled:failure) gh run rerun "$id" --failed ;;
362
+ unlabeled:success) gh run rerun "$id" ;;
363
+ esac
426
364
  ```
427
365
 
428
366
  ## Comment on a pushed commit
@@ -441,6 +379,52 @@ By default, a push gets no comment: Duel sets the `comment` output to
441
379
  the commit job checks nothing out either, and it alone gets the
442
380
  `contents: write` permission that a commit comment takes.
443
381
 
382
+ `duel init` writes the Comment on the commit job only where `.duel.rb`
383
+ sets `c.fail_on = :pull_requests`, as it's the only time Duel says to
384
+ comment on a push, and the job's `contents: write` is otherwise unused.
385
+ To add it later, set `c.fail_on`, remove the workflow, and run
386
+ `duel init` again (it keeps `.duel.rb`), or add the job yourself:
387
+
388
+ ```yaml
389
+ # .github/workflows/perf.yml, in jobs: (with c.fail_on = :pull_requests)
390
+ # The report, as a new comment on a pushed commit, which notifies its
391
+ # author, when Duel says to, as for a pull request (see the Perf comment
392
+ # workflow). It does only where a regression in a push passes, with
393
+ # c.fail_on = :pull_requests in .duel.rb, so the comment says what the
394
+ # check doesn't. Its last comment is the last one on any commit. This
395
+ # job checks nothing out and runs none of the code.
396
+ commit-comment:
397
+ name: Comment on the commit
398
+ needs: duel
399
+ if: ${{ !cancelled() && github.event_name == 'push' && (needs.duel.outputs.comment == 'post' || needs.duel.outputs.comment == 'change') }}
400
+ runs-on: ubuntu-latest
401
+ permissions:
402
+ contents: write # to comment on the commit with the report
403
+ steps:
404
+ - name: Get the report
405
+ id: report
406
+ continue-on-error: true # there's none when the Duel job failed before comparing
407
+ uses: actions/download-artifact@v8
408
+ with:
409
+ name: duel-report
410
+ path: ${{ runner.temp }}/report
411
+ - name: Comment on the commit
412
+ if: steps.report.outcome == 'success'
413
+ env:
414
+ GH_TOKEN: ${{ github.token }}
415
+ COMMENT: ${{ needs.duel.outputs.comment }}
416
+ STATUS: ${{ needs.duel.outputs.status }}
417
+ run: |
418
+ marker="<!-- duel"
419
+ last=$(gh api --paginate "repos/$GITHUB_REPOSITORY/comments" \
420
+ --jq ".[] | select(.user.login == \"github-actions[bot]\") | .body | split(\"\n\")[0] | rtrimstr(\"\r\") | select(startswith(\"$marker status=\"))" | tail -n 1)
421
+ if [ "$COMMENT" = post ] || { [ -n "$last" ] && [ "$last" != "$marker status=$STATUS -->" ]; }; then
422
+ { echo "$marker status=$STATUS -->"; cat "$RUNNER_TEMP/report/duel.md"; } > "$RUNNER_TEMP/comment.md"
423
+ gh api --method POST "repos/$GITHUB_REPOSITORY/commits/$GITHUB_SHA/comments" -F body=@"$RUNNER_TEMP/comment.md" > /dev/null
424
+ fi
425
+
426
+ ```
427
+
444
428
  ## Accept a regression with a label
445
429
 
446
430
  With [`c.accept_label`](configuration.md#accept-a-regression-with-a-label),
@@ -451,6 +435,23 @@ or for a push, of the pull request that merged it, and passes them to
451
435
  `pull-requests: read` permission. Without the labels, such as when
452
436
  GitHub's API fails, nothing is accepted.
453
437
 
438
+ The labels are read as the Duel job runs, so `perf-comment.yml` runs
439
+ the job again when the label changes. Its Run Duel again job runs on
440
+ `pull_request_target`, with this repository's own workflow, when a
441
+ label is added to a pull request or taken off. For the label `.duel.rb`
442
+ names, it waits for the Perf run of the pull request's head commit to
443
+ finish. Then it runs the failed jobs again where the label was added
444
+ to a run that failed, or the whole run again where the label was
445
+ taken off one that passed. It checks nothing out and runs none of the
446
+ pull request's code, and its only permission is `actions: write`. The
447
+ workflows name the label as `.duel.rb` did when `duel init` wrote them,
448
+ so if you rename it, rename it in both workflows too.
449
+
450
+ On a schedule, the same step looks for the pull requests merged since
451
+ the last release with the label, and passes `duel` the last commit
452
+ that merged one, in `DUEL_ACCEPTED`. Duel compares with that commit
453
+ instead of the release, as what it slowed down was accepted.
454
+
454
455
  ## Compare a push with the last commit that passed
455
456
 
456
457
  By default, the workflow compares each push with the commit before it.
@@ -467,8 +468,10 @@ branch that the workflow guards has its own (see
467
468
  nothing to compare, Duel sets the `passed` output to `true`, and the
468
469
  Mark the commit passed job moves the branch to the push, or creates the
469
470
  branch. It never moves the branch back: when two pushes run at once and
470
- the older one finishes last, the branch stays at the newer push. After
471
- a force push rewrites the branch's history, it moves the branch anyway.
471
+ the older one finishes last, the branch stays at the newer push. The
472
+ job runs one at a time for each branch, so two runs can't race to
473
+ create the branch. After a force push rewrites the branch's history,
474
+ it moves the branch anyway.
472
475
  That job checks nothing out either, and it gets the `contents: write`
473
476
  permission that moving a branch takes. Until the branch exists, and
474
477
  when the push's history doesn't have its commit, Duel compares the push
@@ -496,11 +499,85 @@ If you set `c.push_base` back to `:before`, delete the branches too
496
499
  (`git push origin --delete duel/passed/main`), because the step that
497
500
  finds the baseline's Ruby uses one whenever it exists.
498
501
 
502
+ `duel init` writes the Mark the commit passed job only where `.duel.rb`
503
+ sets `c.push_base = :last_passed`, as the job's `contents: write` is
504
+ otherwise unused. To add it later, set `c.push_base`, remove the
505
+ workflow, and run `duel init` again (it keeps `.duel.rb`), or add the job
506
+ yourself:
507
+
508
+ ```yaml
509
+ # .github/workflows/perf.yml, in jobs: (with c.push_base = :last_passed)
510
+ # With c.push_base = :last_passed in .duel.rb, a push is compared with
511
+ # the last commit Duel passed, which a branch marks (each branch pushed
512
+ # has its own), so slowdowns too small for any one push to fail add up
513
+ # until they fail one. This job moves the branch to a push that passed
514
+ # (or makes it), but never back: where a newer push's run moved it
515
+ # first, it stays. It checks nothing out and runs none of the code. To
516
+ # accept a regression, move the branch yourself (for main, git push
517
+ # origin main:duel/passed/main).
518
+ passed:
519
+ name: Mark the commit passed
520
+ needs: duel
521
+ # One at a time for each branch, so that two runs can't both find no
522
+ # branch and race to make it (a push's run doesn't wait for the one
523
+ # before it). One that's running is never cancelled, and one that's
524
+ # waiting gives way to a newer push's, which moves it further anyway.
525
+ concurrency:
526
+ group: ${{ github.workflow }}-passed-${{ github.ref_name }}
527
+ cancel-in-progress: false
528
+ if: ${{ !cancelled() && github.event_name == 'push' && needs.duel.outputs.passed == 'true' }}
529
+ runs-on: ubuntu-latest
530
+ permissions:
531
+ contents: write # to move the branch
532
+ steps:
533
+ - name: Move the branch to the commit
534
+ env:
535
+ GH_TOKEN: ${{ github.token }}
536
+ BRANCH: duel/passed/${{ github.ref_name }}
537
+ run: |
538
+ # (Git can't make a branch where another's name is a folder of its
539
+ # name: a branch named duel blocks duel/passed/main. If one does,
540
+ # this names it.)
541
+ blocked() {
542
+ prefix=$BRANCH
543
+ while [ "${prefix%/*}" != "$prefix" ]; do
544
+ prefix=${prefix%/*}
545
+ if gh api "repos/$GITHUB_REPOSITORY/git/ref/heads/$prefix" > /dev/null 2>&1; then
546
+ echo "::error::The branch $prefix keeps Duel from making $BRANCH, as Git can't have both. Delete it (git push origin --delete $prefix), or rename it."
547
+ fi
548
+ done
549
+ exit 1
550
+ }
551
+ # (Forward: GitHub moves it only where it's behind the push. Where
552
+ # there's no branch, and making it fails, it looks once more, as
553
+ # something else may have made it: a push of it, say.)
554
+ for _ in 1 2; do
555
+ gh api --method PATCH "repos/$GITHUB_REPOSITORY/git/refs/heads/$BRANCH" -f sha="$GITHUB_SHA" > /dev/null 2>&1 && exit 0
556
+ at=$(gh api "repos/$GITHUB_REPOSITORY/git/ref/heads/$BRANCH" --jq .object.sha 2>/dev/null) || at=
557
+ [ -n "$at" ] && break
558
+ gh api --method POST "repos/$GITHUB_REPOSITORY/git/refs" -f ref="refs/heads/$BRANCH" -f sha="$GITHUB_SHA" > /dev/null && exit 0
559
+ done
560
+ [ -n "$at" ] || blocked
561
+ compared=$(gh api "repos/$GITHUB_REPOSITORY/compare/$GITHUB_SHA...$at" --jq .status 2>&1) || case "$compared" in
562
+ # (GitHub can't compare commits with no history in common.)
563
+ *"No common ancestor"*) compared=unrelated ;;
564
+ esac
565
+ case "$compared" in
566
+ # (A newer push's run moved it, or this one's did, run again.)
567
+ ahead | identical) ;;
568
+ # (History rewritten, a force push's, say, or replaced: moved anyway.)
569
+ diverged | behind | unrelated) gh api --method PATCH "repos/$GITHUB_REPOSITORY/git/refs/heads/$BRANCH" -f sha="$GITHUB_SHA" -F force=true > /dev/null ;;
570
+ *) echo "::error::Couldn't compare $BRANCH with this push: $compared" && exit 1 ;;
571
+ esac
572
+
573
+ ```
574
+
499
575
  ## Show whether the last push passed
500
576
 
501
- The Update the badge job keeps a badge that turns red when Duel fails.
502
- After each push to your default branch, it writes `badge.svg` to a
503
- branch of its own, `duel/badge`, so your README can show it:
577
+ With `--badge`, `duel init` adds the Update the badge job, which keeps a
578
+ badge that turns red when Duel fails. After each push to your default
579
+ branch, it writes `badge.svg` to a branch of its own, `duel/badge`, so
580
+ your README can show it:
504
581
 
505
582
  ```markdown
506
583
  [![Duel](https://raw.githubusercontent.com/OWNER/REPO/duel/badge/badge.svg)](https://github.com/sferik/duel-ruby)
@@ -519,8 +596,124 @@ that moving a branch takes. For a project in a subdirectory of its
519
596
  repository, the branch names the directory, such as
520
597
  `duel/badge-gems-parser`.
521
598
 
522
- Until the first push to your default branch after you add the
523
- workflow, the branch doesn't exist, and the badge doesn't show.
599
+ Until the first push to your default branch after you add the job, the
600
+ branch doesn't exist, and the badge doesn't show.
601
+
602
+ `duel init` writes the job only with `--badge` (for a Rails app,
603
+ `bin/rails generate duel:install --badge`), as without the jobs for
604
+ pushes, it's the workflow's only job that writes to the repository. To
605
+ add it later, remove the workflow, and run `duel init --badge` again (it
606
+ keeps `.duel.rb`), or add the job yourself:
607
+
608
+ ```yaml
609
+ # .github/workflows/perf.yml, in jobs: (with duel init --badge)
610
+ # Duel's badge, for the README: green (perf guarded) or red (perf
611
+ # regressed), as the last push to the default branch that Duel compared
612
+ # left it. It's red where the Duel job failed or found a regression,
613
+ # even one that passed (with c.fail_on = :pull_requests in .duel.rb). A
614
+ # push with nothing to compare leaves it as it was, and so does a run
615
+ # for an older push than the badge's. This job writes the badge, alone,
616
+ # to a branch of its own, which the README shows:
617
+ #
618
+ # [![Duel](https://raw.githubusercontent.com/OWNER/REPO/duel/badge/badge.svg)](https://github.com/sferik/duel-ruby)
619
+ #
620
+ # It checks nothing out, runs none of the code, and fetches nothing.
621
+ badge:
622
+ name: Update the badge
623
+ needs: duel
624
+ # One at a time, so that one run's check of which push the badge
625
+ # shows can't interleave with another run's write (a push's run
626
+ # doesn't wait for the one before it). A badge job that's running is
627
+ # never cancelled, and one that's waiting gives way to a newer push's,
628
+ # whose result the badge should show anyway.
629
+ concurrency:
630
+ group: ${{ github.workflow }}-badge
631
+ cancel-in-progress: false
632
+ if: ${{ !cancelled() && github.event_name == 'push' && github.ref_name == github.event.repository.default_branch && (needs.duel.result == 'failure' || needs.duel.outputs.status == 'passed' || needs.duel.outputs.status == 'failed') }}
633
+ runs-on: ubuntu-latest
634
+ permissions:
635
+ contents: write # to move the badge's branch
636
+ steps:
637
+ - name: Update the badge
638
+ env:
639
+ GH_TOKEN: ${{ github.token }}
640
+ BRANCH: duel/badge
641
+ RESULT: ${{ needs.duel.result }}
642
+ STATUS: ${{ needs.duel.outputs.status }}
643
+ run: |
644
+ # (Git can't make a branch where another's name is a folder of its
645
+ # name: a branch named duel blocks duel/passed/main. If one does,
646
+ # this names it.)
647
+ blocked() {
648
+ prefix=$BRANCH
649
+ while [ "${prefix%/*}" != "$prefix" ]; do
650
+ prefix=${prefix%/*}
651
+ if gh api "repos/$GITHUB_REPOSITORY/git/ref/heads/$prefix" > /dev/null 2>&1; then
652
+ echo "::error::The branch $prefix keeps Duel from making $BRANCH, as Git can't have both. Delete it (git push origin --delete $prefix), or rename it."
653
+ fi
654
+ done
655
+ exit 1
656
+ }
657
+ # (Where the badge is a newer push's, that push's run has the say.
658
+ # Its commit names the push.)
659
+ last=$(gh api "repos/$GITHUB_REPOSITORY/commits/$BRANCH" --jq .commit.message 2>/dev/null | sed -n 's/^Duel.s badge, for \([0-9a-f]*\)$/\1/p') || true
660
+ if [ -n "$last" ] && [ "$(gh api "repos/$GITHUB_REPOSITORY/compare/$GITHUB_SHA...$last" --jq .status 2>/dev/null)" = ahead ]; then exit 0; fi
661
+ # (Its image, written here rather than fetched from anywhere: green,
662
+ # or red where the Duel job failed or found a regression.)
663
+ state="perf guarded" width=133 top="#34D058" bottom="#28A745" right="76.939C78.629 0 80 1.343 80"
664
+ if [ "$RESULT" = failure ] || [ "$STATUS" = failed ]; then
665
+ state="perf regressed" width=142 top="#EA4A5A" bottom="#D73A49" right="85.939C87.629 0 89 1.343 89"
666
+ fi
667
+ cat > "$RUNNER_TEMP/badge.svg" <<SVG
668
+ <svg xmlns="http://www.w3.org/2000/svg" width="$width" height="20">
669
+ <title>Duel - $state</title>
670
+ <defs>
671
+ <linearGradient id="duel-fill" x1="50%" y1="0%" x2="50%" y2="100%">
672
+ <stop stop-color="#444D56" offset="0%"></stop>
673
+ <stop stop-color="#24292E" offset="100%"></stop>
674
+ </linearGradient>
675
+ <linearGradient id="state-fill" x1="50%" y1="0%" x2="50%" y2="100%">
676
+ <stop stop-color="$top" offset="0%"></stop>
677
+ <stop stop-color="$bottom" offset="100%"></stop>
678
+ </linearGradient>
679
+ </defs>
680
+ <g fill="none" fill-rule="evenodd">
681
+ <g font-family="'DejaVu Sans',Verdana,Geneva,sans-serif" font-size="11">
682
+ <path d="M0,3 C0,1.3431 1.3552,0 3.02702703,0 L53,0 L53,20 L3.02702703,20 C1.3552,20 0,18.6569 0,17 L0,3 Z" fill="url(#duel-fill)" fill-rule="nonzero"></path>
683
+ <text fill="#010101" fill-opacity=".3">
684
+ <tspan x="22.1981982" y="15" aria-hidden="true">Duel</tspan>
685
+ </text>
686
+ <text fill="#FFFFFF">
687
+ <tspan x="22.1981982" y="14">Duel</tspan>
688
+ </text>
689
+ </g>
690
+ <g transform="translate(53)" font-family="'DejaVu Sans',Verdana,Geneva,sans-serif" font-size="11">
691
+ <path d="M0 0h$right 3v14c0 1.657-1.37 3-3.061 3H0V0z" fill="url(#state-fill)" fill-rule="nonzero"></path>
692
+ <text fill="#010101" fill-opacity=".3" aria-hidden="true">
693
+ <tspan x="4" y="15">$state</tspan>
694
+ </text>
695
+ <text fill="#FFFFFF">
696
+ <tspan x="4" y="14">$state</tspan>
697
+ </text>
698
+ </g>
699
+ <g fill="#959DA5">
700
+ <path d="M4.6 3.6L6.9 4.5L14.3 11.9L12.9 13.3L5.5 5.9Z M17.4 3.6L15.1 4.5L7.7 11.9L9.1 13.3L16.5 5.9Z"></path>
701
+ <circle cx="16.6" cy="15.6" r="1.1"></circle>
702
+ <circle cx="5.4" cy="15.6" r="1.1"></circle>
703
+ </g>
704
+ <g stroke="#959DA5" stroke-linecap="round">
705
+ <path d="M12.1 14.5L15.5 11.1 M9.9 14.5L6.5 11.1" stroke-width="1.6"></path>
706
+ <path d="M13.8 12.8L16 15 M8.2 12.8L6 15" stroke-width="1.8"></path>
707
+ </g>
708
+ </g>
709
+ </svg>
710
+ SVG
711
+ tree=$(jq -n --rawfile svg "$RUNNER_TEMP/badge.svg" '{tree: [{path: "badge.svg", mode: "100644", type: "blob", content: $svg}]}' |
712
+ gh api --method POST "repos/$GITHUB_REPOSITORY/git/trees" --input - --jq .sha)
713
+ commit=$(gh api --method POST "repos/$GITHUB_REPOSITORY/git/commits" -f message="Duel's badge, for $GITHUB_SHA" -f tree="$tree" --jq .sha)
714
+ gh api --method PATCH "repos/$GITHUB_REPOSITORY/git/refs/heads/$BRANCH" -f sha="$commit" -F force=true > /dev/null 2>&1 ||
715
+ gh api --method POST "repos/$GITHUB_REPOSITORY/git/refs" -f ref="refs/heads/$BRANCH" -f sha="$commit" > /dev/null || blocked
716
+ ```
524
717
 
525
718
  ## Guard other branches
526
719
 
@@ -722,6 +915,9 @@ duel:passed:
722
915
  image: alpine:3
723
916
  stage: .post
724
917
  needs: [duel]
918
+ # One at a time, as each deletes the branch and makes it again: two at
919
+ # once could both delete it, and the second to make it would fail.
920
+ resource_group: duel/passed
725
921
  rules:
726
922
  - if: $DUEL_TOKEN && $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
727
923
  variables:
@@ -791,7 +987,8 @@ match. `duel init` adds these steps for a project that has a
791
987
  # request targets (or run by hand, the main branch), a merge queue's
792
988
  # base, the commit before a push (or the last commit Duel passed, PASSED, if there is
793
989
  # one: see the Mark the commit passed job), or on a schedule the last
794
- # release, the newest tag before HEAD that RELEASE_TAGS matches. (If
990
+ # release, the newest tag before HEAD that RELEASE_TAGS matches
991
+ # (or ACCEPTED, the last change accepted since then). (If
795
992
  # .duel.rb sets c.main_branch or c.release_tags, set MAIN_BRANCH or
796
993
  # RELEASE_TAGS to match.)
797
994
  - name: Find the baseline's Ruby
@@ -802,13 +999,14 @@ match. `duel init` adds these steps for a project that has a
802
999
  PASSED: origin/duel/passed/${{ github.ref_name }}
803
1000
  MAIN_BRANCH: origin/${{ github.base_ref || github.event.repository.default_branch }}
804
1001
  RELEASE_TAGS: "v*"
1002
+ ACCEPTED: ${{ steps.labels.outputs.accepted }}
805
1003
  run: |
806
1004
  case "$GITHUB_EVENT_NAME" in
807
1005
  push)
808
1006
  base=$BEFORE
809
1007
  if git merge-base --is-ancestor "$PASSED" HEAD 2>/dev/null; then base=$PASSED; fi ;;
810
1008
  merge_group) base=$QUEUE_BASE ;;
811
- schedule) base=$(git describe --tags --abbrev=0 --match "$RELEASE_TAGS" HEAD^ 2>/dev/null) || base= ;;
1009
+ schedule) base=${ACCEPTED:-$(git describe --tags --abbrev=0 --match "$RELEASE_TAGS" HEAD^ 2>/dev/null)} || base= ;;
812
1010
  *) base=$(git merge-base HEAD "$MAIN_BRANCH" 2>/dev/null) || base= ;;
813
1011
  esac
814
1012
  ruby_of() { git show "$1:./.ruby-version" 2>/dev/null | awk 'NF { print $1; exit }'; }