An intent verb for the trunk-based loop: branch from base → write the file → open a PR → auto-merge (squash) → delete the branch. One call instead of orchestrating file_write_branch + pr_create + workflow_run_status + pr_merge + branch_delete and remembering the conventions each time. The load-bearing safety is a pure, fail-closed CI gate (evaluateShipGate): merge=true ONLY when every workflow run for the PR head commit is completed+success AND the base branch is not review-protected. Every other state — CI pending / red / absent, review-required base, or an unclean merge — fails closed to PR-only and returns the PR with a reason. A change with no CI gate is never auto-merged to trunk (cf. agentsquad#36: non-compiling code reviewer-approved straight to main with no CI wall). - ci_timeout_seconds polls the head commit's runs to completion (default 0 = snapshot, returns pending right after opening the PR). - Derives a deterministic short-lived branch (tbd/<slug>-<hash>); handles new and existing files (fetches the blob sha for updates). - Adds head.sha + mergeable to the PR struct. - Tests: full gate matrix (green/pending/red/cancelled/none/mixed/protected) + Call happy-merge, no-CI fail-closed, red fail-closed, allowlist. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
152 lines
3.9 KiB
Go
152 lines
3.9 KiB
Go
package gitea
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/url"
|
|
)
|
|
|
|
type PullRequest struct {
|
|
Number int `json:"number"`
|
|
Title string `json:"title"`
|
|
Body string `json:"body"`
|
|
HTMLURL string `json:"html_url"`
|
|
State string `json:"state"`
|
|
Draft bool `json:"draft"`
|
|
Head struct {
|
|
Ref string `json:"ref"`
|
|
Sha string `json:"sha"`
|
|
} `json:"head"`
|
|
Base struct {
|
|
Ref string `json:"ref"`
|
|
} `json:"base"`
|
|
Mergeable bool `json:"mergeable"`
|
|
}
|
|
|
|
type CreatePullRequestArgs struct {
|
|
Title string `json:"title"`
|
|
Body string `json:"body"`
|
|
Head string `json:"head"`
|
|
Base string `json:"base"`
|
|
Draft bool `json:"draft"`
|
|
}
|
|
|
|
func (c *Client) CreatePullRequest(ctx context.Context, owner, repo string, args CreatePullRequestArgs) (*PullRequest, error) {
|
|
p := fmt.Sprintf("/api/v1/repos/%s/%s/pulls", owner, repo)
|
|
payload, err := json.Marshal(args)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
body, status, err := c.PostJSON(ctx, p, payload)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := MapStatus(status, body); err != nil {
|
|
return nil, err
|
|
}
|
|
var pr PullRequest
|
|
if err := json.Unmarshal(body, &pr); err != nil {
|
|
return nil, err
|
|
}
|
|
return &pr, nil
|
|
}
|
|
|
|
func (c *Client) GetPullRequest(ctx context.Context, owner, repo string, index int) (*PullRequest, error) {
|
|
p := fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d", owner, repo, index)
|
|
body, status, err := c.GetJSON(ctx, p)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := MapStatus(status, body); err != nil {
|
|
return nil, err
|
|
}
|
|
var pr PullRequest
|
|
if err := json.Unmarshal(body, &pr); err != nil {
|
|
return nil, err
|
|
}
|
|
return &pr, nil
|
|
}
|
|
|
|
type PullRequestFile struct {
|
|
Filename string `json:"filename"`
|
|
Status string `json:"status"` // added | modified | deleted | renamed
|
|
Additions int `json:"additions"`
|
|
Deletions int `json:"deletions"`
|
|
}
|
|
|
|
func (c *Client) GetPullRequestFiles(ctx context.Context, owner, repo string, index int) ([]PullRequestFile, error) {
|
|
p := fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/files", owner, repo, index)
|
|
body, status, err := c.GetJSON(ctx, p)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := MapStatus(status, body); err != nil {
|
|
return nil, err
|
|
}
|
|
var files []PullRequestFile
|
|
if err := json.Unmarshal(body, &files); err != nil {
|
|
return nil, err
|
|
}
|
|
return files, nil
|
|
}
|
|
|
|
// GetPullRequestDiff returns the raw unified diff. The endpoint serves text/plain, not JSON,
|
|
// so we use doRaw to bypass the json Accept header expectation.
|
|
func (c *Client) GetPullRequestDiff(ctx context.Context, owner, repo string, index int) ([]byte, error) {
|
|
p := fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d.diff", owner, repo, index)
|
|
resp, err := c.doRaw(ctx, "GET", p, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := MapStatus(resp.Status, resp.Body); err != nil {
|
|
return nil, err
|
|
}
|
|
return resp.Body, nil
|
|
}
|
|
|
|
type MergePRArgs struct {
|
|
Do string `json:"Do"`
|
|
Title string `json:"merge_message_title,omitempty"`
|
|
Body string `json:"merge_message_field,omitempty"`
|
|
}
|
|
|
|
func (c *Client) MergePullRequest(ctx context.Context, owner, repo string, index int, args MergePRArgs) error {
|
|
p := fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge", owner, repo, index)
|
|
payload, err := json.Marshal(args)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
body, status, err := c.PostJSON(ctx, p, payload)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return MapStatus(status, body)
|
|
}
|
|
|
|
func (c *Client) ListPullRequests(ctx context.Context, owner, repo, state, head string, page, limit int) ([]PullRequest, error) {
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
if limit < 1 {
|
|
limit = 30
|
|
}
|
|
p := fmt.Sprintf("/api/v1/repos/%s/%s/pulls?state=%s&page=%d&limit=%d",
|
|
owner, repo, url.QueryEscape(state), page, limit)
|
|
if head != "" {
|
|
p += "&head=" + url.QueryEscape(head)
|
|
}
|
|
body, status, err := c.GetJSON(ctx, p)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := MapStatus(status, body); err != nil {
|
|
return nil, err
|
|
}
|
|
var prs []PullRequest
|
|
if err := json.Unmarshal(body, &prs); err != nil {
|
|
return nil, err
|
|
}
|
|
return prs, nil
|
|
}
|