Skip to content

Commit 5cbad8b

Browse files
committed
Refactor IssueService to Externalize GraphQL Queries #7621
1 parent 599abea commit 5cbad8b

4 files changed

Lines changed: 73 additions & 44 deletions

File tree

ai/mcp/server/github-workflow/config.mjs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,19 +113,21 @@ const config = {
113113
* @type {number}
114114
*/
115115
maxLabelsPerIssue: 20,
116-
116+
/**
117+
* Maximum number of labels to fetch for the entire repository in GraphQL queries.
118+
* @type {number}
119+
*/
120+
maxRepoLabels: 100,
117121
/**
118122
* Maximum number of assignees to fetch per issue in GraphQL queries.
119123
* @type {number}
120124
*/
121125
maxAssigneesPerIssue: 10,
122-
123126
/**
124127
* Maximum number of comments to fetch per issue in GraphQL queries.
125128
* @type {number}
126129
*/
127130
maxCommentsPerIssue: 100,
128-
129131
/**
130132
* Maximum number of sub-issues to fetch per issue in GraphQL queries.
131133
* @type {number}

ai/mcp/server/github-workflow/services/IssueService.mjs

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import Base from '../../../../../src/core/Base.mjs';
1+
import Base from '../../../../../src/core/Base.mjs';
22
import GraphqlService from './GraphqlService.mjs';
3-
import aiConfig from '../config.mjs';
4-
import logger from '../logger.mjs';
3+
import aiConfig from '../config.mjs';
4+
import logger from '../logger.mjs';
5+
import { GET_ISSUE_AND_LABEL_IDS } from './queries/issueQueries.mjs';
6+
import { ADD_LABELS, REMOVE_LABELS } from './queries/mutations.mjs';
57

68
/**
79
* Service for interacting with GitHub issues via the GraphQL API.
@@ -31,29 +33,14 @@ class IssueService extends Base {
3133
* @private
3234
*/
3335
async #getIds(issueNumber, labelNames) {
34-
const query = `
35-
query GetIds($owner: String!, $repo: String!, $issueNumber: Int!) {
36-
repository(owner: $owner, name: $repo) {
37-
issue(number: $issueNumber) {
38-
id
39-
}
40-
labels(first: 100) { # Assuming max 100 labels
41-
nodes {
42-
id
43-
name
44-
}
45-
}
46-
}
47-
}
48-
`;
49-
5036
const variables = {
51-
owner: aiConfig.owner,
52-
repo: aiConfig.repo,
53-
issueNumber
37+
owner : aiConfig.owner,
38+
repo : aiConfig.repo,
39+
issueNumber,
40+
maxLabels : aiConfig.issueSync.maxRepoLabels
5441
};
5542

56-
const data = await GraphqlService.query(query, variables);
43+
const data = await GraphqlService.query(GET_ISSUE_AND_LABEL_IDS, variables);
5744

5845
const labelableId = data.repository.issue.id;
5946
const repoLabels = data.repository.labels.nodes;
@@ -79,15 +66,7 @@ class IssueService extends Base {
7966
try {
8067
const { labelableId, labelIds } = await this.#getIds(issueNumber, labels);
8168

82-
const mutation = `
83-
mutation AddLabels($labelableId: ID!, $labelIds: [ID!]!) {
84-
addLabelsToLabelable(input: {labelableId: $labelableId, labelIds: $labelIds}) {
85-
clientMutationId
86-
}
87-
}
88-
`;
89-
90-
await GraphqlService.query(mutation, { labelableId, labelIds });
69+
await GraphqlService.query(ADD_LABELS, { labelableId, labelIds });
9170
return { message: `Successfully added labels to issue #${issueNumber}` };
9271
} catch (error) {
9372
logger.error(`Error adding labels to issue #${issueNumber} via GraphQL:`, error);
@@ -109,15 +88,7 @@ class IssueService extends Base {
10988
try {
11089
const { labelableId, labelIds } = await this.#getIds(issueNumber, labels);
11190

112-
const mutation = `
113-
mutation RemoveLabels($labelableId: ID!, $labelIds: [ID!]!) {
114-
removeLabelsFromLabelable(input: {labelableId: $labelableId, labelIds: $labelIds}) {
115-
clientMutationId
116-
}
117-
}
118-
`;
119-
120-
await GraphqlService.query(mutation, { labelableId, labelIds });
91+
await GraphqlService.query(REMOVE_LABELS, { labelableId, labelIds });
12192
return { message: `Successfully removed labels from issue #${issueNumber}` };
12293
} catch (error) {
12394
logger.error(`Error removing labels from issue #${issueNumber} via GraphQL:`, error);

ai/mcp/server/github-workflow/services/queries/issueQueries.mjs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,29 @@ export const DEFAULT_QUERY_LIMITS = {
222222
maxComments : issueSyncConfig.maxCommentsPerIssue,
223223
maxSubIssues: issueSyncConfig.maxSubIssuesPerIssue
224224
};
225+
226+
/**
227+
* Fetches the GraphQL node ID for an issue and all labels in the repository.
228+
* This is a utility query used by mutations that add/remove labels.
229+
*
230+
* Variables required:
231+
* - $owner: String!
232+
* - $repo: String!
233+
* - $issueNumber: Int!
234+
* - $maxLabels: Int!
235+
*/
236+
export const GET_ISSUE_AND_LABEL_IDS = `
237+
query GetIssueAndLabelIds($owner: String!, $repo: String!, $issueNumber: Int!, $maxLabels: Int!) {
238+
repository(owner: $owner, name: $repo) {
239+
issue(number: $issueNumber) {
240+
id
241+
}
242+
labels(first: $maxLabels) {
243+
nodes {
244+
id
245+
name
246+
}
247+
}
248+
}
249+
}
250+
`;

ai/mcp/server/github-workflow/services/queries/mutations.mjs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,33 @@ export const UPDATE_ISSUE = `
6565
}
6666
}
6767
`;
68+
69+
/**
70+
* Mutation to add labels to a "labelable" item (issue or PR).
71+
*
72+
* Variables required:
73+
* - $labelableId: ID! - The global GraphQL ID of the issue or PR
74+
* - $labelIds: [ID!]! - An array of global GraphQL IDs for the labels to add
75+
*/
76+
export const ADD_LABELS = `
77+
mutation AddLabels($labelableId: ID!, $labelIds: [ID!]!) {
78+
addLabelsToLabelable(input: {labelableId: $labelableId, labelIds: $labelIds}) {
79+
clientMutationId
80+
}
81+
}
82+
`;
83+
84+
/**
85+
* Mutation to remove labels from a "labelable" item (issue or PR).
86+
*
87+
* Variables required:
88+
* - $labelableId: ID! - The global GraphQL ID of the issue or PR
89+
* - $labelIds: [ID!]! - An array of global GraphQL IDs for the labels to remove
90+
*/
91+
export const REMOVE_LABELS = `
92+
mutation RemoveLabels($labelableId: ID!, $labelIds: [ID!]!) {
93+
removeLabelsFromLabelable(input: {labelableId: $labelableId, labelIds: $labelIds}) {
94+
clientMutationId
95+
}
96+
}
97+
`;

0 commit comments

Comments
 (0)