Skip to content

Commit 037ba0e

Browse files
committed
✨ Manually add role param (ref anuraghazra#2459)
1 parent c85d592 commit 037ba0e

File tree

5 files changed

+63
-8
lines changed

5 files changed

+63
-8
lines changed

api/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export default async (req, res) => {
3838
border_color,
3939
rank_icon,
4040
show,
41+
role,
4142
} = req.query;
4243
res.setHeader("Content-Type", "image/svg+xml");
4344

@@ -71,6 +72,7 @@ export default async (req, res) => {
7172
username,
7273
parseBoolean(include_all_commits),
7374
parseArray(exclude_repo),
75+
parseArray(role),
7476
showStats.includes("prs_merged") ||
7577
showStats.includes("prs_merged_percentage"),
7678
showStats.includes("discussions_started"),

api/top-langs.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export default async (req, res) => {
3232
border_color,
3333
disable_animations,
3434
hide_progress,
35+
role,
3536
} = req.query;
3637
res.setHeader("Content-Type", "image/svg+xml");
3738

@@ -67,6 +68,7 @@ export default async (req, res) => {
6768
parseArray(exclude_repo),
6869
size_weight,
6970
count_weight,
71+
parseArray(role),
7072
);
7173

7274
let cacheSeconds = parseInt(

src/common/utils.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { themes } from "../../themes/index.js";
66

77
const TRY_AGAIN_LATER = "Please try again later";
88

9+
const OWNER_AFFILIATIONS = ["OWNER", "COLLABORATOR", "ORGANIZATION_MEMBER"];
10+
911
const SECONDARY_ERROR_MESSAGES = {
1012
MAX_RETRY:
1113
"You can deploy own instance or wait until public will be no longer limited",
@@ -15,6 +17,9 @@ const SECONDARY_ERROR_MESSAGES = {
1517
GRAPHQL_ERROR: TRY_AGAIN_LATER,
1618
GITHUB_REST_API_ERROR: TRY_AGAIN_LATER,
1719
WAKATIME_USER_NOT_FOUND: "Make sure you have a public WakaTime profile",
20+
INVALID_AFFILIATION: `Invalid owner affiliations. Valid values are: ${OWNER_AFFILIATIONS.join(
21+
", ",
22+
)}`,
1823
};
1924

2025
/**
@@ -37,6 +42,7 @@ class CustomError extends Error {
3742
static GRAPHQL_ERROR = "GRAPHQL_ERROR";
3843
static GITHUB_REST_API_ERROR = "GITHUB_REST_API_ERROR";
3944
static WAKATIME_ERROR = "WAKATIME_ERROR";
45+
static INVALID_AFFILIATION = "INVALID_AFFILIATION";
4046
}
4147

4248
/**
@@ -585,6 +591,37 @@ const parseEmojis = (str) => {
585591
});
586592
};
587593

594+
/**
595+
* Parse owner affiliations.
596+
*
597+
* @param {string[]} affiliations
598+
* @returns {string[]} Parsed affiliations.
599+
*
600+
* @throws {CustomError} If affiliations contains invalid values.
601+
*/
602+
const parseOwnerAffiliations = (affiliations) => {
603+
// Set default value for ownerAffiliations.
604+
// NOTE: Done here since parseArray() will always return an empty array even nothing
605+
//was specified.
606+
affiliations =
607+
affiliations && affiliations.length > 0
608+
? affiliations.map((affiliation) => affiliation.toUpperCase())
609+
: ["OWNER"];
610+
611+
// Check if ownerAffiliations contains valid values.
612+
if (
613+
affiliations.some(
614+
(affiliation) => !OWNER_AFFILIATIONS.includes(affiliation),
615+
)
616+
) {
617+
throw new CustomError(
618+
"Invalid query parameter",
619+
CustomError.INVALID_AFFILIATION,
620+
);
621+
}
622+
return affiliations;
623+
};
624+
588625
/**
589626
* Get diff in minutes between two dates.
590627
*
@@ -618,11 +655,13 @@ export {
618655
wrapTextMultiline,
619656
logger,
620657
CONSTANTS,
658+
OWNER_AFFILIATIONS,
621659
CustomError,
622660
MissingParamError,
623661
measureText,
624662
lowercaseTrim,
625663
chunkArray,
626664
parseEmojis,
665+
parseOwnerAffiliations,
627666
dateDiff,
628667
};

src/fetchers/stats-fetcher.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ import {
1010
MissingParamError,
1111
request,
1212
wrapTextMultiline,
13+
parseOwnerAffiliations,
1314
} from "../common/utils.js";
1415

1516
dotenv.config();
1617

1718
// GraphQL queries.
1819
const GRAPHQL_REPOS_FIELD = `
19-
repositories(first: 100, ownerAffiliations: OWNER, orderBy: {direction: DESC, field: STARGAZERS}, after: $after) {
20+
repositories(first: 100, after: $after, ownerAffiliations: $ownerAffiliations, orderBy: {direction: DESC, field: STARGAZERS}) {
2021
totalCount
2122
nodes {
2223
name
@@ -32,15 +33,15 @@ const GRAPHQL_REPOS_FIELD = `
3233
`;
3334

3435
const GRAPHQL_REPOS_QUERY = `
35-
query userInfo($login: String!, $after: String) {
36-
user(login: $login) {
36+
query userInfo($login: String!, $after: String, $ownerAffiliations: [RepositoryAffiliation]) {
37+
user(login: $login, ownerAffiliations: $ownerAffiliations) {
3738
${GRAPHQL_REPOS_FIELD}
3839
}
3940
}
4041
`;
4142

4243
const GRAPHQL_STATS_QUERY = `
43-
query userInfo($login: String!, $after: String, $includeMergedPullRequests: Boolean!, $includeDiscussions: Boolean!, $includeDiscussionsAnswers: Boolean!) {
44+
query userInfo($login: String!, $after: String, $ownerAffiliations: [RepositoryAffiliation], $includeMergedPullRequests: Boolean!, $includeDiscussions: Boolean!, $includeDiscussionsAnswers: Boolean!) {
4445
user(login: $login) {
4546
name
4647
login
@@ -106,6 +107,7 @@ const fetcher = (variables, token) => {
106107
*
107108
* @param {object} variables Fetcher variables.
108109
* @param {string} variables.username Github username.
110+
* @param {string[]} variables.ownerAffiliations The owner affiliations to filter by. Default: OWNER.
109111
* @param {boolean} variables.includeMergedPullRequests Include merged pull requests.
110112
* @param {boolean} variables.includeDiscussions Include discussions.
111113
* @param {boolean} variables.includeDiscussionsAnswers Include discussions answers.
@@ -115,6 +117,7 @@ const fetcher = (variables, token) => {
115117
*/
116118
const statsFetcher = async ({
117119
username,
120+
ownerAffiliations,
118121
includeMergedPullRequests,
119122
includeDiscussions,
120123
includeDiscussionsAnswers,
@@ -127,6 +130,7 @@ const statsFetcher = async ({
127130
login: username,
128131
first: 100,
129132
after: endCursor,
133+
ownerAffiliations: ownerAffiliations,
130134
includeMergedPullRequests,
131135
includeDiscussions,
132136
includeDiscussionsAnswers,
@@ -214,6 +218,7 @@ const totalCommitsFetcher = async (username) => {
214218
* @param {string} username GitHub username.
215219
* @param {boolean} include_all_commits Include all commits.
216220
* @param {string[]} exclude_repo Repositories to exclude.
221+
* @param {string[]} ownerAffiliations Owner affiliations. Default: OWNER.
217222
* @param {boolean} include_merged_pull_requests Include merged pull requests.
218223
* @param {boolean} include_discussions Include discussions.
219224
* @param {boolean} include_discussions_answers Include discussions answers.
@@ -223,6 +228,7 @@ const fetchStats = async (
223228
username,
224229
include_all_commits = false,
225230
exclude_repo = [],
231+
ownerAffiliations = [],
226232
include_merged_pull_requests = false,
227233
include_discussions = false,
228234
include_discussions_answers = false,
@@ -245,9 +251,11 @@ const fetchStats = async (
245251
contributedTo: 0,
246252
rank: { level: "C", percentile: 100 },
247253
};
254+
ownerAffiliations = parseOwnerAffiliations(ownerAffiliations);
248255

249256
let res = await statsFetcher({
250257
username,
258+
ownerAffiliations,
251259
includeMergedPullRequests: include_merged_pull_requests,
252260
includeDiscussions: include_discussions,
253261
includeDiscussionsAnswers: include_discussions_answers,

src/fetchers/top-languages-fetcher.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
MissingParamError,
77
request,
88
wrapTextMultiline,
9+
parseOwnerAffiliations,
910
} from "../common/utils.js";
1011

1112
/**
@@ -24,10 +25,10 @@ const fetcher = (variables, token) => {
2425
return request(
2526
{
2627
query: `
27-
query userInfo($login: String!) {
28+
query userInfo($login: String!, $ownerAffiliations: [RepositoryAffiliation]) {
2829
user(login: $login) {
29-
# fetch only owner repos & not forks
30-
repositories(ownerAffiliations: OWNER, isFork: false, first: 100) {
30+
# do not fetch forks
31+
repositories(ownerAffiliations: $ownerAffiliations, isFork: false, first: 100) {
3132
nodes {
3233
name
3334
languages(first: 10, orderBy: {field: SIZE, direction: DESC}) {
@@ -61,6 +62,7 @@ const fetcher = (variables, token) => {
6162
*
6263
* @param {string} username GitHub username.
6364
* @param {string[]} exclude_repo List of repositories to exclude.
65+
* @param {string[]} ownerAffiliations The owner affiliations to filter by. Default: OWNER.
6466
* @param {number} size_weight Weightage to be given to size.
6567
* @param {number} count_weight Weightage to be given to count.
6668
* @returns {Promise<TopLangData>} Top languages data.
@@ -70,12 +72,14 @@ const fetchTopLanguages = async (
7072
exclude_repo = [],
7173
size_weight = 1,
7274
count_weight = 0,
75+
ownerAffiliations = [],
7376
) => {
7477
if (!username) {
7578
throw new MissingParamError(["username"]);
7679
}
80+
ownerAffiliations = parseOwnerAffiliations(ownerAffiliations);
7781

78-
const res = await retryer(fetcher, { login: username });
82+
const res = await retryer(fetcher, { login: username, ownerAffiliations });
7983

8084
if (res.data.errors) {
8185
logger.error(res.data.errors);

0 commit comments

Comments
 (0)