This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
forked from Legit-Labs/legitify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
239 lines (210 loc) · 6.87 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
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, getOctokit } = require("@actions/github");
const artifact = require("@actions/artifact");
const { exit } = require("process");
const logoMarkdown = `<div align="left">
<a href="https://www.legitsecurity.com">
<img width="200" alt="Legitify Logo" src="https://github.com/Legit-Labs/legitify/assets/74864790/c76dc765-e8fd-498e-ab92-1228eb5a1f2d">
</a>
</div>`
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 isPrivateRepo(token) {
const repoName = process.env.GITHUB_REPOSITORY;
if (!repoName) {
core.setFailed("No repository detected.");
return;
}
const parts = repoName.split("/");
const owner = parts[0];
const repo = parts[1];
const octokit = getOctokit(token);
const repoResp = await octokit.rest.repos.get({
owner,
repo,
});
return repoResp.data.private;
}
async function executeLegitify(token, args, uploadCodeScanning) {
let myOutput = "";
let myError = "";
const options = {};
options.listeners = {
stderr: (data) => {
myError += data.toString();
},
};
options.env = { SCM_TOKEN: token };
options.silent = true
const isPrivate = await isPrivateRepo(token)
core.setOutput("is_private", isPrivate)
if (!isPrivate) {
console.log("running in a public repository - only uploading results to Code Scanning (Security Center)")
}
try {
const sarifFile = "legitify-output.sarif"
// generate the output as json
const jsonFile = "legitify-output.json"
const analyzeArgs = ["analyze", ...args, "--output-format", "json", "--output-file", jsonFile]
console.log("execute legitify analyze:", analyzeArgs)
await exec.exec("./legitify", analyzeArgs, options)
// generate a sarif version for the code scanning
if (uploadCodeScanning) {
myError = ""
const convertSarifArgs = ["convert", "--input-file", jsonFile, "--output-format", "sarif", "--output-file", sarifFile]
console.log("execute legitify convert sarif:", convertSarifArgs)
await exec.exec("./legitify", convertSarifArgs, options);
}
// generate a markdown version for the action output
myError = ""
options.listeners.stdout = (data) => {
myOutput += data.toString();
}
const convertMarkdownArgs = ["convert", "--input-file", jsonFile, "--output-format", "markdown"]
console.log("execute legitify convert markdown:", convertMarkdownArgs)
await exec.exec('"./legitify"', convertMarkdownArgs, options)
if (isPrivate) {
fs.writeFileSync(process.env.GITHUB_STEP_SUMMARY, logoMarkdown + '\n\n' + myOutput)
} else {
fs.unlinkSync(jsonFile);
fs.unlinkSync("error.log");
}
} catch (error) {
console.log(error.toString() + " | stderr: " + myError.toString())
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 breakStringToParams(str) {
const pattern = /(-{1,2}\S+)(?:\s+((?!\s*-).+?)(?=\s+-{1,2}|\s*$))?/g;
return str.match(pattern)
}
function generateAnalyzeArgs(repo, owner) {
let args = [];
const scorecard = process.env["scorecard"];
if (scorecard === "yes" || scorecard === "verbose") {
args.push("--scorecard");
args.push(scorecard);
}
if (process.env["analyze_self_only"] === "true") {
args.push("--repo");
args.push(repo);
} else if (process.env["repositories"] !== "") {
args.push("--repo");
args.push(process.env["repositories"]);
} else {
args.push("--org");
args.push(owner);
}
if (process.env["ignore-policies-file"] !== "") {
args.push("--ignore-policies-file");
args.push(process.env["ignore-policies-file"]);
}
const extra = process.env["extra"]
if (extra !== "") {
const splitArgs = breakStringToParams(extra)
args.push(...splitArgs)
}
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 = process.env["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 uploadCodeScanning = (process.env["upload_code_scanning"] === "true");
if (process.env["compile_legitify"] === "true") {
console.log("using the compiled legitify version.");
} else {
const legitifyBaseVersion = process.env["legitify_base_version"];
const fileUrl = await fetchLegitifyReleaseUrl(legitifyBaseVersion);
const filePath = path.join(__dirname, "legitify.tar.gz");
await downloadAndExtract(fileUrl, filePath);
}
const args = generateAnalyzeArgs(repo, owner);
await executeLegitify(token, args, uploadCodeScanning);
} catch (error) {
core.setFailed(error.message);
exit(1);
}
uploadErrorLog();
}
run();