forked from puppetlabs/hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
91 lines (80 loc) · 3.15 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
require 'erb'
require 'base64'
require 'nokogiri'
require 'open-uri'
require 'octokit'
require 'htmlentities'
task :default do
puts 'This rake task will syncronize the contents of the Beginner Friendly Tickets'
puts 'filter from Jira to issues on this repository. It will ruthlessly close any'
puts 'issues created here that do not exist in the Jira filter.'
puts
puts 'If you would like to update the tickets mirrored here, then simply edit'
puts 'the filter at https://tickets.puppetlabs.com/issues/?filter=33481.'
puts
puts 'Simply run `rake sync` to start the mirror process.'
puts
system("rake -T")
end
desc 'Sychronize all issues'
task :sync do
TOKEN = ENV['GITHUB_TOKEN'] || `git config --global github.token`.chomp
ISSUE_LABELS = [
'Windows', 'Documentation', 'chocolatey', 'iis', 'sqlserver', 'registry'
]
if 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 => TOKEN)
rescue => e
puts "Github login error: #{e.message}"
exit 1
end
source = 'https://tickets.puppetlabs.com/sr/jira.issueviews:searchrequest-xml/33481/SearchRequest-33481.xml?tempMax=1000'
repo = 'puppetlabs/hacktoberfest'
gh_issues = client.list_issues(repo)
jira_issues = Nokogiri::XML(URI.parse(source).read).xpath('//item').map do |issue|
if issue.at_xpath('project').content == 'Modules'
component = 'Modules'
else
component = issue.at_xpath('component').content rescue nil
end
description = issue.at_xpath('description').content rescue nil
{
:title => issue.at_xpath('title').content,
:link => issue.at_xpath('link').content,
:created => issue.at_xpath('created').content,
:updated => issue.at_xpath('updated').content,
:component => component,
:description => description,
:labels => issue.xpath('labels/label').map {|label| label.content },
}
end
# find issues in Jira that don't exist in GitHub
create = jira_issues.reject do |jira|
gh_issues.find {|gh| gh[:title] == jira[:title]}
end
create.each do |issue|
puts "+ Creating issue: #{issue[:title]}"
body = ERB.new(File.read('_issue_description.erb')).result(binding)
labels = ['hacktoberfest']
labels << HTMLEntities.new.decode(issue[:component]) unless issue[:component].nil?
labels << issue[:labels].select { |label| ISSUE_LABELS.map(&:downcase).include? label.downcase}.map { |label| HTMLEntities.new.decode(label)}
labels = labels.uniq.flatten.join(',')
client.create_issue(repo, issue[:title], body, :labels => labels)
end
# find issues in GitHub that don't exist in the Jira filter
destroy = gh_issues.reject do |gh|
jira_issues.find {|jira| jira[:title] == gh[:title]}
end
destroy.each do |issue|
puts "- Closing issue: #{issue[:title]}"
client.close_issue(repo, issue[:number])
end
end