Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
78 lines (65 loc) · 2.29 KB
/
Copy pathindex.js
File metadata and controls
78 lines (65 loc) · 2.29 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
const fs = require('fs');
const http = require('http');
const https = require('https');
const core = require('@actions/core');
try {
const serverHostname = core.getInput('serverhostname');
const port = core.getInput('port');
const protocol = core.getInput('protocol');
const apiKey = core.getInput('apikey');
const project = core.getInput('project');
const projectName = core.getInput('projectname');
const projectVersion = core.getInput('projectversion');
const autoCreate = core.getInput('autocreate') != 'false';
const bomFilename = core.getInput('bomfilename');
if (protocol != "http" && protocol != "https") {
throw 'protocol "' + protocol + '" not supported, must be one of: https, http'
}
const client = (protocol == "http") ? http : https
if (project == "" && (projectName == "" || projectVersion == "")) {
throw 'project or projectName + projectVersion must be set'
}
console.log(`Reading BOM: ${bomFilename}...`);
const bomContents = fs.readFileSync(bomFilename);
let encodedBomContents = Buffer.from(bomContents).toString('base64');
if (encodedBomContents.startsWith('77u/')) {
encodedBomContents = encodedBomContents.substring(4);
}
const bomPayload = {
project: project,
projectName: projectName,
projectVersion: projectVersion,
autoCreate: autoCreate,
bom: encodedBomContents
}
const postData = JSON.stringify(bomPayload);
const requestOptions = {
hostname: serverHostname,
port: port,
protocol: protocol + ':',
path: '/api/v1/bom',
method: 'PUT',
headers: {
'X-API-Key': apiKey,
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData)
}
}
console.log(`Uploading to Dependency-Track server ${serverHostname}...`);
const req = client.request(requestOptions, (res) => {
console.log('Response status code:', res.statusCode);
if (res.statusCode >= 200 && res.statusCode < 300) {
console.log('Finished uploading BOM to Dependency-Track server.')
} else {
core.setFailed('Failed response status code:' + res.statusCode);
}
});
req.on('error', (e) => {
console.error(`Problem with request: ${e.message}`);
core.setFailed(e.message);
});
req.write(postData);
req.end();
} catch (error) {
core.setFailed(error.message);
}