Skip to content
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

fix: Fetch all repos for top languages #2111

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
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
31 changes: 21 additions & 10 deletions src/fetchers/top-languages-fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ const fetcher = (variables, token) => {
return request(
{
query: `
query userInfo($login: String!) {
query userInfo($login: String!, $after: String) {
user(login: $login) {
# fetch only owner repos & not forks
repositories(ownerAffiliations: OWNER, isFork: false, first: 100) {
repositories(ownerAffiliations: OWNER, isFork: false, first: 100, after: $after) {
nodes {
name
languages(first: 10, orderBy: {field: SIZE, direction: DESC}) {
Expand All @@ -29,6 +29,10 @@ const fetcher = (variables, token) => {
}
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
Expand All @@ -49,14 +53,23 @@ const fetcher = (variables, token) => {
async function fetchTopLanguages(username, exclude_repo = []) {
if (!username) throw new MissingParamError(["username"]);

const res = await retryer(fetcher, { login: username });
let repoNodes = [];
let hasNextPage = true;
let endCursor = null;
while (hasNextPage) {
const variables = { login: username, first: 100, after: endCursor };
const res = await retryer(fetcher, variables);

if (res.data.errors) {
logger.error(res.data.errors);
throw Error(res.data.errors[0].message || "Could not fetch user");
}

if (res.data.errors) {
logger.error(res.data.errors);
throw Error(res.data.errors[0].message || "Could not fetch user");
repoNodes.push(...res.data.data.user.repositories.nodes);
hasNextPage = res.data.data.user.repositories.pageInfo.hasNextPage;
endCursor = res.data.data.user.repositories.pageInfo.endCursor;
}

let repoNodes = res.data.data.user.repositories.nodes;
let repoToHide = {};

// populate repoToHide map for quick lookup
Expand Down Expand Up @@ -96,14 +109,12 @@ async function fetchTopLanguages(username, exclude_repo = []) {
};
}, {});

const topLangs = Object.keys(repoNodes)
return Object.keys(repoNodes)
.sort((a, b) => repoNodes[b].size - repoNodes[a].size)
.reduce((result, key) => {
result[key] = repoNodes[key];
return result;
}, {});

return topLangs;
}

export { fetchTopLanguages };
Expand Down
10 changes: 10 additions & 0 deletions tests/fetchTopLanguages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ const data_langs = {
data: {
user: {
repositories: {
edges: [
{ cursor: "1" },
{ cursor: "2" },
{ cursor: "3" },
{ cursor: "4" },
],
nodes: [
{
name: "test-repo-1",
Expand Down Expand Up @@ -43,6 +49,10 @@ const data_langs = {
},
},
],
pageInfo: {
hasNextPage: false,
cursor: "cursor",
},
},
},
},
Expand Down
4 changes: 4 additions & 0 deletions tests/top-langs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ const data_langs = {
},
},
],
pageInfo: {
rickstaa marked this conversation as resolved.
Show resolved Hide resolved
hasNextPage: false,
cursor: "cursor",
},
},
},
},
Expand Down