Skip to content

Commit 3682da9

Browse files
committed
Added retries on log retrieval on timeout/reset
1 parent 10d0e59 commit 3682da9

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

audit_workflow_runs.js

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,46 @@ octokit.log.error = () => {};
4141

4242
// Helper function to extract Actions used from workflow logs
4343
async function extractActionsFromLogs(logUrl) {
44-
try {
45-
const response = await octokit.request(`GET ${logUrl}`, {
46-
headers: { Accept: "application/vnd.github+json" },
47-
});
44+
let retries = 3;
4845

49-
// get the zip file content
50-
const zipBuffer = Buffer.from(response.data);
46+
while (retries > 0) {
47+
try {
48+
const response = await octokit.request(`GET ${logUrl}`, {
49+
headers: { Accept: "application/vnd.github+json" },
50+
});
5151

52-
// Unzip the file
53-
const zip = new AdmZip(zipBuffer);
54-
const logEntries = zip.getEntries(); // Get all entries in the zip file
52+
// get the zip file content
53+
const zipBuffer = Buffer.from(response.data);
5554

56-
const [success, actions] = searchForSetUpJob(logEntries);
55+
// Unzip the file
56+
const zip = new AdmZip(zipBuffer);
57+
const logEntries = zip.getEntries(); // Get all entries in the zip file
5758

58-
if (!success) {
59-
actions.push(...searchForTopLevelLog(logEntries));
60-
}
59+
const [success, actions] = searchForSetUpJob(logEntries);
6160

62-
return actions;
63-
} catch (error) {
64-
console.error(`Failed to fetch logs from ${logUrl}:`, error.message);
65-
return [];
61+
if (!success) {
62+
actions.push(...searchForTopLevelLog(logEntries));
63+
}
64+
65+
return actions;
66+
} catch (error) {
67+
if (error.status == 404) {
68+
console.error(
69+
`Failed to fetch logs from ${logUrl}: 404. This may be due to the logs being too old, or the workflow having not run due to an error.`
70+
);
71+
return [];
72+
} else if (error.message.startsWith("Connect Timeout Error ") || error.message === "read ECONNRESET" || error.message === "read ETIMEDOUT") {
73+
console.error(
74+
`Connection timeout/reset. Retrying, attempt ${4 - retries}/3. Waiting 30 seconds...`
75+
);
76+
retries--;
77+
// sleep 30 seconds
78+
await new Promise((resolve) => setTimeout(resolve, 30000));
79+
continue;
80+
}
81+
console.error(`Failed to fetch logs from ${logUrl}:`, error.message);
82+
return [];
83+
}
6684
}
6785
}
6886

0 commit comments

Comments
 (0)