feat(tools): workflow_run_trigger
This commit is contained in:
84
internal/tools/workflow_run_trigger.go
Normal file
84
internal/tools/workflow_run_trigger.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"gitea.d-ma.be/mathias/gitea-mcp/internal/allowlist"
|
||||
"gitea.d-ma.be/mathias/gitea-mcp/internal/gitea"
|
||||
"gitea.d-ma.be/mathias/gitea-mcp/internal/registry"
|
||||
)
|
||||
|
||||
// WorkflowRunTrigger triggers a Gitea Actions workflow_dispatch run.
|
||||
type WorkflowRunTrigger struct {
|
||||
c *gitea.Client
|
||||
a *allowlist.Allowlist
|
||||
baseURL string
|
||||
}
|
||||
|
||||
func NewWorkflowRunTrigger(c *gitea.Client, a *allowlist.Allowlist, baseURL string) *WorkflowRunTrigger {
|
||||
return &WorkflowRunTrigger{c: c, a: a, baseURL: baseURL}
|
||||
}
|
||||
|
||||
func (t *WorkflowRunTrigger) Descriptor() registry.ToolDescriptor {
|
||||
return registry.ToolDescriptor{
|
||||
Name: "workflow_run_trigger",
|
||||
Description: "Trigger a Gitea Actions workflow_dispatch run.",
|
||||
InputSchema: json.RawMessage(`{
|
||||
"type":"object",
|
||||
"properties":{
|
||||
"owner":{"type":"string"},
|
||||
"name":{"type":"string"},
|
||||
"workflow":{"type":"string"},
|
||||
"ref":{"type":"string"},
|
||||
"inputs":{"type":"object"}
|
||||
},
|
||||
"required":["owner","name","workflow"]
|
||||
}`),
|
||||
}
|
||||
}
|
||||
|
||||
type workflowRunTriggerArgs struct {
|
||||
Owner string `json:"owner"`
|
||||
Name string `json:"name"`
|
||||
Workflow string `json:"workflow"`
|
||||
Ref string `json:"ref"`
|
||||
Inputs map[string]any `json:"inputs"`
|
||||
}
|
||||
|
||||
func (t *WorkflowRunTrigger) Call(ctx context.Context, raw json.RawMessage) (json.RawMessage, error) {
|
||||
var args workflowRunTriggerArgs
|
||||
if err := parseArgs(raw, &args); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := t.a.Check(args.Owner); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if args.Workflow == "" {
|
||||
return nil, fmt.Errorf("workflow is required: %w", gitea.ErrValidation)
|
||||
}
|
||||
|
||||
ref := args.Ref
|
||||
if ref == "" {
|
||||
repo, err := t.c.GetRepo(ctx, args.Owner, args.Name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ref = repo.DefaultBranch
|
||||
}
|
||||
|
||||
result, err := t.c.DispatchWorkflow(ctx, args.Owner, args.Name, args.Workflow, gitea.DispatchWorkflowArgs{
|
||||
Ref: ref,
|
||||
Inputs: args.Inputs,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
htmlURL := fmt.Sprintf("%s/%s/%s/actions/runs/%d", t.baseURL, args.Owner, args.Name, result.RunID)
|
||||
return textOK(map[string]any{
|
||||
"run_id": result.RunID,
|
||||
"html_url": htmlURL,
|
||||
})
|
||||
}
|
||||
86
internal/tools/workflow_run_trigger_test.go
Normal file
86
internal/tools/workflow_run_trigger_test.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package tools_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"gitea.d-ma.be/mathias/gitea-mcp/internal/allowlist"
|
||||
"gitea.d-ma.be/mathias/gitea-mcp/internal/gitea"
|
||||
"gitea.d-ma.be/mathias/gitea-mcp/internal/tools"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestWorkflowRunTriggerSuccess(t *testing.T) {
|
||||
// Fake server handles both the repo endpoint (default_branch) and the dispatch endpoint.
|
||||
repoHit := false
|
||||
dispatchHit := false
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch {
|
||||
case r.URL.Path == "/api/v1/repos/mathias/myrepo" && r.Method == http.MethodGet:
|
||||
repoHit = true
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"name":"myrepo","full_name":"mathias/myrepo","default_branch":"main"}`))
|
||||
case r.URL.Path == "/api/v1/repos/mathias/myrepo/actions/workflows/ci.yml/dispatches" && r.Method == http.MethodPost:
|
||||
dispatchHit = true
|
||||
w.Header().Set("Location", "/api/v1/repos/mathias/myrepo/actions/runs/42")
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
tool := tools.NewWorkflowRunTrigger(gitea.NewClient(srv.URL, "tok"), allowlist.New([]string{"mathias"}), srv.URL)
|
||||
out, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"mathias","name":"myrepo","workflow":"ci.yml"}`))
|
||||
require.NoError(t, err)
|
||||
assert.True(t, repoHit, "expected GET /repo for default branch")
|
||||
assert.True(t, dispatchHit, "expected POST dispatch")
|
||||
|
||||
var result map[string]any
|
||||
require.NoError(t, json.Unmarshal(out, &result))
|
||||
assert.Equal(t, float64(42), result["run_id"])
|
||||
assert.Contains(t, result["html_url"], "/mathias/myrepo/actions/runs/42")
|
||||
}
|
||||
|
||||
func TestWorkflowRunTriggerExplicitRef(t *testing.T) {
|
||||
repoHit := false
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/api/v1/repos/mathias/myrepo" {
|
||||
repoHit = true
|
||||
}
|
||||
if r.URL.Path == "/api/v1/repos/mathias/myrepo/actions/workflows/ci.yml/dispatches" {
|
||||
w.Header().Set("Location", "/api/v1/repos/mathias/myrepo/actions/runs/99")
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
http.NotFound(w, r)
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
tool := tools.NewWorkflowRunTrigger(gitea.NewClient(srv.URL, "tok"), allowlist.New([]string{"mathias"}), srv.URL)
|
||||
out, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"mathias","name":"myrepo","workflow":"ci.yml","ref":"develop"}`))
|
||||
require.NoError(t, err)
|
||||
assert.False(t, repoHit, "should not call GET /repo when ref is provided")
|
||||
|
||||
var result map[string]any
|
||||
require.NoError(t, json.Unmarshal(out, &result))
|
||||
assert.Equal(t, float64(99), result["run_id"])
|
||||
}
|
||||
|
||||
func TestWorkflowRunTriggerAllowlistRejects(t *testing.T) {
|
||||
tool := tools.NewWorkflowRunTrigger(gitea.NewClient("http://unused", ""), allowlist.New([]string{"mathias"}), "http://unused")
|
||||
_, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"evil","name":"repo","workflow":"ci.yml"}`))
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestWorkflowRunTriggerRequiresWorkflow(t *testing.T) {
|
||||
// workflow field is present in required schema but let's test empty string fallback guard
|
||||
tool := tools.NewWorkflowRunTrigger(gitea.NewClient("http://unused", ""), allowlist.New([]string{"mathias"}), "http://unused")
|
||||
_, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"mathias","name":"repo","workflow":""}`))
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "workflow")
|
||||
}
|
||||
Reference in New Issue
Block a user