-
Notifications
You must be signed in to change notification settings - Fork 6
/
Rakefile
304 lines (246 loc) · 10.3 KB
/
Rakefile
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
require 'erb'
require 'base64'
require 'httparty'
require 'json'
require 'octokit'
ORG = 'puppet-champions'
REPO = "#{ORG}/puppet-champions.github.io"
TEAM = 'Champions'
NIMBLE_TOKEN = ENV['NIMBLE_TOKEN']
GITHUB_TOKEN = ENV['GITHUB_TOKEN'] || `git config --global github.token`.chomp
@errorlevel = 0
############################### Nimble functions ###############################
def nimble_id_map(name)
['none', 'Extraordinary Puppeteer', 'Champion'].index(name).to_s
end
def nimble_group(name)
if NIMBLE_TOKEN.nil?
puts "You need to generate a Nimble token:"
puts "\t * http://support.nimble.com/en/articles/822159-generate-an-api-key-to-access-the-nimble-api"
puts
puts "Export that as the `NIMNLE_TOKEN` environment variable."
exit 1
end
query = {
"query" => {"custom_fields" => {"Puppet Champion Status" => {"is" => nimble_id_map(name)}}}.to_json,
"fields" => "email,GitHub Username",
}
response = HTTParty.get('https://app.nimble.com/api/v1/contacts',
headers: {"Authorization" => "Bearer #{NIMBLE_TOKEN}"},
query: query,
)
raise response.body unless response.success?
data = JSON.parse(response.body)
without, with = data['resources'].partition {|item| item['fields']['GitHub Username'].nil?}
unless without.empty?
puts 'The following Nimble accounts have no GitHub username associated:'
without.each do|item|
puts " ↳ #{item['fields']['email'].first['value']}"
@errorlevel += 1
end
end
# now return the ones we do know.
with.map {|item| item['fields']['GitHub Username'].first['value'] }
end
############################ End Nimble functions ##############################
############################### GitHub functions ###############################
def client
return @client if @client
if GITHUB_TOKEN.empty?
puts "You need to generate a GitHub token:"
puts "\t * https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line"
puts "\t * git config --global github.token <token>"
puts
puts "Export that as the `GITHUB_TOKEN` environment variable or put it in your ~/.gitconfig."
exit 1
end
begin
@client = Octokit::Client.new(:access_token => GITHUB_TOKEN)
rescue => e
puts "Github login error: #{e.message}"
exit 1
end
@client
end
def get_file(path)
begin
Base64.decode64(client.contents(REPO, path: path).content)
rescue Octokit::NotFound => e
puts "File not found: #{path}"
''
end
end
def add_team_member(username)
@team_id ||= @client.organization_teams(ORG).find {|team| team['name'] == TEAM }[:id]
# check first so we don't inadvertently change someone's access level
return if client.team_member?(@team_id, username)
client.add_team_membership(@team_id, username, {:role => 'member'})
# now give github just a moment to sync up so this doesn't fail when requesting a review
sleep 2
end
def remove_team_member(username)
@team_id ||= @client.organization_teams(ORG).find {|team| team['name'] == TEAM }[:id]
client.remove_team_membership(@team_id, username)
end
def create_profile(member, path)
profile = ERB.new(get_file('_template.erb')).result(binding)
welcome = ERB.new(get_file('_welcome.erb')).result(binding)
subject = "Creating profile for #{member[:login]}"
master = 'heads/master'
branch = "heads/#{member[:login]}"
begin
master_sha = client.ref(REPO, 'heads/master').object.sha
branch_sha = client.create_ref(REPO, branch, master_sha).object.sha
base_tree_sha = client.commit(REPO, master_sha).commit.tree.sha
blob_sha = client.create_blob(REPO, Base64.encode64(profile), 'base64')
new_tree_sha = client.create_tree(REPO,
[ { :path => "#{path}/#{member[:login]}.md",
:mode => '100644',
:type => 'blob',
:sha => blob_sha
}
],
{:base_tree => base_tree_sha }
).sha
new_commit_sha = client.create_commit(REPO, subject, new_tree_sha, branch_sha).sha
updated_ref = client.update_ref(REPO, branch, new_commit_sha)
pull_request = client.create_pull_request(REPO, 'master', member[:login], subject, welcome)
client.request_pull_request_review(REPO, pull_request[:number], reviewers: [member[:login]] )
rescue Octokit::UnprocessableEntity => e
if e.message.match /Reference already exists/
puts " ↳ Branch for #{member[:login]} already exists."
else
puts e.message
end
@errorlevel += 1
end
end
def move_profile(member, source, dest)
subject = "Moving profile for #{member[:login]}"
master = 'heads/master'
branch = "heads/#{member[:login]}"
source = "#{source}/#{member[:login]}.md"
dest = "#{dest}/#{member[:login]}.md"
begin
master_sha = client.ref(REPO, 'heads/master').object.sha
branch_sha = client.create_ref(REPO, branch, master_sha).object.sha
base_tree = client.tree(REPO, master_sha, recursive: true).tree
changed_tree = base_tree.reject { |blob| blob.type == 'tree' }
changed_item = changed_tree.find {|blob| blob.path == source }
# now finally rename the file!
changed_item.path = dest
# we need hashes and to clean up the elements
changed_tree.map!(&:to_hash).each { |blob| blob.delete(:url) && blob.delete(:size) }
new_tree_sha = client.create_tree(REPO, changed_tree).sha
new_commit_sha = client.create_commit(REPO, "Rename #{source} to #{dest}", new_tree_sha, master_sha).sha
updated_ref = client.update_ref(REPO, branch, new_commit_sha)
pull_request = client.create_pull_request(REPO, 'master', member[:login], subject, subject)
# no need for a PR review, the CODEOWNERS should do that for us
#client.request_pull_request_review(REPO, pull_request[:number], reviewers: '@puppet-champions/admins' )
rescue Octokit::UnprocessableEntity => e
if e.message.match /Reference already exists/
puts " ↳ Branch for #{member[:login]} already exists."
else
puts e.message
end
@errorlevel += 1
end
end
def delete_profile(filename)
subject = "Deleting profile #{filename}"
pr_name = "deleting_#{File.basename(filename, '.md')}"
master = 'heads/master'
branch = "heads/#{pr_name}"
begin
master_sha = client.ref(REPO, 'heads/master').object.sha
branch_sha = client.create_ref(REPO, branch, master_sha).object.sha
base_tree = client.tree(REPO, master_sha, recursive: true).tree
changed_tree = base_tree.reject { |blob| blob.type == 'tree' }
# now remove the file
changed_tree.reject! {|blob| blob.path == filename }
# we need hashes and to clean up the elements
changed_tree.map!(&:to_hash).each { |blob| blob.delete(:url) && blob.delete(:size) }
new_tree_sha = client.create_tree(REPO, changed_tree).sha
new_commit_sha = client.create_commit(REPO, subject, new_tree_sha, master_sha).sha
updated_ref = client.update_ref(REPO, branch, new_commit_sha)
pull_request = client.create_pull_request(REPO, 'master', pr_name, subject, subject)
# no need for a PR review, the CODEOWNERS should do that for us
#client.request_pull_request_review(REPO, pull_request[:number], reviewers: '@puppet-champions/admins' )
rescue Octokit::UnprocessableEntity => e
if e.message.match /Reference already exists/
puts " ↳ Branch for #{pr_name} already exists."
else
puts e.message
end
@errorlevel += 1
end
end
############################ End GitHub functions ##############################
task :default do
puts 'This rake task just automates the management of Champion profile pages.'
puts 'It will create/remove/move profiles as markdown pages as users are updated'
puts 'in Nimble.'
puts
puts 'The user is review-requested when necessary with instructions on how to'
puts 'update and approve their profile.'
puts
puts "This sync task will run weekly, but if you'd like you can force an update manually."
puts 'Simply run `rake sync` at the command line each time you update membership.'
puts
system("rake -T")
end
desc 'Sychronize profiles to match team membership'
task :sync do
begin
puppeteer_profiles = client.contents(REPO, :path => '_puppeteers').map {|obj| obj[:path] }.select {|f| f.end_with? '.md' }
champion_profiles = client.contents(REPO, :path => '_champions').map {|obj| obj[:path] }.select {|f| f.end_with? '.md' }
rescue Octokit::NotFound => e
puts "Path does not exist: #{e.message}"
exit 1
end
puppeteers = nimble_group('Extraordinary Puppeteer')
champions = nimble_group('Champion')
puppeteers.each do |login|
member = client.user(login)
profile = "#{login}.md"
next if puppeteer_profiles.include? "_puppeteers/#{profile}"
if champion_profiles.include? "_champions/#{profile}"
champion_profiles.delete("_champions/#{profile}")
puts "Moving #{profile} from _champions to _puppeteers."
move_profile(member, '_champions', '_puppeteers')
else
puts "Creating Puppeteer profile: #{profile}"
add_team_member(member[:login])
create_profile(member, '_puppeteers')
end
end
champions.each do |login|
member = client.user(login)
profile = "#{login}.md"
next if champion_profiles.include? "_champions/#{profile}"
if puppeteer_profiles.include? "_puppeteers/#{profile}"
puppeteer_profiles.delete("_puppeteers/#{profile}")
puts "Moving #{profile} from _puppeteers to _champions."
move_profile(member, '_puppeteers', '_champions')
else
puts "Creating Champion profile: #{profile}"
add_team_member(member[:login])
create_profile(member, '_champions')
end
end
# now clean up leftovers
(puppeteer_profiles - puppeteers.map {|member| "_puppeteers/#{member}.md" }).each do |path|
puts "Removing #{path}"
remove_team_member(File.basename(path, '.md'))
delete_profile(path)
end
(champion_profiles - champions.map {|member| "_champions/#{member}.md" }).each do |path|
puts "Removing #{path}"
remove_team_member(File.basename(path, '.md'))
delete_profile(path)
end
unless @errorlevel == 0
puts '-----------------------------------------'
abort "There were #{@errorlevel} sync warnings."
end
end