Shared LRU avoids repeated Gitea calls for default-branch resolution; the simple stdlib map alternative would race on concurrent access without a mutex per entry, which is more code than the LRU.
85 lines
2.2 KiB
Go
85 lines
2.2 KiB
Go
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 == "" {
|
|
var err error
|
|
ref, err = t.c.DefaultBranch(ctx, args.Owner, args.Name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
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,
|
|
})
|
|
}
|