Skip to content

Commit 5457543

Browse files
committed
WIP Jira feature
1 parent f803467 commit 5457543

File tree

5 files changed

+134
-39
lines changed

5 files changed

+134
-39
lines changed

_locales/en/messages.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
},
77

88
"extensionDescription": {
9-
"message": "Opens selected bug numbers in bugzilla.mozilla.org (and optionally in GitHub).",
9+
"message": "Opens selected bug numbers in bugzilla.mozilla.org (and optionally in GitHub and Jira).",
1010
"description": "Description of the add-on."
1111
},
1212

1313
"contextMenuItem": {
1414
"message": "Open bugs in BMO",
15-
"description": "Title of single context menu item that opens the selected text as bugs in BMO when clicked and no GitHub repos are specified."
15+
"description": "Title of single context menu item that opens the selected text as bugs in BMO when clicked and no GitHub repos or Jira projects are specified."
1616
},
1717

1818
"contextMenuItem1": {
@@ -23,6 +23,11 @@
2323
"contextMenuItem2": {
2424
"message": "in GitHub",
2525
"description": "Title of context menu item that opens the selected text as bugs in GitHub (using options) when clicked."
26+
},
27+
28+
"contextMenuItem3": {
29+
"message": "in Jira",
30+
"description": "Title of context menu item that opens the selected text as item in Jira (using options) when clicked."
2631
}
2732

2833
}

bugme.js

Lines changed: 98 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
'use strict'
22

3-
var repostring = '', submenus = [];
3+
let repostring = '',
4+
projectstring = '',
5+
ghSubmenus = [],
6+
jiraSubmenus = [];
47

58
//browser.runtime.onStartup.addListener(loadOptions);
69

