-
Notifications
You must be signed in to change notification settings - Fork 3
/
jira.js
76 lines (69 loc) · 2.98 KB
/
jira.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
var rp = require("request-promise");
var _ = require("lodash");
var Promise = require("bluebird");
var config = require('./config');
function getJson(uri) {
return rp({
uri: uri,
json: true
});
}
function postJson(uri, postBody) {
return rp({
method: "POST",
uri: uri,
body: postBody,
json: true
});
}
module.exports.getIssue = function (key) {
return getJson("https://issues.openmrs.org/rest/api/2/issue/" + key + "?fields=summary,description,status,resolution,assignee,labels").then(function (issue) {
return _.pick(issue.fields, [
"summary",
"description",
"status",
"resolution",
"assignee",
"labels"
]);
});
};
module.exports.queryOnePage = function (jql, opts) {
var body = _.extend({
jql: jql,
maxResults: 5,
fields: ["summary", "status", "assignee", "labels", "updated"]
}, opts);
return postJson("https://issues.openmrs.org/rest/api/2/search", body);
}
module.exports.query = function (jql, opts) {
var body = _.extend({
jql: jql,
maxResults: 5,
fields: ["summary", "status", "assignee", "labels", "updated"]
}, opts);
var first = postJson("https://issues.openmrs.org/rest/api/2/search", body);
return Promise.join(first,
first.then(function (response) {
var morePages = [];
for (var st = response.startAt + response.maxResults; st < response.total; st += response.maxResults) {
morePages.push(st);
}
return Promise.map(morePages, function (startAt) {
console.log("Getting another page: " + startAt);
return postJson("https://issues.openmrs.org/rest/api/2/search", _.extend({
jql: jql,
maxResults: 5,
fields: ["summary", "status", "assignee", "labels", "updated"]
}, opts, {startAt: startAt}));
})
}),
function (first, rest) {
var pages = _.flatten([first, rest]);
var issues = _.chain(pages).map("issues").flatten().value();
return {
issues: issues,
total: issues.length
}
});
}