duel-rails 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 021457e88a614ab2d707bbaa4e5b0492ad8ce632288b9a2050e12c22e03b4426
4
- data.tar.gz: 4123646895aa5f9988ec93e043247e14acab50c796158ee3c74c102ae6f524a5
3
+ metadata.gz: 81ef95643e521662165107851a57bf291c2ef90d8f3ebf7260c761353afdea1d
4
+ data.tar.gz: c48b15ef972f117df95abcfc8d46303c8b10f30d7758e7aa24551a36f6e5a725
5
5
  SHA512:
6
- metadata.gz: ebc28766c172ebb6b905784a20503f6a7c11afe5a4f92845adef9228761d19bc8040fefdf195c28d4ee904e5d07a09780c6a6416da9233b26b68e460eeeae0b8
7
- data.tar.gz: 79c3f8e04acd75e7510bfef3f700ee03ce5f8aa8310da48c1c90c32dfd5fd2b9be0dbaaaa38a2209d38b14e7455efaeaabd895021febb6625a033fa4ef87046e
6
+ metadata.gz: 3ccf58afcefb2d852c7eb7692c4a6436e07cc0581f50144a734c3ba73bf87681613a9ea4a615d978ad9c39ad919844a2801a7ef635292739e3efecf22e7415e6
7
+ data.tar.gz: 33df2071653cce8e5492ca9f8cda1b5c697358b082ae56255ef4f53143fad1fcbe06528802525a5a7095b8b4671466b1c298507292c75e7af10d15a7702c3d61
data/CHANGELOG.md CHANGED
@@ -7,6 +7,50 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.1.6] - 2026-11-10
11
+
12
+ ### Added
13
+
14
+ - The generator's workflows do what `duel init`'s do with the label that
15
+ accepts a regression: the comment workflow runs the Duel job again
16
+ when the label is added or taken off, and on a schedule, the Duel job
17
+ compares with the last change merged with the label since the last
18
+ release.
19
+
20
+ ### Fixed
21
+
22
+ - The comment workflow fences a fork's report that has a NUL byte as it
23
+ does any other (see Duel's changelog).
24
+ - The Mark the commit passed job runs one at a time for each branch, so
25
+ two pushes that pass before the branch exists can't both try to make
26
+ it (see Duel's changelog).
27
+
28
+ ## [0.1.5] - 2026-11-08
29
+
30
+ ### Changed
31
+
32
+ - `rails generate duel:install` writes the Comment on the commit and
33
+ Mark the commit passed jobs only where `.duel.rb` asks for them, as
34
+ `duel init` does.
35
+ - `rails generate duel:install` writes the Update the badge job only
36
+ with `--badge`, as `duel init` does, so the workflow it writes by
37
+ default only reads.
38
+ - The `.duel.rb` that `rails generate duel:install` writes sets
39
+ `c.accept_label = "duel: accept"`, as `duel init`'s does.
40
+
41
+ ### Fixed
42
+
43
+ - The Perf comment workflow that `rails generate duel:install` writes
44
+ shows a fork's report as text, in a code block, as the workflow that
45
+ `duel init` writes does.
46
+ - The Update the badge job, in the workflow that
47
+ `rails generate duel:install` writes, now writes the badge's image
48
+ itself instead of downloading it from Duel's repository.
49
+ - Two pushes' runs of the Update the badge job, in the workflow that
50
+ `rails generate duel:install` writes, could overlap, and an older
51
+ push's could write its badge over a newer one's. The job now runs
52
+ one at a time, as in the workflow that `duel init` writes.
53
+
10
54
  ## [0.1.4] - 2026-11-06
11
55
 
12
56
  ### Fixed
@@ -206,7 +250,9 @@ Rails 7.2 or later, and Ruby 3.4 or later.
206
250
  CI.
207
251
  - RBS signatures in sig/, checked with Steep.
208
252
 
209
- [Unreleased]: https://github.com/sferik/duel-ruby/compare/duel-rails-v0.1.4...HEAD
253
+ [Unreleased]: https://github.com/sferik/duel-ruby/compare/duel-rails-v0.1.6...HEAD
254
+ [0.1.6]: https://github.com/sferik/duel-ruby/compare/duel-rails-v0.1.5...duel-rails-v0.1.6
255
+ [0.1.5]: https://github.com/sferik/duel-ruby/compare/duel-rails-v0.1.4...duel-rails-v0.1.5
210
256
  [0.1.4]: https://github.com/sferik/duel-ruby/compare/duel-rails-v0.1.3...duel-rails-v0.1.4
211
257
  [0.1.3]: https://github.com/sferik/duel-ruby/compare/duel-rails-v0.1.2...duel-rails-v0.1.3
212
258
  [0.1.2]: https://github.com/sferik/duel-ruby/compare/duel-rails-v0.1.1...duel-rails-v0.1.2
data/README.md CHANGED
@@ -10,7 +10,7 @@ them query the database, render views, or allocate more.
10
10
 
11
11
  ```sh
12
12
  $ bin/rake duel
13
- Comparing 3 benchmarks, 2 at a time: baseline /src/.8e2f1c4 against candidate /src/my_app (slower: a 99.0% interval above 1.02)
13
+ Comparing 3 benchmarks, 2 at a time: baseline /src/.8e2f1c4 against candidate /src/my_app (slower: a 99.0% interval above 1.02, over 12 worker processes of each version)
14
14
  posts: index 2.7s n=4 cpu 1.004 [0.991, 1.018] same active_record_queries 1→2 rails_views 4→7 allocations 988→1530
15
15
  ...
16
16
  REGRESSION posts: index: worse active_record_queries, rails_views, allocations in every measurement
@@ -68,7 +68,7 @@ production does, with everything of its own:
68
68
  | `config/environments/benchmark.rb` | The environment that each version boots in, based on production. |
69
69
  | `duel/requests_duel.rb` | A benchmark of each of your app's first 10 routes that a GET reaches without parameters. |
70
70
  | `duel/boot_duel.rb` | A benchmark of your app's boot. See [Benchmark your app's boot](#benchmark-your-apps-boot). |
71
- | `.github/workflows/perf.yml` | A GitHub Actions workflow that runs Duel on pull requests, on pushes to `main`, and nightly against your last release, and has the second workflow comment on a pull request with Duel's report when a benchmark fails. It never edits a comment, and after a failure, posts one more when a run passes. To comment on every run, or never, set `c.comment` in `.duel.rb`: see [Choose when to comment on a pull request](https://github.com/sferik/duel-ruby/blob/main/docs/configuration.md#choose-when-to-comment-on-a-pull-request) and [Report on a pull request](https://github.com/sferik/duel-ruby#report-on-a-pull-request). For an app in a subdirectory of its repository, the workflow is at the repository's root. See [An app in a monorepo](#an-app-in-a-monorepo). |
71
+ | `.github/workflows/perf.yml` | A GitHub Actions workflow that runs Duel on pull requests, on pushes to `main`, and nightly against your last release, and has the second workflow comment on a pull request with Duel's report when a benchmark fails. It never edits a comment, and after a failure, posts one more when a run passes. To comment on every run, or never, set `c.comment` in `.duel.rb`: see [Choose when to comment on a pull request](https://github.com/sferik/duel-ruby/blob/main/docs/configuration.md#choose-when-to-comment-on-a-pull-request) and [Report on a pull request](https://github.com/sferik/duel-ruby#report-on-a-pull-request). For an app in a subdirectory of its repository, the workflow is at the repository's root. See [An app in a monorepo](#an-app-in-a-monorepo). With `--badge`, it also keeps a badge for your README that turns red when Duel fails a push (see [Show whether the last push passed](https://github.com/sferik/duel-ruby/blob/main/docs/ci.md#show-whether-the-last-push-passed)). |
72
72
  | `.github/workflows/perf-comment.yml` | A second workflow, which comments on a pull request with the report once the first has run, with a token that can write, so a pull request from a fork gets a comment too. It runs none of the pull request's code. See [Comment on a pull request](https://github.com/sferik/duel-ruby/blob/main/docs/ci.md#comment-on-a-pull-request). |
73
73
 
74
74
  The generator also adds `gem "stackprof", platforms: :mri` to your
@@ -451,8 +451,8 @@ and the bundle, in the app's directory (`working-directory`).
451
451
  ## YJIT
452
452
 
453
453
  Rails 7.2 and later turn YJIT on as the app boots. When YJIT is on,
454
- Duel warms each benchmark up for two seconds before it measures it, so
455
- that YJIT has compiled your app's code first.
454
+ Duel warms each benchmark up until it's steady, for up to ten seconds,
455
+ before it measures it, so that YJIT has compiled your app's code first.
456
456
 
457
457
  ## Requirements
458
458
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Duel
4
4
  module Rails
5
- VERSION = "0.1.4"
5
+ VERSION = "0.1.6"
6
6
  end
7
7
  end
@@ -20,6 +20,8 @@ module Duel
20
20
 
21
21
  class_option :database, type: :string, enum: %w[sqlite postgresql mysql2 trilogy],
22
22
  desc: "Each worker's database (default: as the app's own)"
23
+ class_option :badge, type: :boolean, default: false,
24
+ desc: "Write a job that keeps a badge for the README (it needs contents: write)"
23
25
 
24
26
  # The adapters each worker's database can stand in for, by the
25
27
  # database duel-rails calls it.
@@ -69,6 +71,41 @@ module Duel
69
71
 
70
72
  private
71
73
 
74
+ # The app's settings, which decide the workflow's jobs, as for
75
+ # `duel init` (see Duel::Init#settings): its .duel.rb's, which
76
+ # create_config has written by now (or kept), or nil where it doesn't
77
+ # load, and then the workflow has both jobs for pushes, and says so.
78
+ def settings = instance_variable_defined?(:@settings) ? @settings : (@settings = load_settings)
79
+
80
+ def load_settings
81
+ Duel.load_config(File.join(destination_root, ".duel.rb"))
82
+ rescue ScriptError, StandardError => e
83
+ say_status :warning, ".duel.rb didn't load (#{e.class}: #{e}), so the workflow has both jobs for pushes, as if it asked for them"
84
+ nil
85
+ end
86
+
87
+ # Whether the workflow comments on a pushed commit: where a regression
88
+ # in a push passes (c.fail_on = :pull_requests).
89
+ def comment_on_pushes?
90
+ found = settings
91
+ found.nil? || found.fail_on == :pull_requests
92
+ end
93
+
94
+ # Whether the workflow marks a push passed: where a push is compared
95
+ # with the last commit Duel passed (c.push_base = :last_passed).
96
+ def mark_passed?
97
+ found = settings
98
+ found.nil? || found.push_base == :last_passed
99
+ end
100
+
101
+ # The label that accepts a regression, for the workflows, as for
102
+ # `duel init` (see Duel::Init#accept_label).
103
+ def accept_label = settings&.accept_label
104
+
105
+ # Whether the workflow keeps a badge for the README (--badge), as for
106
+ # `duel init` (see Duel::Init#badge?).
107
+ def badge? = options.fetch(:badge)
108
+
72
109
  def gemfile = File.read(File.join(destination_root, "Gemfile"))
73
110
 
74
111
  # The database each worker gets: as asked, or as the app's own
@@ -28,14 +28,18 @@ Duel.configure do |c|
28
28
 
29
29
  # Which runs a regression fails: :every_run, or :pull_requests, for a
30
30
  # project where people push to main directly (on GitHub Actions, another
31
- # run warns and passes, and a pushed commit gets the comment instead).
31
+ # run warns and passes, and a pushed commit gets the comment instead,
32
+ # from a workflow job written only where this is set: see docs/ci.md).
32
33
  # c.fail_on = :every_run
33
34
 
34
- # A label that accepts a regression in a pull request (nil for none).
35
- # c.accept_label = "duel: accept"
35
+ # A label that accepts a regression in a pull request, for a change
36
+ # that's slower on purpose (nil for none).
37
+ c.accept_label = "duel: accept"
36
38
 
37
39
  # What the workflow compares a push with: :before (the commit before it),
38
40
  # or :last_passed (the last commit Duel passed, which a branch,
39
41
  # duel/passed/main, marks), so small slowdowns add up until they fail.
42
+ # (A workflow job moves the branch, written only where this is set: see
43
+ # docs/ci.md.)
40
44
  # c.push_base = :before
41
45
  end
@@ -9,12 +9,28 @@ name: Perf comment<%= " (#{working_directory})" if working_directory %>
9
9
  # the pull request's code, and of what the Duel job kept (where that code
10
10
  # ran), it trusts only what that job could have said: the pull request's
11
11
  # number, if it names the pull request the run compared, and the comment
12
- # and the status, if they're ones Duel says. (The workflow must be on the
13
- # default branch before it runs at all.)
12
+ # and the status, if they're ones Duel says. A fork's report, which the
13
+ # fork's code wrote, is shown as text.
14
+ <% if accept_label -%>
15
+ #
16
+ # And when the label that accepts a regression (c.accept_label in
17
+ # .duel.rb) is added to a pull request, or taken off, it runs the Duel
18
+ # job again, which reads the labels as it runs: a run that failed, once
19
+ # the label's added, or one that passed, once it's taken off.
20
+ <% end -%>
21
+ #
22
+ # (The workflow must be on the default branch before it runs at all.)
14
23
  on:
15
24
  workflow_run:
16
25
  workflows: ["Perf<%= " (#{working_directory})" if working_directory %>"]
17
26
  types: [completed]
27
+ <% if accept_label -%>
28
+ # (This repository's own workflow, as for the comment, with a token
29
+ # that can run a workflow again: it checks nothing out, and runs none
30
+ # of the pull request's code.)
31
+ pull_request_target:
32
+ types: [labeled, unlabeled]
33
+ <% end -%>
18
34
 
19
35
  permissions: {}
20
36
 
@@ -47,13 +63,61 @@ jobs:
47
63
  COMMENT=$(said comment 'post|change')
48
64
  STATUS=$(said status 'passed|failed|neutral')
49
65
  [ -n "$PR" ] && [ -n "$COMMENT" ] && [ -n "$STATUS" ] || exit 0
50
- [ "$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR" --jq .head.sha)" = "$HEAD_SHA" ] || exit 0
66
+ # (The pull request's head commit, and the repository it's from.)
67
+ head=$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR" --jq '"\(.head.sha) \(.head.repo.full_name)"')
68
+ [ "${head%% *}" = "$HEAD_SHA" ] || exit 0
69
+ # The report, as Markdown for a branch of this repository (whose
70
+ # author can push here anyway), but for a fork's, as text: its run
71
+ # ran the fork's code, which could have written anything in it. In
72
+ # a code block, fenced with more backticks than any run of them in
73
+ # the report, it can't mention anyone, link anywhere or show an
74
+ # image. It's cut short where a comment couldn't hold it, and loses
75
+ # any NUL bytes, which GitHub wouldn't take, and grep reads it as
76
+ # text whatever's in it, so it finds every run of backticks.
77
+ report() {
78
+ if [ "${head#* }" = "$GITHUB_REPOSITORY" ]; then cat "$RUNNER_TEMP/report/duel.md" && return; fi
79
+ text="$RUNNER_TEMP/report.txt"
80
+ LC_ALL=C tr -d '\000' < "$RUNNER_TEMP/report/duel.md" | head -c 60000 > "$text"
81
+ fence=$(LC_ALL=C grep -ao '``*' "$text" | awk '{ if (length > n) n = length } END { n = (n < 3) ? 3 : n + 1; while (n-- > 0) printf "`" }')
82
+ printf 'From a fork, so shown as text:\n\n%s\n' "$fence"
83
+ cat "$text"
84
+ printf '\n%s\n' "$fence"
85
+ }
51
86
  marker="<!-- duel<%= " #{working_directory}" if working_directory %>"
52
87
  # The first line of the last comment Duel posted here: its marker,
53
88
  # with its status (none in Duel 0.1.0's, which counts as another).
54
89
  last=$(gh api --paginate "repos/$GITHUB_REPOSITORY/issues/$PR/comments" \
55
90
  --jq ".[] | select(.user.login == \"github-actions[bot]\") | .body | split(\"\n\")[0] | rtrimstr(\"\r\") | select(startswith(\"$marker status=\") or . == \"$marker -->\")" | tail -n 1)
56
91
  if [ "$COMMENT" = post ] || { [ -n "$last" ] && [ "$last" != "$marker status=$STATUS -->" ]; }; then
57
- { echo "$marker status=$STATUS -->"; cat "$RUNNER_TEMP/report/duel.md"; } > "$RUNNER_TEMP/comment.md"
92
+ { echo "$marker status=$STATUS -->"; report; } > "$RUNNER_TEMP/comment.md"
58
93
  gh api --method POST "repos/$GITHUB_REPOSITORY/issues/$PR/comments" -F body=@"$RUNNER_TEMP/comment.md" > /dev/null
59
94
  fi
95
+ <% if accept_label -%>
96
+
97
+ again:
98
+ name: Run Duel again
99
+ if: >-
100
+ github.event_name == 'pull_request_target' && github.event.label.name == '<%= accept_label.gsub("'", "''") %>'
101
+ runs-on: ubuntu-latest
102
+ permissions:
103
+ actions: write # to run the Perf workflow's jobs again
104
+ steps:
105
+ - name: Run Duel again
106
+ env:
107
+ GH_TOKEN: ${{ github.token }}
108
+ GH_REPO: ${{ github.repository }}
109
+ HEAD_SHA: ${{ github.event.pull_request.head.sha }}
110
+ ACTION: ${{ github.event.action }}
111
+ run: |
112
+ # The newest Perf run of the pull request's head commit, once it's
113
+ # done (one that's running may have read the labels already).
114
+ id=$(gh run list --workflow "Perf<%= " (#{working_directory})" if working_directory %>" --event pull_request --commit "$HEAD_SHA" --limit 1 \
115
+ --json databaseId --jq '.[0].databaseId // empty')
116
+ [ -n "$id" ] || exit 0
117
+ gh run watch "$id" --interval 30 > /dev/null || true
118
+ conclusion=$(gh run view "$id" --json conclusion --jq .conclusion)
119
+ case "$ACTION:$conclusion" in
120
+ labeled:failure) gh run rerun "$id" --failed ;;
121
+ unlabeled:success) gh run rerun "$id" ;;
122
+ esac
123
+ <% end -%>
@@ -47,6 +47,51 @@ jobs:
47
47
  with:
