-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
63 lines (52 loc) · 1.6 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
const core = require("@actions/core");
const fs = require("fs");
const path = require("path");
const axios = require("axios");
(async () => {
const fileName = core.getInput("name");
const jsonString = core.getInput("json");
const dir = core.getInput("dir");
const fullPath = path.join(process.env.GITHUB_WORKSPACE, dir || "", fileName);
let fileContent = JSON.stringify(jsonString);
fileContent = JSON.parse(fileContent);
try {
await validateSubscription();
core.info("Creating json file...");
fs.writeFile(fullPath, fileContent, function (error) {
if (error) {
core.setFailed(error.message);
throw error;
}
core.info("JSON file created.");
fs.readFile(fullPath, null, handleFile);
function handleFile(err, data) {
if (err) {
core.setFailed(error.message);
throw err;
}
core.info("JSON checked.");
core.setOutput(
"successfully",
`Successfully created json on ${fullPath} directory with ${fileContent} data`
);
}
});
} catch (err) {
core.setFailed(err.message);
}
})();
async function validateSubscription() {
const API_URL = `https://agent.api.stepsecurity.io/v1/github/${process.env.GITHUB_REPOSITORY}/actions/subscription`;
try {
await axios.get(API_URL, { timeout: 3000 });
} catch (error) {
if (error.response) {
console.error(
"Subscription is not valid. Reach out to [email protected]"
);
process.exit(1);
} else {
core.info("Timeout or API not reachable. Continuing to next step.");
}
}
}