-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathcreate_pr_review.ts
More file actions
182 lines (170 loc) · 5.44 KB
/
Copy pathcreate_pr_review.ts
File metadata and controls
182 lines (170 loc) · 5.44 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
import type { CreatePRReviewParams, PRReviewResponse } from '@/tools/github/types'
import { USER_OUTPUT } from '@/tools/github/types'
import type { ToolConfig } from '@/tools/types'
export const createPRReviewTool: ToolConfig<CreatePRReviewParams, PRReviewResponse> = {
id: 'github_create_pr_review',
name: 'GitHub Create PR Review',
description:
'Submit a review for a pull request. Use APPROVE, REQUEST_CHANGES, or COMMENT. A body is required for REQUEST_CHANGES and COMMENT reviews.',
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',
},
pullNumber: {
type: 'number',
required: true,
visibility: 'user-or-llm',
description: 'Pull request number',
},
event: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description: 'The review action to perform: APPROVE, REQUEST_CHANGES, or COMMENT',
},
body: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'The body text of the review (required for REQUEST_CHANGES and COMMENT)',
},
commit_id: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'The SHA of the commit that needs a review (defaults to the most recent commit)',
},
apiKey: {
type: 'string',
required: true,
visibility: 'user-only',
description: 'GitHub API token',
},
},
request: {
url: (params) =>
`https://api.github.com/repos/${params.owner}/${params.repo}/pulls/${params.pullNumber}/reviews`,
method: 'POST',
headers: (params) => ({
Accept: 'application/vnd.github+json',
Authorization: `Bearer ${params.apiKey}`,
'X-GitHub-Api-Version': '2022-11-28',
}),
body: (params) => {
const body: Record<string, any> = {
event: params.event,
}
if (params.body) body.body = params.body
if (params.commit_id) body.commit_id = params.commit_id
return body
},
},
transformResponse: async (response) => {
if (!response.ok) {
const error = await response.json().catch(() => ({}))
return {
success: false,
error: error.message || `Failed to submit PR review (HTTP ${response.status})`,
output: {
content: '',
metadata: { id: 0, state: '', body: '', html_url: '', commit_id: '' },
},
}
}
const review = await response.json()
const content = `Review submitted for PR #${review.pull_request_url?.split('/').pop() ?? ''}
State: ${review.state}
URL: ${review.html_url}`
return {
success: true,
output: {
content,
metadata: {
id: review.id,
state: review.state,
body: review.body ?? '',
html_url: review.html_url,
commit_id: review.commit_id,
},
},
}
},
outputs: {
content: { type: 'string', description: 'Human-readable review confirmation' },
metadata: {
type: 'object',
description: 'Review metadata',
properties: {
id: { type: 'number', description: 'Review ID' },
state: {
type: 'string',
description: 'Review state (APPROVED/CHANGES_REQUESTED/COMMENTED)',
},
body: { type: 'string', description: 'Review body text' },
html_url: { type: 'string', description: 'GitHub web URL for the review' },
commit_id: { type: 'string', description: 'SHA of the reviewed commit' },
},
},
},
}
export const createPRReviewV2Tool: ToolConfig<CreatePRReviewParams, any> = {
id: 'github_create_pr_review_v2',
name: createPRReviewTool.name,
description: createPRReviewTool.description,
version: '2.0.0',
params: createPRReviewTool.params,
request: createPRReviewTool.request,
transformResponse: async (response: Response) => {
if (!response.ok) {
const error = await response.json().catch(() => ({}))
return {
success: false,
error: error.message || `Failed to submit PR review (HTTP ${response.status})`,
output: {
id: 0,
user: null,
body: null,
state: '',
html_url: '',
pull_request_url: '',
commit_id: '',
submitted_at: null,
},
}
}
const review = await response.json()
return {
success: true,
output: {
id: review.id,
user: review.user ?? null,
body: review.body ?? null,
state: review.state,
html_url: review.html_url,
pull_request_url: review.pull_request_url,
commit_id: review.commit_id,
submitted_at: review.submitted_at ?? null,
},
}
},
outputs: {
id: { type: 'number', description: 'Review ID' },
user: { ...USER_OUTPUT, optional: true },
body: { type: 'string', description: 'Review body text' },
state: { type: 'string', description: 'Review state (APPROVED/CHANGES_REQUESTED/COMMENTED)' },
html_url: { type: 'string', description: 'GitHub web URL for the review' },
pull_request_url: { type: 'string', description: 'API URL of the reviewed pull request' },
commit_id: { type: 'string', description: 'SHA of the reviewed commit' },
submitted_at: { type: 'string', description: 'Review submission timestamp' },
},
}