Class: Git::Pr::Release::CLI
- Inherits:
-
Object
- Object
- Git::Pr::Release::CLI
show all
- Includes:
- Util
- Defined in:
- lib/git/pr/release/cli.rb
Constant Summary
Constants included
from Util
Util::DEFAULT_PR_TEMPLATE
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Util
#build_pr_title_and_body, #dump_result_as_json, #git, #git_config, #git_config_set, #host_and_repository_and_scheme, #merge_pr_body, #obtain_token!, #request_authorization, #say
Constructor Details
#initialize ⇒ CLI
Returns a new instance of CLI.
16
17
18
19
20
21
|
# File 'lib/git/pr/release/cli.rb', line 16
def initialize
@dry_run = false
@json = false
@no_fetch = false
@squashed = false
end
|
Instance Attribute Details
#labels ⇒ Object
Returns the value of attribute labels.
9
10
11
|
# File 'lib/git/pr/release/cli.rb', line 9
def labels
@labels
end
|
#production_branch ⇒ Object
Returns the value of attribute production_branch.
9
10
11
|
# File 'lib/git/pr/release/cli.rb', line 9
def production_branch
@production_branch
end
|
#repository ⇒ Object
Returns the value of attribute repository.
9
10
11
|
# File 'lib/git/pr/release/cli.rb', line 9
def repository
@repository
end
|
#staging_branch ⇒ Object
Returns the value of attribute staging_branch.
9
10
11
|
# File 'lib/git/pr/release/cli.rb', line 9
def staging_branch
@staging_branch
end
|
#template_path ⇒ Object
Returns the value of attribute template_path.
9
10
11
|
# File 'lib/git/pr/release/cli.rb', line 9
def template_path
@template_path
end
|
Class Method Details
.start ⇒ Object
11
12
13
14
|
# File 'lib/git/pr/release/cli.rb', line 11
def self.start
result = self.new.start
exit result
end
|
Instance Method Details
#build_and_merge_pr_title_and_body(release_pr, merged_prs, changed_files) ⇒ Object
235
236
237
238
239
240
241
|
# File 'lib/git/pr/release/cli.rb', line 235
def build_and_merge_pr_title_and_body(release_pr, merged_prs, changed_files)
old_body = (release_pr && release_pr.body != nil) ? release_pr.body : ""
pr_title, new_body = build_pr_title_and_body(release_pr, merged_prs, changed_files, template_path)
[pr_title, merge_pr_body(old_body, new_body)]
end
|
#client ⇒ Object
59
60
61
|
# File 'lib/git/pr/release/cli.rb', line 59
def client
@client ||= Octokit::Client.new :access_token => obtain_token!
end
|
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
|
# File 'lib/git/pr/release/cli.rb', line 63
def configure
host, @repository, scheme = host_and_repository_and_scheme
if host
if scheme == 'https' ssl_no_verify = %w[true 1].include? ENV.fetch('GIT_PR_RELEASE_SSL_NO_VERIFY') { git_config('ssl-no-verify') }
if ssl_no_verify
OpenSSL::SSL.const_set :VERIFY_PEER, OpenSSL::SSL::VERIFY_NONE
end
end
Octokit.configure do |c|
c.api_endpoint = "#{scheme}://#{host}/api/v3"
c.web_endpoint = "#{scheme}://#{host}/"
end
end
@production_branch = ENV.fetch('GIT_PR_RELEASE_BRANCH_PRODUCTION') { git_config('branch.production') } || 'master'
@staging_branch = ENV.fetch('GIT_PR_RELEASE_BRANCH_STAGING') { git_config('branch.staging') } || 'staging'
@template_path = ENV.fetch('GIT_PR_RELEASE_TEMPLATE') { git_config('template') }
_labels = ENV.fetch('GIT_PR_RELEASE_LABELS') { git_config('labels') }
@labels = _labels && _labels.split(/\s*,\s*/) || []
say "Repository: #{repository}", :debug
say "Production branch: #{production_branch}", :debug
say "Staging branch: #{staging_branch}", :debug
say "Template path: #{template_path}", :debug
say "Labels #{labels}", :debug
end
|
#create_release_pr(merged_prs) ⇒ Object
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
|
# File 'lib/git/pr/release/cli.rb', line 186
def create_release_pr(merged_prs)
found_release_pr = detect_existing_release_pr
create_mode = found_release_pr.nil?
if create_mode
if @dry_run
release_pr = nil
changed_files = []
else
release_pr = prepare_release_pr
changed_files = pull_request_files(release_pr)
end
else
release_pr = found_release_pr
changed_files = pull_request_files(release_pr)
end
pr_title, pr_body = if @overwrite_description
build_pr_title_and_body(release_pr, merged_prs, changed_files, template_path)
else
build_and_merge_pr_title_and_body(release_pr, merged_prs, changed_files)
end
if @dry_run
say 'Dry-run. Not updating PR', :info
say pr_title, :notice
say pr_body, :notice
dump_result_as_json( release_pr, merged_prs, changed_files ) if @json
return
end
update_release_pr(release_pr, pr_title, pr_body)
say "#{create_mode ? 'Created' : 'Updated'} pull request: #{release_pr.rels[:html].href}", :notice
dump_result_as_json( release_pr, merged_prs, changed_files ) if @json
end
|
#detect_existing_release_pr ⇒ Object
223
224
225
226
227
|
# File 'lib/git/pr/release/cli.rb', line 223
def detect_existing_release_pr
say 'Searching for existing release pull requests...', :info
user=repository.split("/")[0]
client.pull_requests(repository, head: "#{user}:#{staging_branch}", base: production_branch).first
end
|
#fetch_merged_pr_numbers_from_git_remote ⇒ Object
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
|
# File 'lib/git/pr/release/cli.rb', line 115
def fetch_merged_pr_numbers_from_git_remote
merged_feature_head_sha1s = git(:log, '--merges', '--pretty=format:%P', "origin/#{production_branch}..origin/#{staging_branch}").map do |line|
main_sha1, feature_sha1 = line.chomp.split /\s+/
feature_sha1
end
git('ls-remote', 'origin', 'refs/pull/*/head').map do |line|
sha1, ref = line.chomp.split /\s+/
if merged_feature_head_sha1s.include? sha1
if %r<^refs/pull/(\d+)/head$>.match ref
pr_number = $1.to_i
if git('merge-base', sha1, "origin/#{production_branch}").first.chomp == sha1
say "##{pr_number} (#{sha1}) is already merged into #{production_branch}", :debug
else
pr_number
end
else
say "Bad pull request head ref format: #{ref}", :warn
nil
end
end
end.compact
end
|
#fetch_merged_prs ⇒ Object
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/git/pr/release/cli.rb', line 94
def fetch_merged_prs
bool = git(:'rev-parse', '--is-shallow-repository').first.chomp
if bool == 'true'
git(:fetch, '--unshallow')
end
git :remote, 'update', 'origin' unless @no_fetch
merged_pull_request_numbers = fetch_merged_pr_numbers_from_git_remote
if @squashed
merged_pull_request_numbers.concat(fetch_squash_merged_pr_numbers_from_github)
end
merged_prs = merged_pull_request_numbers.uniq.sort.map do |nr|
pr = client.pull_request repository, nr
say "To be released: ##{pr.number} #{pr.title}", :notice
pr
end
merged_prs
end
|
#fetch_squash_merged_pr_numbers_from_github ⇒ Object
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
|
# File 'lib/git/pr/release/cli.rb', line 150
def fetch_squash_merged_pr_numbers_from_github
shas = git(:log, '--pretty=format:%h', "--abbrev=7", "--no-merges", "--first-parent",
"origin/#{production_branch}..origin/#{staging_branch}").map(&:chomp)
pr_nums = []
query_base = "repo:#{repository} is:pr is:closed"
query = query_base
shas.each do |sha|
if query.length + 1 + sha.length >= 256
pr_nums.concat(search_issue_numbers(query))
query = query_base
end
query += " " + sha
end
if query != query_base
pr_nums.concat(search_issue_numbers(query))
end
pr_nums
end
|
#prepare_release_pr ⇒ Object
229
230
231
232
233
|
# File 'lib/git/pr/release/cli.rb', line 229
def prepare_release_pr
client.create_pull_request(
repository, production_branch, staging_branch, 'Preparing release pull request...', ''
)
end
|
#pull_request_files(pull_request) ⇒ Object
Fetch PR files of specified pull_request
259
260
261
262
263
264
265
266
267
|
# File 'lib/git/pr/release/cli.rb', line 259
def pull_request_files(pull_request)
return [] if pull_request.nil?
client.auto_paginate = true
files = client.pull_request_files repository, pull_request.number
client.auto_paginate = false
return files
end
|
#search_issue_numbers(query) ⇒ Object
141
142
143
144
145
146
147
148
|
# File 'lib/git/pr/release/cli.rb', line 141
def search_issue_numbers(query)
sleep 1
say "search issues with query:#{query}", :debug
client.search_issues("#{query}")[:items].map(&:number)
end
|
#start ⇒ Object
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
|
# File 'lib/git/pr/release/cli.rb', line 25
def start
OptionParser.new do |opts|
opts.on('-n', '--dry-run', 'Do not create/update a PR. Just prints out') do |v|
@dry_run = v
end
opts.on('--json', 'Show data of target PRs in JSON format') do |v|
@json = v
end
opts.on('--no-fetch', 'Do not fetch from remote repo before determining target PRs (CI friendly)') do |v|
@no_fetch = v
end
opts.on('--squashed', 'Handle squash merged PRs') do |v|
@squashed = v
end
opts.on('--overwrite-description', 'Force overwrite PR description') do |v|
@overwrite_description = v
end
end.parse!
configure
merged_prs = fetch_merged_prs
if merged_prs.empty?
say 'No pull requests to be released', :error
return 1
end
create_release_pr(merged_prs)
return 0
end
|
#update_release_pr(release_pr, pr_title, pr_body) ⇒ Object
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
# File 'lib/git/pr/release/cli.rb', line 243
def update_release_pr(release_pr, pr_title, pr_body)
say 'Pull request body:', :debug
say pr_body, :debug
client.update_pull_request(
repository, release_pr.number, :title => pr_title, :body => pr_body
)
unless labels.empty?
client.add_labels_to_an_issue(
repository, release_pr.number, labels
)
end
end
|