Skip to content

Commit fedd763

Browse files
ericproulxclaude
andcommitted
Replace Danger with a dependency-free CHANGELOG check
Danger ran one check, `changelog.check!`, and cost far more than that: * 29 of the 91 gems in the lockfile (octokit, faraday, git, kramdown and their dependencies) were there only for it. They sat in the :test group, which every gemfile evals, so every CI job and every local bundle installed them, and #3045 had to pin `git < 6` to keep it loading. * Its two workflows called a third-party reusable workflow, pinned by tag, with `secrets: inherit`. * It parsed the whole of CHANGELOG.md, so it failed pull requests on entries merged years before, and it failed every pull request opened with the `#PR` placeholder AGENTS.md asks for until the number was filled in. .github/scripts/check_changelog.rb replaces it. It needs nothing but Ruby and git, so .github/workflows/changelog.yml installs no bundle and uses no secret. It reads the lines the pull request adds to CHANGELOG.md: * a new entry must be `* [#N](https://github.com/ruby-grape/grape/pull/N): Description - [@user](https://github.com/user).`, with N this pull request's number; an edited entry keeps the number it was merged with; * a `#PR` or `#XXXX` placeholder is a warning, not a failure; * `* Your contribution here.` stays the last line of each section of the unreleased version; * a pull request that changes more than Markdown files without touching CHANGELOG.md gets a warning, as it did with Danger. Problems are annotations on the CHANGELOG line. RuboCop skips hidden directories, so .rubocop.yml now includes .github/**/*.rb. Removing Danger took kramdown out of the bundle, and with it rexml, which ActiveSupport::XmlMini needs to parse XML when multi_xml is absent: the XML specs only passed because Danger brought it in. The :test group now declares rexml itself. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 9c1a071 commit fedd763

8 files changed

Lines changed: 158 additions & 34 deletions

File tree

