-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathactions.go
More file actions
97 lines (86 loc) · 2.87 KB
/
actions.go
File metadata and controls
97 lines (86 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package github
import (
"context"
"encoding/json"
"fmt"
"github.com/github/github-mcp-server/pkg/translations"
"github.com/google/go-github/v69/github"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
)
// RunWorkflow creates a tool to run an Actions workflow
func RunWorkflow(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
return mcp.NewTool("run_workflow",
mcp.WithDescription(t("TOOL_RUN_WORKFLOW_DESCRIPTION", "Trigger a workflow run")),
mcp.WithString("owner",
mcp.Required(),
mcp.Description("The account owner of the repository. The name is not case sensitive."),
),
mcp.WithString("repo",
mcp.Required(),
mcp.Description("Repository name"),
),
mcp.WithString("workflow_file",
mcp.Required(),
mcp.Description("The workflow file name or ID of the workflow entity."),
),
mcp.WithString("ref",
mcp.Required(),
mcp.Description("Git reference (branch or tag name)"),
),
mcp.WithObject("inputs",
mcp.Description("Input keys and values configured in the workflow file."),
),
),
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
owner, err := requiredParam[string](request, "owner")
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
repo, err := requiredParam[string](request, "repo")
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
workflowFileName, err := requiredParam[string](request, "workflow_file")
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
ref, err := requiredParam[string](request, "ref")
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
// Get the optional inputs parameter
var inputs map[string]any
if inputsObj, exists := request.Params.Arguments["inputs"]; exists && inputsObj != nil {
inputs, _ = inputsObj.(map[string]any)
}
// Convert inputs to the format expected by the GitHub API
inputsMap := make(map[string]any)
for k, v := range inputs {
inputsMap[k] = v
}
// Create the event to dispatch
event := github.CreateWorkflowDispatchEventRequest{
Ref: ref,
Inputs: inputsMap,
}
client, err := getClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub client: %w", err)
}
resp, err := client.Actions.CreateWorkflowDispatchEventByFileName(ctx, owner, repo, workflowFileName, event)
if err != nil {
return nil, fmt.Errorf("failed to trigger workflow: %w", err)
}
defer func() { _ = resp.Body.Close() }()
result := map[string]any{
"success": true,
"message": "Workflow triggered successfully",
}
r, err := json.Marshal(result)
if err != nil {
return nil, fmt.Errorf("failed to marshal response: %w", err)
}
return mcp.NewToolResultText(string(r)), nil
}
}