-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
52 lines (41 loc) · 1.58 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
const core = require('@actions/core');
const github = require('@actions/github');
const SwaggerParser = require('@apidevtools/swagger-parser');
import fetch from 'node-fetch';
(async () => {
try {
const file = core.getInput("file");
const validateOnly = core.getInput("validate-only") === "true";
console.log(`Validating ${file}...`);
const api = await SwaggerParser.validate(file);
console.log("Success!");
if (validateOnly) {
return;
}
const authorization = core.getInput("authorization");
const prAsPrerelease = core.getInput("publish-pr-prereleases") === "true"
const isPr = github.context.eventName === "pull_request";
if (isPr && !prAsPrerelease) {
return;
}
const version = api.info.version + (isPr ? `-pre${github.context.runNumber}` : "");
console.log(`Uploading ${api.info.title} version ${version} to OpenAPI hub.`)
const result = await fetch(`https://openapi-hub.rythm.dev/upload/${api.info.title}/${version}`, {
method: "POST",
headers: {
'Authorization': authorization,
'Content-Type': 'text/plain',
},
body: JSON.stringify(api),
});
if (result.status <200 || result.status > 299) {
core.setFailed(await result.text());
return;
}
console.log("Success!");
core.setOutput("name", api.info.title);
core.setOutput("version", version);
} catch (e) {
core.setFailed(e.message);
}
})();