48
48
  fetch-depth: 0 # the baseline comes from history, tags included
49
49
  persist-credentials: false
50
+ # The labels of the pull request (or for a push, of the one that
51
+ # merged it), for c.accept_label in .duel.rb, as a JSON array. They're
52
+ # read as the job runs, and adding the label (or taking it off) runs
53
+ # the job again: see the Perf comment workflow.
54
+ <% if accept_label -%>
55
+ # On a schedule, the last commit since the last release that merged a
56
+ # pull request with the label (ACCEPT_LABEL, as .duel.rb names it):
57
+ # what that slowed down was accepted, so it's compared with instead.
58
+ <% end -%>
59
+ - name: Find the pull request's labels
60
+ id: labels
61
+ if: github.event_name == 'pull_request' || github.event_name == 'merge_group' || github.event_name == 'push'<% if accept_label %> || github.event_name == 'schedule'<% end %>
62
+ continue-on-error: true # (without them, nothing's accepted)
63
+ env:
64
+ GH_TOKEN: ${{ github.token }}
65
+ PR: ${{ github.event.pull_request.number }}
66
+ QUEUED: ${{ github.event.merge_group.head_ref }}
67
+ <% if accept_label -%>
68
+ ACCEPT_LABEL: '<%= accept_label.gsub("'", "''") %>'
69
+ RELEASE_TAGS: "v*"
70
+ <% end -%>
71
+ run: |
72
+ <% if accept_label -%>
73
+ if [ "$GITHUB_EVENT_NAME" = schedule ]; then
74
+ tag=$(git describe --tags --abbrev=0 --match "$RELEASE_TAGS" HEAD^ 2>/dev/null) || exit 0
75
+ since=$(TZ=UTC git log -1 --date=format-local:%Y-%m-%dT%H:%M:%SZ --format=%cd "$tag")
76
+ merged=$(gh pr list --repo "$GITHUB_REPOSITORY" --state merged --label "$ACCEPT_LABEL" --search "merged:>=$since" --limit 1000 \
77
+ --json mergeCommit --jq '.[].mergeCommit.oid')
78
+ accepted=$(git rev-list "$tag..HEAD" | grep -Fx -m 1 -f <(echo "$merged")) || accepted=
79
+ echo "accepted=$accepted" >> "$GITHUB_OUTPUT"
80
+ exit 0
81
+ fi
82
+ <% end -%>
83
+ # (In a merge queue, the last pull request queued, which the queue's
84
+ # branch names: gh-readonly-queue/main/pr-123-<sha>.)
85
+ if [ -n "$QUEUED" ]; then
86
+ PR=${QUEUED##*/pr-}
87
+ PR=${PR%%-*}
88
+ fi
89
+ if [ -n "$PR" ]; then
90
+ labels=$(gh api "repos/$GITHUB_REPOSITORY/issues/$PR/labels" --jq '[.[].name]')
91
+ else
92
+ labels=$(gh api "repos/$GITHUB_REPOSITORY/commits/$GITHUB_SHA/pulls" --jq '[.[0].labels[]?.name]')
93
+ fi
94
+ echo "labels=$labels" >> "$GITHUB_OUTPUT"
50
95
  <% if database == "postgresql" -%>
51
96
  # Each worker starts a PostgreSQL server of its own (duel-postgresql).
52
97
  - run: sudo apt-get install -y postgresql libpq-dev
@@ -69,7 +114,8 @@ jobs:
69
114
  # request targets (or run by hand, the main branch), a merge queue's
70
115
  # base, the commit before a push (or the last commit Duel passed, PASSED, if there is
71
116
  # one: see the Mark the commit passed job), or on a schedule the last
72
- # release, the newest tag before HEAD that RELEASE_TAGS matches. (If
117
+ # release, the newest tag before HEAD that RELEASE_TAGS matches<% if accept_label %>
118
+ # (or ACCEPTED, the last change accepted since then)<% end %>. (If
73
119
  # .duel.rb sets c.main_branch or c.release_tags, set MAIN_BRANCH or
74
120
  # RELEASE_TAGS to match.)
75
121
  - name: Find the baseline's Ruby
@@ -80,13 +126,16 @@ jobs:
80
126
  PASSED: origin/duel/passed<%= "-#{working_directory.tr("/", "-")}" if working_directory %>/${{ github.ref_name }}
81
127
  MAIN_BRANCH: origin/${{ github.base_ref || github.event.repository.default_branch }}
82
128
  RELEASE_TAGS: "v*"
129
+ <% if accept_label -%>
130
+ ACCEPTED: ${{ steps.labels.outputs.accepted }}
131
+ <% end -%>
83
132
  run: |
84
133
  case "$GITHUB_EVENT_NAME" in
85
134
  push)
