forked from pechdavid/activecollab-jira-json-migration
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathactivecollab-jira-json.rb
executable file
·223 lines (186 loc) · 4.61 KB
/
activecollab-jira-json.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
#!/usr/bin/env ruby
require "net/http"
require "uri"
require "json"
require "time"
if (ARGV.size != 6)
$stderr.puts "Usage: ./active-collab-jira-json.rb AC_URL AC_USER_API_KEY AC_PROJECT_ID JIRA_PROJECT_ID SKIP_PRIVATE SKIP_ARCHIVE > export.json"
$stderr.puts "Example: ./active-collab-jira-json.rb http://ac.mydomain.name 20-123213123123123 12 JIRAPROJ 0 1 > export.json"
$stderr.puts "And import it to Jira!"
exit(0)
end
URL = ARGV[0]
KEY = ARGV[1]
PROJECT_ID = ARGV[2].to_i
TARGET_PROJECT = ARGV[3]
SKIP_PRIVATE = ARGV[4];
SKIP_ARCHIVE = ARGV[5];
BASE_URL = "%s/api.php\?token\=%s\&format\=json\&path_info\=" % [URL, KEY]
$EMAILS = {}
$MILESTONES = {}
def json_url(append)
uri = URI.parse(BASE_URL + append)
response = Net::HTTP.get_response(uri)
body = response.body
if body == "null" then
{}
else
JSON.parse(body)
end
end
def get_milestone(milestone_id)
milestone_id = milestone_id.to_i
if not $MILESTONES[milestone_id]
begin
raw = json_url("projects/" + PROJECT_ID.to_s + "/milestones/" + milestone_id.to_s)
rescue
$stderr.puts "Milestone not found: " + milestone_id.to_s
raw = {"name" => nil}
end
$MILESTONES[milestone_id] = raw["name"]
end
$MILESTONES[milestone_id]
end
def get_email(user_id)
user_id = user_id.to_i
if not $EMAILS[user_id]
begin
raw = json_url("people/1/users/" + user_id.to_s)
rescue
$stderr.puts "User not found: " + user_id.to_s
raw = {"email" => nil}
end
$EMAILS[user_id] = raw["email"]
end
$EMAILS[user_id]
end
def timestamp(str)
if str.nil?
nil
else
Time.parse(str).to_i * 1000
end
end
def body(str)
if str.nil?
nil
else
str.gsub("<p>", "").gsub("</p>", "\n\n")
end
end
def iterate_tickets(append, skipPrivate)
raw_list = json_url("projects/" + PROJECT_ID.to_s + "/tickets" + append)
out_issues = []
raw_list.collect do |ticket_thumb|
raw = json_url("projects/" + PROJECT_ID.to_s + "/tickets/" + ticket_thumb["ticket_id"].to_s)
if raw["visibility"] == 0 && skipPrivate == "1"
next
end
$stderr.puts "#" + ticket_thumb["ticket_id"].to_s + ": " + raw["name"]
attachments = raw["attachments"]
raw["comments"].each do |comment|
attachments = attachments.concat(comment["attachments"])
end
#p raw
assignee = nil
watchers = []
if raw["assignees"]
raw["assignees"].each do |assignee|
watchers.push(get_email(assignee["user_id"]));
end
filtered = raw["assignees"].delete_if {|e| !e["is_owner"]}
unless filtered.empty?
assignee = get_email(filtered.first["user_id"])
end
end
priority = case raw["priority"]
when -2
"Trivial"
when -1
"Minor"
when 1
"Critical"
when 2
"High"
else
"Normal"
end
status = nil
resolution = nil
case append
when "/archive"
status = "Closed"
resolution = "Done"
else
status = "Open"
resolution = nil
end
fixedVersions = []
unless raw["milestone_id"].nil?
fixedVersions = [ get_milestone(raw["milestone_id"]) ]
end
#$stderr.puts attachments
{
"summary" => raw["name"],
"description" => body(raw["body"]),
"issueType" => "Task",
"status" => status,
"resolution" => resolution,
"priority" => priority,
"created" => timestamp(raw["created_on"]),
"updated" => timestamp(raw["updated"]),
"duedate" => timestamp(raw["due_on"]),
"resolutionDate" => timestamp(raw["completed_on"]),
"fixedVersions" => fixedVersions.compact,
"externalId" => ticket_thumb["ticket_id"].to_s,
"comments" => raw["comments"].collect do |cm|
{
"body" => body(cm["body"]),
"created" => timestamp(cm["created_on"]),
"author" => get_email(cm["created_by_id"])
}
end,
"attachments" => attachments.collect do |at|
{
"attacher" => get_email(at["created_by_id"]),
"name" => at["name"],
"created" => timestamp(at["created_on"]),
"uri" => at["permalink"].sub("attachments/", "serve.php?file=")
}
end,
"assignee" => assignee,
"reporter" => get_email(raw["created_by_id"]),
"watchers" => watchers
}
end
end
def export_project(skipPrivate, skipArchive)
issues = iterate_tickets("", skipPrivate)
if skipArchive == "0"
issues = issues.concat(iterate_tickets("/archive", skipPrivate))
end
{
"projects" =>
[
{
"issues" => issues,
"key" => TARGET_PROJECT,
"versions" =>
$MILESTONES.reject {|key, value| value.nil? }.collect do |k, v|
{
"name" => v,
}
end
}
],
"users" =>
$EMAILS.reject {|key, value| value.nil? }.collect do |k, v|
{
"name" => v,
"email" => v,
"groups" => ["activecollab-unused-import"]
}
end
}.to_json
end
puts export_project(SKIP_PRIVATE, SKIP_ARCHIVE)