-
Notifications
You must be signed in to change notification settings - Fork 1
/
jira.js
169 lines (154 loc) · 4.58 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
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
var request = require('superagent');
var _ = require('lodash');
var filesize = require('filesize');
var baseUrl = 'https://jira.roihu2016.fi/rest/';
var moment = require('moment');
function validateLogin(username, password, cb) {
request
.post(baseUrl + 'auth/1/session')
.send({
username: username,
password: password
})
.end(function(err, res) {
console.log(err, res);
if (err && res.status === 401) {
cb(new Error('Wrong username or password'));
} else if (err && res.status === 403) {
console.error('Login failed: ', res.body)
cb(new Error('You cannot log in at this time, please try again later.'));
} else if (err) {
console.error('Login failed: ', err);
cb(new Error('Login failed due to an unknown error.'))
} else {
cb(null, true);
}
});
}
function getToDo(username, password, cb) {
var jql = 'assignee=' + username +' and status="To Do" ORDER BY Rank';
getIssueListBySearch(jql, username, password, cb);
}
function getInProgress(username, password, cb) {
var jql = 'assignee=' + username +' and status="In Progress" ORDER BY Rank';
getIssueListBySearch(jql, username, password, cb);
}
function getDone(username, password, cb) {
var jql = 'assignee=' + username + ' and status="Done" ORDER BY Rank';
getIssueListBySearch(jql, username, password, cb);
}
function getIssueListBySearch(jql, username, password, cb) {
callJira('api/latest/search?maxResults=500&jql=' + jql, username, password, function (err, body) {
if (err) {
return cb(err);
}
_.map(body.issues, function(issue) {
if (issue.fields.duedate != undefined) {
issue.fields.duedate = moment(issue.fields.duedate, 'YYYY-MM-DD').format('DD.MM.YYYY');
}
});
cb(null, body.issues);
});
}
function getIssue(key, username, password, cb) {
callJira('api/latest/issue/' + key, username, password, function (err, body) {
if (err) {
return cb(err);
}
if (body.fields.duedate != undefined) {
body.fields.duedate = moment(body.fields.duedate, 'YYYY-MM-DD').format('DD.MM.YYYY');
}
_.map(body.fields.comment.comments, function(comment) {
moment.locale('fi')
comment.fromNow=moment(comment.created).fromNow();
comment.created=moment(comment.created).format('DD.MM.YYYY HH:mm:ss');
});
_.map(body.fields.attachment, function(attachment) {
attachment.size = filesize(attachment.size);
});
cb(null, body);
});
}
function callJira(path, username, password, cb) {
if (!username || !password) {
return _.defer(function() {
var err = new Error('No username or password supplied');
err.status = 401;
cb(err);
})
}
request
.get(baseUrl + path)
.auth(username, password)
.end(function(err, res) {
if (err) {
return cb(err);
}
cb(null, res.body);
});
}
function getAttachmentThumb(attachmentId, username, password, cb) {
if (!username || !password) {
return _.defer(function() {
var err = new Error('No username or password supplied');
err.status = 401;
cb(err);
})
}
request
.get('https://jira.roihu2016.fi/secure/thumbnail/' + attachmentId + '/')
.auth(username, password)
.end(function (err, res) {
cb(null, res);
});
}
function transitionIssue(issueKey, transitionId, username, password, cb) {
if (!username || !password) {
return _.defer(function() {
var err = new Error('No username or password supplied');
err.status = 401;
cb(err);
})
}
request
.post(baseUrl + 'api/latest/issue/' + issueKey + '/transitions')
.auth(username, password)
.send({ transition: { id: transitionId }})
.end(function(err, res) {
if (err) {
return cb(err);
}
console.log(res.body);
cb(null, res.body);
});
}
function addComment(comment, issueKey, username, password, cb) {
if (!username || !password) {
return _.defer(function() {
var err = new Error('No username or password supplied');
err.status = 401;
cb(err);
})
}
request
.post(baseUrl + 'api/latest/issue/' + issueKey + '/comment')
.auth(username, password)
.send({ body: comment})
.end(function(err, res) {
if (err) {
return cb(err);
}
console.log(res.body);
cb(null, res.body);
});
}
module.exports = {
validateLogin: validateLogin,
getToDo: getToDo,
getInProgress: getInProgress,
getDone: getDone,
getIssue: getIssue,
transitionIssue: transitionIssue,
addComment: addComment,
getAttachmentThumb: getAttachmentThumb
};