forked from doctrine/jira-github-issues
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathversion_to_milestones.php
101 lines (79 loc) · 2.96 KB
/
version_to_milestones.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
<?php
/**
* Jira to Github Migration
*
* Step 1: Create a milestone for every Jira version in the Github Issue Tracker.
*
* This is necessary so that we can attach all issues that we are going to
* import into Github Issues to their respective Jira version.
*
* @example
* $ php version_to_milestones.php <ProjectKey>
*/
require_once 'vendor/autoload.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);
}
$githubRepository = $projects[$project];
$githubHeaders = ['User-Agent: Jira Migration', 'Authorization: token ' . $_SERVER['GITHUB_TOKEN']];
$client = new \Buzz\Browser();
$response = $client->get($_SERVER['JIRA_URL'] . "/rest/api/2/project/$project/versions");
if ($response->getStatusCode() !== 200) {
printf("Could not fetch versions of project '$project'\n");
printf($response->getStatusCode());
exit(2);
}
$versions = json_decode($response->getContent(), true);
$skipVersions = ['Git Master'];
$milestones = [];
foreach ($versions as $version) {
$version['name'] = preg_replace('(^v)', '', $version['name']);
if (in_array($version['name'], $skipVersions)) {
continue;
}
$milestone = [
'title' => $version['name'],
'state' => $version['released'] ? 'closed' : 'open',
];
if (isset($version['releaseDate']) && $version['releaseDate']) {
$milestone['due_on'] = sprintf('%sT23:59:59Z', $version['releaseDate']);
}
$milestones[] = $milestone;
}
usort($milestones, function ($a, $b) {
return version_compare($a['title'], $b['title']) * -1;
});
$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']] = true;
}
$createdMilestones = [];
foreach ($milestones as $milestone) {
if (isset($existingMilestones[$milestone['title']])) {
continue;
}
$response = $client->post('https://api.github.com/repos/' . $_SERVER['GITHUB_ORGANIZATION'] . '/' . $githubRepository . '/milestones', $githubHeaders, json_encode($milestone));
if ($response->getStatusCode() < 400) {
$data = json_decode($response->getContent(), true);
$createdMilestones[$milestone['title']] = $data['id'];
} else {
printf('Error creating milestone "%s": %s', $milestone['title'], $response->getContent()) . "\n";
}
}
printf('Created %d milestones from Jira to Github.', count($createdMilestones));
var_dump($createdMilestones);