forked from ehues/slack-workitems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwi.js
More file actions
176 lines (144 loc) · 5.38 KB
/
Copy pathwi.js
File metadata and controls
176 lines (144 loc) · 5.38 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
174
175
176
var request = require('request');
var Promise = require('promise');
function WorkItems(repo, username, password) {
this.req = request.defaults({
jar: request.jar(),
followAllRedirects: true,
rejectUnauthorized: false
});
this.repo = repo;
this.username = username;
this.password = password;
}
WorkItems.prototype.fetchOSLC = function(workitemNumber, avoidLogin) {
var self = this;
return new Promise(function(resolve, reject) {
self.req.get(self.repo + "/oslc/workitems/" + workitemNumber + ".json",
function (err, resp, body) {
console.log(err);
if (err) {
reject(err);
return;
}
if (resp.statusCode == 401) {
// login and retry
if (avoidLogin) {
reject("Log in failure when fetching work item.");
return;
}
return self.login().then(function() {
console.log("after login");
self.fetchOSLC(workitemNumber, true).then(resolve, reject);
}, reject);
}
if (resp.statusCode == 200) {
resolve(JSON.parse(body));
}
else {
reject("Couldn't fetch work item. Got a " + resp.statusCode + " during request.");
}
}
);
});
};
WorkItems.prototype.login = function() {
var self = this;
return new Promise(function(resolve, reject) {
self.req.get(self.repo + "/authenticated/identity", function(err, resp, body) {
if (err) {
return reject(err);
}
if (resp.statusCode != 401) {
console.log("/auth/id");
return reject("Error during login. Unexpected status code " + resp.statusCode + ".");
}
self.req.post(self.repo + "/authenticated/j_security_check", {
form: {
j_username: self.username,
j_password: self.password
}
}, function(err, resp, body) {
if (resp.statusCode != 404 && resp.statusCode != 500) {
console.log("/auth/j_sec");
return reject("Unexpected status code after login " + resp.statusCode + ".");
}
resolve(self);
});
});
});
};
WorkItems.prototype.queryForCategories = function(paId, perCategoryCallback) {
var self = this;
return new Promise(function(resolve, reject) {
var uri = self.repo + "/oslc/categories?oslc_cm.query=";
uri += encodeURIComponent("rtc_cm:projectArea=\"" + paId + "\"");
console.log(uri);
var catsHandled = 0;
var resultGatherer;
resultGatherer = function(err, resp, body) {
if (err) {
return reject(err);
}
if (resp.statusCode != 200) {
console.log("status code " + resp.statusCode);
return reject("Error during login. Unexpected status code " + resp.statusCode + ".");
}
var answer = JSON.parse(body);
answer["oslc_cm:results"].forEach(perCategoryCallback);
catsHandled += answer["oslc_cm:results"].length;
console.log("Handled " + catsHandled + " of " + answer["oslc_cm:totalCount"]);
if (answer["oslc_cm:next"]) {
self.req.get(answer["oslc_cm:next"], {headers: {
'accept': 'application/json'
}}, resultGatherer);
}
else {
resolve();
}
};
self.req.get(uri, {headers: {
'accept': 'application/json'
}}, resultGatherer);
});
}
/**
* Fire the callback once for each workitem. Returns a promise that is resolved
* when all of the work items have been queried
*/
WorkItems.prototype.queryForWorkItems = function(paId, queryString, perWorkitemCallback, properties) {
var self = this;
return new Promise(function(resolve, reject) {
var uri = self.repo + "/oslc/contexts/" + paId + "/workitems?oslc_cm.query=";
uri += encodeURIComponent(queryString);
if (properties) {
uri += "&oslc_cm.properties=" + encodeURIComponent(properties.join());
}
var wisHandled = 0;
var resultGatherer;
resultGatherer = function(err, resp, body) {
if (err) {
return reject(err);
}
if (resp.statusCode != 200) {
console.log("status code " + resp.statusCode);
return reject("Error during login. Unexpected status code " + resp.statusCode + ".");
}
var answer = JSON.parse(body);
answer["oslc_cm:results"].forEach(perWorkitemCallback);
wisHandled += answer["oslc_cm:results"].length;
console.log("Handled " + wisHandled + " of " + answer["oslc_cm:totalCount"]);
if (answer["oslc_cm:next"]) {
self.req.get(answer["oslc_cm:next"], {headers: {
'accept': 'application/json'
}}, resultGatherer);
}
else {
resolve();
}
};
self.req.get(uri, {headers: {
'accept': 'application/json'
}}, resultGatherer);
});
}
module.exports = WorkItems;