mirror of
https://github.com/tiennm99/github-readme-stats.git
synced 2026-05-22 06:24:40 +00:00
fix: github rate limiter with multiple PATs
This commit is contained in:
+53
-15
@@ -2,25 +2,25 @@ const { request } = require("./utils");
|
||||
const calculateRank = require("./calculateRank");
|
||||
require("dotenv").config();
|
||||
|
||||
async function fetchStats(username) {
|
||||
if (!username) throw Error("Invalid username");
|
||||
|
||||
const res = await request({
|
||||
query: `
|
||||
// creating a fetcher function to reduce duplication
|
||||
const fetcher = (username, token) => {
|
||||
return request(
|
||||
{
|
||||
query: `
|
||||
query userInfo($login: String!) {
|
||||
user(login: $login) {
|
||||
name
|
||||
login
|
||||
repositoriesContributedTo(first: 100, contributionTypes: [COMMIT, ISSUE, PULL_REQUEST, REPOSITORY]) {
|
||||
totalCount
|
||||
}
|
||||
contributionsCollection {
|
||||
totalCommitContributions
|
||||
}
|
||||
pullRequests(first: 100) {
|
||||
repositoriesContributedTo(first: 1, contributionTypes: [COMMIT, ISSUE, PULL_REQUEST, REPOSITORY]) {
|
||||
totalCount
|
||||
}
|
||||
issues(first: 100) {
|
||||
pullRequests(first: 1) {
|
||||
totalCount
|
||||
}
|
||||
issues(first: 1) {
|
||||
totalCount
|
||||
}
|
||||
followers {
|
||||
@@ -36,9 +36,45 @@ async function fetchStats(username) {
|
||||
}
|
||||
}
|
||||
}
|
||||
`,
|
||||
variables: { login: username },
|
||||
});
|
||||
`,
|
||||
variables: { login: username },
|
||||
},
|
||||
{
|
||||
// set the token
|
||||
Authorization: `bearer ${token}`,
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
async function retryer(username, RETRIES) {
|
||||
try {
|
||||
console.log(`Trying PAT_${RETRIES + 1}`);
|
||||
|
||||
// try to fetch with the first token since RETRIES is 0 index i'm adding +1
|
||||
let response = await fetcher(username, process.env[`PAT_${RETRIES + 1}`]);
|
||||
|
||||
// if rate limit is hit increase the RETRIES and recursively call the retryer
|
||||
// with username, and current RETRIES
|
||||
if (
|
||||
response.data.errors &&
|
||||
response.data.errors[0].type === "RATE_LIMITED"
|
||||
) {
|
||||
console.log(`PAT_${RETRIES} Failed`);
|
||||
RETRIES++;
|
||||
// directly return from the function
|
||||
return await retryer(username, RETRIES);
|
||||
}
|
||||
|
||||
// finally return the response
|
||||
return response;
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchStats(username) {
|
||||
let RETRIES = 0;
|
||||
if (!username) throw Error("Invalid username");
|
||||
|
||||
const stats = {
|
||||
name: "",
|
||||
@@ -47,12 +83,14 @@ async function fetchStats(username) {
|
||||
totalIssues: 0,
|
||||
totalStars: 0,
|
||||
contributedTo: 0,
|
||||
rank: "C",
|
||||
rank: { level: "C", score: 0 },
|
||||
};
|
||||
|
||||
let res = await retryer(username, RETRIES);
|
||||
|
||||
if (res.data.errors) {
|
||||
console.log(res.data.errors);
|
||||
throw Error("Could not fetch user");
|
||||
throw Error(res.data.errors[0].message || "Could not fetch user");
|
||||
}
|
||||
|
||||
const user = res.data.data.user;
|
||||
|
||||
Reference in New Issue
Block a user