Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions pkg/github/minimal_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ type MinimalSearchRepositoriesResult struct {
Items []MinimalRepository `json:"items"`
}

// MinimalCodeSearchResult is the trimmed output type for code search results.
type MinimalCodeSearchResult struct {
TotalCount int `json:"total_count"`
IncompleteResults bool `json:"incomplete_results"`
Items []MinimalCodeResult `json:"items"`
}

// MinimalCodeResult is the trimmed output type for a single code search hit.
type MinimalCodeResult struct {
Name string `json:"name"`
Path string `json:"path"`
SHA string `json:"sha"`
HTMLURL string `json:"html_url"`
Repository string `json:"repository"`
TextMatches []*github.TextMatch `json:"text_matches,omitempty"`
}

// MinimalCommitAuthor represents commit author information.
type MinimalCommitAuthor struct {
Name string `json:"name,omitempty"`
Expand Down
28 changes: 25 additions & 3 deletions pkg/github/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,9 @@ func SearchCode(t translations.TranslationHelperFunc) inventory.ServerTool {
}

opts := &github.SearchOptions{
Sort: sort,
Order: order,
Sort: sort,
Order: order,
TextMatch: true,
Comment thread
SamMorrowDrums marked this conversation as resolved.
Resolved
ListOptions: github.ListOptions{
PerPage: pagination.PerPage,
Page: pagination.Page,
Expand Down Expand Up @@ -301,7 +302,28 @@ func SearchCode(t translations.TranslationHelperFunc) inventory.ServerTool {
return ghErrors.NewGitHubAPIStatusErrorResponse(ctx, "failed to search code", resp, body), nil, nil
}

r, err := json.Marshal(result)
minimalItems := make([]MinimalCodeResult, 0, len(result.CodeResults))
for _, code := range result.CodeResults {
item := MinimalCodeResult{
Name: code.GetName(),
Path: code.GetPath(),
SHA: code.GetSHA(),
HTMLURL: code.GetHTMLURL(),
TextMatches: code.TextMatches,
}
if code.Repository != nil {
item.Repository = code.Repository.GetFullName()
}
minimalItems = append(minimalItems, item)
}

minimalResult := &MinimalCodeSearchResult{
TotalCount: result.GetTotal(),
IncompleteResults: result.GetIncompleteResults(),
Items: minimalItems,
}

r, err := json.Marshal(minimalResult)
Comment thread
SamMorrowDrums marked this conversation as resolved.
Resolved
if err != nil {
return utils.NewToolResultErrorFromErr("failed to marshal response", err), nil, nil
}
Expand Down
62 changes: 41 additions & 21 deletions pkg/github/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,18 +430,29 @@ func Test_SearchCode(t *testing.T) {
IncompleteResults: github.Ptr(false),
CodeResults: []*github.CodeResult{
{
Name: github.Ptr("file1.go"),
Path: github.Ptr("path/to/file1.go"),
SHA: github.Ptr("abc123def456"),
HTMLURL: github.Ptr("https://github.com/owner/repo/blob/main/path/to/file1.go"),
Repository: &github.Repository{Name: github.Ptr("repo"), FullName: github.Ptr("owner/repo")},
Name: github.Ptr("file1.go"),
Path: github.Ptr("path/to/file1.go"),
SHA: github.Ptr("abc123def456"),
HTMLURL: github.Ptr("https://github.com/owner/repo/blob/main/path/to/file1.go"),
Repository: &github.Repository{
Name: github.Ptr("repo"),
FullName: github.Ptr("owner/repo"),
},
TextMatches: []*github.TextMatch{
{
Fragment: github.Ptr("func main() { fmt.Println(\"hello\") }"),
},
},
},
{
Name: github.Ptr("file2.go"),
Path: github.Ptr("path/to/file2.go"),
SHA: github.Ptr("def456abc123"),
HTMLURL: github.Ptr("https://github.com/owner/repo/blob/main/path/to/file2.go"),
Repository: &github.Repository{Name: github.Ptr("repo"), FullName: github.Ptr("owner/repo")},
Name: github.Ptr("file2.go"),
Path: github.Ptr("path/to/file2.go"),
SHA: github.Ptr("def456abc123"),
HTMLURL: github.Ptr("https://github.com/owner/repo/blob/main/path/to/file2.go"),
Repository: &github.Repository{
Name: github.Ptr("repo"),
FullName: github.Ptr("owner/repo"),
},
},
},
}
Expand Down Expand Up @@ -540,19 +551,28 @@ func Test_SearchCode(t *testing.T) {
// Parse the result and get the text content if no error
textContent := getTextResult(t, result)

// Unmarshal and verify the result
var returnedResult github.CodeSearchResult
// Unmarshal and verify the minimal result
var returnedResult MinimalCodeSearchResult
err = json.Unmarshal([]byte(textContent.Text), &returnedResult)
require.NoError(t, err)
assert.Equal(t, *tc.expectedResult.Total, *returnedResult.Total)
assert.Equal(t, *tc.expectedResult.IncompleteResults, *returnedResult.IncompleteResults)
assert.Len(t, returnedResult.CodeResults, len(tc.expectedResult.CodeResults))
for i, code := range returnedResult.CodeResults {
assert.Equal(t, *tc.expectedResult.CodeResults[i].Name, *code.Name)
assert.Equal(t, *tc.expectedResult.CodeResults[i].Path, *code.Path)
assert.Equal(t, *tc.expectedResult.CodeResults[i].SHA, *code.SHA)
assert.Equal(t, *tc.expectedResult.CodeResults[i].HTMLURL, *code.HTMLURL)
assert.Equal(t, *tc.expectedResult.CodeResults[i].Repository.FullName, *code.Repository.FullName)
assert.Equal(t, *tc.expectedResult.Total, returnedResult.TotalCount)
assert.Equal(t, *tc.expectedResult.IncompleteResults, returnedResult.IncompleteResults)
assert.Len(t, returnedResult.Items, len(tc.expectedResult.CodeResults))
for i, code := range returnedResult.Items {
assert.Equal(t, tc.expectedResult.CodeResults[i].GetName(), code.Name)
assert.Equal(t, tc.expectedResult.CodeResults[i].GetPath(), code.Path)
assert.Equal(t, tc.expectedResult.CodeResults[i].GetSHA(), code.SHA)
assert.Equal(t, tc.expectedResult.CodeResults[i].GetHTMLURL(), code.HTMLURL)
assert.Equal(t, tc.expectedResult.CodeResults[i].Repository.GetFullName(), code.Repository)
}

// Verify text matches are included when present
if len(tc.expectedResult.CodeResults[0].TextMatches) > 0 {
require.NotEmpty(t, returnedResult.Items[0].TextMatches)
assert.Equal(t,
tc.expectedResult.CodeResults[0].TextMatches[0].GetFragment(),
returnedResult.Items[0].TextMatches[0].GetFragment(),
)
}
})
}
Expand Down
Loading