Skip to content

Commit

Permalink
pr: Add flag fillverbose
Browse files Browse the repository at this point in the history
This is used to fill the body of PR with all commits msg + body
of every commits because there can be lot of useful information.

Signed-off-by: Federico Guerinoni <guerinoni.federico@gmail.com>
  • Loading branch information
guerinoni committed Jan 7, 2024
1 parent c3adaf7 commit 77a93ce
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 140 deletions.
2 changes: 1 addition & 1 deletion git/client.go
Expand Up @@ -242,7 +242,7 @@ func (c *Client) Commits(ctx context.Context, baseRef, headRef string) ([]*Commi
title := 1
body := 2
for _, line := range outputLines(out) {
split := strings.Split(line, ",")
split := strings.SplitN(line, ",", 3)
if len(split) < 2 {
continue
}
Expand Down
10 changes: 10 additions & 0 deletions git/client_test.go
Expand Up @@ -477,6 +477,16 @@ func TestClientCommits(t *testing.T) {
Title: "testing testability test",
}},
},
{
name: "get commits with body",
cmdStdout: "6a6872b918c601a0e730710ad8473938a7516d30,testing testability test,This is the body",
wantCmdArgs: `path/to/git -c log.ShowSignature=false log --pretty=format:%H,%s,%b --cherry SHA1...SHA2`,
wantCommits: []*Commit{{
Sha: "6a6872b918c601a0e730710ad8473938a7516d30",
Title: "testing testability test",
Body: "This is the body",
}},
},
{
name: "no commits between SHAs",
wantCmdArgs: `path/to/git -c log.ShowSignature=false log --pretty=format:%H,%s,%b --cherry SHA1...SHA2`,
Expand Down
88 changes: 71 additions & 17 deletions pkg/cmd/pr/create/create.go
Expand Up @@ -45,6 +45,7 @@ type CreateOptions struct {
RepoOverride string

Autofill bool
FillVerbose bool
FillFirst bool
WebMode bool
RecoverFile string
Expand Down Expand Up @@ -163,6 +164,14 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
return cmdutil.FlagErrorf("`--fill` is not supported with `--fill-first`")
}

if opts.FillVerbose && opts.FillFirst {
return cmdutil.FlagErrorf("`--fill-verbose` is not supported with `--fill-first`")
}

if opts.FillVerbose && opts.Autofill {
return cmdutil.FlagErrorf("`--fill-verbose` is not supported with `--fill`")
}

opts.BodyProvided = cmd.Flags().Changed("body")
if bodyFile != "" {
b, err := cmdutil.ReadFile(bodyFile, opts.IO.In)
Expand All @@ -177,8 +186,8 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
return cmdutil.FlagErrorf("`--template` is not supported when using `--body` or `--body-file`")
}

if !opts.IO.CanPrompt() && !opts.WebMode && !(opts.Autofill || opts.FillFirst) && (!opts.TitleProvided || !opts.BodyProvided) {
return cmdutil.FlagErrorf("must provide `--title` and `--body` (or `--fill` or `fill-first`) when not running interactively")
if !opts.IO.CanPrompt() && !opts.WebMode && !(opts.FillVerbose || opts.Autofill || opts.FillFirst) && (!opts.TitleProvided || !opts.BodyProvided) {
return cmdutil.FlagErrorf("must provide `--title` and `--body` (or `--fill` or `fill-first` or `--fillverbose`) when not running interactively")
}

if runF != nil {
Expand All @@ -196,6 +205,7 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
fl.StringVarP(&opts.BaseBranch, "base", "B", "", "The `branch` into which you want your code merged")
fl.StringVarP(&opts.HeadBranch, "head", "H", "", "The `branch` that contains commits for your pull request (default: current branch)")
fl.BoolVarP(&opts.WebMode, "web", "w", false, "Open the web browser to create a pull request")
fl.BoolVarP(&opts.FillVerbose, "fill-verbose", "", false, "Use commits msg+body for description")
fl.BoolVarP(&opts.Autofill, "fill", "f", false, "Use commit info for title and body")
fl.BoolVar(&opts.FillFirst, "fill-first", false, "Use first commit info for title and body")
fl.StringSliceVarP(&opts.Reviewers, "reviewer", "r", nil, "Request reviews from people or teams by their `handle`")
Expand Down Expand Up @@ -292,7 +302,7 @@ func createRun(opts *CreateOptions) (err error) {
ghrepo.FullName(ctx.BaseRepo))
}

if opts.Autofill || opts.FillFirst || (opts.TitleProvided && opts.BodyProvided) {
if opts.FillVerbose || opts.Autofill || opts.FillFirst || (opts.TitleProvided && opts.BodyProvided) {
err = handlePush(*opts, *ctx)
if err != nil {
return
Expand Down Expand Up @@ -403,7 +413,40 @@ func createRun(opts *CreateOptions) (err error) {
return
}

func initDefaultTitleBody(ctx CreateContext, state *shared.IssueMetadataState, useFirstCommit bool) error {
func chunkString(s string, chunkSize int) []string {
var chunks []string
runes := []rune(s)

if len(runes) == 0 {
return []string{s}
}

for i := 0; i < len(runes); {
nn := i + chunkSize
fmt.Println(nn, i)
if nn > len(runes) {
chunks = append(chunks, string(runes[i:]))
return chunks
}

if runes[nn-1] == ' ' {
chunks = append(chunks, string(runes[i:nn]))
} else {
// find the last space before the end of the chunk
for ; nn > i; nn-- {
if runes[nn] == ' ' {
break
}
}
chunks = append(chunks, string(runes[i:nn]))
}

i += nn
}
return chunks
}

func initDefaultTitleBody(ctx CreateContext, state *shared.IssueMetadataState, useFirstCommit bool, addBody bool) error {
baseRef := ctx.BaseTrackingBranch
headRef := ctx.HeadBranch
gitClient := ctx.GitClient
Expand All @@ -412,19 +455,32 @@ func initDefaultTitleBody(ctx CreateContext, state *shared.IssueMetadataState, u
if err != nil {
return err
}
if len(commits) == 1 || useFirstCommit {
commitIndex := len(commits) - 1
state.Title = commits[commitIndex].Title
body, err := gitClient.CommitBody(context.Background(), commits[commitIndex].Sha)
if err != nil {
return err
}
state.Body = body

if useFirstCommit {
state.Title = commits[len(commits)-1].Title
state.Body = commits[len(commits)-1].Body
} else {
state.Title = humanize(headRef)
var body strings.Builder
for i := len(commits) - 1; i >= 0; i-- {
fmt.Fprintf(&body, "- %s\n", commits[i].Title)
if addBody {
chunks := chunkString(commits[i].Body, 72)
for idx, chunk := range chunks {
if chunk[0] == ' ' {
chunk = chunk[1:]
}
fmt.Fprintf(&body, " %s", chunk)
if idx < len(chunks)-1 {
fmt.Fprintln(&body)
}
}

if i > 0 {
fmt.Fprintln(&body)
fmt.Fprintln(&body)
}
}
}
state.Body = body.String()
}
Expand Down Expand Up @@ -495,9 +551,9 @@ func NewIssueState(ctx CreateContext, opts CreateOptions) (*shared.IssueMetadata
Draft: opts.IsDraft,
}

if opts.Autofill || opts.FillFirst || !opts.TitleProvided || !opts.BodyProvided {
err := initDefaultTitleBody(ctx, state, opts.FillFirst)
if err != nil && (opts.Autofill || opts.FillFirst) {
if opts.FillVerbose || opts.Autofill || opts.FillFirst || !opts.TitleProvided || !opts.BodyProvided {
err := initDefaultTitleBody(ctx, state, opts.FillFirst, opts.FillVerbose)
if err != nil && (opts.FillVerbose || opts.Autofill || opts.FillFirst) {
return nil, fmt.Errorf("could not compute title or body defaults: %w", err)
}
}
Expand Down Expand Up @@ -659,7 +715,6 @@ func NewCreateContext(opts *CreateOptions) (*CreateContext, error) {
Client: client,
GitClient: gitClient,
}, nil

}

func getRemotes(opts *CreateOptions) (ghContext.Remotes, error) {
Expand Down Expand Up @@ -716,7 +771,6 @@ func previewPR(opts CreateOptions, openURL string) error {
fmt.Fprintf(opts.IO.ErrOut, "Opening %s in your browser.\n", text.DisplayURL(openURL))
}
return opts.Browser.Browse(openURL)

}

func handlePush(opts CreateOptions, ctx CreateContext) error {
Expand Down

0 comments on commit 77a93ce

Please sign in to comment.