This repository has been archived by the owner on Jul 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
cloner.rb
283 lines (234 loc) · 8.27 KB
/
cloner.rb
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
require 'open3'
class Cloner
GITHUB_DOMAIN = 'github.com'.freeze
DEFAULTS = {
:tmpdir => nil,
:committers => nil,
:after_sha => nil,
:default_branch => nil,
:sync_method => 'merge',
:destination_hostname => GITHUB_DOMAIN,
:destination_repo => nil,
:originating_hostname => GITHUB_DOMAIN,
:originating_repo => nil,
:git => nil
}.freeze
attr_accessor :tmpdir, :committers, :after_sha, :destination_hostname, :destination_repo
attr_accessor :originating_hostname, :originating_repo, :default_branch, :sync_method
def initialize(options)
logger.level = Logger::WARN if ENV['RACK_ENV'] == 'test'
logger.info 'New Cloner instance initialized'
DEFAULTS.each { |key, value| instance_variable_set("@#{key}", options[key] || value) }
@tmpdir ||= Dir.mktmpdir('repository-sync')
unless github_dotcom_dest?
Octokit.configure do |c|
c.api_endpoint = "https://#{destination_hostname}/api/v3/"
c.web_endpoint = "https://#{destination_hostname}"
end
end
git_init
DEFAULTS.keys { |key| logger.info " * #{key}: #{instance_variable_get("@#{key}")}" }
end
def clone
Bundler.with_clean_env do
Dir.chdir "#{tmpdir}/#{destination_repo}" do
add_remote
fetch
if default_branch.nil?
checkout
apply_sync_method
push
create_pull_request
else
apply_sync_method
submit_to_default_branch
end
delete_branch
logger.info 'fin'
end
end
rescue StandardError => e
logger.warn e
raise
ensure
FileUtils.rm_rf(tmpdir)
logger.info "Cleaning up #{tmpdir}"
end
def originating_token
ghe_token_env_var ||= "#{originating_hostname}_MACHINE_USER_TOKEN".upcase
ghe_token_found = ENV["#{ghe_token_env_var}"] || ghe_token
@originating_token ||= (github_dotcom_origin? ? dotcom_token : ghe_token_found)
end
def destination_token
ghe_token_env_var ||= "#{destination_hostname}_MACHINE_USER_TOKEN".upcase
ghe_token_found = ENV["#{ghe_token_env_var}"] || ghe_token
@destination_token ||= (github_dotcom_dest? ? dotcom_token : ghe_token_found)
end
def dotcom_token
ENV['DOTCOM_MACHINE_USER_TOKEN']
end
def ghe_token
ENV['GHE_MACHINE_USER_TOKEN']
end
def remote_name
@remote_name ||= "otherrepo-#{Time.now.to_i}"
end
def branch_name
@branch_name ||= "update-#{Time.now.to_i}"
end
def safe_destination_repo
@safe_destination_repo ||= destination_repo.tr('/-', '_').upcase
end
def commit_message
@commit_message ||= ENV["#{safe_destination_repo}_COMMIT_MESSAGE"] || 'Sync changes from upstream repository'
end
def files
@files ||= client.compare(destination_repo, 'master', branch_name)['files']
end
def url_with_token(remote = :destination)
token = (remote == :destination) ? destination_token : originating_token
hostname = (remote == :destination) ? destination_hostname : originating_hostname
repo = (remote == :destination) ? destination_repo : originating_repo
"https://#{token}:x-oauth-basic@#{hostname}/#{repo}.git"
end
def originating_url_with_token
@originating_url_with_token ||= url_with_token(:originating)
end
def destination_url_with_token
@destination_url_with_token ||= url_with_token(:destination)
end
def pull_request_title
if files.count == 1
"#{files.first['status'].capitalize} #{files.first['filename']}"
else
ENV["#{safe_destination_repo}_PR_TITLE"] || 'Sync changes from upstream repository'
end
end
def pull_request_body
return ENV["#{safe_destination_repo}_PR_BODY"] if ENV["#{safe_destination_repo}_PR_BODY"]
body = ''
%w(added removed unchanged).each do |type|
filenames = files.select { |f| f['status'] == type }.map { |f| f['filename'] }
next if filenames.empty?
body << "### #{type.capitalize} files: \n\n* #{filenames.join("\n* ")}\n\n"
end
body
end
# Plumbing methods
def logger
@logger ||= Logger.new(STDOUT)
end
def client
@client ||= Octokit::Client.new(:access_token => destination_token)
end
def git
@git ||= begin
logger.info "Cloning #{destination_repo} from #{destination_hostname}..."
Git.clone(destination_url_with_token, "#{tmpdir}/#{destination_repo}")
end
end
def run_command(*args)
logger.info "Running command #{args.join(' ')}"
output, status = Open3.capture2e(*args)
output = output.gsub(/#{dotcom_token}/, '<DOTCOM_TOKEN>') if dotcom_token
output = output.gsub(/#{ghe_token}/, '<GHE_TOKEN>') if ghe_token
logger.info "Result: #{output}"
if status != 0
report_error(output)
error = "Command `#{args.join(' ')}` failed"
error = error.gsub(/#{dotcom_token}/, '<DOTCOM_TOKEN>') if dotcom_token
error = error.gsub(/#{ghe_token}/, '<GHE_TOKEN>') if ghe_token
raise "#{error}: #{output}"
end
output
end
def report_error(command_output)
return unless command_output =~ /Merge conflict|error/i
body = "Hey, I'm really sorry about this, but there was a merge conflict when "
body << "I tried to auto-sync the last time, from #{after_sha}:\n"
body << "\n```\n"
body << command_output
body << "\n```\n"
body << "You'll have to resolve this problem manually, I'm afraid.\n"
body << "![I'm so sorry](https://media.giphy.com/media/NxKcqJI6MdIgo/giphy.gif)"
body << "\n\n /cc #{committers.join(' ')}" unless committers.nil?
client.create_issue originating_repo, 'Merge conflict detected', body
end
# Methods that perform sync actions, in order
def git_init
git.config('user.name', ENV['MACHINE_USER_NAME'])
git.config('user.email', ENV['MACHINE_USER_EMAIL'])
end
def add_remote
logger.info "Adding remote for #{originating_repo} on #{originating_hostname}..."
git.add_remote(remote_name, originating_url_with_token)
end
def fetch
logger.info "Fetching #{originating_repo}..."
git.remote(remote_name).fetch
end
def checkout
logger.info "Checking out #{branch_name}"
run_command('git', 'checkout', '-b', branch_name)
end
def apply_sync_method
if sync_method == 'squash'
squash
elsif sync_method == 'replace_contents'
replace_contents
elsif sync_method == 'merge'
merge
else
logger.warn "Invalid sync method #{sync_method}. Merging by default..."
merge
end
end
def merge
logger.info "Merging #{originating_repo}/master into #{branch_name}..."
run_command('git', 'merge', "#{remote_name}/master")
end
def squash
logger.info "Squashing #{originating_repo}/master into #{branch_name}..."
run_command('git', 'merge', '--squash', "#{remote_name}/master")
git.commit(commit_message)
end
# Using HEAD here is likely the best thing since HEAD will be a pointer to
# the head of the branch we checked out earlier and is likely more reliable
# than manually trying to use "refs/heads/#{branch_name}"
def replace_contents
logger.info "Committing contents of #{originating_repo}/master into #{branch_name} directly..."
commit_id = run_command('git', 'commit-tree', "#{remote_name}/master^{tree}", '-p', 'HEAD', '-m', commit_message)
run_command('git', 'update-ref', 'HEAD', commit_id.chomp)
end
def push
logger.info 'Pushing to origin...'
run_command('git', 'push', 'origin', branch_name)
end
def submit_to_default_branch
run_command('git', 'push', 'origin', default_branch)
end
def create_pull_request
return logger.warn 'No files have changed' if files.empty?
pr = client.create_pull_request(
destination_repo,
'master',
branch_name,
pull_request_title,
pull_request_body
)
logger.info "Pull request ##{pr[:number]} created."
sleep 2 # seems that the PR cannot be merged immediately after it's made?
client.merge_pull_request(destination_repo, pr[:number].to_i)
logger.info "Merged PR ##{pr[:number]}"
end
def delete_branch
logger.info "Deleting #{destination_repo}/#{branch_name}"
client.delete_branch(destination_repo, branch_name)
end
def github_dotcom_dest?
destination_hostname == GITHUB_DOMAIN
end
def github_dotcom_origin?
originating_hostname == GITHUB_DOMAIN
end
end