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
|
# frozen_string_literal: true
require "bundler/gem_tasks"
task :build => ["build_metadata", "man:build", "generate_files"] do
Rake::Task["build_metadata:clean"].tap(&:reenable).real_invoke
end
task :release => ["man:require", "man:build", "release:verify_github", "build_metadata"]
namespace :release do
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?
break if $stdin.gets.strip == "y"
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
task :github, :version do |_t, args|
version = Gem::Version.new(args.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
task :release do |args|
Rake::Task["release:github"].invoke(args.version)
end
desc "Make a patch release with the PRs from master in the patch milestone"
task :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
confirm "You are about to release #{version}, currently #{bundler_spec.version}"
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!
bundler_spec.version = version
branch = version.split(".", 3)[0, 2].push("stable").join("-")
sh("git", "checkout", branch)
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) }
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) ##{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"
abort unless system("zsh")
end
prs.each do |pr|
system("open", "https://github.com/bundler/bundler/pull/#{pr}")
confirm "Add to the changelog"
end
confirm "Update changelog"
sh("git", "commit", "-am", "Version #{version} with changelog")
sh("rake", "release")
sh("git", "checkout", "master")
sh("git", "pull")
sh("git", "merge", "v#{version}", "--no-edit")
sh("git", "push")
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) #(\d+)/ && $2 }.compact
end
last_stable = `git ls-remote origin`.split("\n").map {|r| r =~ %r{refs/tags/v([\d.]+)$} && $1 }.compact.map {|v| Gem::Version.create(v) }.max
last_stable = last_stable.segments[0, 2].<<("stable").join("-")
in_release = prs("HEAD") - prs(last_stable)
in_release.each do |pr|
system("open", "https://github.com/bundler/bundler/pull/#{pr}")
confirm
end
end
end
|