-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredmine_to_github_migration.rb
186 lines (164 loc) · 5.06 KB
/
redmine_to_github_migration.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
require 'rubygems'
require 'rest-client'
require 'json'
require 'octopi'
require 'ruby-debug'
include Octopi
username
token =
authenticated_with :login => username, :token => token do
puts "Authenticated!"
class IssueMigrator
attr_accessor :redmine_issues
attr_accessor :issue_pairs
def get_issues
offset = 0
issues = []
puts "Getting redmine issues!"
begin
json = RestClient.get("http://bugs.joindiaspora.com/issues", {:params => {:format => :json, :status_id => '*', :limit => 100, :offset => offset}})
result = JSON.parse(json)
issues << [*result["issues"]]
offset = offset + result['limit']
print '.'
end while offset < result['total_count']
puts
puts "Retreived redmine issue index."
issues.flatten!
puts "Getting comments"
issues.map! do |issue|
get_comments(issue)
end
puts "Retreived comments."
self.redmine_issues = issues.reverse!
end
def issues
repo.issues
end
def repo
@repo ||= Repository.find(:name => "redmine-issues", :user => "diaspora")
end
def migrate_issues
self.issue_pairs = []
redmine_issues.each do |issue|
migrate_issue issue
end
end
def migrate_issue issue
github_issue = create_issue(issue)
add_labels(github_issue, issue)
migrate_comments(github_issue, issue)
github_issue.close! if ["Fixed", "Rejected", "Won't Fix", "Duplicate", "Obsolete", "Implemented"].include? issue["status"]["name"]
print "."
self.issue_pairs << [github_issue, issue]
github_issue
end
def create_issue redmine_issue
params = { :title => redmine_issue["subject"]}
params[:body] = <<BODY
Issue #{redmine_issue["id"]} from bugs.joindiaspora.com
Created by: **#{redmine_issue["author"]["name"]}**
On #{DateTime.parse(redmine_issue["created_on"]).asctime}
*Priority: #{redmine_issue["priority"]["name"]}*
*Status: #{redmine_issue["status"]["name"]}*
#{
custom_fields = ''
redmine_issue["custom_fields"].each do |field|
custom_fields << "*#{field["name"]}: #{field["value"]}*\n" unless field["value"].nil? || field["value"] == ''
end if redmine_issue["custom_fields"]
custom_fields
}
#{redmine_issue["description"]}
BODY
begin
Issue.open(:repo => self.repo, :params => params)
rescue Exception => e
redmine_issue["retrying?"] = true
retry unless redmine_issue["retrying?"]
puts "Issue open failed for Redmine Issue #{redmine_issue["id"]}"
end
end
def add_labels github_issue, redmine_issue
labels = []
if priority = redmine_issue["priority"]
if priority == "Low"
add_label_to_issue(github_issue, "Low Priority")
elsif ["High", "Urgent", "Immediate"].include?(priority)
add_label_to_issue(github_issue, "High Priority")
end
end
["tracker", "status", "category"].each do |thing|
next unless redmine_issue[thing]
value = redmine_issue[thing]["name"]
first_try = true
add_label_to_issue(github_issue, value) unless ["New", "Fixed"].include?(value)
end
end
def add_label_to_issue github_issue, label
label = "Will Not Fix" if label == "Won't Fix"
first_try = true
begin
github_issue.add_label URI.escape(label)
print ','
rescue Exception => e
puts
pp e
puts
puts label
puts URI.escape(label)
if first_try
first_try = false
retry
end
end
end
def migrate_comments github_issue, redmine_issue
redmine_issue["journals"].each do |j|
next if j["notes"].nil? || j["notes"] == ''
github_issue.comment <<COMMENT
Comment by: **#{j["user"]["name"]}**
On #{DateTime.parse(j["created_on"]).asctime}
#{j["notes"]}
COMMENT
end
end
def get_comments redmine_issue
print "."
issue_json = JSON.parse(RestClient.get("http://bugs.joindiaspora.com/issues/#{redmine_issue["id"]}", :params => {:format => :json, :include => :journals}))
issue_json["issue"]
end
def clear_issues
puts "Clearing issues!"
issues.each do |i|
i.close!
print '.'
end
end
def save_issues filename
full_saveable = []
issue_pairs.each do |pair|
full_saveable << {
:redmine => pair[1].merge(:url => "http://bugs.joindiaspora.com/issues/#{pair[1]["id"]}"),
:github => {
:url => "#{pair[0].repository.url}/issues/#{pair[0].number}",
:number => pair[0].number,
:repo_url => pair[0].repository.url
}
}
end
File.open(filename, 'w') do |f|
f.write(full_saveable.to_json)
end
end
end
m = IssueMigrator.new
m.get_issues
puts "Migrating issues to github..."
m.migrate_issues
m.save_issues "migration.json"
puts "Done migrating!"
## OPEN
# http://github.com/api/v2/json/issues/list/diaspora/diaspora/open
## CLOSED
# http://github.com/api/v2/json/issues/list/diaspora/diaspora/closed
end