Add subcomand 'pulls create' #144

Merged
6543 merged 13 commits from 6543/tea:pulls-create into master 2020-07-16 15:00:54 +00:00
2 changed files with 120 additions and 0 deletions

View File

@ -35,6 +35,7 @@ var CmdPulls = cli.Command{
Subcommands: []*cli.Command{
&CmdPullsCheckout,
&CmdPullsClean,
&CmdPullsCreate,
},
}
@ -264,6 +265,111 @@ call me again with the --ignore-sha flag`, pr.Head.Ref)
return r.TeaDeleteBranch(branch, pr.Head.Ref, auth)
}
// CmdPullsCreate creates a pull request
var CmdPullsCreate = cli.Command{
Name: "create",
Usage: "Create a pull-request",
Description: "Create a pull-request",
Action: runPullsCreate,
Flags: append([]cli.Flag{
&cli.StringFlag{
Name: "head",
Usage: "Set head branch (default is current one)",
},
&cli.StringFlag{
Name: "base",
Aliases: []string{"b"},
Usage: "Set base branch (default is default branch)",
},
&cli.StringFlag{
Name: "title",
Aliases: []string{"t"},
Usage: "Set title of pull (default is head branch name)",
},
&cli.StringFlag{
Name: "description",
Aliases: []string{"d"},
Usage: "Set body of new pull",
},
}, AllDefaultFlags...),
}
func runPullsCreate(ctx *cli.Context) error {
login, ownerArg, repoArg := initCommand()
client := login.Client()
repo, err := login.Client().GetRepo(ownerArg, repoArg)
if err != nil {
log.Fatal(err)
}
// open local git repo
localRepo, err := local_git.RepoForWorkdir()
if err != nil {
log.Fatal(err)
}
base := ctx.String("base")
// default is default branch
if len(base) == 0 {
base = repo.DefaultBranch
}
head := ctx.String("head")
6543 marked this conversation as resolved
Review

There is a chance that head == "" (ex. if TeaGetCurrentBranchName errors), should this be handled?

There is a chance that `head == ""` (ex. if `TeaGetCurrentBranchName` errors), should this be handled?
Review

good question I dont know yet ...

good question I dont know yet ...
Review

@techknowlogick
422 Unprocessable Entity: [{"fieldNames":["Head"],"classification":"RequiredError","message":"Required"},{"fieldNames":["Title"],"classification":"RequiredError","message":"Required"}]

title and head are required :)

@techknowlogick `422 Unprocessable Entity: [{"fieldNames":["Head"],"classification":"RequiredError","message":"Required"},{"fieldNames":["Title"],"classification":"RequiredError","message":"Required"}]` title and head are required :)
Review

So this is only for origin repository branches pull requests? No forked repository?

So this is only for origin repository branches pull requests? No forked repository?
Review

I'd like to get basic feature it first and enhance it later

so you can create pulls from forks but only manualy not automaticaly by branch remote rev
at the moment

I'd like to get basic feature it first and enhance it later so you can create pulls from forks but only manualy not automaticaly by branch remote rev at the moment
// default is current one
if len(head) == 0 {
head, err = localRepo.TeaGetCurrentBranchName()
if err != nil {
log.Fatal(err)
}
}
title := ctx.String("title")
// default is head branch name

If this fails, even if it doesn't kill the whole operation, should it inform the user?

If this fails, even if it doesn't kill the whole operation, should it inform the user?
if len(title) == 0 {
title = head
if strings.Contains(title, ":") {
title = strings.SplitN(title, ":", 2)[1]
}
title = strings.Replace(title, "-", " ", -1)
title = strings.Replace(title, "_", " ", -1)
title = strings.Title(strings.ToLower(title))
}
// title is required
if len(title) == 0 {
fmt.Printf("Title is required")
return nil
}

Maybe something like

Title is required
Maybe something like ``` Title is required ```
// push if possible
err = localRepo.Push(&git.PushOptions{})
if err != nil {
fmt.Printf("Error occurred during 'git push':\n%s\n", err.Error())
6543 marked this conversation as resolved Outdated
fmt.Printf("Error occurred during 'git push':\n%s\n", err.Error())
``` fmt.Printf("Error occurred during 'git push':\n%s\n", err.Error()) ```
}
pr, err := client.CreatePullRequest(ownerArg, repoArg, gitea.CreatePullRequestOption{
Head: head,
6543 marked this conversation as resolved Outdated
Outdated
Review

description?

`description`?
Base: base,
Title: title,
Body: ctx.String("description"),
})
if err != nil {
log.Fatal(err)

Perhaps a link to the pull request here would be nice?

Perhaps a link to the pull request here would be nice?
}
fmt.Printf("#%d %s\n%s created %s\n", pr.Index,
pr.Title,
pr.Poster.UserName,
pr.Created.Format("2006-01-02 15:04:05"),
)
if len(pr.Body) != 0 {
fmt.Printf("\n%s\n", pr.Body)
}
fmt.Println(pr.HTMLURL)
return nil
}
func argToIndex(arg string) (int64, error) {
if strings.HasPrefix(arg, "#") {
arg = arg[1:]

View File

@ -175,3 +175,17 @@ func (r TeaRepo) TeaFindBranchByName(branchName, repoURL string) (b *git_config.
}
return b, b.Validate()
}
// TeaGetCurrentBranchName return the name of the branch witch is currently active
func (r TeaRepo) TeaGetCurrentBranchName() (string, error) {
localHead, err := r.Head()
if err != nil {
return "", err
}
if !localHead.Name().IsBranch() {
return "", fmt.Errorf("active ref is no branch")
}
return strings.TrimLeft(localHead.Name().String(), "refs/heads/"), nil
}