86
135
  base=$BEFORE
87
136
  if git merge-base --is-ancestor "$PASSED" HEAD 2>/dev/null; then base=$PASSED; fi ;;
88
137
  merge_group) base=$QUEUE_BASE ;;
89
- schedule) base=$(git describe --tags --abbrev=0 --match "$RELEASE_TAGS" HEAD^ 2>/dev/null) || base= ;;
138
+ schedule) base=${ACCEPTED:-$(git describe --tags --abbrev=0 --match "$RELEASE_TAGS" HEAD^ 2>/dev/null)} || base= ;;
90
139
  *) base=$(git merge-base HEAD "$MAIN_BRANCH" 2>/dev/null) || base= ;;
91
140
  esac
92
141
  ruby_of() { git show "$1:./.ruby-version" 2>/dev/null | awk 'NF { print $1; exit }'; }
@@ -125,30 +174,6 @@ jobs:
125
174
  ruby-version: "<%= ruby_version %>"
126
175
  <% end -%>
127
176
  bundler-cache: true
128
- # The labels of the pull request (or for a push, of the one that
129
- # merged it), for c.accept_label in .duel.rb, as a JSON array. They're
130
- # read as the job runs, so after adding the label, run it again.
131
- - name: Find the pull request's labels
132
- id: labels
133
- if: github.event_name == 'pull_request' || github.event_name == 'merge_group' || github.event_name == 'push'
134
- continue-on-error: true # (without them, nothing's accepted)
135
- env:
136
- GH_TOKEN: ${{ github.token }}
137
- PR: ${{ github.event.pull_request.number }}
138
- QUEUED: ${{ github.event.merge_group.head_ref }}
139
- run: |
140
- # (In a merge queue, the last pull request queued, which the queue's
141
- # branch names: gh-readonly-queue/main/pr-123-<sha>.)
142
- if [ -n "$QUEUED" ]; then
143
- PR=${QUEUED##*/pr-}
144
- PR=${PR%%-*}
145
- fi
146
- if [ -n "$PR" ]; then
147
- labels=$(gh api "repos/$GITHUB_REPOSITORY/issues/$PR/labels" --jq '[.[].name]')
148
- else
149
- labels=$(gh api "repos/$GITHUB_REPOSITORY/commits/$GITHUB_SHA/pulls" --jq '[.[0].labels[]?.name]')
150
- fi
151
- echo "labels=$labels" >> "$GITHUB_OUTPUT"
152
177
  # Against the branch it targets for a pull request, the queue's base
153
178
  # for a merge queue, what was there before for a push (or as
154
179
  # c.push_base in .duel.rb says, the last commit Duel passed), and the
@@ -164,6 +189,9 @@ jobs:
164
189
  # with itself, which checks the setup.)
165
190
  DUEL_FORCE: ${{ github.event_name == 'workflow_dispatch' && '1' || '' }}
166
191
  DUEL_LABELS: ${{ steps.labels.outputs.labels }}
192
+ <% if accept_label -%>
193
+ DUEL_ACCEPTED: ${{ steps.labels.outputs.accepted }}
194
+ <% end -%>
167
195
  run: bundle exec duel --markdown "$RUNNER_TEMP/duel/duel.md"
168
196
  # For a pull request, what Duel said, for the Perf comment workflow,
169
197
  # which can't see this job's outputs.
@@ -185,6 +213,7 @@ jobs:
185
213
  path: ${{ runner.temp }}/duel/
186
214
  if-no-files-found: ignore # none when Duel didn't get to compare
187
215
  retention-days: 1
216
+ <% if comment_on_pushes? -%>
188
217
 
189
218
  # The report, as a new comment on a pushed commit, which notifies its
190
219
  # author, when Duel says to, as for a pull request (see the Perf comment
@@ -221,6 +250,8 @@ jobs:
221
250
  { echo "$marker status=$STATUS -->"; cat "$RUNNER_TEMP/report/duel.md"; } > "$RUNNER_TEMP/comment.md"
222
251
  gh api --method POST "repos/$GITHUB_REPOSITORY/commits/$GITHUB_SHA/comments" -F body=@"$RUNNER_TEMP/comment.md" > /dev/null
223
252
  fi
253
+ <% end -%>
254
+ <% if mark_passed? -%>
224
255
 
225
256
  # With c.push_base = :last_passed in .duel.rb, a push is compared with
226
257
  # the last commit Duel passed, which a branch marks (each branch pushed
@@ -233,6 +264,13 @@ jobs:
233
264
  passed:
234
265
  name: Mark the commit passed
235
266
  needs: duel
267
+ # One at a time for each branch, so that two runs can't both find no
268
+ # branch and race to make it (a push's run doesn't wait for the one
269
+ # before it). One that's running is never cancelled, and one that's
270
+ # waiting gives way to a newer push's, which moves it further anyway.
271
+ concurrency:
272
+ group: ${{ github.workflow }}-passed-${{ github.ref_name }}
273
+ cancel-in-progress: false
236
274
  if: ${{ !cancelled() && github.event_name == 'push' && needs.duel.outputs.passed == 'true' }}
237
275
  runs-on: ubuntu-latest
238
276
  permissions:
@@ -256,13 +294,16 @@ jobs:
256
294
  done
257
295
  exit 1
258
296
  }
