-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.js
144 lines (123 loc) · 4.88 KB
/
main.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
const fs = require('fs');
const core = require('@actions/core');
const github = require('@actions/github');
const exec = require('@actions/exec');
const utilities = require('./utilities');
async function run() {
try {
core.info('Event name: ' + github.context.eventName);
// Get inputs
const wsDestinationUrl = core.getInput('ws-destination-url');
const wsApiKey = core.getInput('ws-api-key');
const wsUserKey = core.getInput('ws-user-key');
const wsProductKey = core.getInput('ws-product-key');
const debugMode = core.getInput('actions_step_debug');
const uaJarName = 'wss-unified-agent.jar';
const uaDownloadPath = 'https://github.com/whitesource/unified-agent-distribution/releases/latest/download/wss-unified-agent.jar';
// Validate inputs
if (wsApiKey == null || wsApiKey.trim().length < 20) {
core.setFailed('Invalid input: ws-api-key');
return;
} else if (wsUserKey == null || wsUserKey.trim().length < 20) {
core.setFailed('Invalid input: ws-user-key');
return;
} else if (wsDestinationUrl == null || wsDestinationUrl.trim().length === 0 ||
!wsDestinationUrl.startsWith('http') || !wsDestinationUrl.endsWith('/agent')) {
core.setFailed('Invalid input: ws-destination-url');
return;
}
let uaVars = [];
const payload = github.context.payload;
const packageType = payload.registry_package.package_type;
core.info('Package type: ' + packageType);
// If the package type is docker - pull it
if (packageType === 'docker') {
if (debugMode === 'true') {
// Docker version
await exec.exec('docker', ['-v']);
// List existing docker images
await exec.exec('docker', ['images']);
}
// Get the authenticated user of the gp token
const gpToken = core.getInput('gp-token');
if (gpToken == null || gpToken.trim().length === 0) {
core.setFailed('Invalid input: gp-token');
return;
}
const octokit = new github.GitHub(gpToken);
const { data: user } = await octokit.users.getAuthenticated();
const gpUser = user.login;
// Execute the docker login command
await exec.exec('docker', ['login', 'docker.pkg.github.com', '-u', gpUser, '-p', gpToken]);
// Create and execute the docker pull command
const packageName = payload.registry_package.name;
const packageVersion = payload.registry_package.package_version.version;
const repositoryFullName = payload.repository.full_name;
const packageUrl = 'docker.pkg.github.com/' + repositoryFullName.toLowerCase() + '/' + packageName + ':' + packageVersion;
await exec.exec('docker', ['pull', packageUrl]);
if (debugMode === 'true') {
// List existing docker images
await exec.exec('docker', ['images']);
}
uaVars = ['-jar', uaJarName,
'-wss.url', wsDestinationUrl,
'-apiKey', wsApiKey,
'-noConfig', 'true',
'-generateScanReport', 'true',
'-docker.scanImages', 'true',
'-docker.includeSingleScan', '.*' + packageName + '.*',
'-userKey', wsUserKey,
'-project', payload.registry_package.name];
// Else - the package type is not docker - download it
} else {
// Download all package files
for (let i = 0; i < payload.registry_package.package_version.package_files.length; i++) {
let packageFile = payload.registry_package.package_version.package_files[i];
let downloadLink = packageFile.download_url;
let downloadName = packageFile.name;
await utilities.download(downloadLink, downloadName);
}
uaVars = ['-jar', uaJarName,
'-d', '.',
'-wss.url', wsDestinationUrl,
'-apiKey', wsApiKey,
'-noConfig', 'true',
'-generateScanReport', 'true',
'-userKey', wsUserKey,
'-project', payload.registry_package.name];
}
if (wsProductKey != null && wsProductKey.trim().length > 20) {
uaVars.push('-productToken', wsProductKey);
}
if (debugMode === 'true') {
// List files in curr directory
await exec.exec('ls', ['-alF']);
}
// Download the UA
await utilities.download(uaDownloadPath, uaJarName);
// Run the UA
await exec.exec('java', uaVars);
// Get the location of the scan log file
let logFilePath = utilities.findSingleFile('./whitesource/', 'scan_report.json');
let logFileFolder = logFilePath.substr(0, logFilePath.lastIndexOf('/'));
// Set the output parameters
core.setOutput('scan-report-file-path', logFilePath);
core.setOutput('scan-report-folder-path', logFileFolder);
// Print the log file if needed
let isPrintScanReport = core.getInput('print-scan-report');
if (isPrintScanReport === 'true') {
core.info('Print scan report true');
if (logFilePath.length > 0) {
let scanReport = fs.readFileSync(logFilePath, 'utf8');
core.info('Scan report:\n' + scanReport);
} else {
core.warning('Scan report does not exist!');
}
} else {
core.info('Print scan report false');
}
} catch (error) {
core.setFailed(error.message);
}
}
run();