@@ -21,17 +24,24 @@ browser.runtime.onMessage.addListener(handleMessage);
2124
Load options data and populate repostring
2225
*/
2326
function loadOptions() {
24-
var gettingItem = browser.storage.local.get('repos');
25-
gettingItem.then((res) => {
27+
let gettingItemRepo = browser.storage.local.get('repos'),
28+
gettingItemProject = browser.storage.local.get('projects');
29+
gettingItemRepo.then((res) => {
2630
repostring = res.repos || '';
27-
// console.log("repostring follows:");
28-
// console.log(repostring);
31+
// console.log(`repostring follows: ${repostring}`);
2932
if (repostring.length) {
3033
recreateMenu();
3134
createSubmenus();
3235
}
3336
});
34-
// repostring = "[Monitor](https://github.com/mozilla/blurts-server/); [Lockwise Android](https://github.com/mozilla-lockwise/lockwise-android/); [Lockwise iOS](https://github.com/mozilla-lockwise/lockwise-ios/)";
37+
gettingItemProject.then((res) => {
38+
projectstring = res.projects || '';
39+
// console.log(`projectstring follows: ${projectstring}`);
40+
if (projectstring.length) {
41+
recreateMenu();
42+
createSubmenus();
43+
}
44+
});
3545
}
3646

3747

@@ -58,9 +68,13 @@ Remove all submenus because the options may redefine them
5868
function removeSubmenus() {
5969
browser.menus.remove("bugme");
6070
browser.menus.remove("bugme_gh");
61-
for (var i=0; i<submenus.length; i++) {
71+
for (var i=0; i<ghSubmenus.length; i++) {
6272
browser.menus.remove("bugme_gh-" + i);
6373
};
74+
browser.menus.remove("bugme_jira");
75+
for (var i=0; i<jiraSubmenus.length; i++) {
76+
browser.menus.remove("bugme_jira-" + i);
77+
};
6478
}
6579

6680

@@ -94,25 +108,46 @@ function recreateMenu() {
94108
Create submenu if options are set
95109
*/
96110
function createSubmenus() {
97-
browser.menus.create({
98-
id: "bugme_gh",
99-
title: browser.i18n.getMessage("contextMenuItem2"),
100-
contexts: ["selection"]
101-
});
102-
var subitems = repostring.split(/; ?/);
103-
var re = RegExp('\\[([^\\]]+)\\]\\(([^\\)]+)\\)', 'g'); // [name](url)
104-
for (var i=0; i<subitems.length; i++) {
105-
var options;
106-
while ((options = re.exec(subitems[i])) !== null) {
107-
submenus[i] = { name: options[1], url: options[2] };
108-
browser.menus.create({
109-
id: "bugme_gh-" + i,
110-
parentId: "bugme_gh",
111-
title: options[1],
112-
contexts: ["selection"]
113-
});
114-
};
111+
// github repos
112+
browser.menus.create({
113+
id: "bugme_gh",
114+
title: browser.i18n.getMessage("contextMenuItem2"),
115+
contexts: ["selection"]
116+
});
117+
let subitems = repostring.split(/; ?/);
118+
let re = RegExp('\\[([^\\]]+)\\]\\(([^\\)]+)\\)', 'g'); // [name](url)
119+
for (let i=0; i<subitems.length; i++) {
120+
let options;
121+
while ((options = re.exec(subitems[i])) !== null) {
122+
ghSubmenus[i] = { name: options[1], url: options[2] };
123+
browser.menus.create({
124+
id: "bugme_gh-" + i,
125+
parentId: "bugme_gh",
126+
title: options[1],
127+
contexts: ["selection"]
128+
});
115129
};
130+
};
131+
// jira projects
132+
browser.menus.create({
133+
id: "bugme_jira",
134+
title: browser.i18n.getMessage("contextMenuItem3"),
135+
contexts: ["selection"]
136+
});
137+
subitems = projectstring.split(/; ?/);
138+
re = RegExp('\\[([^\\]]+)\\]\\(([^\\)]+)\\)', 'g'); // [name](url)
139+
for (let i=0; i<subitems.length; i++) {
140+
let options;
141+
while ((options = re.exec(subitems[i])) !== null) {
142+
jiraSubmenus[i] = { name: options[1], url: options[2] };
143+
browser.menus.create({
144+
id: "bugme_jira-" + i,
145+
parentId: "bugme_jira",
146+
title: options[1],
147+
contexts: ["selection"]
148+
});
149+
};
150+
};
116151
}
117152

118153

@@ -123,8 +158,9 @@ createMenu();
123158
The click event listener
124159
*/
125160
browser.menus.onClicked.addListener((info, tab) => {
126-
var reBMO = RegExp('((?!\\d{3,4}-\\d{2}-\\d{2})\\d{4,7})', 'g');
127-
var reGH = RegExp('((?!\\d{3,4}-\\d{2}-\\d{2})\\d{1,7})', 'g');
161+
let reJIRA = RegExp('((?!\\d{4}-\\d{2}-\\d{2})[A-Z]{3,}-\\d{1,5})', 'g'); // XYZ-#
162+
let reGH = RegExp('((?!\\d{4}-\\d{2}-\\d{2})\\d{1,7})', 'g'); // #######
163+
let reBMO = RegExp('((?!\\d{4}-\\d{2}-\\d{2})\\d{3,7})', 'g'); // ####
128164
if (info.menuItemId == "bugme") {
129165
/* Don't capture numbers from yyyy-mm-dd, but
130166
there are open bugs matching \d{3}. */
@@ -158,6 +194,41 @@ browser.menus.onClicked.addListener((info, tab) => {
158194
queryTabs.then(insertTab, onError);
159195
}
160196
} else {
197+
if (info.parentMenuItemId == "bugme_jira") {
198+
/* Handle cases where submenu items of Jira are clicked */
199+
/* Don't capture numbers from yyyy-mm-dd, but
200+
Jira keys can be as small as \d{1}, though they
201+
should all be preceded by [A-Z]{3,}-. */
202+
// you are here
203+
var issues = [],
204+
temp,
205+
i = parseInt(info.menuItemId.split('-')[1], 10);
206+
var url = submenus[i].url;
207+
if (url.slice(-1) != "/") { // terminating /
208+
url += "/";
209+
}
210+
while ((temp = reJIRA.exec(info.selectionText)) !== null) {
211+
if (issues.indexOf(temp[0]) < 0) {
212+
issues.push(temp[0]);
213+
}
214+
}
215+
if (issues.length > 0) {
216+
function onError(error) {
217+
console.log(`Error: ${error}`);
218+
}
219+
function insertTab(tabs) {
220+
for (let tab of tabs) {
221+
var newIndex = tab.index + 1;
222+
browser.tabs.create({url: `${url}`, index: newIndex});
223+
}
224+
}
225+
switch (issues.length) {
226+
case 1:
227+
console.log("case 1");
228+
url += // you are here
229+
}
230+
}
231+
}
161232
if (info.parentMenuItemId == "bugme_gh") {
162233
/* Handle cases where submenu items of GH are clicked */
163234
/* Don't capture numbers from yyyy-mm-dd, but

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"manifest_version": 2,
44
"name": "__MSG_extensionName__",
55
"description": "__MSG_extensionDescription__",
6-
"version": "1.2.1",
6+
"version": "1.2.2",
77
"default_locale": "en",
88
"applications": {
99
"gecko": {

options.html

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,16 @@
3030
<body>
3131
<p>You must have access to any repositories you specify here in order for these options to work. Enter the name path to repos in Markdown ([name](url)), using semicolons to separate multiple options.</p>
3232
<form>
33-
<label>GitHub Repository options</label>
33+
<label>GitHub repository options</label>
3434
<textarea id="repostring"></textarea>
35+
<input id="currentrepostring" type="hidden" value="" />
36+
37+
<label>Jira project options</label>
38+
<textarea id="projectstring"></textarea>
39+
<input id="currentprojectstring" type="hidden" value="" />
40+
3541
<button type="submit">Save</button>
36-
<input id="currentstring" type="hidden" value="" />
37-
</form>
42+
</form>
3843
<script src="options.js"></script>
3944
</body>
4045

options.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,39 @@
11
function saveOptions(e) {
2-
var entry = document.querySelector("#repostring").value,
3-
saved = document.querySelector("#currentstring").value;
4-
if (entry.trim() != saved.trim()) {
2+
let repoEntry = document.querySelector("#repostring").value,
3+
repoSaved = document.querySelector("#currentrepostring").value;
4+
if (repoEntry.trim() != repoSaved.trim()) {
55
browser.storage.local.set({
66
repos: document.querySelector("#repostring").value
77
});
88
}
9+
let projectEntry = document.querySelector("#repostring").value,
10+
projectSaved = document.querySelector("#currentrepostring").value;
11+
if (projectEntry.trim() != projectSaved.trim()) {
12+
browser.storage.local.set({
13+
projects: document.querySelector("#repostring").value
14+
});
15+
}
916
browser.runtime.sendMessage({
1017
trigger: "trigger reload"
1118
});
1219
e.preventDefault();
1320
}
1421

1522
function restoreOptions() {
16-
var storageItem = browser.storage.local.get('repos');
17-
storageItem.then((res) => {
23+
let storageItemRepo = browser.storage.local.get('repos'),
24+
storageItemProject = browser.storage.local.get('projects');
25+
storageItemRepo.then((res) => {
1826
if (res.repos != undefined) {
19-
document.querySelector("#currentstring").setAttribute("value", res.repos);
27+
document.querySelector("#currentrepostring").setAttribute("value", res.repos);
2028
document.querySelector("#repostring").innerText = res.repos;
2129
}
2230
});
31+
storageItemProject.then((res) => {
32+
if (res.projects != undefined) {
33+
document.querySelector("#currentprojectstring").setAttribute("value", res.projects);
34+
document.querySelector("#projectstring").innerText = res.projects;
35+
}
36+
});
2337
}
2438

2539
document.addEventListener('DOMContentLoaded', restoreOptions);

0 commit comments

Comments
 (0)