Skip to content

Commit e8460f1

Browse files
committed
.
1 parent a583fae commit e8460f1

File tree

1 file changed

+58
-50
lines changed

1 file changed

+58
-50
lines changed

index.js

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ process.on('SIGTERM', () => {
2424
// Set an absolute maximum timeout for the entire action (30 minutes)
2525
const MAX_ACTION_DURATION_MS = 30 * 60 * 1000;
2626
const actionTimeoutId = setTimeout(() => {
27-
core.warning(`Action timed out after ${MAX_ACTION_DURATION_MS/60000} minutes. This is likely a bug in the action wrapper. Forcing exit.`);
27+
core.warning(`Action timed out after ${MAX_ACTION_DURATION_MS / 60000} minutes. This is likely a bug in the action wrapper. Forcing exit.`);
2828
process.exit(1);
2929
}, MAX_ACTION_DURATION_MS);
3030

@@ -36,14 +36,14 @@ async function run() {
3636
// Step 1: Get Witness-related inputs
3737
const witnessVersion = core.getInput("witness-version") || "0.2.11";
3838
const witnessInstallDir = core.getInput("witness-install-dir") || "./";
39-
39+
4040
// Step 2: First download Witness binary
4141
await downloadWitness(witnessVersion, witnessInstallDir);
42-
42+
4343
// Step 3: Now handle the GitHub Action wrapping
4444
const actionRef = core.getInput("action-ref");
4545
const downloadedActionDir = await downloadAndExtractAction(actionRef);
46-
46+
4747
// Step 4: Prepare witness command
4848
const step = core.getInput("step");
4949
const archivistaServer = core.getInput("archivista-server");
@@ -63,16 +63,19 @@ async function run() {
6363
const productExcludeGlob = core.getInput("product-exclude-glob");
6464
const productIncludeGlob = core.getInput("product-include-glob");
6565
const spiffeSocket = core.getInput("spiffe-socket");
66-
66+
6767
let timestampServers = core.getInput("timestamp-servers");
6868
const trace = core.getInput("trace");
6969
const enableSigstore = core.getInput("enable-sigstore") === "true";
70-
70+
7171
const exportLink = core.getInput("attestor-link-export") === "true";
7272
const exportSBOM = core.getInput("attestor-sbom-export") === "true";
7373
const exportSLSA = core.getInput("attestor-slsa-export") === "true";
7474
const mavenPOM = core.getInput("attestor-maven-pom-path");
75-
75+
76+
77+
78+
7679
// Step 5: Run the downloaded action with Witness
7780
const witnessOutput = await runActionWithWitness(
7881
downloadedActionDir,
@@ -101,45 +104,45 @@ async function run() {
101104
mavenPOM,
102105
}
103106
);
104-
107+
105108
// Step 6: Process the output
106109
const gitOIDs = extractDesiredGitOIDs(witnessOutput);
107-
110+
108111
for (const gitOID of gitOIDs) {
109112
console.log("Extracted GitOID:", gitOID);
110-
113+
111114
// Print the GitOID to the output
112115
core.setOutput("git_oid", gitOID);
113-
116+
114117
// Construct the artifact URL using Archivista server and GitOID
115118
const artifactURL = `${archivistaServer}/download/${gitOID}`;
116-
119+
117120
// Add Job Summary with Markdown content
118121
const summaryHeader = `
119122
## Attestations Created
120123
| Step | Attestors Run | Attestation GitOID
121124
| --- | --- | --- |
122125
`;
123-
126+
124127
// Try to access the step summary file
125128
try {
126129
if (process.env.GITHUB_STEP_SUMMARY) {
127130
// Read the contents of the file
128131
const summaryFile = fs.readFileSync(process.env.GITHUB_STEP_SUMMARY, {
129132
encoding: "utf-8",
130133
});
131-
134+
132135
// Check if the file contains the header
133136
const headerExists = summaryFile.includes(summaryHeader.trim());
134-
137+
135138
// If the header does not exist, append it to the file
136139
if (!headerExists) {
137140
fs.appendFileSync(process.env.GITHUB_STEP_SUMMARY, summaryHeader);
138141
}
139-
142+
140143
// Construct the table row for the current step
141144
const tableRow = `| ${step} | ${attestations.join(", ")} | [${gitOID}](${artifactURL}) |\n`;
142-
145+
143146
// Append the table row to the file
144147
fs.appendFileSync(process.env.GITHUB_STEP_SUMMARY, tableRow);
145148
}
@@ -160,35 +163,35 @@ async function downloadWitness(version, installDir) {
160163
// Check if Witness is already in the tool cache
161164
let witnessPath = tc.find("witness", version);
162165
console.log("Cached Witness Path: " + witnessPath);
163-
166+
164167
if (!witnessPath) {
165168
console.log("Witness not found in cache, downloading now");
166169
let witnessTar;
167-
170+
168171
// Determine the OS-specific download URL
169172
if (process.platform === "win32") {
170173
witnessTar = await tc.downloadTool(
171174
"https://github.com/in-toto/witness/releases/download/v" +
172-
version +
173-
"/witness_" +
174-
version +
175-
"_windows_amd64.tar.gz"
175+
version +
176+
"/witness_" +
177+
version +
178+
"_windows_amd64.tar.gz"
176179
);
177180
} else if (process.platform === "darwin") {
178181
witnessTar = await tc.downloadTool(
179182
"https://github.com/in-toto/witness/releases/download/v" +
180-
version +
181-
"/witness_" +
182-
version +
183-
"_darwin_amd64.tar.gz"
183+
version +
184+
"/witness_" +
185+
version +
186+
"_darwin_amd64.tar.gz"
184187
);
185188
} else {
186189
witnessTar = await tc.downloadTool(
187190
"https://github.com/in-toto/witness/releases/download/v" +
188-
version +
189-
"/witness_" +
190-
version +
191-
"_linux_amd64.tar.gz"
191+
version +
192+
"/witness_" +
193+
version +
194+
"_linux_amd64.tar.gz"
192195
);
193196
}
194197

@@ -201,15 +204,15 @@ async function downloadWitness(version, installDir) {
201204
// Extract and cache Witness
202205
console.log("Extracting witness at: " + installDir);
203206
witnessPath = await tc.extractTar(witnessTar, installDir);
204-
207+
205208
const cachedPath = await tc.cacheFile(
206209
path.join(witnessPath, "witness"),
207210
"witness",
208211
"witness",
209212
version
210213
);
211214
console.log("Witness cached at: " + cachedPath);
212-
215+
213216
witnessPath = cachedPath;
214217
}
215218

@@ -230,7 +233,7 @@ async function downloadAndExtractAction(actionRef) {
230233
const zipUrl = isTag
231234
? `https://github.com/${repo}/archive/refs/tags/${ref}.zip`
232235
: `https://github.com/${repo}/archive/refs/heads/${ref}.zip`;
233-
236+
234237
core.info(`Downloading action from: ${zipUrl}`);
235238

236239
// Create a temporary directory for extraction
@@ -247,7 +250,7 @@ async function downloadAndExtractAction(actionRef) {
247250
},
248251
maxRedirects: 5 // Handle redirects
249252
});
250-
253+
251254
await new Promise((resolve, reject) => {
252255
response.data
253256
.pipe(unzipper.Extract({ path: tempDir }))
@@ -263,14 +266,14 @@ async function downloadAndExtractAction(actionRef) {
263266
core.info("Attempting alternative download URL for branches...");
264267
const altZipUrl = `https://github.com/${repo}/archive/refs/heads/${ref}.zip`;
265268
core.info(`Trying alternative URL: ${altZipUrl}`);
266-
269+
267270
const altResponse = await axios({
268271
url: altZipUrl,
269272
method: "GET",
270273
responseType: "stream",
271274
maxRedirects: 5
272275
});
273-
276+
274277
await new Promise((resolve, reject) => {
275278
altResponse.data
276279
.pipe(unzipper.Extract({ path: tempDir }))
@@ -310,7 +313,11 @@ async function downloadAndExtractAction(actionRef) {
310313

311314
// Run an action with Witness
312315
async function runActionWithWitness(actionDir, witnessOptions) {
313-
const {
316+
317+
318+
319+
// To this:
320+
let {
314321
step,
315322
archivistaServer,
316323
attestations,
@@ -335,21 +342,22 @@ async function runActionWithWitness(actionDir, witnessOptions) {
335342
mavenPOM,
336343
} = witnessOptions;
337344

345+
338346
// Read action.yml from the downloaded action
339347
const actionYmlPath = path.join(actionDir, "action.yml");
340348
// Some actions use action.yaml instead of action.yml
341349
const actionYamlPath = path.join(actionDir, "action.yaml");
342-
350+
343351
let actionConfig;
344-
352+
345353
if (fs.existsSync(actionYmlPath)) {
346354
actionConfig = yaml.load(fs.readFileSync(actionYmlPath, "utf8"));
347355
} else if (fs.existsSync(actionYamlPath)) {
348356
actionConfig = yaml.load(fs.readFileSync(actionYamlPath, "utf8"));
349357
} else {
350358
throw new Error(`Neither action.yml nor action.yaml found in ${actionDir}`);
351359
}
352-
360+
353361
const entryPoint = actionConfig.runs && actionConfig.runs.main;
354362
if (!entryPoint) {
355363
throw new Error("Entry point (runs.main) not defined in action metadata");
@@ -373,7 +381,7 @@ async function runActionWithWitness(actionDir, witnessOptions) {
373381
// We'll set these as environment variables that GitHub Actions uses
374382
const inputPrefix = 'input-';
375383
const nestedInputs = {};
376-
384+
377385
// Get all inputs that start with 'input-'
378386
Object.keys(process.env)
379387
.filter(key => key.startsWith('INPUT_'))
@@ -385,13 +393,13 @@ async function runActionWithWitness(actionDir, witnessOptions) {
385393
core.info(`Passing input '${nestedInputName}' to nested action`);
386394
}
387395
});
388-
396+
389397
// Set environment variables for the nested action
390398
const envVars = { ...process.env };
391399
Object.keys(nestedInputs).forEach(name => {
392400
envVars[`INPUT_${name.toUpperCase()}`] = nestedInputs[name];
393401
});
394-
402+
395403
// Build the witness run command
396404
const cmd = ["run"];
397405

@@ -452,17 +460,17 @@ async function runActionWithWitness(actionDir, witnessOptions) {
452460

453461
if (trace) cmd.push(`--trace=${trace}`);
454462
if (outfile) cmd.push(`--outfile=${outfile}`);
455-
463+
456464
// Prepare the command to run the action
457465
const nodeCmd = 'node';
458466
const nodeArgs = [entryFile];
459-
467+
460468
// Execute the command and capture its output
461469
const runArray = ["witness", ...cmd, "--", nodeCmd, ...nodeArgs],
462470
commandString = runArray.join(" ");
463471

464472
core.info(`Running witness command: ${commandString}`);
465-
473+
466474
// Set up options for execution
467475
const execOptions = {
468476
cwd: actionDir,
@@ -476,10 +484,10 @@ async function runActionWithWitness(actionDir, witnessOptions) {
476484
}
477485
}
478486
};
479-
487+
480488
// Execute and capture output
481489
let output = '';
482-
490+
483491
await exec.exec('sh', ['-c', commandString], {
484492
...execOptions,
485493
listeners: {
@@ -496,7 +504,7 @@ async function runActionWithWitness(actionDir, witnessOptions) {
496504
}
497505
}
498506
});
499-
507+
500508
return output;
501509
}
502510

0 commit comments

Comments
 (0)