Add agentpr pr edit subcommand
Update a PR's title and/or body as the agent user, sending only the fields supplied.
This commit is contained in:
+52
-2
@@ -5,6 +5,7 @@
|
||||
//
|
||||
// agentpr pr create --repo owner/repo --base main --head feature --title T --body B
|
||||
// agentpr pr comment --repo owner/repo --pr 12 --body "..."
|
||||
// agentpr pr edit --repo owner/repo --pr 12 --title T --body B
|
||||
// agentpr whoami
|
||||
package main
|
||||
|
||||
@@ -56,9 +57,9 @@ func client() (*agent.GiteaClient, error) {
|
||||
func newPRCmd() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "pr",
|
||||
Short: "Create PRs and post PR comments",
|
||||
Short: "Create and edit PRs, and post PR comments",
|
||||
}
|
||||
cmd.AddCommand(newPRCreateCmd(), newPRCommentCmd())
|
||||
cmd.AddCommand(newPRCreateCmd(), newPRCommentCmd(), newPREditCmd())
|
||||
return cmd
|
||||
}
|
||||
|
||||
@@ -143,6 +144,55 @@ func newPRCommentCmd() *cobra.Command {
|
||||
return cmd
|
||||
}
|
||||
|
||||
func newPREditCmd() *cobra.Command {
|
||||
var repo, title, body string
|
||||
var pr int
|
||||
cmd := &cobra.Command{
|
||||
Use: "edit",
|
||||
Short: "Edit a pull request's title and/or body",
|
||||
SilenceUsage: true,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
owner, name, err := agent.ParseRepo(repo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if pr <= 0 {
|
||||
return fmt.Errorf("--pr must be a positive PR number")
|
||||
}
|
||||
// Only the flags actually given are sent: omitting --title must
|
||||
// leave the title as it is, not blank it.
|
||||
var opts agent.EditPROptions
|
||||
if cmd.Flags().Changed("title") {
|
||||
opts.Title = &title
|
||||
}
|
||||
if cmd.Flags().Changed("body") {
|
||||
opts.Body = &body
|
||||
}
|
||||
if opts.Title == nil && opts.Body == nil {
|
||||
return fmt.Errorf("at least one of --title or --body is required")
|
||||
}
|
||||
c, err := client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
updated, err := c.EditPR(owner+"/"+name, pr, opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("#%d %s\n", updated.Number, updated.HTMLURL)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
f := cmd.Flags()
|
||||
f.StringVar(&repo, "repo", "", "Repository as owner/repo (required)")
|
||||
f.IntVar(&pr, "pr", 0, "PR number (required)")
|
||||
f.StringVar(&title, "title", "", "New PR title (unchanged when omitted)")
|
||||
f.StringVar(&body, "body", "", "New PR body (unchanged when omitted)")
|
||||
_ = cmd.MarkFlagRequired("repo")
|
||||
_ = cmd.MarkFlagRequired("pr")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func newWhoamiCmd() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "whoami",
|
||||
|
||||
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -16,3 +17,19 @@ func TestExecuteBadRepoErrors(t *testing.T) {
|
||||
t.Fatal("Execute() = nil, want error for a malformed --repo")
|
||||
}
|
||||
}
|
||||
|
||||
// `pr edit` with neither --title nor --body has nothing to send; it must fail
|
||||
// with a usage error before any Vault/Gitea call, so this stays hermetic.
|
||||
func TestPREditRequiresTitleOrBody(t *testing.T) {
|
||||
cmd := newRootCmd()
|
||||
cmd.SetArgs([]string{"pr", "edit", "--repo", "unkin/repo", "--pr", "7"})
|
||||
cmd.SetOut(io.Discard)
|
||||
cmd.SetErr(io.Discard)
|
||||
err := cmd.Execute()
|
||||
if err == nil {
|
||||
t.Fatal("Execute() = nil, want an error when neither --title nor --body is given")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "--title or --body") {
|
||||
t.Errorf("error = %q, want it to name the missing flags", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user