forked from Legit-Labs/legitify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
167 lines (145 loc) · 4.25 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
const core = require("@actions/core");
const fs = require("fs");
const zlib = require("zlib");
const request = require("request");
const tar = require("tar-fs");
const path = require("path");
const fetch = require("node-fetch");
const exec = require("@actions/exec");
const { context } = require("@actions/github");
const artifact = require("@actions/artifact");
const { exit } = require("process");
async function uploadErrorLog() {
const fileName = "error.log";
try {
if (fs.existsSync(fileName)) {
const client = artifact.create();
await client.uploadArtifact(fileName, [fileName], ".", context.runId);
console.log(`Uploaded ${fileName} to the workflow artifact`);
} else {
console.log(`File ${fileName} does not exist so skipping upload`);
}
} catch (error) {
console.error(error);
}
}
async function executeLegitify(token, args) {
let myOutput = "";
let myError = "";
const options = {};
options.listeners = {
stdout: (data) => {
myOutput += data.toString();
},
stderr: (data) => {
myError += data.toString();
},
};
options.env = { GITHUB_TOKEN: token };
options.silent = true
try {
await exec.exec('"./legitify"', ["analyze", ...args, "--output-format", "markdown"], options);
fs.writeFileSync(process.env.GITHUB_STEP_SUMMARY, myOutput)
} catch (error) {
fs.writeFileSync(process.env.GITHUB_STEP_SUMMARY, "legitify failed with:\n" + myError)
core.setFailed(error);
exit(1);
}
}
async function fetchLegitifyReleaseUrl(baseVersion) {
try {
const response = await fetch(
"https://api.github.com/repos/Legit-Labs/legitify/releases"
);
if (!response.ok) {
core.setFailed(`Failed to fetch releases: ${response.statusText}`);
exit(1);
}
const releases = await response.json();
for (const release of releases) {
const version = release.tag_name.slice(1);
if (version.startsWith(baseVersion)) {
const linuxAsset = release.assets.find(
(asset) =>
asset.name.endsWith(".tar.gz") && asset.name.includes("linux_amd64")
);
return linuxAsset.browser_download_url;
}
}
throw new Error(
`No releases found with version starting with ${baseVersion}`
);
} catch (error) {
core.setFailed(error);
exit(1);
}
}
function generateAnalyzeArgs(repo, owner) {
let args = [];
const scorecard = core.getInput("scorecard");
if (scorecard === "yes" || scorecard === "verbose") {
args.push("--scorecard");
args.push(scorecard);
}
if (core.getInput("analyze_self_only") === "true") {
args.push("--repo");
args.push(repo);
return args;
}
if (core.getInput("repositories") !== "") {
args.push("--repo");
args.push(core.getInput("repositories"));
return args;
}
args.push("--org");
args.push(owner);
return args;
}
function downloadAndExtract(fileUrl, filePath) {
console.log(
`downloading legitify binary from the following release URL: ${fileUrl}`
);
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(filePath);
request(fileUrl)
.on("error", (error) => {
reject(error);
})
.pipe(file)
.on("close", () => {
const readStream = fs.createReadStream(filePath);
const extractor = zlib.createGunzip();
readStream
.on("error", (error) => {
reject(error);
})
.pipe(extractor)
.pipe(tar.extract())
.on("finish", () => {
resolve();
});
});
});
}
async function run() {
try {
const token = core.getInput("github_token");
if (!token) {
core.setFailed("No GitHub token provided");
exit(1);
}
const owner = process.env["GITHUB_REPOSITORY_OWNER"];
const repo = process.env["GITHUB_REPOSITORY"];
const legitifyBaseVersion = core.getInput("legitify_base_version");
const fileUrl = await fetchLegitifyReleaseUrl(legitifyBaseVersion);
const filePath = path.join(__dirname, "legitify.tar.gz");
const args = generateAnalyzeArgs(repo, owner);
await downloadAndExtract(fileUrl, filePath);
await executeLegitify(token, args);
} catch (error) {
core.setFailed(error.message);
exit(1);
}
uploadErrorLog();
}
run();