-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
204 lines (195 loc) · 9.21 KB
/
index.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
const jwt = require('json-web-token');
const request = require('request');
const crypto = require('crypto');
function callZapiCloud(METHOD, API_URL, CONTENT_TYPE, ACCESS_KEY, SECRET_KEY, USER, BODY) {
const hash = crypto.createHash('sha256');
const iat = new Date().getTime();
const exp = iat + 3600;
const BASE_URL = 'https://prod-api.zephyr4jiracloud.com/connect';
let RELATIVE_PATH = API_URL.split(BASE_URL)[1].split('?')[0];
let QUERY_STRING = API_URL.split(BASE_URL)[1].split('?')[1];
let CANONICAL_PATH;
if (QUERY_STRING) {
CANONICAL_PATH = `${METHOD}&${RELATIVE_PATH}&${QUERY_STRING}`;
} else {
CANONICAL_PATH = `${METHOD}&${RELATIVE_PATH}&`;
}
hash.update(CANONICAL_PATH);
let encodedQsh = hash.digest('hex');
let payload = {
'sub': USER,
'qsh': encodedQsh,
'iss': ACCESS_KEY,
'iat': iat,
'exp': exp
};
let token = jwt.encode(SECRET_KEY, payload, 'HS256', function(err, token) {
if (err) { console.error(err.name, err.message); }
else { return token; }
});
let options = {
'method': METHOD, 'url': API_URL,
'headers': {
'zapiAccessKey': ACCESS_KEY,
'Authorization': 'JWT ' + token,
'Content-Type': CONTENT_TYPE
},
'json': BODY
};
let result = createPromiseCall(false, options);
return result;
}
function createPromiseCall(debug, params) {
return new Promise(function(resolve, reject) {
request(params, function(error, response, body) {
if (error) return reject(error);
if (debug) {
console.log(params);
console.log(body);
}
resolve(body);
});
}).catch(function(e) { console.log(`An error had occured with the api call: "${e}"`); });
}
module.exports = {
getCycleIdFromCycleName: function(jiraProjectId, jiraProjectVersion, cycleName) {
return callZapiCloud('GET', `https://prod-api.zephyr4jiracloud.com/connect/public/rest/api/1.0/cycles/search?projectId=${jiraProjectId}&versionId=${jiraProjectVersion}`, 'text/plain', ...__ZAPIcreds).then(allCycles => {
let currentCycleId = findCycleByName(JSON.parse(allCycles), cycleName);
if (currentCycleId) {
return { projectId: jiraProjectId, versionId: jiraProjectVersion, id: currentCycleId };
} else {
return null;
}
function findCycleByName(allCycles, name) {
let id = false;
allCycles.forEach(item => {
item.name === name ? id = item.id : id = false;
});
return id;
}
});
},
getAllCycles: function(jiraProjectId, jiraProjectVersion) {
return callZapiCloud('GET', `https://prod-api.zephyr4jiracloud.com/connect/public/rest/api/1.0/cycles/search?expand=executionSummaries&projectId=${jiraProjectId}&versionId=${jiraProjectVersion}`, 'text/plain', ...__ZAPIcreds).then(d => JSON.parse(d).filter(a => a.name != 'Ad hoc'))
.then(allCycles => {
let allCyclesResult = [];
allCycles.forEach(a => {
let buildCycleObject = {};
buildCycleObject.name = a.name;
buildCycleObject.id = a.id;
buildCycleObject.totalExecuted = a.totalExecuted;
buildCycleObject.totalExecutions = a.totalExecutions;
buildCycleObject.execSummaries = (() => {
let resultArray = [];
a.executionSummaries.forEach(a => {
resultArray.push({
status: a.executionStatusName,
count: a.count
});
});
return resultArray;
})();
allCyclesResult.push(buildCycleObject);
});
return allCyclesResult;
});
},
zqlSearch: function(query) {
return callZapiCloud('POST', `https://prod-api.zephyr4jiracloud.com/connect/public/rest/api/1.0/zql/search?`, 'application/json', ...__ZAPIcreds, { 'zqlQuery': `${query}` }).then(searchResults => {
let result = {
totalTests: searchResults.totalCount,
tests: []
};
searchResults.searchObjectList.forEach(a => {
result.tests.push({
key: a.issueKey,
summary: a.issueSummary,
status: a.execution.status.name,
desc: a.issueDescription,
executionId: a.execution.id,
issueId: a.execution.issueId
});
});
return result;
});
},
createNewCycle: function(body, cycleId) {
if (cycleId) {
return callZapiCloud('POST', `https://prod-api.zephyr4jiracloud.com/connect/public/rest/api/1.0/cycle?clonedCycleId=${cycleId}`, 'application/json', ...__ZAPIcreds, body);
} else {
return callZapiCloud('POST', `https://prod-api.zephyr4jiracloud.com/connect/public/rest/api/1.0/cycle`, 'application/json', ...__ZAPIcreds, body);
}
},
createExecution: function(cycleId, projectId, versionId, issueId, testStatus, assignee) {
let body = { 'status': { 'id': testStatus }, 'projectId': projectId, 'issueId': issueId, 'cycleId': cycleId, 'versionId': versionId, 'assigneeType': 'assignee', 'assignee': assignee };
return callZapiCloud('POST', `https://prod-api.zephyr4jiracloud.com/connect/public/rest/api/1.0/execution`, 'application/json', ...__ZAPIcreds, body).then(createExecution => {
return callZapiCloud('PUT', `https://prod-api.zephyr4jiracloud.com/connect/public/rest/api/1.0/execution/${createExecution.execution.id}`, 'application/json', ...__ZAPIcreds, { 'status': { 'id': testStatus }, 'projectId': projectId, 'issueId': issueId, 'cycleId': cycleId, 'versionId': versionId });
});
},
executionStatusUpdate: function(projectId, cycleId, issueId, executionId, versionId, statusId) {
let payload = {"status":{"id":statusId},"id":executionId,"projectId":projectId,"issueId":issueId,"cycleId":cycleId,"versionId":versionId};
return callZapiCloud('PUT', `https://prod-api.zephyr4jiracloud.com/connect/public/rest/api/1.0/execution/${executionId}`, 'application/json', ...__ZAPIcreds, payload);
},
getExecutionStatuses: function() {
return callZapiCloud('GET', `https://prod-api.zephyr4jiracloud.com/connect/public/rest/api/1.0/execution/statuses`, 'application/json', ...__ZAPIcreds).then(getStatuses => JSON.parse(getStatuses).forEach(a => console.log(`${a.id} ${a.name} ${a.description}`)));
},
getExecutionInfo: function(issueId, projectId, cycleId, executionId) {
return callZapiCloud('GET', `https://prod-api.zephyr4jiracloud.com/connect/public/rest/api/1.0/stepresult/search?executionId=${executionId}&issueId=${issueId}`, 'application/text', ...__ZAPIcreds).then(getStepResults => {
let stepIds = [];
JSON.parse(getStepResults).stepResults.forEach(a => {
stepIds.push({ id: a.id, status: a.status.description });
});
stepIds.unshift('spacer');
return { stepIds: stepIds, executionId: executionId };
});
},
stepResultUpdate: function(stepResultId, issueId, executionId, status) {
let testStepData = { 'status': { 'id': status }, 'issueId': issueId, 'stepId': stepResultId, 'executionId': executionId };
return callZapiCloud('PUT', `https://prod-api.zephyr4jiracloud.com/connect/public/rest/api/1.0/stepresult/${stepResultId}`, 'application/json', ...__ZAPIcreds, testStepData);
},
createNewTestStep: function(testStep, testData, expectedResult, testId, projectId) {
let testStepData = { "step": testStep, "data": testData, "result": expectedResult };
return callZapiCloud('POST', `https://prod-api.zephyr4jiracloud.com/connect/public/rest/api/1.0/teststep/${testId}?projectId=${projectId}`, 'application/json', ...__ZAPIcreds, testStepData);
},
testStepUpdate: function(testStep, testData, expectedResult, stepNum, testId, projectId) {
let testStepData = {
'orderId': stepNum,
'issueId': testId,
'step': testStep,
'data': testData,
'result': expectedResult,
'createdBy': 'admin'
};
return callZapiCloud('POST', `https://prod-api.zephyr4jiracloud.com/connect/public/rest/api/1.0/teststep/${testId}?projectId=${projectId}`, 'application/json', ...__ZAPIcreds, testStepData);
},
deleteAllTestSteps: function(testId, projectId) {
return callZapiCloud('GET', `https://prod-api.zephyr4jiracloud.com/connect/public/rest/api/1.0/teststep/${testId}?projectId=${projectId}`, 'application/json', ...__ZAPIcreds).then(testSteps => {
let stepsArray = JSON.parse(testSteps);
(function deleteStep() {
if (stepsArray.length != 0) {
let step = stepsArray.shift();
return callZapiCloud('DELETE', `https://prod-api.zephyr4jiracloud.com/connect/public/rest/api/1.0/teststep/${testId}/${step.id}?projectId=${projectId}`, 'application/text', ...__ZAPIcreds).then(d => {
return deleteStep();
});
} else {
return 'Deleted OK';
}
})();
});
},
getServerInfo: function() {
return callZapiCloud('GET', `https://prod-api.zephyr4jiracloud.com/connect/public/rest/api/1.0/serverinfo`, 'application/text', ...__ZAPIcreds);
},
getLicenseInfo: function() {
return callZapiCloud('GET', `https://prod-api.zephyr4jiracloud.com/connect/public/rest/api/1.0/zlicense/addoninfo`, 'application/text', ...__ZAPIcreds);
},
getGeneralInfo: function() {
return callZapiCloud('GET', `https://prod-api.zephyr4jiracloud.com/connect/public/rest/api/1.0/config/generalinformation`, 'application/text', ...__ZAPIcreds);
},
getCyclesIssueIds: function(cycleId, versionId, projectId) {
return callZapiCloud('GET', `https://prod-api.zephyr4jiracloud.com/connect/public/rest/api/1.0/cycle/${cycleId}?expand=executionSummaries&projectId=${projectId}&versionId=${versionId}`, 'text/plain', ...__ZAPIcreds);
},
getTestSteps: function(issueId, projectId) {
return callZapiCloud('GET', `https://prod-api.zephyr4jiracloud.com/connect/public/rest/api/1.0/teststep/${issueId}?projectId=${projectId}`, 'application/json', ...__ZAPIcreds).then(step => JSON.parse(step));
}
};