-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathissue.handler.ts
More file actions
254 lines (213 loc) · 7.69 KB
/
Copy pathissue.handler.ts
File metadata and controls
254 lines (213 loc) · 7.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import { BaseHandler } from '../../../core/handlers/base.handler.js';
import { BaseToolResponse } from '../../../core/interfaces/tool-handler.interface.js';
import { LinearAuth } from '../../../auth.js';
import { LinearGraphQLClient } from '../../../graphql/client.js';
import {
IssueHandlerMethods,
CreateIssueInput,
CreateIssuesInput,
BulkUpdateIssuesInput,
SearchIssuesInput,
DeleteIssueInput,
DeleteIssuesInput,
CreateIssueResponse,
CreateIssuesResponse,
UpdateIssuesResponse,
SearchIssuesResponse,
DeleteIssueResponse,
Issue,
IssueBatchResponse
} from '../types/issue.types.js';
/**
* Handler for issue-related operations.
* Manages creating, updating, searching, and deleting issues.
*/
export class IssueHandler extends BaseHandler implements IssueHandlerMethods {
constructor(auth: LinearAuth, graphqlClient?: LinearGraphQLClient) {
super(auth, graphqlClient);
}
/**
* Creates a single issue.
*/
async handleCreateIssue(args: CreateIssueInput): Promise<BaseToolResponse> {
try {
const client = this.verifyAuth();
this.validateRequiredParams(args, ['title', 'description', 'teamId']);
const result = await client.createIssue(args) as CreateIssueResponse;
if (!result.issueCreate.success || !result.issueCreate.issue) {
throw new Error('Failed to create issue');
}
const issue = result.issueCreate.issue;
return this.createResponse(
`Successfully created issue\n` +
`Issue: ${issue.identifier}\n` +
`Title: ${issue.title}\n` +
`URL: ${issue.url}\n` +
`Project: ${issue.project ? issue.project.name : 'None'}`
);
} catch (error) {
this.handleError(error, 'create issue');
}
}
/**
* Creates multiple issues in bulk.
*/
async handleCreateIssues(args: CreateIssuesInput): Promise<BaseToolResponse> {
try {
const client = this.verifyAuth();
this.validateRequiredParams(args, ['issues']);
if (!Array.isArray(args.issues)) {
throw new Error('Issues parameter must be an array');
}
const result = await client.createIssues(args.issues) as IssueBatchResponse;
if (!result.issueBatchCreate.success) {
throw new Error('Failed to create issues');
}
const createdIssues = result.issueBatchCreate.issues as Issue[];
return this.createResponse(
`Successfully created ${createdIssues.length} issues:\n` +
createdIssues.map(issue =>
`- ${issue.identifier}: ${issue.title}\n URL: ${issue.url}`
).join('\n')
);
} catch (error) {
this.handleError(error, 'create issues');
}
}
/**
* Updates multiple issues in bulk.
*/
async handleBulkUpdateIssues(args: BulkUpdateIssuesInput): Promise<BaseToolResponse> {
try {
const client = this.verifyAuth();
this.validateRequiredParams(args, ['issueIds', 'update']);
if (!Array.isArray(args.issueIds)) {
throw new Error('IssueIds parameter must be an array');
}
// Handle state name instead of state ID (from PR #3 + fix)
if (
args.update.stateId &&
typeof args.update.stateId === 'string' &&
!args.update.stateId.match(
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
)
) {
// This looks like a state name, not a UUID
const stateName = args.update.stateId.toLowerCase();
// Get all teams to find the state
const teamsResponse = await client.getTeams();
// TODO: Add proper typing for teamsResponse if possible
const teams = (teamsResponse as any).teams.nodes;
let stateId: string | undefined;
// Search through all teams and their states to find a matching state name
for (const team of teams) {
// Access the 'nodes' array within the 'states' object, matching the updated type
// TODO: Add proper typing for team.states if possible
const matchingState = (team.states as any).nodes.find(
(state: any) => state.name.toLowerCase() === stateName
);
if (matchingState) {
stateId = matchingState.id;
break;
}
}
if (!stateId) {
throw new Error(
`Could not find state with name: ${args.update.stateId}`
);
}
// Replace the state name with the state ID
args.update.stateId = stateId;
}
const result = await client.updateIssues(args.issueIds, args.update) as UpdateIssuesResponse;
if (!result.issueUpdate.success) {
throw new Error('Failed to update issues');
}
const updatedCount = result.issueUpdate.issues.length;
return this.createResponse(`Successfully updated ${updatedCount} issues`);
} catch (error) {
this.handleError(error, 'update issues');
}
}
/**
* Searches for issues with filtering and pagination.
*/
async handleSearchIssues(args: SearchIssuesInput): Promise<BaseToolResponse> {
try {
const client = this.verifyAuth();
const filter: Record<string, unknown> = {};
// Check if the query looks like an issue identifier (e.g., EXE-5143) (from PR #3)
if (args.query && /^[A-Z]+-\d+$/.test(args.query.trim())) {
// If it's an issue identifier, parse the team key and issue number
const [teamKey, issueNumber] = args.query.trim().split('-');
// Use team.key and number filters instead of identifier
filter.team = { key: { eq: teamKey } };
filter.number = { eq: parseInt(issueNumber, 10) };
} else if (args.query) {
// Otherwise use it as a search term
filter.search = args.query;
}
if (args.filter?.project?.id?.eq) {
filter.project = { id: { eq: args.filter.project.id.eq } };
}
if (args.teamIds) {
filter.team = { id: { in: args.teamIds } };
}
if (args.assigneeIds) {
filter.assignee = { id: { in: args.assigneeIds } };
}
if (args.states) {
filter.state = { name: { in: args.states } };
}
if (typeof args.priority === 'number') {
filter.priority = { eq: args.priority };
}
const result = await client.searchIssues(
filter,
args.first || 50,
args.after,
args.orderBy || 'updatedAt'
) as SearchIssuesResponse;
return this.createJsonResponse(result);
} catch (error) {
this.handleError(error, 'search issues');
}
}
/**
* Deletes a single issue.
*/
async handleDeleteIssue(args: DeleteIssueInput): Promise<BaseToolResponse> {
try {
const client = this.verifyAuth();
this.validateRequiredParams(args, ['id']);
const result = await client.deleteIssue(args.id) as DeleteIssueResponse;
if (!result.issueDelete.success) {
throw new Error('Failed to delete issue');
}
return this.createResponse(`Successfully deleted issue ${args.id}`);
} catch (error) {
this.handleError(error, 'delete issue');
}
}
/**
* Deletes multiple issues in bulk.
*/
async handleDeleteIssues(args: DeleteIssuesInput): Promise<BaseToolResponse> {
try {
const client = this.verifyAuth();
this.validateRequiredParams(args, ['ids']);
if (!Array.isArray(args.ids)) {
throw new Error('Ids parameter must be an array');
}
const result = await client.deleteIssues(args.ids) as DeleteIssueResponse;
if (!result.issueDelete.success) {
throw new Error('Failed to delete issues');
}
return this.createResponse(
`Successfully deleted ${args.ids.length} issues: ${args.ids.join(', ')}`
);
} catch (error) {
this.handleError(error, 'delete issues');
}
}
}