Skip to content

Commit 917968d

Browse files
committed
Merge branch 'main' into tommy/reduce-get-issue-context
2 parents 1202368 + f04c137 commit 917968d

File tree

3 files changed

+138
-11
lines changed

3 files changed

+138
-11
lines changed

pkg/github/minimal_types.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,48 @@ type MinimalIssue struct {
172172
IssueType string `json:"issue_type,omitempty"`
173173
}
174174

175+
// MinimalPullRequest is the trimmed output type for pull request objects to reduce verbosity.
176+
type MinimalPullRequest struct {
177+
Number int `json:"number"`
178+
Title string `json:"title"`
179+
Body string `json:"body,omitempty"`
180+
State string `json:"state"`
181+
Draft bool `json:"draft"`
182+
Merged bool `json:"merged"`
183+
MergeableState string `json:"mergeable_state,omitempty"`
184+
HTMLURL string `json:"html_url"`
185+
User *MinimalUser `json:"user,omitempty"`
186+
Labels []string `json:"labels,omitempty"`
187+
Assignees []string `json:"assignees,omitempty"`
188+
RequestedReviewers []string `json:"requested_reviewers,omitempty"`
189+
MergedBy string `json:"merged_by,omitempty"`
190+
Head *MinimalPRBranch `json:"head,omitempty"`
191+
Base *MinimalPRBranch `json:"base,omitempty"`
192+
Additions int `json:"additions,omitempty"`
193+
Deletions int `json:"deletions,omitempty"`
194+
ChangedFiles int `json:"changed_files,omitempty"`
195+
Commits int `json:"commits,omitempty"`
196+
Comments int `json:"comments,omitempty"`
197+
CreatedAt string `json:"created_at,omitempty"`
198+
UpdatedAt string `json:"updated_at,omitempty"`
199+
ClosedAt string `json:"closed_at,omitempty"`
200+
MergedAt string `json:"merged_at,omitempty"`
201+
Milestone string `json:"milestone,omitempty"`
202+
}
203+
204+
// MinimalPRBranch is the trimmed output type for pull request branch references.
205+
type MinimalPRBranch struct {
206+
Ref string `json:"ref"`
207+
SHA string `json:"sha"`
208+
Repo *MinimalPRBranchRepo `json:"repo,omitempty"`
209+
}
210+
211+
// MinimalPRBranchRepo is the trimmed repo info nested inside a PR branch.
212+
type MinimalPRBranchRepo struct {
213+
FullName string `json:"full_name"`
214+
Description string `json:"description,omitempty"`
215+
}
216+
175217
// Helper functions
176218

