forked from eWAYPayment/eway-rapid-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelease.rb
More file actions
283 lines (222 loc) · 7.02 KB
/
release.rb
File metadata and controls
283 lines (222 loc) · 7.02 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# usage:
# release.rb -b develop -v 10.6.2
require 'optparse'
require 'open3'
require 'colorize'
require 'octokit'
require 'netrc'
require 'byebug'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: release.rb [options]"
opts.on("-b", "--branch BRANCH", "The branch to build the release from") do |value|
options[:branch] = value
end
opts.on("-v", "--version VERSION", "The new version number") do |value|
options[:version] = value
end
opts.on("-h", "--help", "Prints this help") do
puts opts
exit
end
end.parse!
missing = []
[:branch, :version].each do |opt|
missing << opt if options[opt].nil?
end
if missing.any?
puts "\nThe following required arguments are missing:"
missing.each {|opt| puts " * #{opt}" }
puts "\nUse --help for more details"
puts ""
exit
end
class Release
attr_reader :branch, :version, :version_number
GEM_NAME = "eway_rapid"
VERSION_FILE = "lib/#{GEM_NAME}/version.rb"
REPO = "GetStoreConnect/eway-rapid-ruby"
GEM_FOLDER = "~/#{GEM_NAME}-gems/"
VERSION_STEPS = %w(major minor patch)
def initialize(options)
@branch = options[:branch]
version = options[:version]
@version = VERSION_STEPS.include?(version) ? calculate_version(version) : version
@version_number = "v#{@version}"
@step = 0
end
def release
checkout_source_branch_and_pull
update_version_string
create_release_branch
generate_release_notes
bundle_and_commit
build_gem
if confirm_release
puts "Finalising release...".light_green
complete_release!
else
puts "🚫 Release aborted 🚫".light_red
end
end
private
## steps
def checkout_source_branch_and_pull
step "Checkout branch and pull".light_yellow
run("git checkout #{branch}")
verify_cmd("branch is #{branch}", "git branch --show-current", branch)
run("git pull")
end
def update_version_string
step "Update the version".light_yellow
check("branch is #{branch}", "git branch --show-current", branch)
lines = File.readlines(VERSION_FILE)
line = lines.index {|line| line.start_with?(" VERSION = ") }
lines[line] = %Q( VERSION = "#{version}"\n)
File.open(VERSION_FILE, "w") { |f| f.write(lines.join) }
verify_cmd("version is set to #{version}", "git diff #{VERSION_FILE}", /\+ VERSION = "#{version}"/)
end
def create_release_branch
step "Create the release branch".light_yellow
run("git checkout -b #{release_branch}")
verify_cmd("branch is #{release_branch}", "git branch --show-current", release_branch)
end
def generate_release_notes
step "Generate the release notes".light_yellow
check("branch is #{release_branch}", "git branch --show-current", release_branch)
stop("No commits found") if commits.empty?
changelog = decorate_commits(commits)
dependabot, updates = changelog.partition {|item| item.include?("dependabot")}
release_notes = <<~NOTES
@channel
*Gem #{gem_filename} released*
Branch: *#{branch}*
Updates:
#{updates.sort.join("\n")}
Dependabot:
#{dependabot.reverse.map{|item| item.chomp(" (@dependabot[bot])")}.join("\n")}
NOTES
File.open(".release_notes", "w") {|f| f.write(release_notes)}
run("open .release_notes", quiet: true)
end
def bundle_and_commit
step "Bundle and commit".light_yellow
check("branch is #{release_branch}", "git branch --show-current", release_branch)
run("bundle install")
run(%Q(git commit README_RELEASE.md Gemfile.lock .gitignore #{VERSION_FILE} README_RELEASE.md -m "Updating Gemfile.lock to #{version_number}"))
end
def build_gem
step "Build the gem".light_yellow
check("branch is #{release_branch}", "git branch --show-current", release_branch)
run("gem build #{GEM_NAME}.gemspec")
run("mv #{gem_filename} #{GEM_FOLDER}")
run("open #{GEM_FOLDER}")
end
def confirm_release
puts "Please check everything over and then type 'yes' to confirm this release".light_blue
gets.chomp == "yes"
end
def complete_release!
push_gem
merge_and_push
end
## utility methods
def gem_filename
"#{GEM_NAME}-#{version}.gem"
end
def push_gem
step "Push gem to Gemfury".light_yellow
api_token = run("cat ~/.fury/api-token")
stop("No Gemfury api token found") if api_token.nil?
filepath = "#{GEM_FOLDER}#{gem_filename}"
run("fury push #{filepath} --api-token=#{api_token}")
end
def merge_and_push
step "Merge release branch and push upstream".light_yellow
check("branch is #{release_branch}", "git branch --show-current", release_branch)
run %Q(git tag -a #{version_number} -m "Tagging #{version_number} release")
run %Q(git push origin release/#{version_number}:release/#{version_number} --tags)
run %Q(git checkout #{branch})
run %Q(git merge release/#{version_number})
run %Q(git push origin #{branch}:#{branch})
if branch == "develop" && confirm_master_push
run %Q(git checkout master)
run %Q(git merge release/#{version_number})
run %Q(git push origin master:master)
run %Q(git checkout develop)
end
end
def confirm_master_push
puts "Please type 'master' to confirm".light_blue
gets.chomp == "master"
end
def calculate_version(version_step)
current_version = File.read(VERSION_FILE).scan(/\s*VERSION\s*=\s*"([0-9\.]+)"/).flatten.first
points = current_version.split('.').map(&:to_i)
case version_step.downcase
when "major"
points[0] += 1
when "minor"
points[1] += 1
when "patch"
points[2] += 1
end
points.join('.')
end
def release_branch
"release/#{version_number}"
end
def compatible_version
version.split(".")[0..1].join(".")
end
def commits
@commits ||= begin
last_tag = run(%Q(git describe --tags --abbrev=0 @^), quiet: true).chomp
run(%Q(git log --pretty=format:"%s (@%aN)" #{last_tag}..@), quiet: true).split("\n")
end
end
def decorate_commits(commits)
client = Octokit::Client.new(:netrc => true)
commits.map do |commit|
if match = commit.match(/^(.+?)(\(#(\d+)\))(.+)$/)
pull_no = match[3]
title = client.pull_request(REPO, pull_no)&.title
"- #{title} #{match[2]}#{match[4]}"
end
end.compact
end
def run(cmd, quiet: false)
puts "> #{cmd.blue}" unless quiet
stdout, stderr, status = Open3.capture3(cmd)
case status.exitstatus
when 0
stdout
else
puts stderr.red
stop("command failed: #{cmd}")
end
end
def verify_cmd(situation, cmd, output, quiet: false)
stdout, stderr, status = Open3.capture3(cmd)
verify(situation, stdout.match?(output), quiet: quiet)
end
def verify(situation, passed, quiet: false)
if passed
puts "Verfied: #{situation}".light_magenta unless quiet
else
stop "Failed to verify: #{situation}"
end
end
def check(*args)
verify_cmd(*args, quiet: true)
end
def stop(msg)
puts msg.yellow
exit
end
def step(msg)
@step += 1
puts "Step #{@step}: #{msg}".light_yellow
end
end
Release.new(options).release