feat: enable multi page star fetching for private vercel instances (#2159)

* 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.

* fix: improve stats multi-page fetching behavoir

This commit makes sure that the GraphQL api is only called one time per
100 repositories. The old method added one unnecesairy GraphQL call.

* docs: update documentation

* style: improve code syntax

Co-authored-by: Matteo Pierro <pierromatteo@gmail.com>

* lol happy new year

* docs: remove rate limit documentation for now

Remove the `FETCH_SINGLE_PAGE_STARS` from documentation for now since it
might confuse people.

* fix: fix error in automatic merge

* feat: make sure  env variable is read

Co-authored-by: Matteo Pierro <pierromatteo@gmail.com>
Co-authored-by: Anurag <hazru.anurag@gmail.com>
This commit is contained in:
Rick Staa
2023-01-21 18:32:37 +01:00
committed by GitHub
co-authored by Matteo Pierro Anurag
parent c1dc7b850c
commit 60fae292a3
4 changed files with 210 additions and 166 deletions
+92 -34
View File
@@ -4,7 +4,8 @@ import MockAdapter from "axios-mock-adapter";
import { calculateRank } from "../src/calculateRank.js";
import { fetchStats } from "../src/fetchers/stats-fetcher.js";
const data = {
// Test parameters.
const data_stats = {
data: {
user: {
name: "Anurag Hazra",
@@ -19,15 +20,6 @@ const data = {
followers: { totalCount: 100 },
repositories: {
totalCount: 5,
},
},
},
};
const firstRepositoriesData = {
data: {
user: {
repositories: {
nodes: [
{ name: "test-repo-1", stargazers: { totalCount: 100 } },
{ name: "test-repo-2", stargazers: { totalCount: 100 } },
@@ -42,7 +34,7 @@ const firstRepositoriesData = {
},
};
const secondRepositoriesData = {
const data_repo = {
data: {
user: {
repositories: {
@@ -59,7 +51,7 @@ const secondRepositoriesData = {
},
};
const repositoriesWithZeroStarsData = {
const data_repo_zero_stars = {
data: {
user: {
repositories: {
@@ -93,13 +85,12 @@ const error = {
const mock = new MockAdapter(axios);
beforeEach(() => {
process.env.FETCH_MULTI_PAGE_STARS = "false"; // Set to `false` to fetch only one page of stars.
mock
.onPost("https://api.github.com/graphql")
.replyOnce(200, data)
.replyOnce(200, data_stats)
.onPost("https://api.github.com/graphql")
.replyOnce(200, firstRepositoriesData);
// .onPost("https://api.github.com/graphql") // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
// .replyOnce(200, secondRepositoriesData); // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
.replyOnce(200, data_repo);
});
afterEach(() => {
@@ -114,8 +105,7 @@ describe("Test fetchStats", () => {
totalRepos: 5,
followers: 100,
contributions: 61,
// stargazers: 400, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
stargazers: 300, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
stargazers: 300,
prs: 300,
issues: 200,
});
@@ -126,8 +116,7 @@ describe("Test fetchStats", () => {
totalCommits: 100,
totalIssues: 200,
totalPRs: 300,
// totalStars: 400, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
totalStars: 300, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
totalStars: 300,
rank,
});
});
@@ -136,9 +125,9 @@ describe("Test fetchStats", () => {
mock.reset();
mock
.onPost("https://api.github.com/graphql")
.replyOnce(200, data)
.replyOnce(200, data_stats)
.onPost("https://api.github.com/graphql")
.replyOnce(200, repositoriesWithZeroStarsData);
.replyOnce(200, data_repo_zero_stars);
let stats = await fetchStats("anuraghazra");
const rank = calculateRank({
@@ -178,8 +167,7 @@ describe("Test fetchStats", () => {
totalRepos: 5,
followers: 100,
contributions: 61,
// stargazers: 400, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
stargazers: 300, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
stargazers: 300,
prs: 300,
issues: 200,
});
@@ -190,8 +178,7 @@ describe("Test fetchStats", () => {
totalCommits: 150,
totalIssues: 200,
totalPRs: 300,
// totalStars: 400, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
totalStars: 300, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
totalStars: 300,
rank,
});
});
@@ -207,8 +194,7 @@ describe("Test fetchStats", () => {
totalRepos: 5,
followers: 100,
contributions: 61,
// stargazers: 400, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
stargazers: 300, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
stargazers: 300,
prs: 300,
issues: 200,
});
@@ -219,8 +205,7 @@ describe("Test fetchStats", () => {
totalCommits: 1050,
totalIssues: 200,
totalPRs: 300,
// totalStars: 400, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
totalStars: 300, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
totalStars: 300,
rank,
});
});
@@ -236,8 +221,7 @@ describe("Test fetchStats", () => {
totalRepos: 5,
followers: 100,
contributions: 61,
// stargazers: 300, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
stargazers: 200, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
stargazers: 200,
prs: 300,
issues: 200,
});
@@ -248,8 +232,82 @@ describe("Test fetchStats", () => {
totalCommits: 1050,
totalIssues: 200,
totalPRs: 300,
// totalStars: 300, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
totalStars: 200, // NOTE: Temporarily disable fetching of multiple pages. Done because of #2130.
totalStars: 200,
rank,
});
});
it("should fetch two pages of stars if 'FETCH_MULTI_PAGE_STARS' env variable is set to `true`", async () => {
process.env.FETCH_MULTI_PAGE_STARS = true;
let stats = await fetchStats("anuraghazra");
const rank = calculateRank({
totalCommits: 100,
totalRepos: 5,
followers: 100,
contributions: 61,
stargazers: 400,
prs: 300,
issues: 200,
});
expect(stats).toStrictEqual({
contributedTo: 61,
name: "Anurag Hazra",
totalCommits: 100,
totalIssues: 200,
totalPRs: 300,
totalStars: 400,
rank,
});
});
it("should fetch one page of stars if 'FETCH_MULTI_PAGE_STARS' env variable is set to `false`", async () => {
process.env.FETCH_MULTI_PAGE_STARS = "false";
let stats = await fetchStats("anuraghazra");
const rank = calculateRank({
totalCommits: 100,
totalRepos: 5,
followers: 100,
contributions: 61,
stargazers: 300,
prs: 300,
issues: 200,
});
expect(stats).toStrictEqual({
contributedTo: 61,
name: "Anurag Hazra",
totalCommits: 100,
totalIssues: 200,
totalPRs: 300,
totalStars: 300,
rank,
});
});
it("should fetch one page of stars if 'FETCH_MULTI_PAGE_STARS' env variable is not set", async () => {
process.env.FETCH_MULTI_PAGE_STARS = undefined;
let stats = await fetchStats("anuraghazra");
const rank = calculateRank({
totalCommits: 100,
totalRepos: 5,
followers: 100,
contributions: 61,
stargazers: 300,
prs: 300,
issues: 200,
});
expect(stats).toStrictEqual({
contributedTo: 61,
name: "Anurag Hazra",
totalCommits: 100,
totalIssues: 200,
totalPRs: 300,
totalStars: 300,
rank,
});
});