177219
func convertToMinimalIssue(issue *github.Issue) MinimalIssue {
@@ -239,6 +281,94 @@ func convertToMinimalIssue(issue *github.Issue) MinimalIssue {
239281
return m
240282
}
241283

284+
func convertToMinimalPullRequest(pr *github.PullRequest) MinimalPullRequest {
285+
m := MinimalPullRequest{
286+
Number: pr.GetNumber(),
287+
Title: pr.GetTitle(),
288+
Body: pr.GetBody(),
289+
State: pr.GetState(),
290+
Draft: pr.GetDraft(),
291+
Merged: pr.GetMerged(),
292+
MergeableState: pr.GetMergeableState(),
293+
HTMLURL: pr.GetHTMLURL(),
294+
User: convertToMinimalUser(pr.GetUser()),
295+
Additions: pr.GetAdditions(),
296+
Deletions: pr.GetDeletions(),
297+
ChangedFiles: pr.GetChangedFiles(),
298+
Commits: pr.GetCommits(),
299+
Comments: pr.GetComments(),
300+
}
301+
302+
if pr.CreatedAt != nil {
303+
m.CreatedAt = pr.CreatedAt.Format(time.RFC3339)
304+
}
305+
if pr.UpdatedAt != nil {
306+
m.UpdatedAt = pr.UpdatedAt.Format(time.RFC3339)
307+
}
308+
if pr.ClosedAt != nil {
309+
m.ClosedAt = pr.ClosedAt.Format(time.RFC3339)
310+
}
311+
if pr.MergedAt != nil {
312+
m.MergedAt = pr.MergedAt.Format(time.RFC3339)
313+
}
314+
315+
for _, label := range pr.Labels {
316+
if label != nil {
317+
m.Labels = append(m.Labels, label.GetName())
318+
}
319+
}
320+
321+
for _, assignee := range pr.Assignees {
322+
if assignee != nil {
323+
m.Assignees = append(m.Assignees, assignee.GetLogin())
324+
}
325+
}
326+
327+
for _, reviewer := range pr.RequestedReviewers {
328+
if reviewer != nil {
329+
m.RequestedReviewers = append(m.RequestedReviewers, reviewer.GetLogin())
330+
}
331+
}
332+
333+
if mergedBy := pr.GetMergedBy(); mergedBy != nil {
334+
m.MergedBy = mergedBy.GetLogin()
335+
}
336+
337+
if head := pr.Head; head != nil {
338+
m.Head = convertToMinimalPRBranch(head)
339+
}
340+
341+
if base := pr.Base; base != nil {
342+
m.Base = convertToMinimalPRBranch(base)
343+
}
344+
345+
if milestone := pr.GetMilestone(); milestone != nil {
346+
m.Milestone = milestone.GetTitle()
347+
}
348+
349+
return m
350+
}
351+
352+
func convertToMinimalPRBranch(branch *github.PullRequestBranch) *MinimalPRBranch {
353+
if branch == nil {
354+
return nil
355+
}
356+
357+
b := &MinimalPRBranch{
358+
Ref: branch.GetRef(),
359+
SHA: branch.GetSHA(),
360+
}
361+
362+
if repo := branch.GetRepo(); repo != nil {
363+
b.Repo = &MinimalPRBranchRepo{
364+
FullName: repo.GetFullName(),
365+
Description: repo.GetDescription(),
366+
}
367+
}
368+
369+
return b
370+
}
371+
242372
func convertToMinimalProject(fullProject *github.ProjectV2) *MinimalProject {
243373
if fullProject == nil {
244374
return nil

pkg/github/pullrequests.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,9 @@ func GetPullRequest(ctx context.Context, client *github.Client, deps ToolDepende
186186
}
187187
}
188188

189-
r, err := json.Marshal(pr)
190-
if err != nil {
191-
return nil, fmt.Errorf("failed to marshal response: %w", err)
192-
}
189+
minimalPR := convertToMinimalPullRequest(pr)
193190

194-
return utils.NewToolResultText(string(r)), nil
191+
return MarshalledTextResult(minimalPR), nil
195192
}
196193

197194
func GetPullRequestDiff(ctx context.Context, client *github.Client, owner, repo string, pullNumber int) (*mcp.CallToolResult, error) {

pkg/github/pullrequests_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,14 @@ func Test_GetPullRequest(t *testing.T) {
127127
// Parse the result and get the text content if no error
128128
textContent := getTextResult(t, result)
129129

130-
// Unmarshal and verify the result
131-
var returnedPR github.PullRequest
130+
// Unmarshal and verify the minimal result
131+
var returnedPR MinimalPullRequest
132132
err = json.Unmarshal([]byte(textContent.Text), &returnedPR)
133133
require.NoError(t, err)
134-
assert.Equal(t, *tc.expectedPR.Number, *returnedPR.Number)
135-
assert.Equal(t, *tc.expectedPR.Title, *returnedPR.Title)
136-
assert.Equal(t, *tc.expectedPR.State, *returnedPR.State)
137-
assert.Equal(t, *tc.expectedPR.HTMLURL, *returnedPR.HTMLURL)
134+
assert.Equal(t, tc.expectedPR.GetNumber(), returnedPR.Number)
135+
assert.Equal(t, tc.expectedPR.GetTitle(), returnedPR.Title)
136+
assert.Equal(t, tc.expectedPR.GetState(), returnedPR.State)
137+
assert.Equal(t, tc.expectedPR.GetHTMLURL(), returnedPR.HTMLURL)
138138
})
139139
}
140140
}

0 commit comments

Comments
 (0)