-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-api.js
More file actions
227 lines (215 loc) · 9.53 KB
/
github-api.js
File metadata and controls
227 lines (215 loc) · 9.53 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
import { gh } from './helpers.js';
export async function fetchMyPRs(log) {
log('Fetching my open PRs...', 'info');
const raw = await gh('api', 'search/issues?q=author:@me+type:pr+state:open&per_page=100', { __reason: 'my open PRs' });
const data = JSON.parse(raw);
const prs = data.items.map(item => ({
title: item.title,
html_url: item.html_url,
repo: item.repository_url.split('/').slice(-2).join('/'),
number: item.number,
updated_at: item.updated_at,
}));
log(`Found ${prs.length} open PRs`, 'success');
return prs;
}
export async function fetchReviewPRs(log) {
log('Fetching PRs awaiting my review...', 'info');
const raw = await gh('api', 'search/issues?q=review-requested:@me+type:pr+state:open&per_page=100', { __reason: 'PRs awaiting my review' });
const data = JSON.parse(raw);
const prs = data.items.map(item => ({
title: item.title,
html_url: item.html_url,
repo: item.repository_url.split('/').slice(-2).join('/'),
number: item.number,
author: item.user?.login || 'unknown',
updated_at: item.updated_at,
}));
log(`Found ${prs.length} PRs awaiting review`, 'success');
return prs;
}
export async function fetchMentionedPRs(log, since) {
log(`Fetching PRs I was mentioned in (since ${since})...`, 'info');
const raw = await gh('api', `search/issues?q=mentions:@me+type:pr+updated:>${since}&per_page=100&sort=updated&order=desc`, { __reason: 'mentioned PRs' });
const data = JSON.parse(raw);
const prs = data.items.map(item => ({
title: item.title,
html_url: item.html_url,
repo: item.repository_url.split('/').slice(-2).join('/'),
number: item.number,
author: item.user?.login || 'unknown',
updated_at: item.updated_at,
state: item.pull_request?.merged_at ? 'merged' : item.state,
}));
log(`Found ${prs.length} mentioned PRs`, 'success');
return prs;
}
export async function fetchAssignedIssues(log) {
log('Fetching issues assigned to me...', 'info');
const raw = await gh('api', 'search/issues?q=assignee:@me+type:issue+state:open&per_page=100', { __reason: 'assigned issues' });
const data = JSON.parse(raw);
const issues = data.items.map(item => ({
title: item.title,
html_url: item.html_url,
repo: item.repository_url.split('/').slice(-2).join('/'),
number: item.number,
updated_at: item.updated_at,
}));
log(`Found ${issues.length} assigned issues`, 'success');
return issues;
}
export async function fetchMentionedIssues(log, since) {
log(`Fetching issues I was mentioned in (since ${since})...`, 'info');
const raw = await gh('api', `search/issues?q=mentions:@me+type:issue+state:open+updated:>${since}&per_page=100&sort=updated&order=desc`, { __reason: 'mentioned issues' });
const data = JSON.parse(raw);
const issues = data.items.map(item => ({
title: item.title,
html_url: item.html_url,
repo: item.repository_url.split('/').slice(-2).join('/'),
number: item.number,
updated_at: item.updated_at,
}));
log(`Found ${issues.length} mentioned issues`, 'success');
return issues;
}
export async function fetchCreatedIssues(log) {
log('Fetching issues I created...', 'info');
const raw = await gh('api', 'search/issues?q=author:@me+type:issue+state:open&per_page=100', { __reason: 'created issues' });
const data = JSON.parse(raw);
const issues = data.items.map(item => ({
title: item.title,
html_url: item.html_url,
repo: item.repository_url.split('/').slice(-2).join('/'),
number: item.number,
updated_at: item.updated_at,
}));
log(`Found ${issues.length} created issues`, 'success');
return issues;
}
export async function fetchCommentedPRs(log, since) {
log(`Fetching PRs I commented on (since ${since})...`, 'info');
const raw = await gh('api', `search/issues?q=commenter:@me+type:pr+updated:>${since}&per_page=100&sort=updated&order=desc`, { __reason: 'commented PRs' });
const data = JSON.parse(raw);
const prs = data.items.map(item => ({
title: item.title,
html_url: item.html_url,
repo: item.repository_url.split('/').slice(-2).join('/'),
number: item.number,
author: item.user.login,
updated_at: item.updated_at,
state: item.pull_request?.merged_at ? 'merged' : item.state,
}));
log(`Found ${prs.length} commented PRs`, 'success');
return prs;
}
export async function fetchCommentedIssues(log, since) {
log(`Fetching issues I commented on (since ${since})...`, 'info');
const raw = await gh('api', `search/issues?q=commenter:@me+type:issue+updated:>${since}&per_page=100&sort=updated&order=desc`, { __reason: 'commented issues' });
const data = JSON.parse(raw);
const issues = data.items.map(item => ({
title: item.title,
html_url: item.html_url,
repo: item.repository_url.split('/').slice(-2).join('/'),
number: item.number,
updated_at: item.updated_at,
}));
log(`Found ${issues.length} commented issues`, 'success');
return issues;
}
export async function fetchRecentComments(repo, number, { isPR } = {}) {
const fetches = [gh('api', `repos/${repo}/issues/${number}/comments?per_page=100`, { __reason: `comments for ${repo}#${number}` })];
if (isPR) fetches.push(gh('api', `repos/${repo}/pulls/${number}/comments?per_page=100`, { __reason: `review comments for ${repo}#${number}` }));
const results = await Promise.all(fetches);
const all = results.flatMap(raw => JSON.parse(raw));
return all.map(c => ({
author: c.user?.login || 'unknown',
body: c.body || '',
createdAt: c.created_at,
url: c.html_url,
}));
}
export async function fetchIssueDetails(repo, number, signal) {
const [owner, name] = repo.split('/');
const query = `{repository(owner:${JSON.stringify(owner)},name:${JSON.stringify(name)}){issue(number:${number}){body,labels(first:20){nodes{name}},assignees(first:20){nodes{login}},comments(first:100){nodes{databaseId,body,createdAt,url,author{login},reactions(first:20){nodes{content,user{login}}}}}}}}`;
const raw = await gh('api', 'graphql', '-f', `query=${query}`, { __reason: `issue details for ${repo}#${number}` }, signal);
const issue = JSON.parse(raw).data.repository.issue;
const commentReactions = new Map();
const comments = (issue.comments?.nodes || []).map(c => {
const reactions = (c.reactions?.nodes || []);
if (reactions.length) {
const grouped = {};
for (const r of reactions) {
if (!grouped[r.content]) grouped[r.content] = [];
grouped[r.content].push(r.user?.login || '?');
}
commentReactions.set(c.databaseId, Object.entries(grouped).map(([k, v]) => `${k}:${v.join(',')}`).join(' '));
}
return { author: c.author, body: c.body, createdAt: c.createdAt, url: c.url };
});
return {
body: issue.body,
labels: (issue.labels?.nodes || []),
assignees: (issue.assignees?.nodes || []),
comments,
commentReactions,
};
}
export async function fetchPRSummary(repo, number, signal) {
const raw = await gh('pr', 'view', String(number), '--repo', repo,
'--json', 'reviewDecision,statusCheckRollup,comments,reviews,reviewRequests,latestReviews,updatedAt,isDraft,mergeable,labels,body,headRefName,headRefOid', { __reason: `PR summary for ${repo}#${number}` }, signal);
return JSON.parse(raw);
}
export async function fetchPRPromptData(repo, number, signal) {
const [owner, name] = repo.split('/');
const threadsQuery = `{repository(owner:${JSON.stringify(owner)},name:${JSON.stringify(name)}){pullRequest(number:${number}){url,comments(first:100){nodes{databaseId,reactions(first:20){nodes{content,user{login}}}}},commits(first:100){nodes{commit{oid,message,committedDate,author{user{login}}}}},reviewThreads(first:100){nodes{isOutdated,comments(first:100){nodes{databaseId,path,createdAt,body,author{login},replyTo{databaseId},line,originalLine,diffHunk}}}}}}}`;
const [diffRaw, threadsRaw] = await Promise.all([
gh('pr', 'diff', String(number), '--repo', repo, { __reason: `PR diff for ${repo}#${number}` }, signal),
gh('api', 'graphql', '-f', `query=${threadsQuery}`, { __reason: `PR threads for ${repo}#${number}` }, signal),
]);
const prData = JSON.parse(threadsRaw).data.repository.pullRequest;
const prUrl = prData.url;
// Build map of comment reactions with usernames
const commentReactions = new Map();
for (const c of (prData.comments?.nodes || [])) {
const reactions = (c.reactions?.nodes || []);
if (reactions.length) {
const grouped = {};
for (const r of reactions) {
if (!grouped[r.content]) grouped[r.content] = [];
grouped[r.content].push(r.user?.login || '?');
}
commentReactions.set(c.databaseId, Object.entries(grouped).map(([k, v]) => `${k}:${v.join(',')}`).join(' '));
}
}
const threads = prData.reviewThreads.nodes;
const reviewComments = [];
for (const thread of threads) {
for (const c of thread.comments.nodes) {
const isRoot = !c.replyTo;
reviewComments.push({
id: c.databaseId,
path: c.path,
created_at: c.createdAt,
body: c.body,
user: { login: c.author?.login },
in_reply_to_id: c.replyTo?.databaseId || null,
isOutdated: thread.isOutdated,
line: isRoot ? (c.line || c.originalLine) : null,
diffHunk: isRoot ? c.diffHunk : null,
url: `${prUrl}#discussion_r${c.databaseId}`,
});
}
}
const commits = (prData.commits?.nodes || []).map(n => ({
sha: n.commit.oid?.slice(0, 7),
message: n.commit.message,
date: n.commit.committedDate,
author: n.commit.author?.user?.login,
}));
return {
diff: diffRaw.length > 20000 ? diffRaw.slice(0, 20000) + '\n... (truncated)' : diffRaw,
reviewComments,
commentReactions,
commits,
};
}