-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
69 lines (57 loc) · 2.69 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
const core = require('@actions/core');
const github = require('@actions/github');
const axios = require('axios');
async function waitForScanStatus(emboldUrl, token, repoUid, scanId) {
try {
// Check for upto 1 min if the scan enters processing state
let status = null;
for (let i = 0; i < 30; ++i) {
status = await axios.get(emboldUrl + '/api/v1/repositories/' + repoUid + '/scans/' + scanId + '/status', {
headers: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/x-www-form-urlencoded' }
});
if (status.data.scanStatus !== 'FAIL') {
if (status.data.currentStep === 'SCANBOX_SCHEDULED_SUCCESS') {
// Scan is scheduled, so all good
console.log(`Scan with id ${scanId} is started successfully`);
return;
}
} else {
// Scan failed, so return with failure
throw new Error(`Scan with id ${scanId} failed with error: ${status.data.currentStep}`);
}
// else wait some more...
await new Promise(r => setTimeout(r, 2000));
}
// If we reached here we still didn't get the expected status.
// This could be because the scm sync is taking longer (e.g. large repo), so we can still return
if (status && status.data.currentStep === 'UPDATING_SOURCES') {
console.log(`Scan with id ${scanId} is updating sources`);
}
} catch (error) {
console.error(`Error while checking scan status: ${error.message} for scanId: ${scanId}`)
throw error;
}
}
async function launchScan(emboldUrl, token, repoUid, branch) {
console.log(`Launching Embold scan for repo: ${repoUid} at: ${emboldUrl}`);
try {
let res = await axios.post(emboldUrl + '/api/v1/repositories/' + repoUid + '/scan', `repoBranchOrTag=${branch}`, {
headers: { 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/x-www-form-urlencoded' }
});
// Check back on scan status with the status API until we get the expected state
await waitForScanStatus(emboldUrl, token, repoUid, res.data.scanId);
} catch (error) {
console.log(error);
core.setFailed(error.message);
}
}
try {
const emboldUrl = core.getInput('emboldUrl');
const token = core.getInput('emboldToken');
const repoUid = core.getInput('emboldRepoUid');
// const payload = JSON.stringify(github.context.payload, undefined, 2);
launchScan(emboldUrl, token, repoUid, github.context.payload.ref);
core.setOutput("status", "SUCCESS");
} catch (error) {
core.setFailed(error.message);
}