-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
185 lines (159 loc) · 5.68 KB
/
main.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
178
179
180
181
182
183
184
185
function getJIRAFeed(callback, errorCallback){
var user = document.getElementById("user").value;
if(user == undefined || user.length < 1) {
setStatus('ERROR. Please Enter a User');
return;
}
var url = "https://jira.secondlife.com/activity?maxResults=50&streams=user+IS+" + user + "&providers=issues";
makeRequest(url, "").then(function(response) {
callback(url, response);
}).catch(function(errorMessage) {
errorCallback(errorMessage);
});
}
/**
* @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.
*/
function getQueryResults(searchTerm, callback, errorCallback) {
makeRequest(searchTerm, "json").then(function(response) {
callback(response);
}).catch(function(errorMessage) {
errorCallback(errorMessage);
});
}
async function checkProjectExists() {
try {
return await makeRequest("https://jira.secondlife.com/rest/api/2/project/SUN", "json");
} catch(errorMessage) {
setStatus('ERROR. ' + errorMessage);
}
}
function makeRequest(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;
});
}
/**
* @param {string} results - A list of results from a query.
* @param {function(result)} formatResult - called when a result needs formatting
*/
function renderResults(results, formatResult) {
var list = document.createElement('ul');
if (results) {
for (var i = 0; i < results.length; i++) {
var result = formatResult(results[i]);
var item = document.createElement('li');
item.innerHTML = result;
list.appendChild(item);
}
}
var resultDiv = document.getElementById('query-result');
if(list.childNodes.length > 0){
resultDiv.innerHTML = list.outerHTML;
} else {
setStatus('There are no activity results.');
}
resultDiv.hidden = false;
document.getElementById('results-header').hidden = false;
}
// utility
function buildJQL(project, status, inStatusFor) {
var url = "https://jira.secondlife.com/rest/api/2/search?jql=";
return url += `project=${project}+and+status=${status}+and+status+changed+to+${status}+before+-${inStatusFor}d&fields=id,status,key,assignee,summary&maxresults=100`;;
}
// utility
function domify(str) {
var dom = (new DOMParser()).parseFromString('<!doctype html><body>' + str,'text/html');
return dom.body.textContent;
}
// utility
function setStatus(message) {
document.getElementById('status').innerHTML = message;
document.getElementById('status').hidden = false
}
// Setup for the Ticket Status Query
function createQueryClickHandler() {
document.getElementById("query").onclick = function() {
var project = document.getElementById("project").value;
var status = document.getElementById("statusSelect").value;
var inStatusFor = document.getElementById("daysPast").value;
if (!project || !inStatusFor) {
if (!project) setStatus('ERROR. Please enter a project name.');
else if (!inStatusFor) setStatus('ERROR. Please set number of days.');
return;
}
var query = buildJQL(project, status, inStatusFor);
setStatus('Performing JIRA search for ' + query);
getQueryResults(query, function(response) {
setStatus('Query term: ' + query + '\n');
renderResults(response.issues, function(issue) {
return issue.key + ": " + issue.fields.summary
});
}, function(errorMessage) {
setStatus('ERROR. ' + errorMessage);
});
}
}
// Setup for the JIRA Activity Query
function createActivityFeedClickHandler() {
document.getElementById("feed").onclick = function() {
// get the xml feed
getJIRAFeed(function(url, xmlDoc) {
setStatus('Activity query: ' + url + '\n');
// render results
var feed = xmlDoc.getElementsByTagName('feed');
renderResults(feed[0].getElementsByTagName("entry"), function(entry) {
var html = entry.getElementsByTagName('title')[0].innerHTML;
var updated = entry.getElementsByTagName('updated')[0].innerHTML;
return new Date(updated).toLocaleString() + " - " + domify(html);
});
}, function(errorMessage) {
setStatus('ERROR. ' + errorMessage);
});
};
}
// Setup
document.addEventListener('DOMContentLoaded', function() {
// if logged in, setup listeners
checkProjectExists().then(function() {
//load saved options
loadOptions();
createQueryClickHandler();
createActivityFeedClickHandler();
}).catch(function(errorMessage) {
setStatus('ERROR. ' + errorMessage);
});
});