fix: fetch all repos for for the stats card (#2100)

* fetch all stars

* stop fetching when there are repos with zero stars

* remove not needed parameters from the query

* add docstring

* removed not needed mock

* style: update formatting

Co-authored-by: rickstaa <rick.staa@outlook.com>
This commit is contained in:
Matteo Pierro
2022-10-04 13:10:50 +02:00
committed by GitHub
co-authored by rickstaa
parent acbc03dc0f
commit af97e5765b
3 changed files with 175 additions and 18 deletions
+68 -10
View File
@@ -29,10 +29,10 @@ const fetcher = (variables, token) => {
totalCommitContributions
restrictedContributionsCount
}
repositoriesContributedTo(first: 1, contributionTypes: [COMMIT, ISSUE, PULL_REQUEST, REPOSITORY]) {
repositoriesContributedTo(contributionTypes: [COMMIT, ISSUE, PULL_REQUEST, REPOSITORY]) {
totalCount
}
pullRequests(first: 1) {
pullRequests {
totalCount
}
openIssues: issues(states: OPEN) {
@@ -44,14 +44,41 @@ const fetcher = (variables, token) => {
followers {
totalCount
}
repositories(first: 100, ownerAffiliations: OWNER, orderBy: {direction: DESC, field: STARGAZERS}) {
repositories(ownerAffiliations: OWNER) {
totalCount
}
}
}
`,
variables,
},
{
Authorization: `bearer ${token}`,
},
);
};
/**
* @param {import('axios').AxiosRequestHeaders} variables
* @param {string} token
*/
const repositoriesFetcher = (variables, token) => {
return request(
{
query: `
query userInfo($login: String!, $after: String) {
user(login: $login) {
repositories(first: 100, ownerAffiliations: OWNER, orderBy: {direction: DESC, field: STARGAZERS}, after: $after) {
nodes {
name
stargazers {
totalCount
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
@@ -99,6 +126,43 @@ const totalCommitsFetcher = async (username) => {
return 0;
};
/**
* Fetch all the stars for all the repositories of a given username
* @param {string} username
* @param {array} repoToHide
*/
const totalStarsFetcher = async (username, repoToHide) => {
let nodes = [];
let hasNextPage = true;
let endCursor = null;
while (hasNextPage) {
const variables = { login: username, first: 100, after: endCursor };
let res = await retryer(repositoriesFetcher, variables);
if (res.data.errors) {
logger.error(res.data.errors);
throw new CustomError(
res.data.errors[0].message || "Could not fetch user",
CustomError.USER_NOT_FOUND,
);
}
const allNodes = res.data.data.user.repositories.nodes;
const nodesWithStars = allNodes.filter(
(node) => node.stargazers.totalCount !== 0,
);
nodes.push(...nodesWithStars);
hasNextPage =
allNodes.length === nodesWithStars.length &&
res.data.data.user.repositories.pageInfo.hasNextPage;
endCursor = res.data.data.user.repositories.pageInfo.endCursor;
}
return nodes
.filter((data) => !repoToHide[data.name])
.reduce((prev, curr) => prev + curr.stargazers.totalCount, 0);
};
/**
* @param {string} username
* @param {boolean} count_private
@@ -166,13 +230,7 @@ async function fetchStats(
stats.contributedTo = user.repositoriesContributedTo.totalCount;
// Retrieve stars while filtering out repositories to be hidden
stats.totalStars = user.repositories.nodes
.filter((data) => {
return !repoToHide[data.name];
})
.reduce((prev, curr) => {
return prev + curr.stargazers.totalCount;
}, 0);
stats.totalStars = await totalStarsFetcher(username, repoToHide);
stats.rank = calculateRank({
totalCommits: stats.totalCommits,