Skip to content

Commit 10d02a0

Browse files
committed
adds option to fetch issues via labels (closes #28)
1 parent dc73307 commit 10d02a0

File tree

4 files changed

+64
-16
lines changed

4 files changed

+64
-16
lines changed

src/v1/adapters/github/graphql.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export const Project = (
7171
* @param {GITHUB_MILESTONE_ISSUE_STATES[] | null} issues_states - The states of the milestone issues.
7272
* @param {GRAMMATICAL_NUMBER} [milestones_amount=GRAMMATICAL_NUMBER.PLURAL] - The amount of milestones.
7373
* @param {number | null} [milestone_number=null] - The number of the milestone.
74+
* @param {string[]} [labels] - The labels of the issues.
7475
* @return {unknown} The result of the function.
7576
*/
7677
export const getAllRepositoriesInProject = (
@@ -80,6 +81,7 @@ export const getAllRepositoriesInProject = (
8081
issues_states: GITHUB_MILESTONE_ISSUE_STATES[] | null = null,
8182
milestones_amount: GRAMMATICAL_NUMBER = GRAMMATICAL_NUMBER.PLURAL,
8283
milestone_number: number | null = null,
84+
labels?: string[],
8385
) => {
8486
const repository = new Repository({
8587
scopes: repository_scopes,
@@ -88,7 +90,7 @@ export const getAllRepositoriesInProject = (
8890
return Project(
8991
project_name,
9092
project_scopes,
91-
repository.getQuery(issues_states, milestones_amount, milestone_number),
93+
repository.getQuery(issues_states, milestones_amount, milestone_number, labels),
9294
);
9395
};
9496

src/v1/adapters/github/index.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,10 +1011,13 @@ const ACCOUNT_LEVEL_CHILDREN = (login_type: "organization" | "user") =>
10111011
{
10121012
scopeName: "count",
10131013
pageSize: query.rootPageSize ?? 1,
1014-
continueAfter:
1015-
query.rootContinueAfter,
1014+
continueAfter: query.rootContinueAfter,
10161015
},
10171016
] as PageSize<GITHUB_REPOSITORY_SCOPES>[],
1017+
null,
1018+
undefined,
1019+
null,
1020+
query.labels?.split(','),
10181021
),
10191022
login_type,
10201023
),
@@ -1026,8 +1029,21 @@ const ACCOUNT_LEVEL_CHILDREN = (login_type: "organization" | "user") =>
10261029
return response;
10271030
},
10281031
{
1032+
query: t.Object({
1033+
rootPageSize: t.Optional(
1034+
t.Numeric({ minimum: 1, maximum: 100 }),
1035+
),
1036+
rootContinueAfter: t.Optional(
1037+
t.MaybeEmpty(t.String()),
1038+
),
1039+
pageSize: t.Optional(
1040+
t.Numeric({ minimum: 1, maximum: 100 }),
1041+
),
1042+
continueAfter: t.Optional(t.MaybeEmpty(t.String())),
1043+
labels: t.Optional(t.String()),
1044+
}),
10291045
detail: {
1030-
description: `Request repository issues in the ${login_type} project.`,
1046+
description: `Request repository issues in the ${login_type} project. Filter by labels with: labels=sprint-01,bug,feature`,
10311047
tags: ["github"],
10321048
},
10331049
},
@@ -1245,6 +1261,9 @@ const ACCOUNT_LEVEL_CHILDREN = (login_type: "organization" | "user") =>
12451261
},
12461262
] as PageSize<GITHUB_REPOSITORY_SCOPES>[],
12471263
issues_states,
1264+
undefined,
1265+
null,
1266+
query.labels?.split(','),
12481267
),
12491268
login_type,
12501269
),
@@ -1276,9 +1295,10 @@ const ACCOUNT_LEVEL_CHILDREN = (login_type: "organization" | "user") =>
12761295
t.MaybeEmpty(t.String()),
12771296
),
12781297
issues_states: t.Optional(t.String()), // enum arrays can not be passed directly in query params, that is why this parameter is validated in the callback
1298+
labels: t.Optional(t.String()),
12791299
}),
12801300
detail: {
1281-
description: `Request repository milestones issues in the ${login_type} project.`,
1301+
description: `Request repository milestones issues in the ${login_type} project. Filter by labels with: labels=sprint-01,bug,feature`,
12821302
tags: ["github"],
12831303
},
12841304
},
@@ -1414,6 +1434,7 @@ const ACCOUNT_LEVEL_CHILDREN = (login_type: "organization" | "user") =>
14141434
issues_states,
14151435
GRAMMATICAL_NUMBER.SINGULAR,
14161436
milestone_id,
1437+
query.labels?.split(','),
14171438
),
14181439
login_type,
14191440
),
@@ -1445,9 +1466,12 @@ const ACCOUNT_LEVEL_CHILDREN = (login_type: "organization" | "user") =>
14451466
t.MaybeEmpty(t.String()),
14461467
),
14471468
issues_states: t.Optional(t.String()), // enum arrays can not be passed directly in query params, that is why this parameter is validated in the callback
1469+
labels: t.Optional(t.String()),
14481470
}),
14491471
detail: {
1450-
description: `Request repository milestone issues in the ${login_type} project. (issues_states=open,closed || issues_states=open || issues_states=closed)`,
1472+
description: `Request repository milestone issues in the ${login_type} project.
1473+
(issues_states=open,closed || issues_states=open || issues_states=closed)
1474+
Filter by labels with: labels=sprint-01,bug,feature`,
14511475
tags: ["github"],
14521476
},
14531477
},

