|
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 | returnmcp.NewToolResultText(string(r)),nil
|
611 | 612 | }
|
612 | 613 | }
|
| 614 | + |
| 615 | +funcsecurityFeatureToggle(isEnabledbool)map[string]string { |
| 616 | +ifisEnabled { |
| 617 | +returnmap[string]string{"status":"enabled"} |
| 618 | +} |
| 619 | +returnmap[string]string{"status":"disabled"} |
| 620 | +} |
| 621 | + |
| 622 | +functoggleSecretProtectionFeatures(client*github.Client,t translations.TranslationHelperFunc) (tool mcp.Tool,handler server.ToolHandlerFunc) { |
| 623 | +returnmcp.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 | +iferr!=nil { |
| 649 | +returnmcp.NewToolResultError(err.Error()),nil |
| 650 | +} |
| 651 | +repo,err:=requiredParam[string](request,"repo") |
| 652 | +iferr!=nil { |
| 653 | +returnmcp.NewToolResultError(err.Error()),nil |
| 654 | +} |
| 655 | +secretScanningEnabled,err:=requiredParam[bool](request,"secret_scanning") |
| 656 | +iferr!=nil { |
| 657 | +returnmcp.NewToolResultError(err.Error()),nil |
| 658 | +} |
| 659 | +pushProtectionEnabled,err:=optionalParam[bool](request,"secret_scanning_push_protection") |
| 660 | +iferr!=nil { |
| 661 | +returnmcp.NewToolResultError(err.Error()),nil |
| 662 | +} |
| 663 | +aiDetectionEnabled,err:=optionalParam[bool](request,"secret_scanning_ai_detection") |
| 664 | +iferr!=nil { |
| 665 | +returnmcp.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 | +iferr!=nil { |
| 680 | +returnnil,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 | +iferr!=nil { |
| 689 | +returnnil,fmt.Errorf("failed to create request: %w",err) |
| 690 | +} |
| 691 | + |
| 692 | +resp,err:=client.Client().Do(req) |
| 693 | +iferr!=nil { |
| 694 | +returnnil,fmt.Errorf("failed to send request: %w",err) |
| 695 | +} |
| 696 | +deferfunc() {_=resp.Body.Close() }() |
| 697 | + |
| 698 | +ifresp.StatusCode!=http.StatusOK&&resp.StatusCode!=http.StatusNoContent { |
| 699 | +body,err:=io.ReadAll(resp.Body) |
| 700 | +iferr!=nil { |
| 701 | +returnnil,fmt.Errorf("failed to read response body: %w",err) |
| 702 | +} |
| 703 | +returnmcp.NewToolResultError(fmt.Sprintf("failed to toggle Secret Protection Features: %s",string(body))),nil |
| 704 | +} |
| 705 | + |
| 706 | +returnmcp.NewToolResultText("Secret Protection features toggled successfully"),nil |
| 707 | +} |
| 708 | +} |
| 709 | + |
| 710 | +// getRepositorySettings creates a tool to get repository settings including security features |
| 711 | +funcgetRepositorySettings(client*github.Client,t translations.TranslationHelperFunc) (tool mcp.Tool,handler server.ToolHandlerFunc) { |
| 712 | +returnmcp.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 | +iferr!=nil { |
| 726 | +returnmcp.NewToolResultError(err.Error()),nil |
| 727 | +} |
| 728 | +repo,err:=requiredParam[string](request,"repo") |
| 729 | +iferr!=nil { |
| 730 | +returnmcp.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 | +iferr!=nil { |
| 739 | +returnnil,fmt.Errorf("failed to create request: %w",err) |
| 740 | +} |
| 741 | + |
| 742 | +resp,err:=client.Client().Do(req) |
| 743 | +iferr!=nil { |
| 744 | +returnnil,fmt.Errorf("failed to send request: %w",err) |
| 745 | +} |
| 746 | +deferfunc() {_=resp.Body.Close() }() |
| 747 | + |
| 748 | +ifresp.StatusCode!=http.StatusOK { |
| 749 | +body,err:=io.ReadAll(resp.Body) |
| 750 | +iferr!=nil { |
| 751 | +returnnil,fmt.Errorf("failed to read response body: %w",err) |
| 752 | +} |
| 753 | +returnmcp.NewToolResultError(fmt.Sprintf("failed to get security analysis settings: %s",string(body))),nil |
| 754 | +} |
| 755 | + |
| 756 | +body,err:=io.ReadAll(resp.Body) |
| 757 | +iferr!=nil { |
| 758 | +returnnil,fmt.Errorf("failed to read response body: %w",err) |
| 759 | +} |
| 760 | + |
| 761 | +returnmcp.NewToolResultText(string(body)),nil |
| 762 | +} |
| 763 | +} |