Skip to content

fix: increase github rate limit with multiple PATs #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ module.exports = async (req, res) => {
} = req.query;
let stats;

res.setHeader("Cache-Control", "public, max-age=300");
res.setHeader("Content-Type", "image/svg+xml");

try {
stats = await fetchStats(username);
} catch (err) {
Expand Down
2 changes: 2 additions & 0 deletions api/pin.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ module.exports = async (req, res) => {
} = req.query;

let repoData;

res.setHeader("Cache-Control", "public, max-age=300");
res.setHeader("Content-Type", "image/svg+xml");

try {
Expand Down
17 changes: 11 additions & 6 deletions src/fetchRepo.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ async function fetchRepo(username, reponame) {
throw new Error("Invalid username or reponame");
}

const res = await request({
query: `
const res = await request(
{
query: `
fragment RepoInfo on Repository {
name
stargazers {
Expand All @@ -33,11 +34,15 @@ async function fetchRepo(username, reponame) {
}
}
`,
variables: {
login: username,
repo: reponame,
variables: {
login: username,
repo: reponame,
},
},
});
{
Authorization: `bearer ${process.env.PAT_1}`,
}
);

const data = res.data.data;

Expand Down
68 changes: 53 additions & 15 deletions src/fetchStats.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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: "",
Expand All @@ -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;
Expand Down
12 changes: 9 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ function isValidHexColor(hexColor) {
).test(hexColor);
}

function request(data) {
function request(data, headers) {
return new Promise((resolve, reject) => {
axios({
url: "https://api.github.com/graphql",
method: "post",
headers: {
Authorization: `bearer ${process.env.GITHUB_TOKEN}`,
...headers,
},
data,
})
Expand All @@ -48,4 +48,10 @@ function request(data) {
});
}

module.exports = { renderError, kFormatter, encodeHTML, isValidHexColor, request };
module.exports = {
renderError,
kFormatter,
encodeHTML,
isValidHexColor,
request,
};
2 changes: 1 addition & 1 deletion tests/fetchStats.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe("Test fetchStats", () => {
mock.onPost("https://api.github.com/graphql").reply(200, error);

await expect(fetchStats("anuraghazra")).rejects.toThrow(
"Could not fetch user"
"Could not resolve to a User with the login of 'noname'."
);
});
});