259
- # (Forward: GitHub moves it only where it's behind the push.)
260
- gh api --method PATCH "repos/$GITHUB_REPOSITORY/git/refs/heads/$BRANCH" -f sha="$GITHUB_SHA" > /dev/null 2>&1 && exit 0
261
- at=$(gh api "repos/$GITHUB_REPOSITORY/git/ref/heads/$BRANCH" --jq .object.sha 2>/dev/null) || at=
262
- if [ -z "$at" ]; then
263
- gh api --method POST "repos/$GITHUB_REPOSITORY/git/refs" -f ref="refs/heads/$BRANCH" -f sha="$GITHUB_SHA" > /dev/null || blocked
264
- exit 0
265
- fi
297
+ # (Forward: GitHub moves it only where it's behind the push. Where
298
+ # there's no branch, and making it fails, it looks once more, as
299
+ # something else may have made it: a push of it, say.)
300
+ for _ in 1 2; do
301
+ gh api --method PATCH "repos/$GITHUB_REPOSITORY/git/refs/heads/$BRANCH" -f sha="$GITHUB_SHA" > /dev/null 2>&1 && exit 0
302
+ at=$(gh api "repos/$GITHUB_REPOSITORY/git/ref/heads/$BRANCH" --jq .object.sha 2>/dev/null) || at=
303
+ [ -n "$at" ] && break
304
+ gh api --method POST "repos/$GITHUB_REPOSITORY/git/refs" -f ref="refs/heads/$BRANCH" -f sha="$GITHUB_SHA" > /dev/null && exit 0
305
+ done
306
+ [ -n "$at" ] || blocked
266
307
  compared=$(gh api "repos/$GITHUB_REPOSITORY/compare/$GITHUB_SHA...$at" --jq .status 2>&1) || case "$compared" in
