fix(workflow_run_trigger): 204 dispatch is success; resolve run via listing (#41)
CD / Lint / Test / Vet (push) Successful in 6s
CD / Build & Import (push) Successful in 21s
CD / Deploy via GitOps (push) Has been skipped

Gitea's workflow_dispatch endpoint returns 204 No Content with no Location
header. DispatchWorkflow required that header, so every successful dispatch
errored with "missing Location header" and never yielded a run ID — making
the tool unusable for CAD dispatch.

- DispatchWorkflow now returns error-only; 204 = success, no Location needed.
  Body already carried ref+inputs; kept and covered by test.
- Tool snapshots the newest existing workflow_dispatch run before dispatch,
  then polls ListWorkflowRuns after and returns the newest run with ID above
  that baseline (avoids returning a stale prior run). Falls back to an honest
  "dispatched, run not yet registered" result rather than failing.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-03 21:12:45 +02:00
co-authored by Claude Opus 4.8
parent 711dc46e5e
commit 4ebea7d023
4 changed files with 167 additions and 82 deletions
+81 -6
View File
@@ -4,12 +4,22 @@ import (
"context"
"encoding/json"
"fmt"
"time"
"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"
)
// dispatchResolveBudget bounds how many times we poll for the dispatched run to
// appear, and dispatchResolvePoll is the gap between polls. Gitea returns 204
// with no run ID, so we list workflow_dispatch runs and take the newest above
// the pre-dispatch baseline; it may register a beat after the 204.
const (
dispatchResolveBudget = 5
dispatchResolvePoll = time.Second
)
// WorkflowRunTrigger triggers a Gitea Actions workflow_dispatch run.
type WorkflowRunTrigger struct {
c *gitea.Client
@@ -68,17 +78,82 @@ func (t *WorkflowRunTrigger) Call(ctx context.Context, raw json.RawMessage) (jso
}
}
result, err := t.c.DispatchWorkflow(ctx, args.Owner, args.Name, args.Workflow, gitea.DispatchWorkflowArgs{
// Snapshot the newest existing workflow_dispatch run BEFORE dispatching, so we
// can tell our fresh run apart from a prior one (Gitea's 204 carries no run ID).
baseline := t.newestDispatchRunID(ctx, args.Owner, args.Name, args.Workflow, ref)
if err := t.c.DispatchWorkflow(ctx, args.Owner, args.Name, args.Workflow, gitea.DispatchWorkflowArgs{
Ref: ref,
Inputs: args.Inputs,
})
if err != nil {
}); err != nil {
return nil, err
}
htmlURL := fmt.Sprintf("%s/%s/%s/actions/runs/%d", t.baseURL, args.Owner, args.Name, result.RunID)
// Resolve the new run by listing workflow_dispatch runs and taking the newest
// one whose ID exceeds the baseline. Poll briefly: the run can register a beat
// after the 204.
var run *gitea.WorkflowRun
for i := 0; i < dispatchResolveBudget; i++ {
if i > 0 {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-time.After(dispatchResolvePoll):
}
}
if r := t.newestDispatchRun(ctx, args.Owner, args.Name, args.Workflow, ref); r != nil && r.ID > baseline {
run = r
break
}
}
// Dispatch succeeded (204). If the run has not surfaced yet, say so honestly
// rather than failing — the workflow IS firing; the caller can list runs.
if run == nil {
return textOK(map[string]any{
"dispatched": true,
"note": "dispatch accepted but the run did not register within the resolve window; " +
"list recent workflow_dispatch runs to find it",
})
}
htmlURL := run.HTMLURL
if htmlURL == "" {
htmlURL = fmt.Sprintf("%s/%s/%s/actions/runs/%d", t.baseURL, args.Owner, args.Name, run.ID)
}
return textOK(map[string]any{
"run_id": result.RunID,
"html_url": htmlURL,
"dispatched": true,
"run_id": run.ID,
"html_url": htmlURL,
})
}
// newestDispatchRun returns the most recent workflow_dispatch run for the given
// workflow and ref, or nil if none / on listing error (best-effort resolution).
func (t *WorkflowRunTrigger) newestDispatchRun(ctx context.Context, owner, name, workflow, ref string) *gitea.WorkflowRun {
resp, err := t.c.ListWorkflowRuns(ctx, owner, name, gitea.ListWorkflowRunsArgs{
Event: "workflow_dispatch",
Workflow: workflow,
Branch: ref,
Limit: 20,
})
if err != nil || resp == nil {
return nil
}
var newest *gitea.WorkflowRun
for i := range resp.WorkflowRuns {
r := &resp.WorkflowRuns[i]
if newest == nil || r.ID > newest.ID {
newest = r
}
}
return newest
}
// newestDispatchRunID is newestDispatchRun's ID, or 0 if none.
func (t *WorkflowRunTrigger) newestDispatchRunID(ctx context.Context, owner, name, workflow, ref string) int64 {
if r := t.newestDispatchRun(ctx, owner, name, workflow, ref); r != nil {
return r.ID
}
return 0
}