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
|
# File 'lib/git/pr/release/cli.rb', line 16
def initialize
@dry_run = false
@json = false
@no_fetch = 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
164
165
166
167
168
169
170
|
# File 'lib/git/pr/release/cli.rb', line 164
def build_and_merge_pr_title_and_body(release_pr, merged_prs, changed_files)
old_body = release_pr ? 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
50
51
52
|
# File 'lib/git/pr/release/cli.rb', line 50
def client
@client ||= Octokit::Client.new :access_token => obtain_token!
end
|
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
|
# File 'lib/git/pr/release/cli.rb', line 54
def configure
host, @repository, scheme = host_and_repository_and_scheme
if host
OpenSSL::SSL.const_set :VERIFY_PEER, OpenSSL::SSL::VERIFY_NONE
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
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
|
# File 'lib/git/pr/release/cli.rb', line 119
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 = build_and_merge_pr_title_and_body(release_pr, merged_prs, changed_files)
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
152
153
154
155
156
|
# File 'lib/git/pr/release/cli.rb', line 152
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_prs ⇒ Object
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
|
# File 'lib/git/pr/release/cli.rb', line 81
def fetch_merged_prs
git :remote, 'update', 'origin' unless @no_fetch
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
merged_pull_request_numbers = 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
merged_prs = merged_pull_request_numbers.map do |nr|
pr = client.pull_request repository, nr
say "To be released: ##{pr.number} #{pr.title}", :notice
pr
end
merged_prs
end
|
#prepare_release_pr ⇒ Object
158
159
160
161
162
|
# File 'lib/git/pr/release/cli.rb', line 158
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
188
189
190
191
192
193
194
195
196
|
# File 'lib/git/pr/release/cli.rb', line 188
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
|
#start ⇒ Object
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
|
# File 'lib/git/pr/release/cli.rb', line 22
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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
# File 'lib/git/pr/release/cli.rb', line 172
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
|