Skip to content

Commit

Permalink
Add retries for fetching SBOM data (#9972)
Browse files Browse the repository at this point in the history
  • Loading branch information
rithviknishad authored Jan 15, 2025
1 parent e3533a3 commit fe50f07
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions scripts/generate-sbom-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,43 @@ const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const sbomFolderPath = path.join(__dirname, "..", "public", "sbom");

const fetchSbomData = async (repo: `${string}/${string}`) => {
async function delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

const fetchSbomData = async (repo: `${string}/${string}`, retries = 3) => {
const url = `https://api.github.com/repos/${repo}/dependency-graph/sbom`;
const response = await fetch(url, {
headers: {
Accept: "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
},
});

if (!response.ok) {
throw new Error(
`Error fetching SBOM data from ${url}: ${response.statusText}`,
);
}

return await response.json();
for (let attempt = 1; attempt <= retries; attempt++) {
try {
const response = await fetch(url, {
headers: {
Accept: "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
},
});

if (response.ok) {
return await response.json();
}

if (attempt === retries) {
throw new Error(
`Error fetching SBOM data from ${url}: ${response.statusText}`,
);
}

// Wait before retrying (exponential backoff)
await delay(Math.pow(2, attempt) * 1000);
} catch (error) {
if (attempt === retries) {
throw error;
}

// Wait before retrying (exponential backoff)
await delay(Math.pow(2, attempt) * 1000);
}
}
};

async function main() {
Expand Down

0 comments on commit fe50f07

Please sign in to comment.