267
308
  # (GitHub can't compare commits with no history in common.)
268
309
  *"No common ancestor"*) compared=unrelated ;;
@@ -274,6 +315,8 @@ jobs:
274
315
  diverged | behind | unrelated) gh api --method PATCH "repos/$GITHUB_REPOSITORY/git/refs/heads/$BRANCH" -f sha="$GITHUB_SHA" -F force=true > /dev/null ;;
275
316
  *) echo "::error::Couldn't compare $BRANCH with this push: $compared" && exit 1 ;;
276
317
  esac
318
+ <% end -%>
319
+ <% if badge? -%>
277
320
 
278
321
  # Duel's badge, for the README: green (perf guarded) or red (perf
279
322
  # regressed), as the last push to the default branch that Duel compared
@@ -285,10 +328,18 @@ jobs:
285
328
  #
286
329
  # [![Duel](https://raw.githubusercontent.com/OWNER/REPO/duel/badge<%= "-#{working_directory.tr("/", "-")}" if working_directory %>/badge.svg)](https://github.com/sferik/duel-ruby)
287
330
  #
288
- # It checks nothing out and runs none of the code.
331
+ # It checks nothing out, runs none of the code, and fetches nothing.
289
332
  badge:
290
333
  name: Update the badge
291
334
  needs: duel
