|
1 | 1 | package github |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "context" |
5 | 6 | "encoding/json" |
6 | 7 | "fmt" |
@@ -610,3 +611,153 @@ func pushFiles(client *github.Client, t translations.TranslationHelperFunc) (too |
610 | 611 | return mcp.NewToolResultText(string(r)), nil |
611 | 612 | } |
612 | 613 | } |
| 614 | + |
| 615 | +func securityFeatureToggle(isEnabled bool) map[string]string { |
| 616 | + if isEnabled { |
| 617 | + return map[string]string{"status": "enabled"} |
| 618 | + } |
| 619 | + return map[string]string{"status": "disabled"} |
| 620 | +} |
| 621 | + |
| 622 | +func toggleSecretProtectionFeatures(client *github.Client, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { |
| 623 | + return mcp.NewTool("toggle_secret_protection_features", |
| 624 | + mcp.WithDescription(t("TOOL_TOGGLE_SECRET_PROTECTION_FEATURES_DESCRIPTION", "Enable or disable Secret Protection features for a repository")), |
| 625 | + mcp.WithString("owner", |
| 626 | + mcp.Required(), |
| 627 | + mcp.Description("Repository owner"), |
| 628 | + ), |
| 629 | + mcp.WithString("repo", |
| 630 | + mcp.Required(), |
| 631 | + mcp.Description("Repository name"), |
| 632 | + ), |
| 633 | + mcp.WithBoolean("secret_scanning", |
| 634 | + mcp.Required(), |
| 635 | + mcp.Description("Enable or disable secret scanning"), |
| 636 | + ), |
| 637 | + mcp.WithBoolean("secret_scanning_push_protection", |
| 638 | + mcp.Required(), |
| 639 | + mcp.Description("Enable or disable secret scanning push protection"), |
| 640 | + ), |
| 641 | + mcp.WithBoolean("secret_scanning_ai_detection", |
| 642 | + mcp.Required(), |
| 643 | + mcp.Description("Enable or disable secret scanning AI detection"), |
| 644 | + ), |
| 645 | + ), |
| 646 | + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 647 | + owner, err := requiredParam[string](request, "owner") |
| 648 | + if err != nil { |
| 649 | + return mcp.NewToolResultError(err.Error()), nil |
| 650 | + } |
| 651 | + repo, err := requiredParam[string](request, "repo") |
| 652 | + if err != nil { |
| 653 | + return mcp.NewToolResultError(err.Error()), nil |
| 654 | + } |
| 655 | + secretScanningEnabled, err := requiredParam[bool](request, "secret_scanning") |
| 656 | + if err != nil { |
| 657 | + return mcp.NewToolResultError(err.Error()), nil |
| 658 | + } |
| 659 | + pushProtectionEnabled, err := optionalParam[bool](request, "secret_scanning_push_protection") |
| 660 | + if err != nil { |
| 661 | + return mcp.NewToolResultError(err.Error()), nil |
| 662 | + } |
| 663 | + aiDetectionEnabled, err := optionalParam[bool](request, "secret_scanning_ai_detection") |
| 664 | + if err != nil { |
| 665 | + return mcp.NewToolResultError(err.Error()), nil |
| 666 | + } |
| 667 | + |
| 668 | + securityAndAnalysis := map[string]map[string]string{ |
| 669 | + "secret_scanning": securityFeatureToggle(secretScanningEnabled), |
| 670 | + "secret_scanning_push_protection": securityFeatureToggle(pushProtectionEnabled), |
| 671 | + "secret_scanning_ai_detection": securityFeatureToggle(aiDetectionEnabled), |
| 672 | + } |
| 673 | + |
| 674 | + requestBody := map[string]interface{}{ |
| 675 | + "security_and_analysis": securityAndAnalysis, |
| 676 | + } |
| 677 | + |
| 678 | + jsonBody, err := json.Marshal(requestBody) |
| 679 | + if err != nil { |
| 680 | + return nil, fmt.Errorf("failed to marshal request body: %w", err) |
| 681 | + } |
| 682 | + |
| 683 | + req, err := http.NewRequest( |
| 684 | + "PATCH", |
| 685 | + fmt.Sprintf("%srepos/%s/%s", client.BaseURL.String(), owner, repo), |
| 686 | + bytes.NewBuffer(jsonBody), |
| 687 | + ) |
| 688 | + if err != nil { |
| 689 | + return nil, fmt.Errorf("failed to create request: %w", err) |
| 690 | + } |
| 691 | + |
| 692 | + resp, err := client.Client().Do(req) |
| 693 | + if err != nil { |
| 694 | + return nil, fmt.Errorf("failed to send request: %w", err) |
| 695 | + } |
| 696 | + defer func() { _ = resp.Body.Close() }() |
| 697 | + |
| 698 | + if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent { |
| 699 | + body, err := io.ReadAll(resp.Body) |
| 700 | + if err != nil { |
| 701 | + return nil, fmt.Errorf("failed to read response body: %w", err) |
| 702 | + } |
| 703 | + return mcp.NewToolResultError(fmt.Sprintf("failed to toggle Secret Protection Features: %s", string(body))), nil |
| 704 | + } |
| 705 | + |
| 706 | + return mcp.NewToolResultText("Secret Protection features toggled successfully"), nil |
| 707 | + } |
| 708 | +} |
| 709 | + |
| 710 | +// getRepositorySettings creates a tool to get repository settings including security features |
| 711 | +func getRepositorySettings(client *github.Client, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { |
| 712 | + return mcp.NewTool("get_repository_settings", |
| 713 | + mcp.WithDescription(t("TOOL_GET_REPOSITORY_SETTINGS_DESCRIPTION", "Get repository settings including security features")), |
| 714 | + mcp.WithString("owner", |
| 715 | + mcp.Required(), |
| 716 | + mcp.Description("Repository owner"), |
| 717 | + ), |
| 718 | + mcp.WithString("repo", |
| 719 | + mcp.Required(), |
| 720 | + mcp.Description("Repository name"), |
| 721 | + ), |
| 722 | + ), |
| 723 | + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 724 | + owner, err := requiredParam[string](request, "owner") |
| 725 | + if err != nil { |
| 726 | + return mcp.NewToolResultError(err.Error()), nil |
| 727 | + } |
| 728 | + repo, err := requiredParam[string](request, "repo") |
| 729 | + if err != nil { |
| 730 | + return mcp.NewToolResultError(err.Error()), nil |
| 731 | + } |
| 732 | + |
| 733 | + req, err := http.NewRequest( |
| 734 | + "GET", |
| 735 | + fmt.Sprintf("%srepos/%s/%s", client.BaseURL.String(), owner, repo), |
| 736 | + nil, |
| 737 | + ) |
| 738 | + if err != nil { |
| 739 | + return nil, fmt.Errorf("failed to create request: %w", err) |
| 740 | + } |
| 741 | + |
| 742 | + resp, err := client.Client().Do(req) |
| 743 | + if err != nil { |
| 744 | + return nil, fmt.Errorf("failed to send request: %w", err) |
| 745 | + } |
| 746 | + defer func() { _ = resp.Body.Close() }() |
| 747 | + |
| 748 | + if resp.StatusCode != http.StatusOK { |
| 749 | + body, err := io.ReadAll(resp.Body) |
| 750 | + if err != nil { |
| 751 | + return nil, fmt.Errorf("failed to read response body: %w", err) |
| 752 | + } |
| 753 | + return mcp.NewToolResultError(fmt.Sprintf("failed to get security analysis settings: %s", string(body))), nil |
| 754 | + } |
| 755 | + |
| 756 | + body, err := io.ReadAll(resp.Body) |
| 757 | + if err != nil { |
| 758 | + return nil, fmt.Errorf("failed to read response body: %w", err) |
| 759 | + } |
| 760 | + |
| 761 | + return mcp.NewToolResultText(string(body)), nil |
| 762 | + } |
| 763 | +} |
0 commit comments