-
Notifications
You must be signed in to change notification settings - Fork 0
/
getWabbiGatePass.js
51 lines (44 loc) · 2.05 KB
/
getWabbiGatePass.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
const bent = require('bent');
/**
* Given information to access a wabbi gate endpoint and an array of ticket keys
* associated with the gate, returns the gate pass status
* @param {string} wabbiHost scheme and host support for wabbi auth services.
* @param {string} wabbiGateToken Authentication token to access wabbi auth endpoint
* @param {string} wabbiProjectId Id of project as identified in the wabbi database
* @param {string} wabbiGateId Id of Wabbi gate associated with ticket keys
* @param {Array.<string>} ticketKeys ticket keys associated with wabbi gate
* @returns {'PASSED' | 'FAILED' | undefined} wabbi gate pass status. The status
* is undefined if not ticket keys are provided.
*/
const getWabbiGatePass = async (wabbiHost, wabbiGateToken,
wabbiProjectId, wabbiGateId, ticketKeys) => {
// if ticket keys array is empty or does not exist, the gate pass status is undefined
if (!Array.isArray(ticketKeys) || !ticketKeys.length) {
return undefined;
}
// Define wabbi gates endpoint
const authenticateUrl = `${wabbiHost}/auth/refresh`;
const postAuthenticate = bent(authenticateUrl, 'POST', 'json');
const gatesUrl = `${wabbiHost}/api/projects/${wabbiProjectId}/security-gates/${wabbiGateId}/passes`;
const postGates = bent(gatesUrl, 'POST', 'json');
// Define authentication header for authenticate endpoint
const authenticateHeader = {
Authorization: `Bearer ${wabbiGateToken}`
};
// Access wabbi gate with Jira Ticket keys info and get gate status
let result = await postAuthenticate(null, {}, authenticateHeader);
const tokenHeader = {
'Content-Type': 'application/json',
'Content-Length': 0,
Authorization: `Bearer ${result.accessToken}`
};
// Stringiffy the ticket key array for wabbi gates pass request body
// example ['AB-1', 'AB-2', 'AB-3'] becomes '["AB-1","AB-2","AB-3"]
const gatesBody = {
ticketKeys: `["${ticketKeys.join('","')}"]`
};
result = await postGates(null, gatesBody, tokenHeader);
let status = result && result.lastPass ? result.lastPass.status : undefined;
return status;
};
module.exports = getWabbiGatePass;