forked from interview-github/chrome_extension
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
173 lines (149 loc) · 6.19 KB
/
main.js
File metadata and controls
173 lines (149 loc) · 6.19 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
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
function getJIRAFeed(callback, errorCallback){
var user = document.getElementById("user").value;
if(user == undefined) return;
var url = "https://jira.secondlife.com/activity?maxResults=50&streams=user+IS+"+user+"&providers=issues";
make_request(url, "").then(function(response) {
// empty response type allows the request.responseXML property to be returned in the makeRequest call
callback(url, response);
}, errorCallback);
}
/**
* @param {string} searchTerm - Search term for JIRA Query.
* @param {function(string)} callback - Called when the query results have been
* formatted for rendering.
* @param {function(string)} errorCallback - Called when the query or call fails.
*/
async function getQueryResults(s, callback, errorCallback) {
try {
var response = await make_request(s, "json");
callback(createHTMLElementResult(response));
} catch (error) {
errorCallback(error);
}
}
function make_request(url, responseType) {
return new Promise(function(resolve, reject) {
var req = new XMLHttpRequest();
req.open('GET', url);
req.responseType = responseType;
req.onload = function() {
var response = responseType ? req.response : req.responseXML;
if(response && response.errorMessages && response.errorMessages.length > 0){
reject(response.errorMessages[0]);
return;
}
resolve(response);
};
// Handle network errors
req.onerror = function() {
reject(Error("Network Error"));
}
req.onreadystatechange = function() {
if(req.readyState == 4 && req.status == 401) {
reject("You must be logged in to JIRA to see this project.");
}
}
// Make the request
req.send();
});
}
function loadOptions(){
chrome.storage.sync.get({
project: 'Sunshine',
user: 'nyx.linden'
}, function(items) {
document.getElementById('project').value = items.project;
document.getElementById('user').value = items.user;
});
}
function buildJQL(callback) {
var callbackBase = "https://jira.secondlife.com/rest/api/2/search?jql=";
var project = document.getElementById("project").value;
var status = document.getElementById("statusSelect").value;
var inStatusFor = document.getElementById("daysPast").value
var fullCallbackUrl = callbackBase;
fullCallbackUrl += `project=${project}+and+status=${status}+and+status+changed+to+${status}+before+-${inStatusFor}d&fields=id,status,key,assignee,summary&maxresults=100`;
callback(fullCallbackUrl);
}
function createHTMLElementResult(response){
//
// Create HTML output to display the search results.
// results.json in the "json_results" folder contains a sample of the API response
// hint: you may run the application as well if you fix the bug.
//
return '<p>There may be results, but you must read the response and display them.</p>';
}
// utility
function domify(str){
var dom = (new DOMParser()).parseFromString('<!doctype html><body>' + str,'text/html');
return dom.body.textContent;
}
function checkProjectExists(){
try {
return await make_request("https://jira.secondlife.com/rest/api/2/project/SUN", "json");
} catch (errorMessage) {
document.getElementById('status').innerHTML = 'ERROR. ' + errorMessage;
document.getElementById('status').hidden = false;
}
}
// Setup
document.addEventListener('DOMContentLoaded', function() {
// if logged in, setup listeners
checkProjectExists().then(function() {
//load saved options
loadOptions();
// query click handler
document.getElementById("query").onclick = function(){
// build query
buildJQL(function(url) {
document.getElementById('status').innerHTML = 'Performing JIRA search for ' + url;
document.getElementById('status').hidden = false;
// perform the search
getQueryResults(url, function(return_val) {
// render the results
document.getElementById('status').innerHTML = 'Query term: ' + url + '\n';
document.getElementById('status').hidden = false;
var jsonResultDiv = document.getElementById('query-result');
jsonResultDiv.innerHTML = return_val;
jsonResultDiv.hidden = false;
}, function(errorMessage) {
document.getElementById('status').innerHTML = 'ERROR. ' + errorMessage;
document.getElementById('status').hidden = false;
});
});
}
// activity feed click handler
document.getElementById("feed").onclick = function(){
// get the xml feed
getJIRAFeed(function(url, xmlDoc) {
document.getElementById('status').innerHTML = 'Activity query: ' + url + '\n';
document.getElementById('status').hidden = false;
// render result
var feed = xmlDoc.getElementsByTagName('feed');
var entries = feed[0].getElementsByTagName("entry");
var list = document.createElement('ul');
for (var index = 0; index < entries.length; index++) {
var html = entries[index].getElementsByTagName("title")[0].innerHTML;
var updated = entries[index].getElementsByTagName("updated")[0].innerHTML;
var item = document.createElement('li');
item.innerHTML = new Date(updated).toLocaleString() + " - " + domify(html);
list.appendChild(item);
}
var feedResultDiv = document.getElementById('query-result');
if(list.childNodes.length > 0){
feedResultDiv.innerHTML = list.outerHTML;
} else {
document.getElementById('status').innerHTML = 'There are no activity results.';
document.getElementById('status').hidden = false;
}
feedResultDiv.hidden = false;
}, function(errorMessage) {
document.getElementById('status').innerHTML = 'ERROR. ' + errorMessage;
document.getElementById('status').hidden = false;
});
};
}).catch(function(errorMessage) {
document.getElementById('status').innerHTML = 'ERROR. ' + errorMessage;
document.getElementById('status').hidden = false;
});
});