Skip to content

Commit b6ba771

Browse files
authored
chore: Better error and edge case checking when modifying version and changelog (#374)
* chore: Better error and edge case checking when modifying version and changelog * chore: Support forced release of all gems * chore: Update documentation to cover --gems=all for release requests
1 parent b3198f1 commit b6ba771

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

.toys/release/.lib/release_requester.rb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,17 +225,27 @@ def modify_version_file
225225
path = @utils.gem_version_rb_path(@gem_name, from: :context)
226226
@utils.log("Modifying version file #{path}")
227227
content = ::File.read(path)
228-
content.sub!(/ VERSION = "\d+\.\d+\.\d+(?:\.\w+)*"/,
229-
" VERSION = \"#{@new_version}\"")
228+
original_content = content.dup
229+
changed = content.sub!(/ VERSION\s*=\s*(["'])\d+\.\d+\.\d+(?:\.\w+)*["']/,
230+
" VERSION = \\1#{@new_version}\\1")
231+
@utils.error("Could not find VERSION constant for #{@gem_name} in #{path}") unless changed
232+
if content == original_content
233+
@utils.warning("Version constant for #{@gem_name} is already #{@new_version}.")
234+
return
235+
end
230236
::File.open(path, "w") { |file| file.write(content) }
231237
end
232238

233239
def modify_changelog_file
234240
path = @utils.gem_changelog_path(@gem_name, from: :context)
235241
@utils.log("Modifying changelog file #{path}")
236242
content = ::File.read(path)
237-
changed = content.sub!(%r{\n### (v\d+\.\d+\.\d+ / \d\d\d\d-\d\d-\d\d)},
238-
"\n#{@full_changelog}\n\n### \\1")
243+
if content =~ %r{\n### v#{@new_version} / \d\d\d\d-\d\d-\d\d\n}
244+
@utils.warning("Changelog entry for #{@gem_name} #{@new_version} already seems to exist.")
245+
return
246+
end
247+
changed = content.sub!(%r{\n### (v\d+\.\d+\.\d+ / \d\d\d\d-\d\d-\d\d)\n},
248+
"\n#{@full_changelog}\n\n### \\1\n")
239249
unless changed
240250
content << "\n" until content.end_with?("\n\n")
241251
content << @full_changelog << "\n"
@@ -287,6 +297,9 @@ def initial_setup
287297

288298
def gem_info(gem_name, override_version: nil)
289299
raise "Gem list is already finished" if @gem_info_list.frozen?
300+
if @gem_info_list.any? { |gem_info| gem_info.gem_name == gem_name }
301+
@utils.error("Gem #{gem_name} listed multiple times in release request")
302+
end
290303
initial_setup unless @performed_initial_setup
291304
info = GemInfo.new(@utils, gem_name, override_version, @release_ref)
292305
@gem_info_list << info
@@ -346,6 +359,9 @@ def determine_commit_info
346359
end
347360

348361
def create_release_commit
362+
if @utils.capture(["git", "diff"]).strip.empty?
363+
@utils.error("No changes to make. Are you sure the version to release is correct?")
364+
end
349365
check_branch_cmd = ["git", "rev-parse", "--verify", "--quiet", @release_branch_name]
350366
if @utils.exec(check_branch_cmd, e: false).success?
351367
@utils.exec(["git", "branch", "-D", @release_branch_name])

.toys/release/create-labels.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,15 @@ def update_label(label)
9393
label_name = label["name"]
9494
return unless yes || confirm("Update fields of \"#{label_name}\"? ", default: true)
9595
body = ::JSON.dump(color: label["color"], description: label["description"])
96-
exec(["gh", "api", "-XPATCH", ::CGI.escape("repos/#{@utils.repo_path}/labels/#{label_name}"),
96+
exec(["gh", "api", "-XPATCH", "repos/#{@utils.repo_path}/labels/#{label_name}",
9797
"--input", "-", "-H", "Accept: application/vnd.github.v3+json"],
9898
in: [:string, body], out: :null)
9999
end
100100

101101
def delete_label(label)
102102
label_name = label["name"]
103103
return unless yes || confirm("Label \"#{label_name}\" unrecognized. Delete? ", default: true)
104-
exec(["gh", "api", "-XDELETE", ::CGI.escape("repos/#{@utils.repo_path}/labels/#{label_name}"),
104+
exec(["gh", "api", "-XDELETE", "repos/#{@utils.repo_path}/labels/#{label_name}",
105105
"-H", "Accept: application/vnd.github.v3+json"],
106106
out: :null)
107107
end

.toys/release/request.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@
4343
"If no version is specified for a gem, a version is inferred from the" \
4444
" conventional commit messages. If this flag is omitted or left blank," \
4545
" all gems in the repository that have at least one commit of type" \
46-
" 'fix', 'feat', or 'docs', or a breaking change, will be released."
46+
" 'fix', 'feat', or 'docs', or a breaking change, will be released.",
47+
"",
48+
"You can also use the special gem name 'all' which forces release of all" \
49+
" gems in the repository regardless of whether they have significant" \
50+
" changes. You can also supply a version with 'all' to release all gems" \
51+
" with the same version."
4752
end
4853
flag :git_remote, "--git-remote=VAL" do
4954
default "origin"
@@ -109,7 +114,13 @@ def populate_requester
109114
gem_list = gems.to_s.empty? ? @utils.all_gems : gems.split(/[\s,]+/)
110115
gem_list.each do |entry|
111116
gem_name, override_version = entry.split(":", 2)
112-
requester.gem_info(gem_name, override_version: override_version)
117+
if gem_name == "all"
118+
@utils.all_gems.each do |name|
119+
requester.gem_info(name, override_version: override_version)
120+
end
121+
else
122+
requester.gem_info(gem_name, override_version: override_version)
123+
end
113124
end
114125
requester
115126
end

CONTRIBUTING.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,9 @@ Releases are normally performed using GitHub Actions.
229229
by including their names, space-delimited, in this this field. You can
230230
optionally append `:<version>` to any gem in the list to specify the
231231
version to release, or omit the version to let the script decide based
232-
on conventional commits.
232+
on conventional commits. You can also use the special name `all` to
233+
force release of all gems (and even `all:<version>` to release all gems
234+
with the same version.)
233235
3. The workflow will analyze the conventional commit messages for the gems to
234236
release, and will open a _release pull request_. This pull request will
235237
include the appropriate changes to each gem's version constants, and an

0 commit comments

Comments
 (0)