Skip to content

Commit f969d44

Browse files
committed
make search_issues field value enrichment best-effort
1 parent 439ec71 commit f969d44

2 files changed

Lines changed: 68 additions & 8 deletions

File tree

pkg/github/issues.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1967,9 +1967,9 @@ func fetchIssueReadEnrichment(ctx context.Context, gqlClient *githubv4.Client, n
19671967
return enrichment, nil
19681968
}
19691969

1970-
// searchIssuesHandler runs the REST issues search, enriches each hit with custom field values
1971-
// fetched via a single follow-up GraphQL nodes() query, and applies any post-process options
1972-
// (e.g. IFC labelling).
1970+
// searchIssuesHandler runs the REST issues search, enriches each hit (best-effort) with custom
1971+
// field values fetched via a single follow-up GraphQL nodes() query, and applies any post-process
1972+
// options (e.g. IFC labelling).
19731973
func searchIssuesHandler(ctx context.Context, deps ToolDependencies, args map[string]any, options ...searchOption) (*mcp.CallToolResult, error) {
19741974
const errorPrefix = "failed to search issues"
19751975

@@ -1996,15 +1996,18 @@ func searchIssuesHandler(ctx context.Context, deps ToolDependencies, args map[st
19961996
return ghErrors.NewGitHubAPIStatusErrorResponse(ctx, errorPrefix, resp, body), nil
19971997
}
19981998

1999+
// The field value enrichment is best-effort: a failure here (e.g. a server whose
2000+
// GraphQL schema predates the issueFieldValues field) must never fail the search.
19992001
var fieldValuesByID map[string][]MinimalFieldValue
20002002
if len(result.Issues) > 0 {
20012003
gqlClient, err := deps.GetGQLClient(ctx)
20022004
if err != nil {
2003-
return utils.NewToolResultErrorFromErr(errorPrefix+": failed to get GitHub GraphQL client", err), nil
2004-
}
2005-
fieldValuesByID, err = fetchIssueFieldValuesByNodeID(ctx, gqlClient, result.Issues)
2006-
if err != nil {
2007-
return ghErrors.NewGitHubGraphQLErrorResponse(ctx, errorPrefix+": failed to fetch issue field values", err), nil
2005+
_, _ = ghErrors.NewGitHubGraphQLErrorToCtx(ctx, errorPrefix+": failed to get GitHub GraphQL client", err)
2006+
} else {
2007+
fieldValuesByID, err = fetchIssueFieldValuesByNodeID(ctx, gqlClient, result.Issues)
2008+
if err != nil {
2009+
_, _ = ghErrors.NewGitHubGraphQLErrorToCtx(ctx, errorPrefix+": failed to fetch issue field values", err)
2010+
}
20082011
}
20092012
}
20102013

pkg/github/issues_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,63 @@ func Test_SearchIssues_FieldValuesEnrichment(t *testing.T) {
14201420
assert.Empty(t, response.Items[1].FieldValues)
14211421
}
14221422

1423+
func Test_SearchIssues_FieldValuesEnrichmentUnsupported(t *testing.T) {
1424+
// Verify search_issues still returns its REST hits when the server's GraphQL
1425+
// schema does not support the issueFieldValues enrichment.
1426+
serverTool := SearchIssues(translations.NullTranslationHelper)
1427+
1428+
mockSearchResult := &github.IssuesSearchResult{
1429+
Total: github.Ptr(1),
1430+
IncompleteResults: github.Ptr(false),
1431+
Issues: []*github.Issue{
1432+
{
1433+
Number: github.Ptr(42),
1434+
Title: github.Ptr("Bug: Something is broken"),
1435+
State: github.Ptr("open"),
1436+
HTMLURL: github.Ptr("https://github.com/owner/repo/issues/42"),
1437+
NodeID: github.Ptr("I_node_42"),
1438+
User: &github.User{Login: github.Ptr("user1")},
1439+
},
1440+
},
1441+
}
1442+
1443+
restClient := MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
1444+
GetSearchIssues: mockResponse(t, http.StatusOK, mockSearchResult),
1445+
})
1446+
1447+
gqlVars := map[string]any{
1448+
"ids": []any{"I_node_42"},
1449+
}
1450+
gqlResponse := githubv4mock.ErrorResponse("Field 'issueFieldValues' doesn't exist on type 'Issue'")
1451+
1452+
const nodesQueryString = "query($ids:[ID!]!){nodes(ids: $ids){... on Issue{id,issueFieldValues(first: 25){nodes{__typename,... on IssueFieldDateValue{field{... on IssueFieldDate{name,fullDatabaseId},... on IssueFieldNumber{name,fullDatabaseId},... on IssueFieldSingleSelect{name,fullDatabaseId},... on IssueFieldText{name,fullDatabaseId}},value},... on IssueFieldNumberValue{field{... on IssueFieldDate{name,fullDatabaseId},... on IssueFieldNumber{name,fullDatabaseId},... on IssueFieldSingleSelect{name,fullDatabaseId},... on IssueFieldText{name,fullDatabaseId}},valueNumber: value},... on IssueFieldSingleSelectValue{field{... on IssueFieldDate{name,fullDatabaseId},... on IssueFieldNumber{name,fullDatabaseId},... on IssueFieldSingleSelect{name,fullDatabaseId},... on IssueFieldText{name,fullDatabaseId}},value},... on IssueFieldTextValue{field{... on IssueFieldDate{name,fullDatabaseId},... on IssueFieldNumber{name,fullDatabaseId},... on IssueFieldSingleSelect{name,fullDatabaseId},... on IssueFieldText{name,fullDatabaseId}},value}}}}}}"
1453+
matcher := githubv4mock.NewQueryMatcher(nodesQueryString, gqlVars, gqlResponse)
1454+
gqlClient := githubv4.NewClient(githubv4mock.NewMockedHTTPClient(matcher))
1455+
1456+
deps := BaseDeps{
1457+
Client: mustNewGHClient(t, restClient),
1458+
GQLClient: gqlClient,
1459+
}
1460+
handler := serverTool.Handler(deps)
1461+
1462+
request := createMCPRequest(map[string]any{
1463+
"query": "repo:owner/repo is:open",
1464+
})
1465+
1466+
result, err := handler(ContextWithDeps(context.Background(), deps), &request)
1467+
require.NoError(t, err)
1468+
require.False(t, result.IsError, "expected result to not be an error")
1469+
1470+
textContent := getTextResult(t, result)
1471+
1472+
var response SearchIssuesResponse
1473+
require.NoError(t, json.Unmarshal([]byte(textContent.Text), &response))
1474+
require.Equal(t, 1, *response.Total)
1475+
require.Len(t, response.Items, 1)
1476+
assert.Equal(t, 42, *response.Items[0].Number)
1477+
assert.Empty(t, response.Items[0].FieldValues)
1478+
}
1479+
14231480
func Test_CreateIssue(t *testing.T) {
14241481
// Verify tool definition once
14251482
serverTool := IssueWrite(translations.NullTranslationHelper)

0 commit comments

Comments
 (0)