-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathprocedure
executable file
·163 lines (145 loc) · 5.39 KB
/
procedure
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
#!/usr/bin/env ruby
require 'date'
require 'erb'
require 'optparse'
require 'optparse/date'
require 'ostruct'
require 'pathname'
class Context < OpenStruct
def rel_eng_script(name)
"[#{name}](https://github.com/theforeman/theforeman-rel-eng/blob/master/#{name})"
end
end
def header(filename)
git_commit = `git rev-parse HEAD`.strip
git_filename = Pathname.new(filename).relative_path_from(`git rev-parse --show-toplevel`.strip)
<<~HEADER
This procedure was generated from [#{git_filename} at #{git_commit}](https://github.com/theforeman/theforeman-rel-eng/blob/#{git_commit}/#{git_filename})
[ ] Make this post a wiki ([help](https://meta.discourse.org/t/create-a-wiki-post/30802))
HEADER
end
def render_file(filename, context)
erb = ERB.new(File.read(filename), trim_mode: '-')
erb.filename = File.expand_path(filename)
header(filename) + erb.result(Context.new(context).instance_eval { binding })
end
def render(project, procedure, context)
filename = procedure_filename(project, procedure)
puts render_file(filename, context)
end
def procedure_filename(project, procedure)
File.join(__dir__, 'procedures', project, "#{procedure}.md.erb")
end
def bump_last(version)
parts = version.split('.')
(parts[0..-2] + [(parts.last.to_i + 1).to_s]).join('.')
end
def previous_release(version)
parts = version.split('.')
(parts[0..-2] + [(parts.last.to_i - 1).to_s]).join('.')
end
def discourse_username(name)
name.start_with?('@') ? name : "@#{name}"
end
context = {}
global = OptionParser.new do |opts|
opts.banner = "Usage: #{opts.program_name} [branch|release]"
end
procedures = {
'branch' => OptionParser.new do |opts|
opts.banner = "Usage: #{opts.program_name} branch --project [foreman|katello|candlepin] --release [X.Y] --target-date [YYYY-MM-DD] --owner [OWNER] [--engineer [ENGINEER]]"
opts.on('--project [PROJECT]', ['foreman', 'katello', 'candlepin'],
'Project to generate branch procedure for') do |t|
context[:project] = t
end
opts.on('--release [RELEASE]', /^\d+\.\d+$/,
'Version currently in nightly in the major.minor format, like 3.7') do |release|
context[:release] = release
context[:develop] = bump_last(release)
end
opts.on('--target-date [YYYY-MM-DD]', Date,
'Target date that the procedure should be completed on') do |date|
context[:target_date] = date
context[:four_weeks_before] = date - 28
context[:three_weeks_before] = date - 21
context[:two_weeks_before] = date - 14
context[:one_week_before] = date - 7
end
opts.on('--owner [OWNER]', "The release owner's username on Discourse") do |t|
owner = discourse_username(t)
context[:owner] = owner
context[:engineer] ||= owner
end
opts.on('--engineer [ENGINEER]', "The release engineer's username on Discourse") do |t|
context[:engineer] = discourse_username(t)
end
end,
'release' => OptionParser.new do |opts|
opts.on('--project [PROJECT]', ['foreman', 'katello'],
'Project to generate release procedure for') do |project|
context[:project] = project
end
opts.on('--version [FULL_VERSION]',
/^(?<version>(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+))([-.](?<extra>rc\d+))?$/,
'Version that the branch will have - like 1.20.0-rc1') do |full_version, version, major, minor, patch, extra|
debian_full_version = version + (extra ? "~#{extra.downcase}" : '') + '-1'
is_rc = !extra.nil?
context.merge!(
is_rc: is_rc,
is_first_rc: extra == 'rc1',
is_first_ga: patch == '0' && !is_rc,
extra: extra,
short_version: "#{major}.#{minor}", # 1.20
debian_full_version: debian_full_version, # 1.20.0~rc1-1
full_version: full_version, # 1.20.0-rc1
version: version, # 1.20.0
short_version_minus_two: "#{major}.#{minor.to_i - 2}",
short_version_minus_five: "#{major}.#{minor.to_i - 5}",
next_patch_version: "#{major}.#{minor}.#{patch.to_i + 1}",
)
end
opts.on('--foreman-version [X.Y]', /^\d+\.\d+$/, 'Foreman version, for Katello') do |foreman_version|
context[:foreman_version] = foreman_version
end
opts.on('--target-date [YYYY-MM-DD]', Date,
'Target date that the procedure should be completed on') do |date|
context[:target_date] = date
context[:two_weeks_before] = date - 14
context[:one_week_before] = date - 7
end
opts.on('--owner [OWNER]', "The release owner's username on Discourse") do |t|
owner = discourse_username(t)
context[:owner] = owner
context[:engineer] ||= owner
end
opts.on('--engineer [ENGINEER]', "The release engineer's username on Discourse") do |t|
context[:engineer] = discourse_username(t)
end
end
}
def missing_required_option?(procedure, context)
case context[:project]
when 'candlepin'
context[:release].nil?
when 'katello'
context[:target_date].nil? || context[:owner].nil? || (procedure == 'release' && context[:foreman_version].nil?)
when 'foreman'
context[:target_date].nil? || context[:owner].nil?
else
true
end
end
global.order!
procedure = ARGV.shift
if procedures.key?(procedure)
command = procedures[procedure]
command.order!
if missing_required_option?(procedure, context)
STDERR.puts command.help
exit 1
end
render(context[:project], procedure, context)
else
STDERR.puts global.help
exit 1
end