Skip to content
Draft
Show file tree
Hide file tree
Changes from 6 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
2 changes: 2 additions & 0 deletions api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module.exports = async (req, res) => {
disable_animations,
border_radius,
border_color,
role,
} = req.query;
res.setHeader("Content-Type", "image/svg+xml");

Expand All @@ -47,6 +48,7 @@ module.exports = async (req, res) => {
try {
const stats = await fetchStats(
username,
parseArray(role),
parseBoolean(count_private),
parseBoolean(include_all_commits),
);
Expand Down
3 changes: 2 additions & 1 deletion api/top-langs.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module.exports = async (req, res) => {
locale,
border_radius,
border_color,
role,
} = req.query;
res.setHeader("Content-Type", "image/svg+xml");

Expand All @@ -44,8 +45,8 @@ module.exports = async (req, res) => {
try {
const topLangs = await fetchTopLanguages(
username,
parseArray(role),
parseArray(exclude_repo),
parseArray(hide),
);

const cacheSeconds = clampValue(
Expand Down
12 changes: 9 additions & 3 deletions src/fetchers/stats-fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const fetcher = (variables, token) => {
return request(
{
query: `
query userInfo($login: String!) {
query userInfo($login: String!, $ownerAffiliations: [RepositoryAffiliation]) {
user(login: $login) {
name
login
Expand All @@ -34,7 +34,7 @@ const fetcher = (variables, token) => {
followers {
totalCount
}
repositories(first: 100, ownerAffiliations: OWNER, orderBy: {direction: DESC, field: STARGAZERS}) {
repositories(first: 100, ownerAffiliations: $ownerAffiliations, orderBy: {direction: DESC, field: STARGAZERS}) {
totalCount
nodes {
stargazers {
Expand Down Expand Up @@ -89,6 +89,7 @@ const totalCommitsFetcher = async (username) => {

async function fetchStats(
username,
ownerAffiliations,
count_private = false,
include_all_commits = false,
) {
Expand All @@ -104,7 +105,12 @@ async function fetchStats(
rank: { level: "C", score: 0 },
};

let res = await retryer(fetcher, { login: username });
// Set default value for ownerAffiliations in GraphQL query won't work because
// parseArray() will always return an empty array even nothing was specified
// and GraphQL would consider that empty arr as a valid value. Nothing will be
// queried in that case as no affiliation is presented.
ownerAffiliations = ownerAffiliations.length > 0 ? ownerAffiliations : ["OWNER"];
let res = await retryer(fetcher, { login: username, ownerAffiliations });

if (res.data.errors) {
logger.error(res.data.errors);
Expand Down
15 changes: 10 additions & 5 deletions src/fetchers/top-languages-fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ const fetcher = (variables, token) => {
return request(
{
query: `
query userInfo($login: String!) {
query userInfo($login: String!, $ownerAffiliations: [RepositoryAffiliation]) {
user(login: $login) {
# fetch only owner repos & not forks
repositories(ownerAffiliations: OWNER, isFork: false, first: 100) {
# do not fetch forks
repositories(ownerAffiliations: $ownerAffiliations, isFork: false, first: 100) {
nodes {
name
languages(first: 10, orderBy: {field: SIZE, direction: DESC}) {
Expand All @@ -34,10 +34,15 @@ const fetcher = (variables, token) => {
);
};

async function fetchTopLanguages(username, exclude_repo = []) {
async function fetchTopLanguages(username, ownerAffiliations, exclude_repo = []) {
if (!username) throw Error("Invalid username");

const res = await retryer(fetcher, { login: username });
// Set default value for ownerAffiliations in GraphQL query won't work because
// parseArray() will always return an empty array even nothing was specified
// and GraphQL would consider that empty arr as a valid value. Nothing will be
// queried in that case as no affiliation is presented.
ownerAffiliations = ownerAffiliations.length > 0 ? ownerAffiliations : ["OWNER"];
const res = await retryer(fetcher, { login: username, ownerAffiliations });

if (res.data.errors) {
logger.error(res.data.errors);
Expand Down
8 changes: 4 additions & 4 deletions tests/fetchStats.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe("Test fetchStats", () => {
it("should fetch correct stats", async () => {
mock.onPost("https://api.github.com/graphql").reply(200, data);

let stats = await fetchStats("anuraghazra");
let stats = await fetchStats("anuraghazra", []);
const rank = calculateRank({
totalCommits: 100,
totalRepos: 5,
Expand All @@ -77,15 +77,15 @@ describe("Test fetchStats", () => {
it("should throw error", async () => {
mock.onPost("https://api.github.com/graphql").reply(200, error);

await expect(fetchStats("anuraghazra")).rejects.toThrow(
await expect(fetchStats("anuraghazra", [])).rejects.toThrow(
"Could not resolve to a User with the login of 'noname'.",
);
});

it("should fetch and add private contributions", async () => {
mock.onPost("https://api.github.com/graphql").reply(200, data);

let stats = await fetchStats("anuraghazra", true);
let stats = await fetchStats("anuraghazra", [], true);
const rank = calculateRank({
totalCommits: 150,
totalRepos: 5,
Expand Down Expand Up @@ -113,7 +113,7 @@ describe("Test fetchStats", () => {
.onGet("https://api.github.com/search/commits?q=author:anuraghazra")
.reply(200, { total_count: 1000 });

let stats = await fetchStats("anuraghazra", true, true);
let stats = await fetchStats("anuraghazra", [], true, true);
const rank = calculateRank({
totalCommits: 1050,
totalRepos: 5,
Expand Down
4 changes: 2 additions & 2 deletions tests/fetchTopLanguages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe("FetchTopLanguages", () => {
it("should fetch correct language data", async () => {
mock.onPost("https://api.github.com/graphql").reply(200, data_langs);

let repo = await fetchTopLanguages("anuraghazra");
let repo = await fetchTopLanguages("anuraghazra", []);
expect(repo).toStrictEqual({
HTML: {
color: "#0f0",
Expand All @@ -77,7 +77,7 @@ describe("FetchTopLanguages", () => {
it("should throw error", async () => {
mock.onPost("https://api.github.com/graphql").reply(200, error);

await expect(fetchTopLanguages("anuraghazra")).rejects.toThrow(
await expect(fetchTopLanguages("anuraghazra", [])).rejects.toThrow(
"Could not resolve to a User with the login of 'noname'.",
);
});
Expand Down
2 changes: 1 addition & 1 deletion vercel.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"functions": {
"api/*.js": {
"memory": 128,
"maxDuration": 30
"maxDuration": 10
}
},
"redirects": [
Expand Down