-
Notifications
You must be signed in to change notification settings - Fork 12
/
setup.rb
executable file
·102 lines (92 loc) · 3.47 KB
/
setup.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
#! /usr/bin/env ruby
require 'io/console'
begin
require 'octokit'
rescue LoadError
puts "This script requires the 'octokit' gem."
puts "Either install it with:"
puts " $ gem install octokit"
puts
puts "or run this script with bundler (preferred):"
puts " $ gem install bundler"
puts " $ bundle install --path vendor/bundle"
puts " $ bundle exec #{__FILE__}"
exit 1
end
def question(message, required=false, noecho=false)
loop do
print "#{message} "
answer = noecho ? STDIN.noecho(&:gets) : STDIN.gets
answer.strip!
puts if noecho
next if required and answer.empty?
return answer
end
end
begin
oauth = nil
upstream = 'puppetlabs/contributor-summit-project'
origin = `git config --get remote.origin.url`.strip.match(/github.com[\/:]([\w-]+\/[\w-]+)(?:.git)?$/)[1]
username = origin.split('/').first
password = question('What is your GitHub password?', true, true)
otpcode = question('Type your two-factor code, or press [enter] to skip:').delete(' ')
rescue => e
raise "Please run this script from within the Contributor Summit project template repository"
end
begin
# inspired by https://github.com/TimothyBritt/github-issue-migrate
client = Octokit::Client.new(:login => username, :password => password)
unless otpcode.empty?
begin
# create a temporary oauth token to use while cloning issues
oauth = client.create_authorization(:scopes => ['repo'],
:note => 'Contributor Summit Project',
:headers => { 'X-GitHub-OTP' => otpcode })
rescue Octokit::OneTimePasswordRequired
puts
puts 'Please ensure that your two-factor code is correct.'
exit 1
rescue Octokit::UnprocessableEntity
puts
puts 'Check https://github.com/settings/tokens'
puts "Make sure you don't already have a token for Contributor Summit."
puts 'Delete it if you do, and then run this again.'
exit 1
end
# save a copy of the password-authed client for token cleanup
pclient = client
# Creating the authorization isn't enough. Now we've got to re-login with that token
client = Octokit::Client.new(:access_token => oauth[:token])
end
client.auto_paginate = true
client.edit_repository(origin, options = { :has_issues => true })
milestones = client.milestones(upstream)
milestones.each do |source|
print '.'
client.create_milestone(origin, source.title, { :description => source.description,
:due_on => source.due_on })
end
issues = client.issues(upstream).reverse!
issues.each do |source|
next if source.pull_request
print '.'
client.create_issue(origin, source.title, source.body, { :milestone => source.milestone.number,
:labels => source.labels })
end
rescue => e
puts
puts e.message
puts "Error source: #{e.backtrace.last}"
end
puts
if oauth
# now let's clean up that oauth token
begin
pclient.delete_authorization(oauth[:id], :headers => { 'X-GitHub-OTP' => otpcode })
rescue Octokit::OneTimePasswordRequired
# 2fa has expired, let's grab it again
otpcode = question('Your two-factor code has expired. Type a new code so we can cleanup:').delete(' ')
pclient = Octokit::Client.new(:login => username, :password => password)
pclient.delete_authorization(oauth[:id], :headers => { 'X-GitHub-OTP' => otpcode })
end
end