fix(workflow_run_trigger): 204 dispatch is success; resolve run via listing (#41)
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:
@@ -3,8 +3,10 @@ package tools_test
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.d-ma.be/mathias/gitea-mcp/internal/allowlist"
|
||||
@@ -14,10 +16,67 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestWorkflowRunTriggerSuccess(t *testing.T) {
|
||||
// Fake server handles both the repo endpoint (default_branch) and the dispatch endpoint.
|
||||
// runsListJSON is a workflow_runs listing body with a single run of the given id.
|
||||
func runsListJSON(id int) string {
|
||||
return `{"total_count":1,"workflow_runs":[{"id":` +
|
||||
fmtInt(id) +
|
||||
`,"status":"queued","event":"workflow_dispatch","html_url":"http://gitea.example/mathias/myrepo/actions/runs/` +
|
||||
fmtInt(id) + `"}]}`
|
||||
}
|
||||
|
||||
func fmtInt(i int) string { b, _ := json.Marshal(i); return string(b) }
|
||||
|
||||
// The dispatch endpoint returns 204 with NO Location header (real Gitea). The
|
||||
// tool must treat that as success, forward inputs, and resolve the new run by
|
||||
// listing workflow_dispatch runs and picking the newest one above the
|
||||
// pre-dispatch baseline.
|
||||
func TestWorkflowRunTriggerResolvesRunViaListing(t *testing.T) {
|
||||
dispatched := false
|
||||
var gotBody []byte
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch {
|
||||
case r.URL.Path == "/api/v1/repos/mathias/myrepo/actions/workflows/ci.yml/dispatches" && r.Method == http.MethodPost:
|
||||
gotBody, _ = io.ReadAll(r.Body)
|
||||
dispatched = true
|
||||
w.WriteHeader(http.StatusNoContent) // no Location header
|
||||
case strings.HasPrefix(r.URL.Path, "/api/v1/repos/mathias/myrepo/actions/runs"):
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if !dispatched {
|
||||
_, _ = w.Write([]byte(`{"total_count":0,"workflow_runs":[]}`)) // baseline: none yet
|
||||
return
|
||||
}
|
||||
_, _ = w.Write([]byte(runsListJSON(100)))
|
||||
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","ref":"main","inputs":{"issue_number":"36","harness":"agentsquad"}}`))
|
||||
require.NoError(t, err)
|
||||
assert.True(t, dispatched, "expected POST dispatch")
|
||||
|
||||
// inputs forwarded to the dispatch body
|
||||
var body map[string]any
|
||||
require.NoError(t, json.Unmarshal(gotBody, &body))
|
||||
assert.Equal(t, "main", body["ref"])
|
||||
inputs, ok := body["inputs"].(map[string]any)
|
||||
require.True(t, ok, "inputs must be present in dispatch body")
|
||||
assert.Equal(t, "36", inputs["issue_number"])
|
||||
assert.Equal(t, "agentsquad", inputs["harness"])
|
||||
|
||||
// run id resolved via listing (not a Location header)
|
||||
var result map[string]any
|
||||
require.NoError(t, json.Unmarshal(out, &result))
|
||||
assert.Equal(t, float64(100), result["run_id"])
|
||||
assert.Contains(t, result["html_url"], "/actions/runs/100")
|
||||
}
|
||||
|
||||
func TestWorkflowRunTriggerDefaultBranch(t *testing.T) {
|
||||
repoHit := false
|
||||
dispatchHit := false
|
||||
dispatched := 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:
|
||||
@@ -25,9 +84,15 @@ func TestWorkflowRunTriggerSuccess(t *testing.T) {
|
||||
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")
|
||||
dispatched = true
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
case strings.HasPrefix(r.URL.Path, "/api/v1/repos/mathias/myrepo/actions/runs"):
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if !dispatched {
|
||||
_, _ = w.Write([]byte(`{"total_count":0,"workflow_runs":[]}`))
|
||||
return
|
||||
}
|
||||
_, _ = w.Write([]byte(runsListJSON(55)))
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
@@ -38,37 +103,10 @@ func TestWorkflowRunTriggerSuccess(t *testing.T) {
|
||||
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"])
|
||||
assert.Equal(t, float64(55), result["run_id"])
|
||||
}
|
||||
|
||||
func TestWorkflowRunTriggerAllowlistRejects(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user