-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbackground.js
177 lines (147 loc) · 3.92 KB
/
background.js
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
let user = null;
async function ensureUser() {
if (user) {
return user;
}
const { github_key, bugzilla_key } = await browser.storage.local.get();
if (!github_key || !bugzilla_key) {
browser.runtime.openOptionsPage();
throw new Error("Missing keys.");
}
user = {
bugzilla_key,
github_key,
};
return user;
}
browser.runtime.onMessage.addListener((data, sender) => {
switch (data.type) {
case "move-to-bugzilla": {
moveToBugzilla(data);
break;
}
case "get-components": {
return getComponents(data);
break;
}
}
});
async function getComponents(data) {
const { product } = data;
const result = await fetch(
`https://bugzilla.mozilla.org/rest/product?names=${product}`);
const productData = await result.json();
return productData.products[0].components.map(c => c.name);
}
async function githubIssueApi({ issue, path = '', data, method = 'GET' }) {
const body = {
... issue,
... data,
};
const user = await ensureUser();
const request = {
method,
headers: {'Authorization': `token ${user.github_key}`},
};
if (method === 'POST') {
request.body = JSON.stringify(body);
}
const response = await fetch(
`https://api.github.com/repos/${issue.owner}/${issue.repo}`
+ `/issues/${issue.issue_number}${path}`,
request
);
if (!response.ok) {
throw new Error(`Github request failed: ${await response.text()}`);
}
const result = await response.json();
return result;
}
function bugzillaDescription(githubData) {
// Add ">" to each line.
const quote = (githubData.body || "")
.split('\n')
.map(s => '> ' + s)
.join('\n');
return `From github: ${githubData.html_url}.\n\n`
+ `${quote}\n\n`
+ `Change performed by the [Move to Bugzilla add-on](`
+ `https://addons.mozilla.org/en-US/firefox/addon/move-to-bugzilla/).`;
}
// These are IDs for GitHub labels that corresponds to a "defect"
const DEFECT_TYPE_LABEL_IDS = new Set([
875862810
]);
// These are IDs for GitHub labels that corresponds to a "enhancement"
const ENHANCEMENT_TYPE_LABEL_IDS = new Set([
877027717
]);
function bugzillaType(githubData) {
if (!("labels" in githubData)) {
return "defect";
}
for (label of githubData.labels) {
if (DEFECT_TYPE_LABEL_IDS.has(label.id)) {
return "defect";
}
if (ENHANCEMENT_TYPE_LABEL_IDS.has(label.id)) {
return "enhancement";
}
}
return "defect";
}
async function moveToBugzilla(data) {
const { github, component, product, opSys, severity, priority, bugType } = data;
const user = await ensureUser();
const issue = {
owner: github.owner,
repo: github.repo,
issue_number: github.issueId,
};
const githubData = await githubIssueApi({
issue
});
const bugzillaRequest = {
api_key: user.bugzilla_key,
product,
component,
type: bugType || bugzillaType(githubData),
version: "unspecified",
op_sys: opSys,
platform: "unspecified",
severity,
priority,
summary: githubData.title,
description: bugzillaDescription(githubData),
};
// Create bug in Bugzilla first
const bugzillaResponse = await fetch('https://bugzilla.mozilla.org/rest/bug', {
method: 'POST',
body: JSON.stringify(bugzillaRequest),
});
const response = await bugzillaResponse.json();
const bugzillaId = response.id;
if (!bugzillaId) {
throw new Error(`Could not create bugzilla bug: ${JSON.stringify(response)}`);
}
// Add comment
await githubIssueApi({
issue,
method: 'POST',
path: '/comments',
data: {
body: `Moved to bugzilla: `
+ `https://bugzilla.mozilla.org/show_bug.cgi?id=${bugzillaId}\n\n`
+ `Change performed by the [Move to Bugzilla add-on](`
+ `https://addons.mozilla.org/en-US/firefox/addon/move-to-bugzilla/).`,
}
});
// Close issue
await githubIssueApi({
issue,
method: 'POST',
data: {
state: "closed",
},
});
}