mirror of
https://gitea.com/gitea/gitea-mcp.git
synced 2026-08-29 11:37:44 +00:00
a5d3910a89
Fetch the PR head SHA from repos/{owner}/{repo}/pulls/{pull_number}
and use it to query repos/{owner}/{repo}/actions/runs?head_sha=...,
returning the slim run list for the pull request.
Co-Authored-By: Codet <codet@commitgo.dev> (GPT-5-Codex)
99 lines
2.5 KiB
Go
99 lines
2.5 KiB
Go
package actions
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"testing"
|
|
|
|
"gitea.com/gitea/gitea-mcp/pkg/flag"
|
|
|
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
|
)
|
|
|
|
func Test_listPullRequestActionRunsFn(t *testing.T) {
|
|
const (
|
|
owner = "octo"
|
|
repo = "demo"
|
|
pullNumber = 42
|
|
headSHA = "abc123"
|
|
)
|
|
|
|
var gotRunsQuery string
|
|
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch r.URL.Path {
|
|
case fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d", owner, repo, pullNumber):
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write(fmt.Appendf(nil, `{"number":%d,"head":{"sha":"%s"}}`, pullNumber, headSHA))
|
|
case fmt.Sprintf("/api/v1/repos/%s/%s/actions/runs", owner, repo):
|
|
gotRunsQuery = r.URL.RawQuery
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"total_count":1,"workflow_runs":[{"id":9,"name":"CI","status":"success"}]}`))
|
|
default:
|
|
http.NotFound(w, r)
|
|
}
|
|
})
|
|
|
|
server := httptest.NewServer(handler)
|
|
defer server.Close()
|
|
|
|
origHost := flag.Host
|
|
origToken := flag.Token
|
|
flag.Host = server.URL
|
|
flag.Token = ""
|
|
defer func() {
|
|
flag.Host = origHost
|
|
flag.Token = origToken
|
|
}()
|
|
|
|
args := map[string]any{
|
|
"owner": owner,
|
|
"repo": repo,
|
|
"pull_number": float64(pullNumber),
|
|
}
|
|
|
|
result, err := listPullRequestActionRunsFn(context.Background(), args)
|
|
if err != nil {
|
|
t.Fatalf("listPullRequestActionRunsFn() error = %v", err)
|
|
}
|
|
if result.IsError {
|
|
t.Fatalf("listPullRequestActionRunsFn() returned error result: %+v", result)
|
|
}
|
|
|
|
if gotRunsQuery == "" {
|
|
t.Fatalf("expected actions/runs to be called")
|
|
}
|
|
values, err := url.ParseQuery(gotRunsQuery)
|
|
if err != nil {
|
|
t.Fatalf("parse actions/runs query: %v", err)
|
|
}
|
|
if got := values.Get("head_sha"); got != headSHA {
|
|
t.Fatalf("actions/runs head_sha = %q, want %q", got, headSHA)
|
|
}
|
|
|
|
if len(result.Content) == 0 {
|
|
t.Fatalf("expected content in result")
|
|
}
|
|
textContent, ok := result.Content[0].(*mcp.TextContent)
|
|
if !ok {
|
|
t.Fatalf("expected text content, got %T", result.Content[0])
|
|
}
|
|
|
|
var parsed struct {
|
|
WorkflowRuns []map[string]any `json:"workflow_runs"`
|
|
}
|
|
if err := json.Unmarshal([]byte(textContent.Text), &parsed); err != nil {
|
|
t.Fatalf("unmarshal result text: %v", err)
|
|
}
|
|
if len(parsed.WorkflowRuns) != 1 {
|
|
t.Fatalf("expected 1 run, got %d", len(parsed.WorkflowRuns))
|
|
}
|
|
if got := parsed.WorkflowRuns[0]["name"]; got != "CI" {
|
|
t.Fatalf("run name = %v, want %q", got, "CI")
|
|
}
|
|
}
|