duel-rails 0.1.7 → 0.1.8

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: 10c5462b8eb10bcfe6d1baf0b9d8d8ffa456040fb40af93d5a302e338529b12a
4
- data.tar.gz: f34caca4a75935765dbb6515729de4ae9514088e67ca1f6e6ac0f6ab61351829
3
+ metadata.gz: 6d979498ecebfdd28819166ca2213526b2f896d884768ce83d3b587af03a5897
4
+ data.tar.gz: 9aadce2e4a34cc790028f25e4494f6090734d40bb51446591ca1c7200c2d4e80
5
5
  SHA512:
6
- metadata.gz: e34e619290e089caba95bec6cefe5917d0adc46d215d1d2176f07c754d7b927a56ad2cd0ce4d21d6fbadd8e337fd5f713c8ef20f23155c2bf9ab5785ce8e041e
7
- data.tar.gz: be449ca36d29f39d4359a50c9fb051c7ca6dbf1b258c6fd4ffe995ddcc108eca3f07dfa4227e687a31374e0bd212225b9e0f920f702ad0dde68c42dd85a6cb75
6
+ metadata.gz: 7ef4f7fc60b8bae95e87ada152590579dd3dd60a46b7606dd3f894391523e3ea1cb00d4c59de0587791989276c9cf3eb85d0a0c2044f91240aac043d34cef017
7
+ data.tar.gz: c3ea9ee90b33743ec21716e70fe30819a93b16f8eb2ef0f1d0614f5cf7e11941ab8424846a7754b1926aedec5d603cb414a90d35d608062d2454f732a67cc5a0
data/CHANGELOG.md CHANGED
@@ -7,6 +7,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.1.8] - 2026-11-12
11
+
12
+ ### Added
13
+
14
+ - `bin/rails generate duel:install --check` says how the app's
15
+ workflows differ from what the generator writes, as a diff, writes
16
+ nothing, and exits with 1 where one does.
17
+
18
+ ### Changed
19
+
20
+ - The generator's comment workflow is Duel's: it posts a report from a
21
+ fork's pull request as Markdown, like any other, instead of as text in
22
+ a code block, and runs `duel comment`, from RubyGems, which also hides
23
+ Duel's earlier comments as outdated.
24
+
10
25
  ## [0.1.7] - 2026-11-10
11
26
 
12
27
  No changes of its own: released in step with the other Duel gems.
@@ -254,7 +269,8 @@ Rails 7.2 or later, and Ruby 3.4 or later.
254
269
  CI.
255
270
  - RBS signatures in sig/, checked with Steep.
256
271
 
257
- [Unreleased]: https://github.com/sferik/duel-ruby/compare/duel-rails-v0.1.7...HEAD
272
+ [Unreleased]: https://github.com/sferik/duel-ruby/compare/duel-rails-v0.1.8...HEAD
273
+ [0.1.8]: https://github.com/sferik/duel-ruby/compare/duel-rails-v0.1.7...duel-rails-v0.1.8
258
274
  [0.1.7]: https://github.com/sferik/duel-ruby/compare/duel-rails-v0.1.6...duel-rails-v0.1.7
259
275
  [0.1.6]: https://github.com/sferik/duel-ruby/compare/duel-rails-v0.1.5...duel-rails-v0.1.6
260
276
  [0.1.5]: https://github.com/sferik/duel-ruby/compare/duel-rails-v0.1.4...duel-rails-v0.1.5
