|
6 | 6 | "fmt" |
7 | 7 | "io" |
8 | 8 | "net/http" |
| 9 | + "time" |
9 | 10 |
|
10 | 11 | ghErrors "github.com/github/github-mcp-server/pkg/errors" |
11 | 12 | "github.com/github/github-mcp-server/pkg/inventory" |
@@ -430,3 +431,158 @@ func SearchOrgs(t translations.TranslationHelperFunc) inventory.ServerTool { |
430 | 431 | }, |
431 | 432 | ) |
432 | 433 | } |
| 434 | + |
| 435 | +// SearchCommits creates a tool to search for commits across GitHub repositories. |
| 436 | +func SearchCommits(t translations.TranslationHelperFunc) inventory.ServerTool { |
| 437 | + schema := &jsonschema.Schema{ |
| 438 | + Type: "object", |
| 439 | + Properties: map[string]*jsonschema.Schema{ |
| 440 | + "query": { |
| 441 | + Type: "string", |
| 442 | + Description: "Commit search query. Examples: 'repo:owner/repo fix bug', 'author:defunkt', 'committer-date:>2024-01-01'. Supports advanced search syntax.", |
| 443 | + }, |
| 444 | + "sort": { |
| 445 | + Type: "string", |
| 446 | + Description: "Sort field ('author-date' or 'committer-date')", |
| 447 | + Enum: []any{"author-date", "committer-date"}, |
| 448 | + }, |
| 449 | + "order": { |
| 450 | + Type: "string", |
| 451 | + Description: "Sort order", |
| 452 | + Enum: []any{"asc", "desc"}, |
| 453 | + }, |
| 454 | + }, |
| 455 | + Required: []string{"query"}, |
| 456 | + } |
| 457 | + WithPagination(schema) |
| 458 | + |
| 459 | + return NewTool( |
| 460 | + ToolsetMetadataRepos, |
| 461 | + mcp.Tool{ |
| 462 | + Name: "search_commits", |
| 463 | + Description: t("TOOL_SEARCH_COMMITS_DESCRIPTION", "Search for commits across GitHub repositories using specialized commit search syntax. Great for finding specific changes, authors, or messages."), |
| 464 | + Annotations: &mcp.ToolAnnotations{ |
| 465 | + Title: t("TOOL_SEARCH_COMMITS_USER_TITLE", "Search commits"), |
| 466 | + ReadOnlyHint: true, |
| 467 | + }, |
| 468 | + InputSchema: schema, |
| 469 | + }, |
| 470 | + []scopes.Scope{scopes.Repo}, |
| 471 | + func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) { |
| 472 | + query, err := RequiredParam[string](args, "query") |
| 473 | + if err != nil { |
| 474 | + return utils.NewToolResultError(err.Error()), nil, nil |
| 475 | + } |
| 476 | + sort, err := OptionalParam[string](args, "sort") |
| 477 | + if err != nil { |
| 478 | + return utils.NewToolResultError(err.Error()), nil, nil |
| 479 | + } |
| 480 | + order, err := OptionalParam[string](args, "order") |
| 481 | + if err != nil { |
| 482 | + return utils.NewToolResultError(err.Error()), nil, nil |
| 483 | + } |
| 484 | + pagination, err := OptionalPaginationParams(args) |
| 485 | + if err != nil { |
| 486 | + return utils.NewToolResultError(err.Error()), nil, nil |
| 487 | + } |
| 488 | + |
| 489 | + opts := &github.SearchOptions{ |
| 490 | + Sort: sort, |
| 491 | + Order: order, |
| 492 | + ListOptions: github.ListOptions{ |
| 493 | + Page: pagination.Page, |
| 494 | + PerPage: pagination.PerPage, |
| 495 | + }, |
| 496 | + } |
| 497 | + |
| 498 | + client, err := deps.GetClient(ctx) |
| 499 | + if err != nil { |
| 500 | + return utils.NewToolResultErrorFromErr("failed to get GitHub client", err), nil, nil |
| 501 | + } |
| 502 | + result, resp, err := client.Search.Commits(ctx, query, opts) |
| 503 | + if err != nil { |
| 504 | + return ghErrors.NewGitHubAPIErrorResponse(ctx, |
| 505 | + fmt.Sprintf("failed to search commits with query '%s'", query), |
| 506 | + resp, |
| 507 | + err, |
| 508 | + ), nil, nil |
| 509 | + } |
| 510 | + defer func() { _ = resp.Body.Close() }() |
| 511 | + |
| 512 | + if resp.StatusCode != http.StatusOK { |
| 513 | + body, err := io.ReadAll(resp.Body) |
| 514 | + if err != nil { |
| 515 | + return utils.NewToolResultErrorFromErr("failed to read response body", err), nil, nil |
| 516 | + } |
| 517 | + return ghErrors.NewGitHubAPIStatusErrorResponse(ctx, "failed to search commits", resp, body), nil, nil |
| 518 | + } |
| 519 | + |
| 520 | + minimalCommits := make([]MinimalCommit, 0, len(result.Commits)) |
| 521 | + for _, commit := range result.Commits { |
| 522 | + minimalCommit := MinimalCommit{ |
| 523 | + SHA: commit.GetSHA(), |
| 524 | + HTMLURL: commit.GetHTMLURL(), |
| 525 | + } |
| 526 | + |
| 527 | + if commit.Commit != nil { |
| 528 | + minimalCommit.Commit = &MinimalCommitInfo{ |
| 529 | + Message: commit.Commit.GetMessage(), |
| 530 | + } |
| 531 | + |
| 532 | + if commit.Commit.Author != nil { |
| 533 | + minimalCommit.Commit.Author = &MinimalCommitAuthor{ |
| 534 | + Name: commit.Commit.Author.GetName(), |
| 535 | + Email: commit.Commit.Author.GetEmail(), |
| 536 | + } |
| 537 | + if commit.Commit.Author.Date != nil { |
| 538 | + minimalCommit.Commit.Author.Date = commit.Commit.Author.Date.Format(time.RFC3339) |
| 539 | + } |
| 540 | + } |
| 541 | + |
| 542 | + if commit.Commit.Committer != nil { |
| 543 | + minimalCommit.Commit.Committer = &MinimalCommitAuthor{ |
| 544 | + Name: commit.Commit.Committer.GetName(), |
| 545 | + Email: commit.Commit.Committer.GetEmail(), |
| 546 | + } |
| 547 | + if commit.Commit.Committer.Date != nil { |
| 548 | + minimalCommit.Commit.Committer.Date = commit.Commit.Committer.Date.Format(time.RFC3339) |
| 549 | + } |
| 550 | + } |
| 551 | + } |
| 552 | + |
| 553 | + if commit.Author != nil { |
| 554 | + minimalCommit.Author = &MinimalUser{ |
| 555 | + Login: commit.Author.GetLogin(), |
| 556 | + ID: commit.Author.GetID(), |
| 557 | + ProfileURL: commit.Author.GetHTMLURL(), |
| 558 | + AvatarURL: commit.Author.GetAvatarURL(), |
| 559 | + } |
| 560 | + } |
| 561 | + |
| 562 | + if commit.Committer != nil { |
| 563 | + minimalCommit.Committer = &MinimalUser{ |
| 564 | + Login: commit.Committer.GetLogin(), |
| 565 | + ID: commit.Committer.GetID(), |
| 566 | + ProfileURL: commit.Committer.GetHTMLURL(), |
| 567 | + AvatarURL: commit.Committer.GetAvatarURL(), |
| 568 | + } |
| 569 | + } |
| 570 | + |
| 571 | + minimalCommits = append(minimalCommits, minimalCommit) |
| 572 | + } |
| 573 | + |
| 574 | + minimalResult := &MinimalSearchCommitsResult{ |
| 575 | + TotalCount: result.GetTotal(), |
| 576 | + IncompleteResults: result.GetIncompleteResults(), |
| 577 | + Items: minimalCommits, |
| 578 | + } |
| 579 | + |
| 580 | + r, err := json.Marshal(minimalResult) |
| 581 | + if err != nil { |
| 582 | + return utils.NewToolResultErrorFromErr("failed to marshal response", err), nil, nil |
| 583 | + } |
| 584 | + |
| 585 | + return utils.NewToolResultText(string(r)), nil, nil |
| 586 | + }, |
| 587 | + ) |
| 588 | +} |
0 commit comments