src/v1/adapters/github/scopes.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
GITHUB_MILESTONE_ISSUE_STATES,
44
GITHUB_REPOSITORY_SCOPES,
55
GRAMMATICAL_NUMBER,
6+
IssueFilters,
67
type PageSize,
78
} from "./types";
89
import { DEV_MODE } from "../../../environment";
@@ -583,6 +584,7 @@ export class Repository extends FetcherExtended {
583584
issues_states: GITHUB_MILESTONE_ISSUE_STATES[] | null = null,
584585
milestones_amount: GRAMMATICAL_NUMBER = GRAMMATICAL_NUMBER.PLURAL,
585586
milestone_number: number | null = null,
587+
labels?: IssueFilters["labels"],
586588
) {
587589
const final_issue_states = issues_states ?? [
588590
GITHUB_MILESTONE_ISSUE_STATES.OPEN,
@@ -597,6 +599,7 @@ export class Repository extends FetcherExtended {
597599
final_issue_states,
598600
milestones_amount,
599601
milestone_number,
602+
labels,
600603
);
601604
}
602605

@@ -656,6 +659,7 @@ export class Repository extends FetcherExtended {
656659
issues_states: GITHUB_MILESTONE_ISSUE_STATES[] | null = null,
657660
milestones_amount: GRAMMATICAL_NUMBER = GRAMMATICAL_NUMBER.PLURAL,
658661
milestone_number: number | null = null,
662+
labels?: string[]
659663
) {
660664
if (this.name) {
661665
return `
@@ -674,7 +678,9 @@ export class Repository extends FetcherExtended {
674678
${this.#milestonesBody(
675679
issues_states ?? [GITHUB_MILESTONE_ISSUE_STATES.OPEN],
676680
milestones_amount,
677-
milestone_number)}
681+
milestone_number,
682+
labels
683+
)}
678684
679685
${this.#collaboratorsBody()}
680686
${this.#contributionsBody()}
@@ -707,7 +713,9 @@ export class Repository extends FetcherExtended {
707713
${this.#milestonesBody(
708714
issues_states ?? [GITHUB_MILESTONE_ISSUE_STATES.OPEN],
709715
milestones_amount,
710-
milestone_number)}
716+
milestone_number,
717+
labels
718+
)}
711719
712720
${this.#collaboratorsBody()}
713721
${this.#contributionsBody()}
@@ -1067,6 +1075,7 @@ export class Repository extends FetcherExtended {
10671075
issues_state: GITHUB_MILESTONE_ISSUE_STATES[],
10681076
milestones_amount: GRAMMATICAL_NUMBER = GRAMMATICAL_NUMBER.PLURAL,
10691077
milestone_number: number | null = null,
1078+
labels?: string[]
10701079
) {
10711080
const IS_SINGULAR = milestones_amount === GRAMMATICAL_NUMBER.SINGULAR;
10721081
const head =
@@ -1096,7 +1105,7 @@ export class Repository extends FetcherExtended {
10961105
? `
10971106
${info_body}
10981107
1099-
${this.#issuesBody(issues_state)}
1108+
${this.#issuesBody(issues_state, labels)}
11001109
`
11011110
: `
11021111
${this.#count_nodes ? "totalCount" : ""}
@@ -1109,7 +1118,7 @@ export class Repository extends FetcherExtended {
11091118
nodes {
11101119
${info_body}
11111120
1112-
${this.#issuesBody(issues_state)}
1121+
${this.#issuesBody(issues_state, labels)}
11131122
}
11141123
`
11151124
}
@@ -1119,26 +1128,34 @@ export class Repository extends FetcherExtended {
11191128
if (this.#doFetchIssues) {
11201129
if (this.#log) console.info("fetching issues");
11211130

1122-
return this.#issuesBody(issues_state);
1131+
return this.#issuesBody(issues_state, labels);
11231132
}
11241133

11251134
return "";
11261135
}
11271136

1128-
#issuesBody(state: GITHUB_MILESTONE_ISSUE_STATES[]) {
1137+
#issuesBody(state: GITHUB_MILESTONE_ISSUE_STATES[], labels?: string[]) {
11291138
if (this.#doFetchIssues) {
11301139
const fetchOpen = state.includes(GITHUB_MILESTONE_ISSUE_STATES.OPEN);
11311140
const fetchClosed = state.includes(GITHUB_MILESTONE_ISSUE_STATES.CLOSED);
1141+
1142+
const labelsFilter = labels && labels.length > 0
1143+
? `, labels: [${labels.map(label => `"${label}"`).join(', ')}]`
1144+
: '';
11321145

11331146
const open = `
1134-
open_issues: issues(first: ${this.#issuesPageSize
1135-
}, states: [OPEN], after: ${this.#issuesContinueAfter}) {
1147+
open_issues: issues(first: ${this.#issuesPageSize},
1148+
states: [OPEN]${labelsFilter},
1149+
after: ${this.#issuesContinueAfter}
1150+
) {
11361151
${this.#issuesNodes()}
11371152
}`;
11381153

11391154
const closed = `
1140-
closed_issues: issues(first: ${this.#issuesPageSize
1141-
}, states: [CLOSED], after: ${this.#issuesContinueAfter}) {
1155+
closed_issues: issues(first: ${this.#issuesPageSize},
1156+
states: [CLOSED]${labelsFilter},
1157+
after: ${this.#issuesContinueAfter}
1158+
) {
11421159
${this.#issuesNodes()}
11431160
}`;
11441161

src/v1/adapters/github/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ export interface PageSize<T> {
5151
continueAfter?: string | undefined | null;
5252
}
5353

54+
export interface IssueFilters {
55+
labels?: string[];
56+
states?: GITHUB_MILESTONE_ISSUE_STATES[] | null;
57+
}
58+
5459
export enum GRAMMATICAL_NUMBER {
5560
SINGULAR = 1,
5661
PLURAL = 0,

0 commit comments

Comments
 (0)