forked from Gitlink/gitlink-cli
545 lines
18 KiB
Go
545 lines
18 KiB
Go
package pr
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
|
|
clierrors "github.com/gitlink-org/gitlink-cli/internal/errors"
|
|
"github.com/gitlink-org/gitlink-cli/internal/output"
|
|
"github.com/gitlink-org/gitlink-cli/shortcuts/common"
|
|
)
|
|
|
|
func Shortcuts() []*common.Shortcut {
|
|
return []*common.Shortcut{
|
|
newApproveShortcut(),
|
|
newRequestChangesShortcut(),
|
|
newReviewsShortcut(),
|
|
{
|
|
Name: "list",
|
|
Description: "List pull requests",
|
|
Example: " gitlink-cli pr +list --state open\n gitlink-cli pr +list --state merged --page 1 --limit 50\n gitlink-cli pr +list --columns id,title,state,user --state all",
|
|
Flags: []common.Flag{
|
|
{Name: "state", Short: "s", Usage: "Filter by state", Default: "open", Choices: []string{"open", "merged", "closed", "all"}},
|
|
{Name: "page", Short: "p", Usage: "Page number", Default: "1"},
|
|
{Name: "limit", Short: "l", Usage: "Items per page", Default: "20"},
|
|
},
|
|
Run: func(ctx *common.RuntimeContext) error {
|
|
if err := ctx.ResolveOwnerRepo(); err != nil {
|
|
return err
|
|
}
|
|
q := url.Values{}
|
|
q.Set("page", ctx.Arg("page"))
|
|
q.Set("limit", ctx.Arg("limit"))
|
|
if s := ctx.Arg("state"); s != "" {
|
|
q.Set("state", s)
|
|
}
|
|
env, err := ctx.CallAPIWithQuery("GET", ctx.RepoPath()+"/pulls", q)
|
|
if err != nil {
|
|
return clierrors.OpError(clierrors.KindServer, "list", "pull requests", err).WithCommand(ctx.CommandName)
|
|
}
|
|
return ctx.Output(env)
|
|
},
|
|
},
|
|
{
|
|
Name: "create",
|
|
Description: "Create a pull request",
|
|
Long: "Create a new pull request from --head branch to --base branch. Requires --title and --head.",
|
|
Example: " gitlink-cli pr +create --title \"Fix login crash\" --head feat/new-login\n gitlink-cli pr +create --title \"New feature\" --head dev --base master --body \"Description...\"",
|
|
DryRun: true,
|
|
DryRunHint: func(ctx *common.RuntimeContext) (string, error) {
|
|
title := ctx.Arg("title")
|
|
head := ctx.Arg("head")
|
|
base := ctx.Arg("base")
|
|
if base == "" {
|
|
base = "master"
|
|
}
|
|
return fmt.Sprintf("Create PR: %s (%s -> %s)", title, head, base), nil
|
|
},
|
|
Flags: []common.Flag{
|
|
{Name: "title", Short: "t", Usage: "PR title", Required: true},
|
|
{Name: "body", Short: "b", Usage: "PR description"},
|
|
{Name: "head", Usage: "Source branch", Required: true},
|
|
{Name: "base", Usage: "Target branch", Default: "master"},
|
|
},
|
|
Run: func(ctx *common.RuntimeContext) error {
|
|
if err := ctx.ResolveOwnerRepo(); err != nil {
|
|
return err
|
|
}
|
|
title, err := ctx.RequireArg("title", `--title "Fix login crash"`)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
head, err := ctx.RequireArg("head", `--head feat/new-login`)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
base := ctx.Arg("base")
|
|
if base == "" {
|
|
base = "master"
|
|
}
|
|
payload := map[string]interface{}{
|
|
"title": title,
|
|
"head": head,
|
|
"base": base,
|
|
}
|
|
if b := ctx.Arg("body"); b != "" {
|
|
payload["body"] = b
|
|
}
|
|
env, err := ctx.CallAPI("POST", ctx.RepoPath()+"/pulls", payload)
|
|
if err != nil {
|
|
return clierrors.OpError(clierrors.KindServer, "create", "pull request", err).WithCommand(ctx.CommandName)
|
|
}
|
|
return ctx.Output(env)
|
|
},
|
|
},
|
|
{
|
|
Name: "view",
|
|
Description: "View pull request details",
|
|
Flags: []common.Flag{
|
|
{Name: "id", Short: "i", Usage: "PR number", Required: true},
|
|
},
|
|
Run: func(ctx *common.RuntimeContext) error {
|
|
if err := ctx.ResolveOwnerRepo(); err != nil {
|
|
return err
|
|
}
|
|
id, err := ctx.RequireArg("id", "--id 42")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
env, err := ctx.CallAPI("GET", fmt.Sprintf("%s/pulls/%s", ctx.RepoPath(), id), nil)
|
|
if err != nil {
|
|
return clierrors.OpError(clierrors.KindNotFound, "view", "pull request", err).WithCommand(ctx.CommandName)
|
|
}
|
|
return ctx.Output(env)
|
|
},
|
|
},
|
|
{
|
|
Name: "merge",
|
|
Description: "Merge a pull request",
|
|
Example: " gitlink-cli pr +merge --id 42\n gitlink-cli pr +merge --id 42 --method rebase\n gitlink-cli pr +merge --id 42 --method squash --dry-run",
|
|
DryRun: true,
|
|
DryRunHint: func(ctx *common.RuntimeContext) (string, error) {
|
|
id := ctx.Arg("id")
|
|
method := ctx.Arg("method")
|
|
if method == "" {
|
|
method = "merge"
|
|
}
|
|
return fmt.Sprintf("Merge PR #%s (method: %s)", id, method), nil
|
|
},
|
|
Flags: []common.Flag{
|
|
{Name: "id", Short: "i", Usage: "PR number", Required: true},
|
|
{Name: "method", Short: "m", Usage: "Merge method", Default: "merge", Choices: []string{"merge", "rebase", "squash"}},
|
|
},
|
|
Run: func(ctx *common.RuntimeContext) error {
|
|
if err := ctx.ResolveOwnerRepo(); err != nil {
|
|
return err
|
|
}
|
|
id, err := ctx.RequireArg("id", "--id 42")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
method := ctx.Arg("method")
|
|
if method == "" {
|
|
method = "merge"
|
|
}
|
|
payload := map[string]interface{}{
|
|
"do": method,
|
|
}
|
|
env, err := ctx.CallAPI("POST", fmt.Sprintf("%s/pulls/%s/pr_merge", ctx.RepoPath(), id), payload)
|
|
if err != nil {
|
|
return clierrors.OpError(clierrors.KindServer, "merge", "pull request", err).WithCommand(ctx.CommandName)
|
|
}
|
|
return ctx.Output(env)
|
|
},
|
|
},
|
|
{
|
|
Name: "close",
|
|
Description: "Close a pull request",
|
|
DryRun: true,
|
|
DryRunHint: func(ctx *common.RuntimeContext) (string, error) {
|
|
id := ctx.Arg("id")
|
|
return fmt.Sprintf("Close PR #%s", id), nil
|
|
},
|
|
Flags: []common.Flag{
|
|
{Name: "id", Short: "i", Usage: "PR number", Required: true},
|
|
},
|
|
Run: func(ctx *common.RuntimeContext) error {
|
|
if err := ctx.ResolveOwnerRepo(); err != nil {
|
|
return err
|
|
}
|
|
id, err := ctx.RequireArg("id", "--id 42")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
env, err := ctx.CallAPI("POST", fmt.Sprintf("%s/pulls/%s/refuse_merge", ctx.RepoPath(), id), nil)
|
|
if err != nil {
|
|
return clierrors.OpError(clierrors.KindServer, "close", "pull request", err).WithCommand(ctx.CommandName)
|
|
}
|
|
return ctx.Output(env)
|
|
},
|
|
},
|
|
{
|
|
Name: "files",
|
|
Description: "List changed files in a pull request",
|
|
Flags: []common.Flag{
|
|
{Name: "id", Short: "i", Usage: "PR number", Required: true},
|
|
},
|
|
Run: func(ctx *common.RuntimeContext) error {
|
|
if err := ctx.ResolveOwnerRepo(); err != nil {
|
|
return err
|
|
}
|
|
id, err := ctx.RequireArg("id", "--id 42")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
env, err := ctx.CallAPI("GET", fmt.Sprintf("%s/pulls/%s/files", ctx.RepoPath(), id), nil)
|
|
if err != nil {
|
|
return clierrors.OpError(clierrors.KindServer, "list", "PR files", err).WithCommand(ctx.CommandName)
|
|
}
|
|
return ctx.Output(env)
|
|
},
|
|
},
|
|
{
|
|
Name: "diff",
|
|
Description: "Show diff for a pull request",
|
|
Flags: []common.Flag{
|
|
{Name: "id", Short: "i", Usage: "PR number", Required: true},
|
|
},
|
|
Run: func(ctx *common.RuntimeContext) error {
|
|
if err := ctx.ResolveOwnerRepo(); err != nil {
|
|
return err
|
|
}
|
|
id, err := ctx.RequireArg("id", "--id 42")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
env, err := ctx.CallAPI("GET", fmt.Sprintf("%s/pulls/%s/files", ctx.RepoPath(), id), nil)
|
|
if err != nil {
|
|
return clierrors.OpError(clierrors.KindServer, "view", "PR diff", err).WithCommand(ctx.CommandName)
|
|
}
|
|
return ctx.Output(env)
|
|
},
|
|
},
|
|
{
|
|
Name: "comment",
|
|
Description: "Add a comment to a pull request",
|
|
DryRun: true,
|
|
DryRunHint: func(ctx *common.RuntimeContext) (string, error) {
|
|
id := ctx.Arg("id")
|
|
return fmt.Sprintf("Add comment to PR #%s", id), nil
|
|
},
|
|
Flags: []common.Flag{
|
|
{Name: "id", Short: "i", Usage: "PR number", Required: true},
|
|
{Name: "body", Short: "b", Usage: "Comment body", Required: true},
|
|
},
|
|
Run: func(ctx *common.RuntimeContext) error {
|
|
if err := ctx.ResolveOwnerRepo(); err != nil {
|
|
return err
|
|
}
|
|
id, err := ctx.RequireArg("id", "--id 42")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
body, err := ctx.RequireArg("body", `--body "Looks good to me"`)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
prEnv, err := ctx.CallAPI("GET", fmt.Sprintf("%s/pulls/%s", ctx.RepoPath(), id), nil)
|
|
if err != nil {
|
|
return clierrors.OpError(clierrors.KindNotFound, "view", "pull request", err).WithCommand(ctx.CommandName)
|
|
}
|
|
issueID, err := extractIssueID(prEnv)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
payload := map[string]interface{}{
|
|
"notes": body,
|
|
}
|
|
env, err := ctx.CallAPI("POST", fmt.Sprintf("/v1/%s/%s/issues/%d/journals", ctx.Owner, ctx.Repo, issueID), payload)
|
|
if err != nil {
|
|
return clierrors.OpError(clierrors.KindServer, "comment", "pull request", err).WithCommand(ctx.CommandName)
|
|
}
|
|
return ctx.Output(env)
|
|
},
|
|
},
|
|
// === PR 增强 ===
|
|
{
|
|
Name: "reopen",
|
|
Description: "Reopen a closed pull request",
|
|
DryRun: true,
|
|
DryRunHint: func(ctx *common.RuntimeContext) (string, error) {
|
|
return fmt.Sprintf("重新打开 PR #%s", ctx.Arg("id")), nil
|
|
},
|
|
Flags: []common.Flag{
|
|
{Name: "id", Short: "i", Usage: "PR number", Required: true},
|
|
},
|
|
Run: func(ctx *common.RuntimeContext) error {
|
|
if err := ctx.ResolveOwnerRepo(); err != nil {
|
|
return err
|
|
}
|
|
id, err := ctx.RequireArg("id", "--id 42")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
env, err := ctx.CallAPI("POST", fmt.Sprintf("/v1/%s/%s/pulls/%s/reopen", ctx.Owner, ctx.Repo, id), nil)
|
|
if err != nil {
|
|
return clierrors.OpError(clierrors.KindServer, "reopen", "pull request", err).WithCommand(ctx.CommandName)
|
|
}
|
|
return ctx.Output(env)
|
|
},
|
|
},
|
|
{
|
|
Name: "update",
|
|
Description: "Update a pull request",
|
|
DryRun: true,
|
|
DryRunHint: func(ctx *common.RuntimeContext) (string, error) {
|
|
return fmt.Sprintf("更新 PR #%s", ctx.Arg("id")), nil
|
|
},
|
|
Flags: []common.Flag{
|
|
{Name: "id", Short: "i", Usage: "PR number", Required: true},
|
|
{Name: "title", Short: "t", Usage: "New title"},
|
|
{Name: "body", Short: "b", Usage: "New description"},
|
|
{Name: "head", Usage: "Source branch"},
|
|
{Name: "base", Usage: "Target branch"},
|
|
},
|
|
Run: func(ctx *common.RuntimeContext) error {
|
|
if err := ctx.ResolveOwnerRepo(); err != nil {
|
|
return err
|
|
}
|
|
id, err := ctx.RequireArg("id", "--id 42")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// Fetch existing PR to fill required fields
|
|
prEnv, err := ctx.CallAPI("GET", fmt.Sprintf("%s/pulls/%s", ctx.RepoPath(), id), nil)
|
|
if err != nil {
|
|
return clierrors.OpError(clierrors.KindNotFound, "view", "pull request", err).WithCommand(ctx.CommandName)
|
|
}
|
|
prData, ok := prEnv.Data.(map[string]interface{})
|
|
if !ok {
|
|
return clierrors.InputError("unexpected PR format", "API 返回格式异常").WithCommand(ctx.CommandName)
|
|
}
|
|
payload := map[string]interface{}{
|
|
"title": prData["title"],
|
|
"body": prData["body"],
|
|
"head": prData["head"],
|
|
"base": prData["base"],
|
|
"issue_tag_ids": []string{},
|
|
"receivers_login": []string{},
|
|
}
|
|
if t := ctx.Arg("title"); t != "" {
|
|
payload["title"] = t
|
|
}
|
|
if b := ctx.Arg("body"); b != "" {
|
|
payload["body"] = b
|
|
}
|
|
if h := ctx.Arg("head"); h != "" {
|
|
payload["head"] = h
|
|
}
|
|
if bs := ctx.Arg("base"); bs != "" {
|
|
payload["base"] = bs
|
|
}
|
|
env, err := ctx.CallAPI("PUT", fmt.Sprintf("%s/pulls/%s", ctx.RepoPath(), id), payload)
|
|
if err != nil {
|
|
return clierrors.OpError(clierrors.KindServer, "update", "pull request", err).WithCommand(ctx.CommandName)
|
|
}
|
|
return ctx.Output(env)
|
|
},
|
|
},
|
|
{
|
|
Name: "commits",
|
|
Description: "List commits in a pull request",
|
|
Flags: []common.Flag{
|
|
{Name: "id", Short: "i", Usage: "PR number", Required: true},
|
|
},
|
|
Run: func(ctx *common.RuntimeContext) error {
|
|
if err := ctx.ResolveOwnerRepo(); err != nil {
|
|
return err
|
|
}
|
|
id, err := ctx.RequireArg("id", "--id 42")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
env, err := ctx.CallAPI("GET", fmt.Sprintf("%s/pulls/%s/commits", ctx.RepoPath(), id), nil)
|
|
if err != nil {
|
|
return clierrors.OpError(clierrors.KindServer, "list", "PR commits", err).WithCommand(ctx.CommandName)
|
|
}
|
|
return ctx.Output(env)
|
|
},
|
|
},
|
|
{
|
|
Name: "versions",
|
|
Description: "List versions of a pull request",
|
|
Flags: []common.Flag{
|
|
{Name: "id", Short: "i", Usage: "PR number", Required: true},
|
|
},
|
|
Run: func(ctx *common.RuntimeContext) error {
|
|
if err := ctx.ResolveOwnerRepo(); err != nil {
|
|
return err
|
|
}
|
|
id, err := ctx.RequireArg("id", "--id 42")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
env, err := ctx.CallAPI("GET", fmt.Sprintf("/v1/%s/%s/pulls/%s/versions", ctx.Owner, ctx.Repo, id), nil)
|
|
if err != nil {
|
|
return clierrors.OpError(clierrors.KindServer, "list", "PR versions", err).WithCommand(ctx.CommandName)
|
|
}
|
|
return ctx.Output(env)
|
|
},
|
|
},
|
|
{
|
|
Name: "vdiff",
|
|
Description: "Show diff of a specific PR version",
|
|
Flags: []common.Flag{
|
|
{Name: "id", Short: "i", Usage: "PR number", Required: true},
|
|
{Name: "version", Short: "v", Usage: "Version ID", Required: true},
|
|
{Name: "filepath", Usage: "Filter by file path"},
|
|
},
|
|
Run: func(ctx *common.RuntimeContext) error {
|
|
if err := ctx.ResolveOwnerRepo(); err != nil {
|
|
return err
|
|
}
|
|
id, err := ctx.RequireArg("id", "--id 42")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
versionID, err := ctx.RequireArg("version", "--version 5")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
q := url.Values{}
|
|
if fp := ctx.Arg("filepath"); fp != "" {
|
|
q.Set("filepath", fp)
|
|
}
|
|
env, err := ctx.CallAPIWithQuery("GET", fmt.Sprintf("/v1/%s/%s/pulls/%s/versions/%s/diff", ctx.Owner, ctx.Repo, id, versionID), q)
|
|
if err != nil {
|
|
return clierrors.OpError(clierrors.KindServer, "view", "PR version diff", err).WithCommand(ctx.CommandName)
|
|
}
|
|
return ctx.Output(env)
|
|
},
|
|
},
|
|
{
|
|
Name: "filesv1",
|
|
Description: "List changed files (v1 API with pagination)",
|
|
Flags: []common.Flag{
|
|
{Name: "id", Short: "i", Usage: "PR number", Required: true},
|
|
{Name: "filepath", Usage: "Filter by file path"},
|
|
{Name: "page", Short: "p", Usage: "Page number", Default: "1"},
|
|
{Name: "limit", Short: "l", Usage: "Items per page", Default: "20"},
|
|
},
|
|
Run: func(ctx *common.RuntimeContext) error {
|
|
if err := ctx.ResolveOwnerRepo(); err != nil {
|
|
return err
|
|
}
|
|
id, err := ctx.RequireArg("id", "--id 42")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
q := url.Values{}
|
|
q.Set("page", ctx.Arg("page"))
|
|
q.Set("limit", ctx.Arg("limit"))
|
|
if fp := ctx.Arg("filepath"); fp != "" {
|
|
q.Set("filepath", fp)
|
|
}
|
|
env, err := ctx.CallAPIWithQuery("GET", fmt.Sprintf("/v1/%s/%s/pulls/%s/files", ctx.Owner, ctx.Repo, id), q)
|
|
if err != nil {
|
|
return clierrors.OpError(clierrors.KindServer, "list", "PR files", err).WithCommand(ctx.CommandName)
|
|
}
|
|
return ctx.Output(env)
|
|
},
|
|
},
|
|
// === PR 评论管理 ===
|
|
{
|
|
Name: "comment-edit",
|
|
Description: "Edit a PR review comment",
|
|
Flags: []common.Flag{
|
|
{Name: "id", Short: "i", Usage: "PR number", Required: true},
|
|
{Name: "comment-id", Short: "c", Usage: "Comment ID", Required: true},
|
|
{Name: "body", Short: "b", Usage: "New comment body", Required: true},
|
|
{Name: "commit", Usage: "Commit SHA"},
|
|
{Name: "state", Usage: "Comment state", Default: "opened", Choices: []string{"opened", "resolved", "disabled"}},
|
|
},
|
|
Run: func(ctx *common.RuntimeContext) error {
|
|
if err := ctx.ResolveOwnerRepo(); err != nil {
|
|
return err
|
|
}
|
|
id, err := ctx.RequireArg("id", "--id 42")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
commentID, err := ctx.RequireArg("comment-id", "--comment-id 123")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
body, err := ctx.RequireArg("body", `--body "updated comment"`)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
payload := map[string]interface{}{
|
|
"note": body,
|
|
"state": ctx.Arg("state"),
|
|
}
|
|
if commit := ctx.Arg("commit"); commit != "" {
|
|
payload["commit_id"] = commit
|
|
} else {
|
|
payload["commit_id"] = ""
|
|
}
|
|
env, err := ctx.CallAPI("PUT", fmt.Sprintf("/v1/%s/%s/pulls/%s/journals/%s", ctx.Owner, ctx.Repo, id, commentID), payload)
|
|
if err != nil {
|
|
return clierrors.OpError(clierrors.KindServer, "edit", "PR comment", err).WithCommand(ctx.CommandName)
|
|
}
|
|
return ctx.Output(env)
|
|
},
|
|
},
|
|
{
|
|
Name: "comment-delete",
|
|
Description: "Delete a PR review comment",
|
|
DryRun: true,
|
|
DryRunHint: func(ctx *common.RuntimeContext) (string, error) {
|
|
return fmt.Sprintf("删除 PR #%s 的评论 #%s", ctx.Arg("id"), ctx.Arg("comment-id")), nil
|
|
},
|
|
Flags: []common.Flag{
|
|
{Name: "id", Short: "i", Usage: "PR number", Required: true},
|
|
{Name: "comment-id", Short: "c", Usage: "Comment ID", Required: true},
|
|
},
|
|
Run: func(ctx *common.RuntimeContext) error {
|
|
if err := ctx.ResolveOwnerRepo(); err != nil {
|
|
return err
|
|
}
|
|
id, err := ctx.RequireArg("id", "--id 42")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
commentID, err := ctx.RequireArg("comment-id", "--comment-id 123")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
env, err := ctx.CallAPI("DELETE", fmt.Sprintf("/v1/%s/%s/pulls/%s/journals/%s", ctx.Owner, ctx.Repo, id, commentID), nil)
|
|
if err != nil {
|
|
return clierrors.OpError(clierrors.KindServer, "delete", "PR comment", err).WithCommand(ctx.CommandName)
|
|
}
|
|
return ctx.Output(env)
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func extractIssueID(env *output.Envelope) (int64, error) {
|
|
data, ok := env.Data.(map[string]interface{})
|
|
if !ok {
|
|
return 0, clierrors.InputError("unexpected PR response format", "API 返回格式异常,请稍后重试")
|
|
}
|
|
issue, ok := data["issue"].(map[string]interface{})
|
|
if !ok {
|
|
return 0, clierrors.InputError("PR response missing issue field", "API 返回数据中缺少 issue 字段,请稍后重试")
|
|
}
|
|
idFloat, ok := issue["id"].(float64)
|
|
if !ok {
|
|
return 0, clierrors.InputError("PR response missing issue.id field", "API 返回数据中缺少 issue.id 字段,请稍后重试")
|
|
}
|
|
return int64(idFloat), nil
|
|
}
|