data/README.md CHANGED
@@ -80,7 +80,9 @@ production does, with everything of its own:
80
80
  [duel-mysql](https://github.com/sferik/duel-ruby/tree/main/plugins/mysql) too.
81
81
  Run `bundle install` again.
82
82
 
83
- 4. Review the generated files, and commit them.
83
+ 4. Review the generated files, and commit them. After you upgrade
84
+ duel-rails, `bin/rails generate duel:install --check` shows how your
85
+ workflows differ from what it writes now, and writes nothing.
84
86
 
85
87
  5. Run the benchmarks against your main branch:
86
88
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Duel
4
4
  module Rails
5
- VERSION = "0.1.7"
5
+ VERSION = "0.1.8"
6
6
  end
7
7
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "erb"
3
4
  require "open3"
4
5
  require "pathname"
5
6
  require "rails/generators"
@@ -22,6 +23,8 @@ module Duel
22
23
  desc: "Each worker's database (default: as the app's own)"
23
24
  class_option :badge, type: :boolean, default: false,
24
25
  desc: "Write a job that keeps a badge for the README (it needs contents: write)"
26
+ class_option :check, type: :boolean, default: false,
27
+ desc: "Say how the workflows differ from what it writes (with the same options), and write nothing"
25
28
 
26
29
  # The adapters each worker's database can stand in for, by the
27
30
  # database duel-rails calls it.
@@ -37,28 +40,23 @@ module Duel
37
40
  # Controllers of Rails's own (its health check, Active Storage...).
38
41
  FRAMEWORK = %r{\A(rails|active_storage|action_mailbox|turbo)/}
39
42
 
40
- def create_config = template("duel.rb.tt", ".duel.rb")
43
+ def create_config = (template("duel.rb.tt", ".duel.rb") unless checking?)
41
44
 
42
- def create_environment = template("benchmark.rb.tt", "config/environments/benchmark.rb")
45
+ def create_environment = (template("benchmark.rb.tt", "config/environments/benchmark.rb") unless checking?)
43
46
 
44
- def create_benchmarks = template("requests_duel.rb.tt", "duel/requests_duel.rb")
47
+ def create_benchmarks = (template("requests_duel.rb.tt", "duel/requests_duel.rb") unless checking?)
45
48
 
46
- def create_boot_benchmark = template("boot_duel.rb.tt", "duel/boot_duel.rb")
49
+ def create_boot_benchmark = (template("boot_duel.rb.tt", "duel/boot_duel.rb") unless checking?)
47
50
 
48
- # The workflows, at the repository's root: perf.yml, and
49
- # perf-comment.yml, which comments on a pull request with the
50
- # report, or, for an app in a subdirectory, perf-<its path>.yml
51
- # (perf-apps-blog.yml) and perf-<its path>-comment.yml, so that each
52
- # of a monorepo's apps can have its own.
51
+ # The workflows, at the repository's root (see #workflows).
53
52
  def create_workflow
54
- dir = working_directory
55
- name = dir ? "perf-#{dir.tr("/", "-")}" : "perf"
56
- template("perf.yml.tt", File.join(repository, ".github", "workflows", "#{name}.yml"))
57
- template("perf-comment.yml.tt", File.join(repository, ".github", "workflows", "#{name}-comment.yml"))
53
+ workflows.each { |source, path| template(source, path) } unless checking?
58
54
  end
59
55
 
60
56
  # The plugin that gives each worker its database's server.
61
57
  def add_plugin
58
+ return if checking?
59
+
62
60
  plugin = PLUGINS[database]
63
61
  gem(plugin, group: :development) if plugin && !gemfile.include?(plugin)
64
62
  end
@@ -66,11 +64,56 @@ module Duel
66
64
  # StackProf, for profiles of regressions: it runs on CRuby only, so
67
65
  # it's left out of JRuby's and TruffleRuby's bundles.
68
66
  def add_stackprof
69
- gem("stackprof", platforms: :mri, group: :development) unless gemfile.include?("stackprof")
67
+ gem("stackprof", platforms: :mri, group: :development) unless checking? || gemfile.include?("stackprof")
68
+ end
69
+
70
+ # With --check, says whether each workflow is what it writes, or how
71
+ # it differs (as diff -u has it), or that it's missing, as `duel init
72
+ # --check` does, and exits with 1 where one isn't (a generator's
73
+ # failure doesn't change its exit status otherwise).
74
+ def check_workflows
75
+ return unless checking?
76
+
77
+ differ = workflows.count { |source, path| !same?(source, path) }
78
+ exit(1) if differ.positive?
70
79
  end
71
80
 
72
81
  private
73
82
 
83
+ def checking? = options.fetch(:check)
84
+
85
+ # The workflows' templates, by where they're written: perf.yml, and
86
+ # perf-comment.yml, which comments on a pull request with the
87
+ # report, or, for an app in a subdirectory, perf-<its path>.yml
88
+ # (perf-apps-blog.yml) and perf-<its path>-comment.yml, so that each
89
+ # of a monorepo's apps can have its own.
90
+ def workflows
91
+ dir = working_directory
92
+ name = dir ? "perf-#{dir.tr("/", "-")}" : "perf"
93
+ {"perf.yml.tt" => File.join(repository, ".github", "workflows", "#{name}.yml"),
94
+ "perf-comment.yml.tt" => File.join(repository, ".github", "workflows", "#{name}-comment.yml")}
95
+ end
96
+
97
+ # Whether the workflow at +path+ is what the template +source+ makes,
98
+ # saying so, or how it differs, or that it's missing.
99
+ def same?(source, path)
100
+ shown = Pathname(path).relative_path_from(destination_root).to_s
101
+ unless File.file?(path)
102
+ say_status :missing, shown
103
+ return false
104
+ end
105
+
106
+ content = ERB.new(File.read(find_in_source_paths(source)), trim_mode: "-").result(template_binding)
107
+ differences, = Open3.capture2("diff", "-u", "-L", shown, "-L", "#{shown} (duel:install)", path, "-", stdin_data: content)
108
+ say_status(differences.empty? ? :identical : :differs, shown)
109
+ say differences unless differences.empty?
110
+ differences.empty?
111
+ end
112
+
113
+ # A binding for a template, as Thor's: of the generator, and none of
114
+ # a method's variables.
115
+ def template_binding = binding
116
+
74
117
  # The app's settings, which decide the workflow's jobs, as for
75
118
  # `duel init` (see Duel::Init#settings): its .duel.rb's, which
76
119
  # create_config has written by now (or kept), or nil where it doesn't
@@ -161,6 +204,10 @@ module Duel
161
204
  path = Pathname(File.realpath(destination_root)).relative_path_from(File.realpath(repository)).to_s
162
205
  path unless path == "."
163
206
  end
207
+
208
+ # What marks the workflow's comment on a pull request, as `duel
209
+ # init`'s does (see Duel::Init#marker).
210
+ def marker = ["duel", working_directory].compact.join(" ")
164
211
  end
165
212
  end
166
213
  end
@@ -3,14 +3,15 @@ name: Perf comment<%= " (#{working_directory})" if working_directory %>
3
3
  # Duel's report, as a new comment on the pull request, when Duel says to:
4
4
  # always (post), or only when its last comment's status differs from the
5
5
  # run's (change), so the latest comment is never stale. It never edits or
6
- # deletes a comment. It runs once the Perf workflow has, with this
7
- # repository's own workflow and a token that can write, so a pull request
8
- # from a fork gets a comment too. It checks nothing out and runs none of
9
- # the pull request's code, and of what the Duel job kept (where that code
10
- # ran), it trusts only what that job could have said: the pull request's
11
- # number, if it names the pull request the run compared, and the comment
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.
6
+ # deletes a comment, but hides Duel's earlier ones as outdated, so the
7
+ # newest is the one in view. It runs once the Perf workflow has, with
8
+ # this repository's own workflow and a token that can write, so a pull
9
+ # request from a fork gets a comment too. It checks nothing out and runs
10
+ # none of the pull request's code, only `duel comment` (see Duel's
11
+ # docs/ci.md), which trusts only what the Duel job could have said in
12
+ # what it kept: the pull request's number, if it names the pull request
13
+ # the run compared, and the comment and the status, if they're ones Duel
14
+ # says. Its report, a fork's too, is the comment, as Markdown.
14
15
  <% if accept_label -%>
15
16
  #
16
17
  # And when the label that accepts a regression (c.accept_label in
@@ -52,46 +53,20 @@ jobs:
52
53
  path: ${{ runner.temp }}/report
53
54
  run-id: ${{ github.event.workflow_run.id }}
54
55
  github-token: ${{ github.token }}
56
+ - uses: ruby/setup-ruby@v1
57
+ if: steps.report.outcome == 'success'
58
+ with:
59
+ ruby-version: ruby
60
+ # Duel's own code, from RubyGems (none of the pull request's), so a
61
+ # fix to how it comments comes with a release of Duel.
55
62
  - name: Comment on the pull request
56
63
  if: steps.report.outcome == 'success'
57
64
  env:
58
65
  GH_TOKEN: ${{ github.token }}
59
66
  HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
60
67
  run: |
61
- said() { grep -xE "$1=($2)" "$RUNNER_TEMP/report/comment.env" | tail -n 1 | cut -d= -f2; }
62
- PR=$(said pr '[0-9]+')
63
- COMMENT=$(said comment 'post|change')
64
- STATUS=$(said status 'passed|failed|neutral')
65
- [ -n "$PR" ] && [ -n "$COMMENT" ] && [ -n "$STATUS" ] || 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
- }
86
- marker="<!-- duel<%= " #{working_directory}" if working_directory %>"
87
- # The first line of the last comment Duel posted here: its marker,
88
- # with its status (none in Duel 0.1.0's, which counts as another).
89
- last=$(gh api --paginate "repos/$GITHUB_REPOSITORY/issues/$PR/comments" \
90
- --jq ".[] | select(.user.login == \"github-actions[bot]\") | .body | split(\"\n\")[0] | rtrimstr(\"\r\") | select(startswith(\"$marker status=\") or . == \"$marker -->\")" | tail -n 1)
91
- if [ "$COMMENT" = post ] || { [ -n "$last" ] && [ "$last" != "$marker status=$STATUS -->" ]; }; then
92
- { echo "$marker status=$STATUS -->"; report; } > "$RUNNER_TEMP/comment.md"
93
- gh api --method POST "repos/$GITHUB_REPOSITORY/issues/$PR/comments" -F body=@"$RUNNER_TEMP/comment.md" > /dev/null
94
- fi
68
+ gem install duel --version '~> 0.1.8' --no-document
69
+ duel comment --report "$RUNNER_TEMP/report" --head "$HEAD_SHA" --marker '<%= marker.gsub("'") { "'\\''" } %>'
95
70
  <% if accept_label -%>
96
71
 
97
72
  again:
data/sig/duel/rails.rbs CHANGED
@@ -227,8 +227,23 @@ module Duel
227
227
  def add_plugin: () -> void
228
228
  def add_stackprof: () -> void
229
229
 
230
+ # With --check, says how each workflow differs, and exits with 1
231
+ # where one does.
232
+ def check_workflows: () -> void
233
+
230
234
  private
231
235
 
236
+ def checking?: () -> bool
237
+
238
+ # The workflows' templates, by where they're written.
239
+ def workflows: () -> Hash[String, String]
240
+
241
+ # Whether the workflow at +path+ is what +source+ makes, saying so.
242
+ def same?: (String source, String path) -> bool
243
+
244
+ # A binding for a template, of the generator.
245
+ def template_binding: () -> Binding
246
+
232
247
  @settings: Config?
233
248
 
234
249
  # The app's settings, which decide the workflow's jobs (nil where
@@ -259,6 +274,9 @@ module Duel
259
274
  def ruby_version: () -> String?
260
275
  def repository: () -> String
261
276
  def working_directory: () -> String?
277
+
278
+ # What marks the workflow's comment on a pull request.
279
+ def marker: () -> String
262
280
  end
263
281
  end
264
282
  end
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.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erik Berlin