-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
108 lines (91 loc) · 3.33 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
const fs = require('fs');
const core = require('@actions/core');
async function run() {
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 projectTags = core.getInput('projecttags');
const autoCreate = core.getInput('autocreate') !== 'false';
const bomFilename = core.getInput('bomfilename');
const parent = core.getInput('parent');
const parentName = core.getInput('parentname');
const parentVersion = core.getInput('parentversion');
if (protocol !== "http" && protocol !== "https") {
throw 'protocol "' + protocol + '" not supported, must be one of: https, http'
}
if (project === "" && (projectName === "" || projectVersion === "")) {
throw 'project or projectName + projectVersion must be set'
}
if (!autoCreate && project === "") {
throw 'project can\'t be empty if autoCreate is false'
}
if (project === "" && (projectName === "" || projectVersion === "")) {
throw 'project or projectName + projectVersion must be set'
}
if ((parentName === "" && parentVersion !== "") || (parentName !== "" && parentVersion === "")) {
throw 'parentName + parentVersion must both be set'
}
core.info(`Reading BOM: ${bomFilename}...`);
const bomContents = fs.readFileSync(bomFilename);
let encodedBomContents = Buffer.from(bomContents).toString('base64');
if (encodedBomContents.startsWith('77u/')) {
encodedBomContents = encodedBomContents.substring(4);
}
let bomPayload;
if (autoCreate) {
bomPayload = {
projectName: projectName,
projectVersion: projectVersion,
autoCreate: autoCreate,
bom: encodedBomContents
}
if (projectTags) {
bomPayload.projectTags = projectTags.split(',').map(tag => ({name: tag.trim()}));
}
} else {
bomPayload = {
project: project,
bom: encodedBomContents
}
}
if (parent && parent.trim().length > 0) {
bomPayload.parentUUID = parent;
} else if (parentName && parentName.trim().length > 0 && parentVersion && parentVersion.trim().length > 0) {
bomPayload.parentName = parentName;
bomPayload.parentVersion = parentVersion;
}
const postData = JSON.stringify(bomPayload);
const requestOptions = {
method: 'PUT',
headers: {
'X-API-Key': apiKey,
'Content-Type': 'application/json',
},
body: postData
};
const url = new URL(`${protocol}://${serverHostname}`);
if (port) {
url.port = port;
}
url.pathname = '/api/v1/bom';
core.info(`Uploading to Dependency-Track server ${serverHostname}...`);
const response = await fetch(url.toString(), requestOptions);
if (response.ok) {
core.info('Finished uploading BOM to Dependency-Track server.');
} else {
const responseBody = await response.text();
if (responseBody) {
core.debug(responseBody);
}
core.setFailed('Failed response status code:' + response.status);
}
} catch (error) {
core.setFailed(error.message);
}
}
run();