335
+ # One at a time, so that one run's check of which push the badge
336
+ # shows can't interleave with another run's write (a push's run
337
+ # doesn't wait for the one before it). A badge job that's running is
338
+ # never cancelled, and one that's waiting gives way to a newer push's,
339
+ # whose result the badge should show anyway.
340
+ concurrency:
341
+ group: ${{ github.workflow }}-badge
342
+ cancel-in-progress: false
292
343
  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') }}
293
344
  runs-on: ubuntu-latest
294
345
  permissions:
@@ -300,7 +351,6 @@ jobs:
300
351
  BRANCH: duel/badge<%= "-#{working_directory.tr("/", "-")}" if working_directory %>
301
352
  RESULT: ${{ needs.duel.result }}
302
353
  STATUS: ${{ needs.duel.outputs.status }}
303
- BADGES: https://raw.githubusercontent.com/sferik/duel-ruby/main/assets
304
354
  run: |
305
355
  # (Git can't make a branch where another's name is a folder of its
306
356
  # name: a branch named duel blocks duel/passed/main. If one does,
@@ -319,11 +369,59 @@ jobs:
319
369
  # Its commit names the push.)
320
370
  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
321
371
  if [ -n "$last" ] && [ "$(gh api "repos/$GITHUB_REPOSITORY/compare/$GITHUB_SHA...$last" --jq .status 2>/dev/null)" = ahead ]; then exit 0; fi
