-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgithub-bugzilla-background.js
More file actions
118 lines (100 loc) · 3.22 KB
/
github-bugzilla-background.js
File metadata and controls
118 lines (100 loc) · 3.22 KB
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
"use strict";
/**
* JS that runs in the background and has access to browser tabs.
*/
browser.runtime.onMessage.addListener(createAttachTab);
browser.runtime.onMessage.addListener(createMergeCommentTab);
/**
* Sanitize text for inserting into JavaScript template.
*/
function sanitizeForTemplate(text) {
// Escape all \
text = text.replace(/\\/g, "\\\\");
// Escape all "
text = text.replace(/"/g, "\\\"");
return text;
}
/**
* Retrieve the currently active tab.
*/
async function getActiveTab() {
const allTabs = await browser.tabs.query({ currentWindow: true });
for (let i in allTabs) {
let tabInfo = allTabs[i];
if (tabInfo.active) {
return tabInfo;
}
}
return null;
}
/**
* Creates a new tab related to the currently active tab, open the Bugzilla
* attach url, and populate the fields.
*/
async function createAttachTab(msg) {
if (msg.eventName !== 'attachLink') {
return;
}
const tab = await getActiveTab();
var tabId = tab.id;
// Create a tab with the Bugzilla attach url
const newTab = await browser.tabs.create({
url: msg.attachUrl,
active: true,
openerTabId: tabId,
index: tabId
});
var attValue = sanitizeForTemplate(msg.prUrl);
var descValue = sanitizeForTemplate(`[${msg.repoOrg}/${msg.repoName}] ${msg.prTitle} (#${msg.prNum})`);
// We need to add values for this specific attachment, so we build the
// template with local values and then execute the code in the tab.
var attachScript = `
// Add PR link
let att = document.getElementById("att-textarea");
att.value = "${attValue}";
// Trigger an input event so the textarea input handling kicks off.
var input_event = new Event('input', {
bubbles: true,
cancelable: true
});
att.dispatchEvent(input_event);
// Stomp on description with the PR title
let att_desc = document.getElementById("att-description");
att_desc.value = "${descValue}";
att.focus();
`;
await browser.tabs.executeScript(newTab.id, {
code: attachScript
});
console.info('rob-bugson: done!');
}
/**
* Creates a new tab related to the currently active tab, open the Bugzilla
* bug url, and add a comment.
*/
async function createMergeCommentTab(msg) {
if (msg.eventName !== 'mergeComment') {
return;
}
const tab = await getActiveTab();
var tabId = tab.id;
// Create a tab with the Bugzilla attach url
const newTab = await browser.tabs.create({
url: msg.bugUrl,
active: true,
openerTabId: tabId,
index: tabId
});
var mergeComment = `[${msg.author}](${msg.authorUrl}) merged PR [\[${msg.repoOrg}/${msg.repoName}\]: ${msg.prTitle} (#${msg.prNum})](${msg.prUrl}) in [${msg.commitSha}](${msg.commitUrl}).`;
mergeComment = sanitizeForTemplate(mergeComment);
var attachScript = `
// Add comment
let textarea = document.getElementById("comment");
textarea.value = "${mergeComment}";
textarea.focus();
`;
await browser.tabs.executeScript(newTab.id, {
code: attachScript
});
console.info('rob-bugson: done!');
}