-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathcompare_commits.ts
More file actions
303 lines (286 loc) · 8.86 KB
/
Copy pathcompare_commits.ts
File metadata and controls
303 lines (286 loc) · 8.86 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import {
COMMIT_DATA_OUTPUT,
COMMIT_FILE_OUTPUT_PROPERTIES,
USER_FULL_OUTPUT,
} from '@/tools/github/types'
import type { ToolConfig } from '@/tools/types'
interface CompareCommitsParams {
owner: string
repo: string
base: string
head: string
per_page?: number
page?: number
apiKey: string
}
interface CompareCommitsResponse {
success: boolean
output: {
content: string
metadata: {
status: string
ahead_by: number
behind_by: number
total_commits: number
html_url: string
diff_url: string
patch_url: string
base_commit: { sha: string; html_url: string }
merge_base_commit: { sha: string; html_url: string }
commits: Array<{
sha: string
html_url: string
message: string
author: { login?: string; name: string }
}>
files: Array<{
filename: string
status: string
additions: number
deletions: number
changes: number
}>
}
}
}
export const compareCommitsTool: ToolConfig<CompareCommitsParams, CompareCommitsResponse> = {
id: 'github_compare_commits',
name: 'GitHub Compare Commits',
description:
'Compare two commits or branches to see the diff, commits between them, and changed files',
version: '1.0.0',
params: {
owner: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description: 'Repository owner',
},
repo: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description: 'Repository name',
},
base: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description: 'Base branch/tag/SHA for comparison',
},
head: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description: 'Head branch/tag/SHA for comparison',
},
per_page: {
type: 'number',
required: false,
visibility: 'user-or-llm',
description: 'Results per page for files (max 100, default: 30)',
default: 30,
},
page: {
type: 'number',
required: false,
visibility: 'user-or-llm',
description: 'Page number for files (default: 1)',
default: 1,
},
apiKey: {
type: 'string',
required: true,
visibility: 'user-only',
description: 'GitHub API token',
},
},
request: {
url: (params) => {
const url = new URL(
`https://api.github.com/repos/${params.owner}/${params.repo}/compare/${params.base}...${params.head}`
)
if (params.per_page) url.searchParams.append('per_page', String(params.per_page))
if (params.page) url.searchParams.append('page', String(params.page))
return url.toString()
},
method: 'GET',
headers: (params) => ({
Accept: 'application/vnd.github.v3+json',
Authorization: `Bearer ${params.apiKey}`,
'X-GitHub-Api-Version': '2022-11-28',
}),
},
transformResponse: async (response) => {
const data = await response.json()
const commits = (data.commits ?? []).map((c: any) => ({
sha: c.sha,
html_url: c.html_url,
message: c.commit.message,
author: {
login: c.author?.login,
name: c.commit.author.name,
},
}))
const files = (data.files ?? []).map((f: any) => ({
filename: f.filename,
status: f.status,
additions: f.additions,
deletions: f.deletions,
changes: f.changes,
}))
const metadata = {
status: data.status,
ahead_by: data.ahead_by,
behind_by: data.behind_by,
total_commits: data.total_commits,
html_url: data.html_url,
diff_url: data.diff_url,
patch_url: data.patch_url,
base_commit: {
sha: data.base_commit.sha,
html_url: data.base_commit.html_url,
},
merge_base_commit: {
sha: data.merge_base_commit.sha,
html_url: data.merge_base_commit.html_url,
},
commits,
files,
}
const content = `Comparing ${data.base_commit.sha.substring(0, 7)}...${data.commits?.length > 0 ? data.commits[data.commits.length - 1].sha.substring(0, 7) : 'HEAD'}
Status: ${data.status} | Ahead: ${data.ahead_by} | Behind: ${data.behind_by}
Total commits: ${data.total_commits} | Files changed: ${files.length}
${data.html_url}
Commits:
${commits.map((c: any) => ` ${c.sha.substring(0, 7)} - ${c.message.split('\n')[0]}`).join('\n')}
Files changed:
${files.map((f: any) => ` ${f.status}: ${f.filename} (+${f.additions} -${f.deletions})`).join('\n')}`
return {
success: true,
output: {
content,
metadata,
},
}
},
outputs: {
content: { type: 'string', description: 'Human-readable comparison' },
metadata: {
type: 'object',
description: 'Comparison metadata',
properties: {
status: { type: 'string', description: 'ahead, behind, identical, or diverged' },
ahead_by: { type: 'number', description: 'Commits ahead' },
behind_by: { type: 'number', description: 'Commits behind' },
total_commits: { type: 'number', description: 'Total commits between' },
html_url: { type: 'string', description: 'GitHub web URL' },
diff_url: { type: 'string', description: 'Diff URL' },
patch_url: { type: 'string', description: 'Patch URL' },
base_commit: { type: 'object', description: 'Base commit info' },
merge_base_commit: { type: 'object', description: 'Merge base commit info' },
commits: {
type: 'array',
description: 'Commits between base and head',
items: {
type: 'object',
properties: {
sha: { type: 'string', description: 'Commit SHA' },
html_url: { type: 'string', description: 'Web URL' },
message: { type: 'string', description: 'Commit message' },
author: { type: 'object', description: 'Author info' },
},
},
},
files: {
type: 'array',
description: 'Changed files',
items: {
type: 'object',
properties: {
filename: { type: 'string', description: 'File path' },
status: { type: 'string', description: 'Change type' },
additions: { type: 'number', description: 'Lines added' },
deletions: { type: 'number', description: 'Lines deleted' },
changes: { type: 'number', description: 'Total changes' },
},
},
},
},
},
},
}
export const compareCommitsV2Tool: ToolConfig<CompareCommitsParams, any> = {
id: 'github_compare_commits_v2',
name: compareCommitsTool.name,
description: compareCommitsTool.description,
version: '2.0.0',
params: compareCommitsTool.params,
request: compareCommitsTool.request,
transformResponse: async (response: Response) => {
const data = await response.json()
return {
success: true,
output: {
...data,
commits: data.commits ?? [],
files: data.files ?? [],
},
}
},
outputs: {
url: { type: 'string', description: 'API URL' },
html_url: { type: 'string', description: 'GitHub web URL' },
permalink_url: { type: 'string', description: 'Permanent link URL' },
diff_url: { type: 'string', description: 'Diff download URL' },
patch_url: { type: 'string', description: 'Patch download URL' },
status: {
type: 'string',
description: 'Comparison status (ahead, behind, identical, diverged)',
},
ahead_by: { type: 'number', description: 'Commits head is ahead of base' },
behind_by: { type: 'number', description: 'Commits head is behind base' },
total_commits: { type: 'number', description: 'Total commits in comparison' },
base_commit: {
type: 'object',
description: 'Base commit object',
properties: {
sha: { type: 'string', description: 'Commit SHA' },
html_url: { type: 'string', description: 'Web URL' },
commit: COMMIT_DATA_OUTPUT,
author: USER_FULL_OUTPUT,
committer: USER_FULL_OUTPUT,
},
},
merge_base_commit: {
type: 'object',
description: 'Merge base commit object',
properties: {
sha: { type: 'string', description: 'Commit SHA' },
html_url: { type: 'string', description: 'Web URL' },
},
},
commits: {
type: 'array',
description: 'Commits between base and head',
items: {
type: 'object',
properties: {
sha: { type: 'string', description: 'Commit SHA' },
html_url: { type: 'string', description: 'Web URL' },
commit: COMMIT_DATA_OUTPUT,
author: USER_FULL_OUTPUT,
committer: USER_FULL_OUTPUT,
},
},
},
files: {
type: 'array',
description: 'Changed files (diff entries)',
items: {
type: 'object',
properties: COMMIT_FILE_OUTPUT_PROPERTIES,
},
},
},
}