-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIssue.ts
More file actions
211 lines (182 loc) · 7.01 KB
/
Copy pathIssue.ts
File metadata and controls
211 lines (182 loc) · 7.01 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
import { components } from '@octokit/openapi-types';
import { Filter, ListModel, NewData, Stream, toggle } from 'mobx-restful';
import { buildURLData } from 'web-utility';
import { BaseFilter, githubClient } from './client';
import { PullRequestModel } from './PullRequest';
import { User } from './User';
export type Issue = components['schemas']['issue'];
export type IssueComment = components['schemas']['issue-comment'];
export interface IssueFilter extends Filter<Issue>, BaseFilter {
sort?: 'created' | 'updated' | 'comments';
}
export type IssueCommentFilter = Filter<IssueComment> & BaseFilter;
export class IssueModel extends Stream<Issue, IssueFilter>(ListModel) {
client = githubClient;
constructor(
public owner: string,
public repository: string
) {
super();
this.baseURI = `repos/${owner}/${repository}/issues`;
}
async *openStream(filter: IssueFilter) {
var per_page = this.pageSize,
count = 0;
for (let page = 1; ; page++) {
const { body } = await this.client.get<Issue[]>(
`${this.baseURI}?${buildURLData({ per_page, page, ...filter })}`
);
const list = body!.filter(({ pull_request }) => !pull_request);
if (!body![0]) break;
count += list.length;
yield* list;
if (body.length < this.pageSize) break;
}
this.totalCount = count;
}
/**
* Create or update an issue, with support for Copilot assignee
*
* @see {@link https://docs.github.com/en/rest/issues/issues#create-an-issue}
* @see {@link https://docs.github.com/en/rest/issues/issues#update-an-issue}
*/
async updateOne({ assignees = [], ...rest }: Partial<NewData<Issue>>, id?: number) {
const assigneeList = assignees as string[];
const humanAssignees = assigneeList.filter(login => login !== 'copilot-swe-agent');
const hasCopilotAssignee = assigneeList.length !== humanAssignees.length;
const issueData = {
...rest,
...(humanAssignees && { assignees: humanAssignees })
} as Partial<NewData<Issue>>;
const issue = await super.updateOne(issueData, id);
if (hasCopilotAssignee) await this.assignOneToCopilot(issue);
return issue;
}
/**
* Assign Copilot bot to an issue using GitHub GraphQL API
*
* @see {@link https://docs.github.com/en/copilot/how-tos/use-copilot-agents/coding-agent/create-a-pr#assigning-an-existing-issue}
* @see {@link https://github.com/orgs/community/discussions/164267}
*/
@toggle('uploading')
async assignOneToCopilot(issue: Issue) {
const getUserQuery = `
query ($owner: String!, $name: String!) {
repository(owner: $owner, name: $name) {
suggestedActors(
loginNames: "copilot"
capabilities: [CAN_BE_ASSIGNED]
first: 1
) {
nodes {
login
__typename
... on Bot { id }
}
}
}
}`;
type UserQueryResult = {
data: { repository: { suggestedActors: { nodes: User[] } } };
};
const { body: userResult } = await this.client.post<UserQueryResult>(
'https://api.github.com/graphql',
{
query: getUserQuery,
variables: { owner: this.owner, name: this.repository }
}
);
const userId = userResult!.data.repository.suggestedActors.nodes[0].id;
const assignMutation = `
mutation ($issueId: ID!, $userId: ID!) {
replaceActorsForAssignable(input: {assignableId: $issueId, actorIds: [$userId]}) {
assignable {
... on Issue {
id
title
assignees(first: 10) {
nodes { login }
}
}
}
}
}`;
const { body: assignResult } = await this.client.post('https://api.github.com/graphql', {
query: assignMutation,
variables: { issueId: issue.id, userId }
});
return assignResult;
}
/**
* Get pull requests that close an issue using GraphQL
*
* @see {@link https://docs.github.com/en/graphql/reference/objects#pullrequest}
*/
@toggle('downloading')
async getLinkedPRs(issueNumber: number, maxCount = 10) {
const prNumbers = await this.getLinkedPRNumbers(issueNumber, maxCount);
const prModel = new PullRequestModel(this.owner, this.repository);
return Promise.all(prNumbers.map(number => prModel.getOne(number)));
}
private async getLinkedPRNumbers(issueNumber: number, maxCount = 10) {
const query = `
query ($owner: String!, $name: String!, $number: Int!, $maxCount: Int!) {
repository(owner: $owner, name: $name) {
issue(number: $number) {
closedByPullRequestsReferences(first: $maxCount) {
nodes {
number
}
}
}
}
}`;
type IssuePRQueryResult = {
data: {
repository: {
issue: { closedByPullRequestsReferences: { nodes: { number: number }[] } };
};
};
};
const { body } = await this.client.post<IssuePRQueryResult>(
`https://api.github.com/graphql`,
{
query,
variables: {
owner: this.owner,
name: this.repository,
number: issueNumber,
maxCount
}
}
);
return body!.data.repository.issue.closedByPullRequestsReferences.nodes.map(
({ number }) => number
);
}
}
export class IssueCommentModel extends Stream<IssueComment, IssueCommentFilter>(ListModel) {
client = githubClient;
constructor(
public owner: string,
public repository: string,
public issue: number
) {
super();
this.baseURI = `repos/${owner}/${repository}/issues/${issue}/comments`;
}
async *openStream(filter: IssueCommentFilter) {
const { client, baseURI, pageSize: per_page } = this;
var count = 0;
for (let page = 1; ; page++) {
const { body } = await client.get<IssueComment[]>(
`${baseURI}?${buildURLData({ ...filter, per_page, page })}`
);
if (!body![0]) break;
count += body!.length;
yield* body!;
if (body!.length < per_page) break;
}
this.totalCount = count;
}
}