Slice 10: create_pr (open a pull request)
forge CreatePullRequest (Gitea; empty base resolves to the repo default branch). Service CreatePR records a pr-created event. HTTP POST /api/repo/pr/create; MCP tool create_pr; <pr-list> New pull request button (head = selected repo current branch, base = default). Live-tested end-to-end: app create_pr opened a real PR (base auto-resolved), listed, then cleaned up; main untouched. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -48,11 +48,21 @@ type MergeResult struct {
|
||||
BranchDeleted bool `json:"branchDeleted"`
|
||||
}
|
||||
|
||||
// NewPR describes a pull request to open. An empty Base means "the repo's
|
||||
// default branch".
|
||||
type NewPR struct {
|
||||
Head string
|
||||
Base string
|
||||
Title string
|
||||
Body string
|
||||
}
|
||||
|
||||
// Provider talks to one hosting provider.
|
||||
type Provider interface {
|
||||
// Handles reports whether this provider serves the given remote host.
|
||||
Handles(host string) bool
|
||||
ListPullRequests(ctx context.Context, owner, repo string) ([]PullRequest, error)
|
||||
CreatePullRequest(ctx context.Context, owner, repo string, pr NewPR) (PullRequest, error)
|
||||
MergeAndCleanup(ctx context.Context, owner, repo string, number int64, method MergeMethod) (MergeResult, error)
|
||||
}
|
||||
|
||||
|
||||
@@ -54,6 +54,36 @@ func (g *Gitea) ListPullRequests(_ context.Context, owner, repo string) ([]PullR
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// CreatePullRequest opens a PR. An empty Base resolves to the repo's default
|
||||
// branch. Requires the head branch to already exist on the remote.
|
||||
func (g *Gitea) CreatePullRequest(_ context.Context, owner, repo string, pr NewPR) (PullRequest, error) {
|
||||
if strings.TrimSpace(pr.Head) == "" {
|
||||
return PullRequest{}, fmt.Errorf("a head branch is required")
|
||||
}
|
||||
base := strings.TrimSpace(pr.Base)
|
||||
if base == "" {
|
||||
r, _, err := g.client.GetRepo(owner, repo)
|
||||
if err != nil {
|
||||
return PullRequest{}, err
|
||||
}
|
||||
base = r.DefaultBranch
|
||||
}
|
||||
title := pr.Title
|
||||
if strings.TrimSpace(title) == "" {
|
||||
title = pr.Head
|
||||
}
|
||||
created, _, err := g.client.CreatePullRequest(owner, repo, gitea.CreatePullRequestOption{
|
||||
Head: pr.Head,
|
||||
Base: base,
|
||||
Title: title,
|
||||
Body: pr.Body,
|
||||
})
|
||||
if err != nil {
|
||||
return PullRequest{}, err
|
||||
}
|
||||
return toPR(created), nil
|
||||
}
|
||||
|
||||
// MergeAndCleanup merges the PR and deletes its head branch (when the head is in
|
||||
// the same repo — never a fork's branch). Callers MUST have confirmed with the
|
||||
// user first (§1.4).
|
||||
|
||||
@@ -81,6 +81,15 @@ type mergePRInput struct {
|
||||
Number int64 `json:"number" jsonschema:"the pull request number to merge and clean up"`
|
||||
}
|
||||
|
||||
// createPRInput describes a pull request to open.
|
||||
type createPRInput struct {
|
||||
Path string `json:"path" jsonschema:"absolute path of the repository, from list_repos"`
|
||||
Head string `json:"head" jsonschema:"the branch to merge from (must already exist on the remote)"`
|
||||
Base string `json:"base" jsonschema:"the branch to merge into; leave empty for the repo's default branch"`
|
||||
Title string `json:"title" jsonschema:"the pull request title"`
|
||||
Body string `json:"body" jsonschema:"the pull request description (optional)"`
|
||||
}
|
||||
|
||||
// gitCommitInput is the argument schema for git_commit.
|
||||
type gitCommitInput struct {
|
||||
Path string `json:"path" jsonschema:"absolute path of the repository, from list_repos"`
|
||||
@@ -196,6 +205,18 @@ func NewServer(svc *service.Service, version string) *mcpsdk.Server {
|
||||
return nil, prListOutput{PRs: prs}, nil
|
||||
})
|
||||
|
||||
// create_pr — open a pull request from a branch.
|
||||
mcpsdk.AddTool(s, &mcpsdk.Tool{
|
||||
Name: "create_pr",
|
||||
Description: "Open a pull request from head into base (leave base empty for the repo's default branch). The head branch must already exist on the remote — push it first. Path is from list_repos.",
|
||||
}, func(ctx context.Context, _ *mcpsdk.CallToolRequest, in createPRInput) (*mcpsdk.CallToolResult, forge.PullRequest, error) {
|
||||
pr, err := svc.CreatePR(ctx, activity.ActorClaude, in.Path, in.Head, in.Base, in.Title, in.Body)
|
||||
if err != nil {
|
||||
return nil, forge.PullRequest{}, err
|
||||
}
|
||||
return nil, pr, nil
|
||||
})
|
||||
|
||||
// merge_and_cleanup_pr — DESTRUCTIVE: merges a PR and deletes its branch.
|
||||
mcpsdk.AddTool(s, &mcpsdk.Tool{
|
||||
Name: "merge_and_cleanup_pr",
|
||||
|
||||
@@ -137,6 +137,22 @@ func (s *Service) ForgePRs(ctx context.Context, repoPath string) ([]forge.PullRe
|
||||
return s.forge.ListPullRequests(ctx, owner, repo)
|
||||
}
|
||||
|
||||
// CreatePR opens a pull request from head into base (empty base = the repo's
|
||||
// default branch) and records the action. The head branch must already exist on
|
||||
// the remote (push it first).
|
||||
func (s *Service) CreatePR(ctx context.Context, actor activity.Actor, repoPath, head, base, title, body string) (forge.PullRequest, error) {
|
||||
owner, repo, err := s.resolveForge(ctx, repoPath)
|
||||
if err != nil {
|
||||
return forge.PullRequest{}, err
|
||||
}
|
||||
pr, err := s.forge.CreatePullRequest(ctx, owner, repo, forge.NewPR{Head: head, Base: base, Title: title, Body: body})
|
||||
if err != nil {
|
||||
return forge.PullRequest{}, err
|
||||
}
|
||||
s.feed.Record(actor, "pr-created", filepath.Clean(repoPath), fmt.Sprintf("PR #%d %s → %s", pr.Number, pr.Head, pr.Base))
|
||||
return pr, nil
|
||||
}
|
||||
|
||||
// MergeAndCleanup merges a PR and deletes its branch, then records the action.
|
||||
// The caller is responsible for confirming with the user first (§1.4); actor
|
||||
// distinguishes a UI action (user) from an MCP one (claude).
|
||||
|
||||
Reference in New Issue
Block a user