‎.github/scripts/check_changelog.rb‎

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# frozen_string_literal: true
2+
3+
# Checks what a pull request does to CHANGELOG.md. It uses no gem, so the job
4+
# that runs it (.github/workflows/changelog.yml) installs no bundle.
5+
#
6+
# Only the lines the pull request adds are checked: an entry merged long ago is
7+
# not its author's to fix. Problems are printed as GitHub annotations on the
8+
# CHANGELOG line they are about. An error fails the job, a warning does not.
9+
#
10+
# Locally, against origin/master:
11+
#
12+
# ruby .github/scripts/check_changelog.rb
13+
# PR_NUMBER=1234 ruby .github/scripts/check_changelog.rb
14+
15+
CHANGELOG = 'CHANGELOG.md'
16+
PLACEHOLDER = '* Your contribution here.'
17+
EXAMPLE = '* [#1234](https://github.com/ruby-grape/grape/pull/1234): Describe the change - [@you](https://github.com/you).'
18+
AUTHOR = %r{\[@[\w-]+(?:\[bot\])?\]\(https://github\.com/[^)\s]+\)}
19+
ENTRY = %r{\A\* \[#(?<number>\d+)\]\(https://github\.com/ruby-grape/grape/pull/\k<number>\): [`[:upper:]].*[^.,] - #{AUTHOR}(?:, #{AUTHOR})*\.\z}
20+
# What an entry holds until its pull request is open and has a number.
21+
PENDING = /\A\* \[#(?:PR|X+)\]/i
22+
23+
def git(*args)
24+
output = IO.popen(['git', *args], &:read)
25+
abort "git #{args.join(' ')} failed" unless Process.last_status.success?
26+
output
27+
end
28+
29+
def annotate(level, message, line: nil)
30+
puts "::#{level}#{" file=#{CHANGELOG},line=#{line}" if line}::#{message}"
31+
end
32+
33+
# The lines the diff adds, with their line numbers in the new file, and the
34+
# pull request numbers of the entries it removes.
35+
def changelog_diff(range)
36+
added = []
37+
removed = []
38+
line_no = nil
39+
git('diff', '--unified=0', range, '--', CHANGELOG).each_line(chomp: true) do |line|
40+
if (hunk = line.match(/\A@@ -\S+ \+(\d+)/))
41+
line_no = hunk[1].to_i
42+
elsif line_no && line.start_with?('+')
43+
added << [line_no, line[1..]]
44+
line_no += 1
45+
elsif line_no && line.start_with?('-')
46+
removed << line[/\A-\* \[#(\d+)\]/, 1]
47+
end
48+
end
49+
[added, removed.compact]
50+
end
51+
52+
# A line that is meant to be an entry: not a heading, the placeholder or blank.
53+
def entry_line?(line)
54+
!(line.strip.empty? || line.start_with?('#') || line == PLACEHOLDER)
55+
end
56+
57+
# [level, message] for an added entry line, or nil when it is fine.
58+
def entry_problem(line, removed_numbers, pr_number)
59+
return [:warning, 'Replace the placeholder with the number of this pull request once it is open.'] if line.match?(PENDING)
60+
61+
entry = line.match(ENTRY)
62+
return [:error, "This line should look like `#{EXAMPLE}`"] unless entry
63+
64+
number = entry[:number]
65+
[:error, "This entry points at ##{number}, but this is ##{pr_number}."] if wrong_number?(number, removed_numbers, pr_number)
66+
end
67+
68+
# An edited entry keeps the number it was merged with, so only a new one has to
69+
# point at this pull request. Without PR_NUMBER (a local run) nothing does.
70+
def wrong_number?(number, removed_numbers, pr_number)
71+
pr_number && number != pr_number && !removed_numbers.include?(number)
72+
end
73+
74+
# Lines with a problem, as [level, line number, message].
75+
def entry_problems(added, removed_numbers, pr_number)
76+
added.filter_map do |line_no, line|
77+
level, message = entry_problem(line, removed_numbers, pr_number) if entry_line?(line)
78+
[level, line_no, message] if level
79+
end
80+
end
81+
82+
# The lines of the unreleased version as [line number, line], or none when the
83+
# first version in the file has been released.
84+
def unreleased_section(lines)
85+
first = lines.index { |line| line.start_with?('### ') }
86+
return [] unless first && lines[first].include?('(Next)')
87+
88+
stop = lines.each_index.find { |i| i > first && lines[i].start_with?('### ') } || lines.size
89+
((first + 1)...stop).map { |i| [i + 1, lines[i]] }
90+
end
91+
92+
# The subsections of the unreleased version whose last line is not the
93+
# placeholder, as [heading line number, heading].
94+
def misplaced_placeholders(lines)
95+
unreleased_section(lines).slice_before { |_, line| line.start_with?('#### ') }.filter_map do |(line_no, heading), *rest|
96+
next unless heading.start_with?('#### ')
97+
98+
last = rest.map(&:last).reject { |line| line.strip.empty? }.last
99+
[line_no, heading] unless last == PLACEHOLDER
100+
end
101+
end
102+
103+
range = "#{ENV.fetch('BASE_REF', 'origin/master')}...HEAD"
104+
pr_number = ENV.fetch('PR_NUMBER', nil)
105+
changed = git('diff', '--name-only', range).split("\n")
106+
107+
unless changed.include?(CHANGELOG)
108+
code_changed = changed.any? { |path| !path.end_with?('.md') }
109+
annotate :warning, "Unless this only refactors code or changes the documentation, add a line to #{CHANGELOG}: #{EXAMPLE}" if code_changed
110+
exit
111+
end
112+
113+
added, removed_numbers = changelog_diff(range)
114+
problems = entry_problems(added, removed_numbers, pr_number)
115+
misplaced_placeholders(File.readlines(CHANGELOG, chomp: true)).each do |line_no, heading|
116+
problems << [:error, line_no, "`#{PLACEHOLDER}` should stay the last line under `#{heading}`."]
117+
end
118+
119+
problems.each { |level, line_no, message| annotate level, message, line: line_no }
120+
exit 1 if problems.any? { |level, _, _| level == :error }

‎.github/workflows/changelog.yml‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: changelog
2+
3+
on: pull_request
4+
5+
permissions:
6+
contents: read
7+
8+
jobs:
9+
changelog:
10+
name: CHANGELOG
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v6
14+
with:
15+
# The merge commit and its first parent, the base branch it merges into.
16+
fetch-depth: 2
17+
18+
- name: Set up Ruby
19+
uses: ruby/setup-ruby@v1
20+
with:
21+
ruby-version: 3.4
22+
23+
- name: Check CHANGELOG.md
24+
run: ruby .github/scripts/check_changelog.rb
25+
env:
26+
BASE_REF: HEAD^1
27+
PR_NUMBER: ${{ github.event.pull_request.number }}

‎.github/workflows/danger-comment.yml‎

Lines changed: 0 additions & 11 deletions
This file was deleted.

‎.github/workflows/danger.yml‎

Lines changed: 0 additions & 13 deletions
This file was deleted.

‎.rubocop.yml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
inherit_mode:
2+
merge:
3+
- Include
4+
15
AllCops:
26
NewCops: enable
37
TargetRubyVersion: 3.3
48
SuggestExtensions: false
9+
# Hidden directories are skipped unless included: lint the CI scripts too.
10+
Include:
11+
- .github/**/*.rb
512
Exclude:
613
- vendor/**/*
714
- bin/**/*

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* [#3050](https://github.com/ruby-grape/grape/pull/3050): Remove the unused `ParamsScope#root?` and cover the placeholder skip in `validate_elements!` - [@ericproulx](https://github.com/ericproulx).
1717
* [#3051](https://github.com/ruby-grape/grape/pull/3051): Look up the validation error format once per response instead of once per message - [@ericproulx](https://github.com/ericproulx).
1818
* [#3055](https://github.com/ruby-grape/grape/pull/3055): Stop a 405 behind `Rack::Cascade` from making the next app answer 405 too - [@ericproulx](https://github.com/ericproulx).
19+
* [#3057](https://github.com/ruby-grape/grape/pull/3057): Replace Danger with a dependency-free CHANGELOG check - [@ericproulx](https://github.com/ericproulx).
1920
* Your contribution here.
2021

2122
### 4.1.0 (2026-10-31)

‎Dangerfile‎

Lines changed: 0 additions & 5 deletions
This file was deleted.

‎Gemfile‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@ group :development do
2121
end
2222

2323
group :test do
24-
gem 'danger', require: false
25-
gem 'danger-changelog', require: false
26-
gem 'danger-pr-comment', require: false
27-
# danger 9.6.1 fails to load with git 6: https://github.com/danger/danger/issues/1560
28-
gem 'git', '< 6', require: false
2924
gem 'rack-contrib', require: false
3025
gem 'rack-test', '~> 2.1'
26+
# ActiveSupport::XmlMini, Grape's XML backend without multi_xml, parses with
27+
# REXML, a bundled gem rather than a default one.
28+
gem 'rexml', require: false
3129
gem 'rspec', '~> 3.13'
3230
gem 'simplecov', '~> 1.1', require: false
3331
end

0 commit comments

Comments
 (0)