322
- badge="badge.svg"
323
- if [ "$RESULT" = failure ] || [ "$STATUS" = failed ]; then badge="badge-regressed.svg"; fi
324
- curl -fsSL "$BADGES/$badge" -o "$RUNNER_TEMP/badge.svg"
372
+ # (Its image, written here rather than fetched from anywhere: green,
373
+ # or red where the Duel job failed or found a regression.)
374
+ state="perf guarded" width=133 top="#34D058" bottom="#28A745" right="76.939C78.629 0 80 1.343 80"
375
+ if [ "$RESULT" = failure ] || [ "$STATUS" = failed ]; then
376
+ state="perf regressed" width=142 top="#EA4A5A" bottom="#D73A49" right="85.939C87.629 0 89 1.343 89"
377
+ fi
378
+ cat > "$RUNNER_TEMP/badge.svg" <<SVG
379
+ <svg xmlns="http://www.w3.org/2000/svg" width="$width" height="20">
380
+ <title>Duel - $state</title>
381
+ <defs>
382
+ <linearGradient id="duel-fill" x1="50%" y1="0%" x2="50%" y2="100%">
383
+ <stop stop-color="#444D56" offset="0%"></stop>
384
+ <stop stop-color="#24292E" offset="100%"></stop>
385
+ </linearGradient>
386
+ <linearGradient id="state-fill" x1="50%" y1="0%" x2="50%" y2="100%">
387
+ <stop stop-color="$top" offset="0%"></stop>
388
+ <stop stop-color="$bottom" offset="100%"></stop>
389
+ </linearGradient>
390
+ </defs>
391
+ <g fill="none" fill-rule="evenodd">
392
+ <g font-family="'DejaVu Sans',Verdana,Geneva,sans-serif" font-size="11">
393
+ <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>
394
+ <text fill="#010101" fill-opacity=".3">
395
+ <tspan x="22.1981982" y="15" aria-hidden="true">Duel</tspan>
396
+ </text>
397
+ <text fill="#FFFFFF">
398
+ <tspan x="22.1981982" y="14">Duel</tspan>
399
+ </text>
400
+ </g>
401
+ <g transform="translate(53)" font-family="'DejaVu Sans',Verdana,Geneva,sans-serif" font-size="11">
402
+ <path d="M0 0h$right 3v14c0 1.657-1.37 3-3.061 3H0V0z" fill="url(#state-fill)" fill-rule="nonzero"></path>
403
+ <text fill="#010101" fill-opacity=".3" aria-hidden="true">
404
+ <tspan x="4" y="15">$state</tspan>
405
+ </text>
406
+ <text fill="#FFFFFF">
407
+ <tspan x="4" y="14">$state</tspan>
408
+ </text>
409
+ </g>
410
+ <g fill="#959DA5">
411
+ <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>
412
+ <circle cx="16.6" cy="15.6" r="1.1"></circle>
413
+ <circle cx="5.4" cy="15.6" r="1.1"></circle>
414
+ </g>
415
+ <g stroke="#959DA5" stroke-linecap="round">
416
+ <path d="M12.1 14.5L15.5 11.1 M9.9 14.5L6.5 11.1" stroke-width="1.6"></path>
417
+ <path d="M13.8 12.8L16 15 M8.2 12.8L6 15" stroke-width="1.8"></path>
418
+ </g>
419
+ </g>
420
+ </svg>
421
+ SVG
325
422
  tree=$(jq -n --rawfile svg "$RUNNER_TEMP/badge.svg" '{tree: [{path: "badge.svg", mode: "100644", type: "blob", content: $svg}]}' |
326
423
  gh api --method POST "repos/$GITHUB_REPOSITORY/git/trees" --input - --jq .sha)
