forked from doctrine/jira-github-issues
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimport_tickets.php
110 lines (88 loc) · 3.17 KB
/
import_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
<?php
/**
* Jira to Github Migration
*
* @example
*
* $ php import_tickets.php DDC
*/
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';
$client = new \Buzz\Browser();
if (!isset($projects[$project])) {
printf("Unknown project: $project\n");
exit(2);
}
$githubRepository = $projects[$project];
$githubHeaders = [
'User-Agent: Jira Migration',
'Authorization: token ' . $_SERVER['GITHUB_TOKEN'],
'Accept: application/vnd.github.golden-comet-preview+json'
];
$jiraHeaders = ['Authorization: Basic ' . base64_encode(sprintf('%s:%s', $_SERVER['JIRA_USER'], $_SERVER['JIRA_PASSWORD']))];
$ticketStatus = [];
if (file_exists('data/' . $project . '.status.json')) {
$ticketStatus = json_decode(file_get_contents('data/' . $project . '.status.json'), true);
}
$files = scandir('data/' . $project);
if (isset($argv[2])) {
$files = [$argv[2] . ".json"];
}
$issueIds = [];
foreach ($files as $file) {
if ($file === "." || $file === "..") continue;
$issueId = str_replace($project . '-', '', str_replace('.json', '', $file));
array_push($issueIds, intval($issueId));
}
sort($issueIds);
$count = 0;
foreach ($issueIds as $issueId) {
$issueKey = $project . '-' . $issueId;
$file = $issueKey . '.json';
$issue = json_decode(file_get_contents('data/' . $project . '/' . $file), true);
printf("Preparing %s... ", $issueKey);
if (isset($ticketStatus[$issueKey])) {
if ($ticketStatus[$issueKey]['status'] === 'pending') {
printf("pending, skipped\n");
continue;
$response = $client->get($ticketStatus[$issueKey]['url'], $githubHeaders);
if ($response->getStatusCode() == 200) {
$ticketStatus[$issueKey] = json_decode($response->getContent(), true);
file_put_contents("data/" . $project . ".status.json", json_encode($ticketStatus, JSON_PRETTY_PRINT));
printf("updated status... ");
}
}
if ($ticketStatus[$issueKey]['status'] === 'pending') {
printf("pending, skipped\n");
continue;
}
if ($ticketStatus[$issueKey]['status'] === 'imported') {
printf("imported, skipped\n");
continue;
}
if ($ticketStatus[$issueKey]['status'] === 'failed') {
printf("Error importing, retry... ", $issueKey);
}
}
//printf("debug skip\n"); continue;
$response = $client->post('https://api.github.com/repos/' . $_SERVER['GITHUB_ORGANIZATION'] . '/' . $githubRepository . '/import/issues', $githubHeaders, json_encode($issue));
if ($response->getStatusCode() >= 400) {
printf("Error: " . $response->getContent());
exit;
}
$ticketStatus[$issueKey] = json_decode($response->getContent(), true);
file_put_contents("data/" . $project . ".status.json", json_encode($ticketStatus, JSON_PRETTY_PRINT));
printf("imported %s\n", $issueKey);
$count++;
if (($count % 10) === 0) {
//exit;
}
}