forked from doctrine/jira-github-issues
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexport_jira_tickets.php
188 lines (156 loc) · 7.29 KB
/
export_jira_tickets.php
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
<?php
/**
* Github to Jira Migration
*
* Step 2: Export all tickets from Jira into JSON file(s) on disk.
*
* We don't want to require both Jira and Github uptime, so we use an intermediate
* format for all issues, where we export Jira issues into the format that the Github
* bulk import API needs. This script is written in a way so that it can be "continued"
* after abort.
*
* @example
* $ php export_jira_tickets <Project> <StartAt>
*/
require_once 'vendor/autoload.php';
require_once 'jira_markdown.php';
$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();
if (!isset($argv[1])) {
printf("Missing argument: Project Key\n");
exit(1);
}
$project = $argv[1];
$projects = require 'projects.php';
if (!isset($projects[$project])) {
printf("Unknown project: $project\n");
exit(2);
}
$startAt = isset($argv[2]) ? (int)($argv[2]) : 0;
$githubRepository = $projects[$project];
$githubHeaders = ['User-Agent: Jira Migration', 'Authorization: token ' . $_SERVER['GITHUB_TOKEN']];
$jiraHeaders = ['Authorization: Basic ' . base64_encode(sprintf('%s:%s', $_SERVER['JIRA_USER'], $_SERVER['JIRA_PASSWORD']))];
$client = new \Buzz\Browser();
$response = $client->get('https://api.github.com/repos/' . $_SERVER['GITHUB_ORGANIZATION'] . '/' . $githubRepository . '/milestones?state=all&per_page=100', $githubHeaders);
if ($response->getStatusCode() !== 200) {
printf("Could not fetch existing Github Milestones\n");
var_dump($response->getContent());
exit(3);
}
$existingMilestones = [];
foreach(json_decode($response->getContent(), true) as $existingMilestone) {
$existingMilestones[$existingMilestone['title']] = $existingMilestone['number'];
}
$count = 0;
@mkdir("data/" . $project, 0777);
$knownIssueTypes = ['Bug', 'New Feature', 'Improvement', 'Code Review', 'Epic', 'Sub-task', 'Support', 'Task', 'Technical task', 'User Story'];
$knownAssigneesMap = [
'ymarcon' => 'ymarcon',
'cfontin' => 'meek0',
'rhaeri' => 'kazoompa',
'samir' => 'smirfolio',
'plaflamm' => 'plaflamme',
'cthiebault' => 'cthiebault',
'tombisho' => 'tombisho',
'dean' => 'inglis-dl',
'rbouchakri' => 'Rima-B',
'rroces' => 'firone',
'emorency' => 'emorency',
'ctamisier' => 'ctamisier',
'hudson' => 'obiba-ci',
'delson' => 'dwainelson'
];
while (true) {
$response = $client->get($_SERVER['JIRA_URL'] . "/rest/api/2/search?jql=" . urlencode("project = $project ORDER BY created ASC") . "&fields=" . urlencode("*all") . "&startAt=" . $startAt, $jiraHeaders);
if ($response->getStatusCode() !== 200) {
printf("Could not fetch versions of project '$project'\n");
printf($response->getStatusCode());
exit(2);
}
$issues = json_decode($response->getContent(), true);
if (count($issues['issues']) === 0) {
printf("Exported %d issues from Jira into data/%s/ folder.\n", $count, $project);
return;
}
$count += count($issues['issues']);
foreach ($issues['issues'] as $issue) {
if ($issue['key'] === 'DDC-93') var_dump($issue);
$import = [
'issue' => [
'title' => sprintf('%s: %s', $issue['key'], $issue['fields']['summary']),
'body' => sprintf(
"Jira issue originally created by user %s:\n\n%s",
mentionName($issue['fields']['creator']['name']),
toMarkdown($issue['fields']['description'])
),
'created_at' => substr($issue['fields']['created'], 0, 19) . 'Z',
'closed' => in_array($issue['fields']['status']['name'], ['Resolved', 'Closed']),
],
];
if (isset($issue['fields']['issuetype']['name']) && in_array($issue['fields']['issuetype']['name'], $knownIssueTypes)) {
$import['issue']['labels'] = [strtolower($issue['fields']['issuetype']['name'])];
}
if (isset($issue['fields']['fixVersions']) && count($issue['fields']['fixVersions']) > 0) {
$milestoneVersion = array_reduce($issue['fields']['fixVersions'], function ($last, $version) {
$versionName = preg_replace('(^v)', '', $version['name']);
if (version_compare($last, $versionName) > 0) {
return $versionName;
}
return $last;
}, '10.0.0');
if (isset($existingMilestones[$milestoneVersion])) {
$import['issue']['milestone'] = $existingMilestones[$milestoneVersion];
}
}
if (isset($issue['fields']['assignee']) && $issue['fields']['assignee'] && in_array($issue['fields']['assignee']['name'], $knownAssigneesMap)) {
$import['issue']['assignee'] = $knownAssigneesMap[$issue['fields']['assignee']['name']];
}
$import['comments'] = [];
/*if (isset($issue['fields']['issuelinks']) && $issue['fields']['issuelinks']) {
$comment = "";
foreach ($issue['fields']['issuelinks'] as $link) {
if (isset($link['inwardIssue'])) {
$comment .= sprintf("* %s [%s: %s](http://www.doctrine-project.org/jira/browse/%s)\n", $link['type']['inward'], $link['inwardIssue']['key'], $link['inwardIssue']['fields']['summary'], $link['inwardIssue']['key']);
} else if (isset($link['outwardIssue'])) {
$comment .= sprintf("* %s [%s: %s](http://www.doctrine-project.org/jira/browse/%s)\n", $link['type']['outward'], $link['outwardIssue']['key'], $link['outwardIssue']['fields']['summary'], $link['outwardIssue']['key']);
}
}
$import['comments'][] = [
'body' => $comment,
'created_at' => substr($issue['fields']['created'], 0, 19) . 'Z',
];
}*/
if (isset($issue['fields']['comment']) && count($issue['fields']['comment']['comments']) > 0) {
foreach ($issue['fields']['comment']['comments'] as $comment) {
$import['comments'][] = [
'created_at' => substr($comment['created'], 0, 19) . 'Z',
'body' => sprintf(
"Comment created by %s:\n\n%s",
mentionName($comment['author']['name']),
toMarkdown($comment['body'])
),
];
}
}
if (isset($issue['fields']['resolutiondate']) && $issue['fields']['resolutiondate']) {
$import['comments'][] = [
'created_at' => substr($issue['fields']['resolutiondate'], 0, 19) . 'Z',
'body' => sprintf('Issue was closed with resolution "%s"', $issue['fields']['resolution']['name']),
];
}
if (count($import['comments']) === 0) {
unset($import['comments']);
}
file_put_contents("data/" . $project . "/" . $issue['key'] . ".json", json_encode($import, JSON_PRETTY_PRINT));
printf("Processed issue: %s (Idx: %d)\n", $issue['key'], $startAt);
$startAt++;
}
printf("Completed batch, continuing with start at %d\n", $startAt);
}
function mentionName($name) {
global $knownAssigneesMap;
if (isset($knownAssigneesMap[$name])) {
return '@' . $knownAssigneesMap[$name];
}
return $name;
}