This repository has been archived by the owner on Feb 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
199 lines (179 loc) · 5.56 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const core = require("@actions/core");
const base64 = require("base-64");
const fetch = require("node-fetch");
const FormData = require("form-data");
const fs = require("fs");
const yaml = require("js-yaml");
// const testFolder = "./";
function createConnectorCallInit(host, jwtToken) {
return {
host: host,
path: "/services/cicd-connector/v2/deployment",
method: "POST",
protocol: "https:",
headers: {
Authorization: `Bearer ` + jwtToken,
},
};
}
async function sendInfoToAPI(host, jwtToken, {manifestJSON, stage, version, dependenciesFile, dependencyManager}) {
const options = createConnectorCallInit(host, jwtToken);
const formData = new FormData();
// stage should not be null
stage = stage ? stage : "";
formData.append("manifest", manifestJSON);
formData.append("dependencies", dependenciesFile);
formData.append("data", JSON.stringify({ version, stage, dependencyManager }), {
contentType: "application/json",
});
return new Promise((resolve, reject) => {
const req = formData.submit(options, (err, res) => {
if (err) {
"Http-response to connector API was not ok - http status response: " +
res.statusCode +
" " +
err.message;
return reject(new Error(err.message));
}
if (res.statusCode < 200 || res.statusCode > 299) {
return reject(new Error(`HTTP status code ${res.statusCode}`));
}
const body = [];
res.on("data", (chunk) => body.push(chunk));
res.on("end", () => {
const resString = Buffer.concat(body).toString();
resolve(resString);
});
});
})
.then((result) => {
return result;
})
.catch((reason) => {
console.error("Couldn't access the API");
throw new Error(reason);
});
}
async function loadManifestJSON(manifestPath) {
return new Promise(function (resolve, reject) {
fs.readFile("." + manifestPath, "utf8", function (error, manifest) {
if (error) reject(error);
else {
resolve(manifest);
}
});
})
.then((manifest) => {
if (!manifest) {
throw new Error(
"Something is wrong with the manifest file. Please verify your file and filepath!"
);
}
return JSON.stringify(yaml.load(manifest));
})
.catch((reason) => {
console.error("Couldn't find or read the manifest file!");
throw new Error(reason);
});
}
function createAPIAccessTokenRequestInit(secret) {
const headers = new fetch.Headers();
headers.append(
"Authorization",
"Basic " + base64.encode("apitoken:" + secret)
);
headers.append("Content-Type", "application/x-www-form-urlencoded");
return {
method: "post",
auth: {
user: "apitoken",
password: secret,
},
body: "grant_type=client_credentials",
headers: headers,
json: true,
};
}
function getJWTToken(secret, host) {
return fetch(
"https://" + host + "/services/mtm/v1/oauth2/token",
createAPIAccessTokenRequestInit(secret)
)
.then((response) => {
if (!response.ok) {
throw new Error(
"Http-response was not ok - http status response: " +
response.status +
" " +
response.statusText
);
}
return response.json();
})
.then((json) => {
return json.access_token;
})
.catch((reason) => {
console.error("Couldn't get API-Token!");
throw new Error(reason);
});
}
function getDependenciesFile(dependencyManager) {
console.log("Getting generated dependency file");
const conf = {
"MAVEN": './target/generated-resources/licenses.xml',
"GRADLE": './build/reports/licenses.json', // make sure init.gradle file is generating with exact name
"NPM": './licenses.json'
}
if(!fs.existsSync(conf[dependencyManager])) {
core.warning("Could not find dependency file");
return null;
}
return fs.createReadStream(conf[dependencyManager]);
}
function getCoreInputOrError(name) {
let input = core.getInput(name);
if(!input) {
throw new Error(`Input (${name}) cannot be empty`);
}
return input;
}
async function main() {
const allowFailure = core.getInput("allow-failure") === 'true';
try {
// the host-address of the connector API
const host = core.getInput("host");
// the API-token of the customer
const secret = core.getInput("api-token");
// the path where the manifest is found in the repository
const manifestPath = core.getInput("manifest-path");
// The stage that is deployed to
const stage = core.getInput("stage");
// the version of the deployed product
const version = getCoreInputOrError("version");
const dependencyManager = core.getInput("dependency-manager");
const jwtToken = await getJWTToken(secret, host);
const manifestJSON = await loadManifestJSON(manifestPath);
const dependenciesFile = getDependenciesFile(dependencyManager);
const apiResult = await sendInfoToAPI(
host,
jwtToken,
{manifestJSON, stage, version, dependenciesFile, dependencyManager}
);
console.log("API-result: " + apiResult);
// use for finding pom.xml, package.json etc.
/* fs.readdirSync(testFolder).forEach((file) => {
console.log(file);
}); */
} catch (error) {
// concerning this error handling here: We don't want the action to fail at any time and kill the workflow.
// Just log the error and don't affect the success.
console.log(error.message);
if (allowFailure) {
process.exit(0);
} else {
core.setFailed(error.message);
}
}
}
main();