Skip to content

Commit e692eff

Browse files
committed
use env var GITHUB_TOKEN for api calls (if set)
1 parent ba3c4cd commit e692eff

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

scripts/updateGitHubApiData.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from "node:fs";
2+
import process from "node:process";
23
import {getJson} from "./utils.js";
34

45
let queryCount = 0;
@@ -12,8 +13,8 @@ function printProgress (count, total) {
1213
// Function to check whether new data should be retrieved.
1314
function shouldFetch (repository) {
1415
let retrieve = false;
15-
if (repository.url.includes("github.com")) {
16-
if (queryCount < maxQueryCount) {
16+
if (repository.url.includes("github.com") && maxQueryCount > 0) {
17+
if (queryCount < maxQueryCount || process.env.GITHUB_TOKEN) {
1718
retrieve = true;
1819
}
1920
}
@@ -75,6 +76,11 @@ async function updateData () {
7576

7677
sortModuleListByLastUpdate(previousData, moduleList);
7778

79+
const headers = {};
80+
if (process.env.GITHUB_TOKEN) {
81+
headers.Authorization = `Bearer ${process.env.GITHUB_TOKEN}`;
82+
}
83+
7884
for (const module of moduleList) {
7985
const repositoryId = module.id;
8086
const repositoryApiUrl = `https://api.github.com/repos/${repositoryId}`;
@@ -85,12 +91,12 @@ async function updateData () {
8591

8692
if (shouldFetchData) {
8793
printProgress(moduleCount, moduleListLength);
88-
const response = await fetch(repositoryApiUrl);
94+
const response = await fetch(repositoryApiUrl, {headers});
8995
const data = await response.json();
9096
queryCount += 1;
9197

9298
const branchUrl = `https://api.github.com/repos/${repositoryId}/commits/${data.default_branch}`;
93-
const branchResponse = await fetch(branchUrl);
99+
const branchResponse = await fetch(branchUrl, {headers});
94100
const branchData = await branchResponse.json();
95101
queryCount += 1;
96102

@@ -176,6 +182,7 @@ async function updateData () {
176182

177183
fs.writeFileSync(localFilePath, JSON.stringify(updateInfo, null, 2));
178184
fs.writeFileSync("docs/data/modules.stage.2.json", JSON.stringify(sortedModuleList, null, 2));
185+
if (maxQueryCount < queryCount) {maxQueryCount = 0}
179186
console.info("\nGitHub data update completed. queryCount:", queryCount, "maxQueryCount:", maxQueryCount, "results:", results.length, "modules:", moduleListLength);
180187
} catch (error) {
181188
console.error("Error fetching GitHub API data:", error);

0 commit comments

Comments
 (0)