forked from doctrine/jira-github-issues
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathattachments.php
101 lines (77 loc) · 2.81 KB
/
attachments.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
*
* Import non-binary attachments into a Gist and comment on the relevant issue
* with a link.
*
* @example
*
* $ php attachments.php DDC
*/
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';
$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'
];
$issueMap = json_decode(file_get_contents('data/' . $project . '.issues.json'), true);
$issues = scandir('data/attachments/' . $project);
$count = 0;
foreach ($issues as $issue) {
if (!isset($issueMap[$issue])) continue;
$count++;
if (isset($argv[2]) && $count <= $argv[2]) continue;
$attachments = scandir('data/attachments/' . $project . '/' . $issue);
$gist = [
'description' => 'Attachments to Jira Issue ' . $issue . ' - https://github.com/' . $_SERVER['GITHUB_ORGANIZATION'] . '/' . $githubRepository . '/issues/' . $issueMap[$issue],
'public' => false,
'files' => [],
];
foreach ($attachments as $attachment) {
if (in_array($attachment, ['.', '..'])) continue;
$ext = pathinfo($attachment, PATHINFO_EXTENSION);
if (!in_array($ext, ['php', 'patch', 'diff', 'xml', 'yml', 'txt', 'sql', 'log', 'xsd'])) {
continue;
}
$gist['files'][$attachment]['content'] = file_get_contents('data/attachments/' . $project . '/' . $issue . '/' . $attachment);
}
if (count($gist['files']) === 0) {
continue;
}
$response = $client->post(
'https://api.github.com/gists',
$githubHeaders,
json_encode($gist)
);
if ($response->getStatusCode() >= 400) {
throw new \Exception($issue . " attachment import failed: " . $response->getContent());
}
$gistData = json_decode($response->getContent(), true);
$url = $gistData['html_url'];
$comment = "Imported " . count($gist["files"]) . " attachments from Jira into " . $url . "\n\n";
foreach ($gist['files'] as $file => $_) {
$anchor = "#file-" . str_replace(".", "-", $file);
$comment .= "- [" . $file . "](" . $url . $anchor . ")\n";
}
$response = $client->post(
'https://api.github.com/repos/' . $_SERVER['GITHUB_ORGANIZATION'] . '/' . $githubRepository . '/issues/' . $issueMap[$issue] . '/comments',
$githubHeaders,
json_encode(['body' => $comment])
);
printf("%04d - Imported %s into %s\n", $count, $issue, $url);
}