327
424
  commit=$(gh api --method POST "repos/$GITHUB_REPOSITORY/git/commits" -f message="Duel's badge, for $GITHUB_SHA" -f tree="$tree" --jq .sha)
328
425
  gh api --method PATCH "repos/$GITHUB_REPOSITORY/git/refs/heads/$BRANCH" -f sha="$commit" -F force=true > /dev/null 2>&1 ||
329
426
  gh api --method POST "repos/$GITHUB_REPOSITORY/git/refs" -f ref="refs/heads/$BRANCH" -f sha="$commit" > /dev/null || blocked
427
+ <% end -%>
data/sig/duel/rails.rbs CHANGED
@@ -229,6 +229,25 @@ module Duel
229
229
 
230
230
  private
231
231
 
232
+ @settings: Config?
233
+
234
+ # The app's settings, which decide the workflow's jobs (nil where
235
+ # .duel.rb doesn't load).
236
+ def settings: () -> Config?
237
+ def load_settings: () -> Config?
238
+
239
+ # Whether the workflow comments on a pushed commit.
240
+ def comment_on_pushes?: () -> bool
241
+
242
+ # Whether the workflow marks a push passed.
243
+ def mark_passed?: () -> bool
244
+
245
+ # The label that accepts a regression, for the workflows, or nil.
246
+ def accept_label: () -> String?
247
+
248
+ # Whether the workflow keeps a badge for the README.
249
+ def badge?: () -> bool
250
+
232
251
  # The app's Gemfile.
233
252
  def gemfile: () -> String
234
253
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: duel-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erik Berlin