Skip to content

Commit 8f64975

Browse files
committed
feat: enable multi-page stars' fetching for private vercel instances
This commit enables multi-page stars' support from fetching on private Vercel instances. This feature can be disabled on the public Vercel instance by adding the `FETCH_SINGLE_PAGE_STARS=true` as an env variable in the public Vercel instance. This variable will not be present when people deploy their own Vercel instance, causing the code to fetch multiple star pages.
1 parent 39535db commit 8f64975

File tree

3 files changed

+84
-33
lines changed

3 files changed

+84
-33
lines changed

readme.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ Visit <https://indiafightscorona.giveindia.org> and make a small donation to hel
8080

8181
# Features
8282

83+
- [Important Notice](#important-notice)
8384
- [GitHub Stats Card](#github-stats-card)
8485
- [GitHub Extra Pins](#github-extra-pins)
8586
- [Top Languages Card](#top-languages-card)
@@ -93,6 +94,11 @@ Visit <https://indiafightscorona.giveindia.org> and make a small donation to hel
9394
- [Wakatime Card Exclusive Option](#wakatime-card-exclusive-options)
9495
- [Deploy Yourself](#deploy-on-your-own-vercel-instance)
9596

97+
# Important Notice
98+
99+
> **Warning**
100+
> Since the GitHub API only [allows 5k requests per hour per user account](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:
101+
96102
# GitHub Stats Card
97103

98104
Copy-paste this into your markdown content, and that is it. Simple!
@@ -167,13 +173,13 @@ You can customize the appearance of your `Stats Card` or `Repo Card` however you
167173
- `border_color` - Card's border color _(hex color)_. Default: `e4e2e2` (Does not apply when `hide_border` is enabled).
168174
- `bg_color` - Card's background color _(hex color)_ **or** a gradient in the form of _angle,start,end_. Default: `fffefe`
169175
- `hide_border` - Hides the card's border _(boolean)_. Default: `false`
170-
- `theme` - name of the theme, choose from [all available themes](./themes/README.md). Default: `default` theme.
176+
- `theme` - name of the theme, choose from [all available themes](./themes/README.md). Default: `default` theme.
171177
- `cache_seconds` - set the cache header manually _(min: 7200, max: 86400)_. Default: `14400 seconds (4 hours)`.
172178
- `locale` - set the language in the card _(e.g. cn, de, es, etc.)_. Default: `en`.
173179
- `border_radius` - Corner rounding on the card. Default: `4.5`.
174180

175181
> **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.
182+
> 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.
177183
178184
##### Gradient in bg_color
179185

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

263269
> **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._
270+
> 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.\_
265271
266272
### Usage
267273

@@ -405,17 +411,14 @@ By default, GitHub does not lay out the cards side by side. To do that, you can
405411

406412
## Deploy on your own Vercel instance
407413

408-
#### [Check Out Step By Step Video Tutorial By @codeSTACKr](https://youtu.be/n6d4KHSKqGk?t=107)
414+
As explained [above](#important-notice) the GitHub API only [allows 5k requests per hour per user account](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! Click on [the deploy button](https://vercel.com/import/project?template=https://github.com/anuraghazra/github-readme-stats) and follow the video or step-to-step guides below to get started! :rocket:
415+
416+
[![Deploy to Vercel](https://vercel.com/button)](https://vercel.com/import/project?template=https://github.com/anuraghazra/github-readme-stats)
409417

410418
> **Warning**
411419
> If you are on the [hobby (i.e. free)](https://vercel.com/pricing) Vercel plan, please make sure you change the `maxDuration` parameter in the [vercel.json](https://github.com/anuraghazra/github-readme-stats/blob/master/vercel.json) file from `30` to `10` (see [#1416](https://github.com/anuraghazra/github-readme-stats/issues/1416#issuecomment-950275476) for more information).
412420
413-
Since the GitHub API only allows 5k requests per hour, my `https://github-readme-stats.vercel.app/api` could possibly hit the rate limiter. If you host it on your own Vercel server, then you do not have to worry about anything. Click on the deploy button to get started!
414-
415-
> **Note**
416-
> Since [#58](https://github.com/anuraghazra/github-readme-stats/pull/58), we should be able to handle more than 5k requests and have fewer issues with downtime :grin:.
417-
418-
[![Deploy to Vercel](https://vercel.com/button)](https://vercel.com/import/project?template=https://github.com/anuraghazra/github-readme-stats)
421+
#### :film_projector: [Check Out Step By Step Video Tutorial By @codeSTACKr](https://youtu.be/n6d4KHSKqGk?t=107)
419422

420423
<details>
421424
<summary><b>:hammer_and_wrench: Step-by-step guide on setting up your own Vercel instance</b></summary>

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)