Skip to content

Commit 8c3a077

Browse files
committed
feat: enable multi page star fetching for public vercel instances
1 parent 39535db commit 8c3a077

File tree

3 files changed

+78
-25
lines changed

3 files changed

+78
-25
lines changed

readme.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ Visit <https://indiafightscorona.giveindia.org> and make a small donation to hel
9393
- [Wakatime Card Exclusive Option](#wakatime-card-exclusive-options)
9494
- [Deploy Yourself](#deploy-on-your-own-vercel-instance)
9595

96+
# Important Notice
97+
98+
> **Warning**
99+
> Since the GitHub API only [allows 5k requests per hour](https://docs.github.com/en/graphql/overview/resource-limitations), the public Vercel instance hosted on `https://github-readme-stats.vercel.app/api` could possibly hit the rate limiter (see #1471). Because of this, we have limited the public API to only fetch the first 100 repositories with stars and languages. As a result, for a very active GitHub account, the language, stars and commits results might be off when using the public API. These limits do not apply when you deploy [your own Vercel instance](#deploy-on-your-own-vercel-instance), so in that case, you do not have to worry about anything! :rocket:
100+
96101
# GitHub Stats Card
97102

98103
Copy-paste this into your markdown content, and that is it. Simple!
@@ -173,7 +178,7 @@ You can customize the appearance of your `Stats Card` or `Repo Card` however you
173178
- `border_radius` - Corner rounding on the card. Default: `4.5`.
174179

175180
> **Warning**
176-
> We use caching to decrease the load on our servers (see https://github.com/anuraghazra/github-readme-stats/issues/1471#issuecomment-1271551425). Our cards have a default cache of 4 hours (14400 seconds). Also, note that the cache is clamped to a minimum of 4 hours and a maximum of 24 hours.
181+
> We use caching to decrease the load on our servers (see <https://github.com/anuraghazra/github-readme-stats/issues/1471#issuecomment-1271551425>). Our cards have a default cache of 4 hours (14400 seconds). Also, note that the cache is clamped to a minimum of 4 hours and a maximum of 24 hours.
177182
178183
##### Gradient in bg_color
179184

@@ -261,7 +266,7 @@ Use [show_owner](#customization) variable to include the repo's owner username
261266
The top languages card shows a GitHub user's most frequently used top language.
262267

263268
> **Note**
264-
> Top Languages does not indicate my skill level or anything like that; it's a GitHub metric to determine which languages have the most code on GitHub. It is a new feature of github-readme-stats._
269+
> Top Languages does not indicate my skill level or anything like that; it's a GitHub metric to determine which languages have the most code on GitHub. It is a new feature of github-readme-stats.\_
265270
266271
### Usage
267272

src/fetchers/stats-fetcher.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,14 @@ const totalStarsFetcher = async (username, repoToHide) => {
168168
(node) => node.stargazers.totalCount !== 0,
169169
);
170170
nodes.push(...nodesWithStars);
171-
// hasNextPage =
172-
// allNodes.length === nodesWithStars.length &&
173-
// res.data.data.user.repositories.pageInfo.hasNextPage;
174-
hasNextPage = false; // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
171+
172+
// Disable multi page fetching on public Vercel instance due to rate limits.
173+
hasNextPage =
174+
process.env.FETCH_SINGLE_PAGE_STARS === "true"
175+
? false
176+
: allNodes.length === nodesWithStars.length &&
177+
res.data.data.user.repositories.pageInfo.hasNextPage;
178+
175179
endCursor = res.data.data.user.repositories.pageInfo.endCursor;
176180
}
177181

tests/fetchStats.test.js

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import MockAdapter from "axios-mock-adapter";
44
import { calculateRank } from "../src/calculateRank.js";
55
import { fetchStats } from "../src/fetchers/stats-fetcher.js";
66

7+
// Test parameters.
78
const data = {
89
data: {
910
user: {
@@ -93,13 +94,14 @@ const error = {
9394
const mock = new MockAdapter(axios);
9495

9596
beforeEach(() => {
97+
process.env.FETCH_SINGLE_PAGE_STARS = "true"; // Set to true to fetch only one page of stars.
9698
mock
9799
.onPost("https://api.github.com/graphql")
98100
.replyOnce(200, data)
99101
.onPost("https://api.github.com/graphql")
100-
.replyOnce(200, firstRepositoriesData);
101-
// .onPost("https://api.github.com/graphql") // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
102-
// .replyOnce(200, secondRepositoriesData); // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
102+
.replyOnce(200, firstRepositoriesData)
103+
.onPost("https://api.github.com/graphql")
104+
.replyOnce(200, secondRepositoriesData);
103105
});
104106

105107
afterEach(() => {
@@ -114,8 +116,7 @@ describe("Test fetchStats", () => {
114116
totalRepos: 5,
115117
followers: 100,
116118
contributions: 61,
117-
// stargazers: 400, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
118-
stargazers: 300, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
119+
stargazers: 300,
119120
prs: 300,
120121
issues: 200,
121122
});
@@ -126,8 +127,7 @@ describe("Test fetchStats", () => {
126127
totalCommits: 100,
127128
totalIssues: 200,
128129
totalPRs: 300,
129-
// totalStars: 400, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
130-
totalStars: 300, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
130+
totalStars: 300,
131131
rank,
132132
});
133133
});
@@ -178,8 +178,7 @@ describe("Test fetchStats", () => {
178178
totalRepos: 5,
179179
followers: 100,
180180
contributions: 61,
181-
// stargazers: 400, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
182-
stargazers: 300, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
181+
stargazers: 300,
183182
prs: 300,
184183
issues: 200,
185184
});
@@ -190,8 +189,7 @@ describe("Test fetchStats", () => {
190189
totalCommits: 150,
191190
totalIssues: 200,
192191
totalPRs: 300,
193-
// totalStars: 400, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
194-
totalStars: 300, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
192+
totalStars: 300,
195193
rank,
196194
});
197195
});
@@ -207,8 +205,7 @@ describe("Test fetchStats", () => {
207205
totalRepos: 5,
208206
followers: 100,
209207
contributions: 61,
210-
// stargazers: 400, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
211-
stargazers: 300, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
208+
stargazers: 300,
212209
prs: 300,
213210
issues: 200,
214211
});
@@ -219,8 +216,7 @@ describe("Test fetchStats", () => {
219216
totalCommits: 1050,
220217
totalIssues: 200,
221218
totalPRs: 300,
222-
// totalStars: 400, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
223-
totalStars: 300, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
219+
totalStars: 300,
224220
rank,
225221
});
226222
});
@@ -236,8 +232,7 @@ describe("Test fetchStats", () => {
236232
totalRepos: 5,
237233
followers: 100,
238234
contributions: 61,
239-
// stargazers: 300, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
240-
stargazers: 200, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
235+
stargazers: 200,
241236
prs: 300,
242237
issues: 200,
243238
});
@@ -248,8 +243,57 @@ describe("Test fetchStats", () => {
248243
totalCommits: 1050,
249244
totalIssues: 200,
250245
totalPRs: 300,
251-
// totalStars: 300, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
252-
totalStars: 200, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
246+
totalStars: 200,
247+
rank,
248+
});
249+
});
250+
251+
it("should fetch two pages of stars if 'FETCH_SINGLE_PAGE_STARS' env variable is not defined", async () => {
252+
process.env.FETCH_SINGLE_PAGE_STARS = undefined;
253+
254+
let stats = await fetchStats("anuraghazra");
255+
const rank = calculateRank({
256+
totalCommits: 100,
257+
totalRepos: 5,
258+
followers: 100,
259+
contributions: 61,
260+
stargazers: 400,
261+
prs: 300,
262+
issues: 200,
263+
});
264+
265+
expect(stats).toStrictEqual({
266+
contributedTo: 61,
267+
name: "Anurag Hazra",
268+
totalCommits: 100,
269+
totalIssues: 200,
270+
totalPRs: 300,
271+
totalStars: 400,
272+
rank,
273+
});
274+
});
275+
276+
it("should fetch two pages of stars if 'FETCH_SINGLE_PAGE_STARS' env variable is set to `false`", async () => {
277+
process.env.FETCH_SINGLE_PAGE_STARS = "false";
278+
279+
let stats = await fetchStats("anuraghazra");
280+
const rank = calculateRank({
281+
totalCommits: 100,
282+
totalRepos: 5,
283+
followers: 100,
284+
contributions: 61,
285+
stargazers: 400,
286+
prs: 300,
287+
issues: 200,
288+
});
289+
290+
expect(stats).toStrictEqual({
291+
contributedTo: 61,
292+
name: "Anurag Hazra",
293+
totalCommits: 100,
294+
totalIssues: 200,
295+
totalPRs: 300,
296+
totalStars: 400,
253297
rank,
254298
});
255299
});

0 commit comments

Comments
 (0)