1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
# frozen_string_literal: true
require_relative "../lib/bundler/gem_tasks"
task :build => ["build_metadata"] do
Rake::Task["build_metadata:clean"].tap(&:reenable).real_invoke
end
task "release:rubygem_push" => ["release:verify_docs", "release:verify_files", "release:verify_github", "build_metadata", "release:github"]
namespace :release do
task :verify_docs => :"man:check"
task :verify_files do
git_list = IO.popen("git ls-files -z", &:read).split("\x0").select {|f| f.match(%r{^(lib|man|exe)/}) }
git_list += %w[CHANGELOG.md LICENSE.md README.md bundler.gemspec]
gem_list = Gem::Specification.load("bundler.gemspec").files
extra_files = gem_list.to_set - git_list.to_set
error_msg = <<~MSG
You intend to ship some files with the gem that are not generated man pages
nor source control files. Please review the extra list of files and try
again:
#{extra_files.to_a.join("\n ")}
MSG
raise error_msg if extra_files.any?
puts "The file list is correct for a release."
end
def gh_api_post(opts)
gem "netrc", "~> 0.11.0"
require "netrc"
require "net/http"
require "json"
_username, token = Netrc.read["api.github.com"]
host = opts.fetch(:host) { "https://api.github.com/" }
path = opts.fetch(:path)
uri = URI.join(host, path)
uri.query = [uri.query, "access_token=#{token}"].compact.join("&")
headers = {
"Content-Type" => "application/json",
"Accept" => "application/vnd.github.v3+json",
"Authorization" => "token #{token}",
}.merge(opts.fetch(:headers, {}))
body = opts.fetch(:body) { nil }
response = if body
Net::HTTP.post(uri, body.to_json, headers)
else
Net::HTTP.get_response(uri)
end
if response.code.to_i >= 400
raise "#{uri}\n#{response.inspect}\n#{begin
JSON.parse(response.body)
rescue JSON::ParseError
response.body
end}"
end
JSON.parse(response.body)
end
task :verify_github do
require "pp"
gh_api_post :path => "/user"
end
def confirm(prompt = "")
loop do
print(prompt)
print(": ") unless prompt.empty?
answer = $stdin.gets.strip
break if answer == "y"
abort if answer == "n"
end
rescue Interrupt
abort
end
def gh_api_request(opts)
require "net/http"
require "json"
host = opts.fetch(:host) { "https://api.github.com/" }
path = opts.fetch(:path)
response = Net::HTTP.get_response(URI.join(host, path))
links = Hash[*(response["Link"] || "").split(", ").map do |link|
href, name = link.match(/<(.*?)>; rel="(\w+)"/).captures
[name.to_sym, href]
end.flatten]
parsed_response = JSON.parse(response.body)
if n = links[:next]
parsed_response.concat gh_api_request(:host => host, :path => n)
end
parsed_response
end
def release_notes(version)
title_token = "## "
current_version_title = "#{title_token}#{version}"
current_minor_title = "#{title_token}#{version.segments[0, 2].join(".")}"
text = File.open("CHANGELOG.md", "r:UTF-8", &:read)
lines = text.split("\n")
current_version_index = lines.find_index {|line| line.strip =~ /^#{current_version_title}($|\b)/ }
unless current_version_index
raise "Update the changelog for the last version (#{version})"
end
current_version_index += 1
previous_version_lines = lines[current_version_index.succ...-1]
previous_version_index = current_version_index + (
previous_version_lines.find_index {|line| line.start_with?(title_token) && !line.start_with?(current_minor_title) } ||
lines.count
)
relevant = lines[current_version_index..previous_version_index]
relevant.join("\n").strip
end
desc "Push the release to Github releases"
task :github, :version do |_t, args|
version = Gem::Version.new(args.version || bundler_spec.version)
tag = "v#{version}"
gh_api_post :path => "/repos/bundler/bundler/releases",
:body => {
:tag_name => tag,
:name => tag,
:body => release_notes(version),
:prerelease => version.prerelease?,
}
end
desc "Prepare a patch release with the PRs from master in the patch milestone"
task :prepare_patch, :version do |_t, args|
version = args.version
version ||= begin
version = bundler_spec.version
segments = version.segments
if segments.last.is_a?(String)
segments << "1"
else
segments[-1] += 1
end
segments.join(".")
end
puts "Cherry-picking PRs milestoned for #{version} (currently #{bundler_spec.version}) into the stable branch..."
milestones = gh_api_request(:path => "repos/bundler/bundler/milestones?state=open")
unless patch_milestone = milestones.find {|m| m["title"] == version }
abort "failed to find #{version} milestone on GitHub"
end
prs = gh_api_request(:path => "repos/bundler/bundler/issues?milestone=#{patch_milestone["number"]}&state=all")
prs.map! do |pr|
abort "#{pr["html_url"]} hasn't been closed yet!" unless pr["state"] == "closed"
next unless pr["pull_request"]
pr["number"].to_s
end
prs.compact!
branch = version.split(".", 3)[0, 2].push("stable").join("-")
sh("git", "checkout", "-b", "release/#{version}", branch)
commits = `git log --oneline origin/master --`.split("\n").map {|l| l.split(/\s/, 2) }.reverse
commits.select! {|_sha, message| message =~ /(Auto merge of|Merge pull request|Merge) ##{Regexp.union(*prs)}/ }
abort "Could not find commits for all PRs" unless commits.size == prs.size
if commits.any? && !system("git", "cherry-pick", "-x", "-m", "1", *commits.map(&:first))
warn "Opening a new shell to fix the cherry-pick errors. Press Ctrl-D when done to resume the task"
unless system(ENV["SHELL"] || "zsh")
abort "Failed to resolve conflicts on a different shell. Resolve conflicts manually and finish the task manually"
end
end
version_file = "lib/bundler/version.rb"
version_contents = File.read(version_file)
unless version_contents.sub!(/^(\s*VERSION = )"#{Gem::Version::VERSION_PATTERN}"/, "\\1#{version.to_s.dump}")
abort "failed to update #{version_file}, is it in the expected format?"
end
File.open(version_file, "w") {|f| f.write(version_contents) }
sh("git", "commit", "-am", "Version #{version}")
end
desc "Open all PRs that have not been included in a stable release"
task :open_unreleased_prs do
def prs(on = "master")
commits = `git log --oneline origin/#{on} --`.split("\n")
commits.reverse_each.map {|c| c =~ /(Auto merge of|Merge pull request|Merge) #(\d+)/ && $2 }.compact
end
def minor_release_tags
`git ls-remote origin`.split("\n").map {|r| r =~ %r{refs/tags/v([\d.]+)$} && $1 }.compact.map {|v| Gem::Version.create(Gem::Version.create(v).segments[0, 2].join(".")) }.sort.uniq
end
def to_stable_branch(release_tag)
release_tag.segments[0, 2].<<("stable").join("-")
end
last_stable = to_stable_branch(minor_release_tags[-1])
previous_to_last_stable = to_stable_branch(minor_release_tags[-2])
in_release = prs("HEAD") - prs(last_stable) - prs(previous_to_last_stable)
print "About to review #{in_release.size} pending PRs. "
confirm "Continue? (y/n)"
in_release.each do |pr|
url_opener = /darwin/ =~ RUBY_PLATFORM ? "open" : "xdg-open"
url = "https://github.com/bundler/bundler/pull/#{pr}"
print "#{url}. (n)ext/(o)pen? "
system(url_opener, url, :out => IO::NULL, :err => IO::NULL) if $stdin.gets.strip == "o"
end
end
end
|