From c50a65182b6146d606408accf47a26b953dee0f2 Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Fri, 21 Feb 2020 22:21:44 +0100 Subject: [PATCH 01/11] Add pull-request command --- cmd/pull-request.go | 76 +++++++++++++++++++++++++++++++++++++++++++++ main.go | 1 + 2 files changed, 77 insertions(+) create mode 100644 cmd/pull-request.go diff --git a/cmd/pull-request.go b/cmd/pull-request.go new file mode 100644 index 0000000..79cf18f --- /dev/null +++ b/cmd/pull-request.go @@ -0,0 +1,76 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package cmd + +import ( + "fmt" + "log" + + "code.gitea.io/sdk/gitea" + + "github.com/urfave/cli/v2" +) + +// CmdPullRequest creates a pull request +var CmdPullRequest = cli.Command{ + Name: "pull-request", + Usage: "Create a pull-request", + Description: "Create a pull-request", + Action: runCreatePullRequest, + Flags: append([]cli.Flag{ + &cli.StringFlag{ + Name: "head", + Usage: "pull-request head", + }, + &cli.StringFlag{ + Name: "base, b", + Usage: "pull-request base", + }, + &cli.StringFlag{ + Name: "title, t", + Usage: "pull-request title", + }, + &cli.StringFlag{ + Name: "description, d", + Usage: "pull-request description", + }, + }, LoginRepoFlags...), +} + +func runCreatePullRequest(ctx *cli.Context) error { + login, owner, repo := initCommand() + + /* + Head string `json:"head" binding:"Required"` + Base string `json:"base" binding:"Required"` + Title string `json:"title" binding:"Required"` + Body string `json:"body"` + Assignee string `json:"assignee"` + Assignees []string `json:"assignees"` + Milestone int64 `json:"milestone"` + Labels []int64 `json:"labels"` + // swagger:strfmt date-time + Deadline *time.Time `json:"due_date"` + */ + + pr, err := login.Client().CreatePullRequest(owner, repo, gitea.CreatePullRequestOption{ + Head: ctx.String("head"), + Base: ctx.String("base"), + Title: ctx.String("title"), + Body: ctx.String("body"), + }) + + if err != nil { + log.Fatal(err) + } + + fmt.Printf("#%d %s\n%s created %s\n\n%s", pr.Index, + pr.Title, + pr.Poster.UserName, + pr.Created.Format("2006-01-02 15:04:05"), + pr.Body, + ) + return nil +} diff --git a/main.go b/main.go index 1b22a43..2d70108 100644 --- a/main.go +++ b/main.go @@ -38,6 +38,7 @@ func main() { &cmd.CmdLogout, &cmd.CmdIssues, &cmd.CmdPulls, + &cmd.CmdPullRequest, &cmd.CmdReleases, &cmd.CmdRepos, &cmd.CmdLabels, -- 2.40.1 From e0d5487da06019830270a86cbdc8580f43fbdc48 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 13 Jul 2020 17:13:52 +0200 Subject: [PATCH 02/11] refactor --- cmd/pull-request.go | 76 --------------------------------------------- cmd/pulls.go | 67 +++++++++++++++++++++++++++++++++++++++ main.go | 1 - 3 files changed, 67 insertions(+), 77 deletions(-) delete mode 100644 cmd/pull-request.go diff --git a/cmd/pull-request.go b/cmd/pull-request.go deleted file mode 100644 index 79cf18f..0000000 --- a/cmd/pull-request.go +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright 2020 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package cmd - -import ( - "fmt" - "log" - - "code.gitea.io/sdk/gitea" - - "github.com/urfave/cli/v2" -) - -// CmdPullRequest creates a pull request -var CmdPullRequest = cli.Command{ - Name: "pull-request", - Usage: "Create a pull-request", - Description: "Create a pull-request", - Action: runCreatePullRequest, - Flags: append([]cli.Flag{ - &cli.StringFlag{ - Name: "head", - Usage: "pull-request head", - }, - &cli.StringFlag{ - Name: "base, b", - Usage: "pull-request base", - }, - &cli.StringFlag{ - Name: "title, t", - Usage: "pull-request title", - }, - &cli.StringFlag{ - Name: "description, d", - Usage: "pull-request description", - }, - }, LoginRepoFlags...), -} - -func runCreatePullRequest(ctx *cli.Context) error { - login, owner, repo := initCommand() - - /* - Head string `json:"head" binding:"Required"` - Base string `json:"base" binding:"Required"` - Title string `json:"title" binding:"Required"` - Body string `json:"body"` - Assignee string `json:"assignee"` - Assignees []string `json:"assignees"` - Milestone int64 `json:"milestone"` - Labels []int64 `json:"labels"` - // swagger:strfmt date-time - Deadline *time.Time `json:"due_date"` - */ - - pr, err := login.Client().CreatePullRequest(owner, repo, gitea.CreatePullRequestOption{ - Head: ctx.String("head"), - Base: ctx.String("base"), - Title: ctx.String("title"), - Body: ctx.String("body"), - }) - - if err != nil { - log.Fatal(err) - } - - fmt.Printf("#%d %s\n%s created %s\n\n%s", pr.Index, - pr.Title, - pr.Poster.UserName, - pr.Created.Format("2006-01-02 15:04:05"), - pr.Body, - ) - return nil -} diff --git a/cmd/pulls.go b/cmd/pulls.go index 11e1294..61ee37c 100644 --- a/cmd/pulls.go +++ b/cmd/pulls.go @@ -35,6 +35,7 @@ var CmdPulls = cli.Command{ Subcommands: []*cli.Command{ &CmdPullsCheckout, &CmdPullsClean, + &CmdPullsCreate, }, } @@ -264,6 +265,72 @@ 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", + Aliases: []string{"h"}, + Usage: "pull-request head", + }, + &cli.StringFlag{ + Name: "base", + Aliases: []string{"b"}, + Usage: "pull-request base", + }, + &cli.StringFlag{ + Name: "title", + Aliases: []string{"t"}, + Usage: "pull-request title", + }, + &cli.StringFlag{ + Name: "description", + Aliases: []string{"d"}, + Usage: "pull-request description", + }, + }, AllDefaultFlags...), +} + +func runPullsCreate(ctx *cli.Context) error { + login, owner, repo := initCommand() + + /* + Head string `json:"head" binding:"Required"` + Base string `json:"base" binding:"Required"` + Title string `json:"title" binding:"Required"` + Body string `json:"body"` + Assignee string `json:"assignee"` + Assignees []string `json:"assignees"` + Milestone int64 `json:"milestone"` + Labels []int64 `json:"labels"` + // swagger:strfmt date-time + Deadline *time.Time `json:"due_date"` + */ + + pr, err := login.Client().CreatePullRequest(owner, repo, gitea.CreatePullRequestOption{ + Head: ctx.String("head"), + Base: ctx.String("base"), + Title: ctx.String("title"), + Body: ctx.String("body"), + }) + + if err != nil { + log.Fatal(err) + } + + fmt.Printf("#%d %s\n%s created %s\n\n%s", pr.Index, + pr.Title, + pr.Poster.UserName, + pr.Created.Format("2006-01-02 15:04:05"), + pr.Body, + ) + return nil +} + func argToIndex(arg string) (int64, error) { if strings.HasPrefix(arg, "#") { arg = arg[1:] diff --git a/main.go b/main.go index b982886..d73ba41 100644 --- a/main.go +++ b/main.go @@ -38,7 +38,6 @@ func main() { &cmd.CmdLogout, &cmd.CmdIssues, &cmd.CmdPulls, - &cmd.CmdPullRequest, &cmd.CmdReleases, &cmd.CmdRepos, &cmd.CmdLabels, -- 2.40.1 From a6d192cacefb05072cddc4f2769d03334aa37d2f Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 13 Jul 2020 17:18:32 +0200 Subject: [PATCH 03/11] fix help menue of pull create --- cmd/pulls.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/pulls.go b/cmd/pulls.go index 61ee37c..bb7a2ed 100644 --- a/cmd/pulls.go +++ b/cmd/pulls.go @@ -274,7 +274,6 @@ var CmdPullsCreate = cli.Command{ Flags: append([]cli.Flag{ &cli.StringFlag{ Name: "head", - Aliases: []string{"h"}, Usage: "pull-request head", }, &cli.StringFlag{ -- 2.40.1 From 7440e6053b003b7dda4493e0c4b123c01fc569b5 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 13 Jul 2020 17:56:36 +0200 Subject: [PATCH 04/11] impruve --- cmd/pulls.go | 62 +++++++++++++++++++++++++++---------------- modules/git/branch.go | 13 +++++++++ 2 files changed, 52 insertions(+), 23 deletions(-) diff --git a/cmd/pulls.go b/cmd/pulls.go index bb7a2ed..b1c8c71 100644 --- a/cmd/pulls.go +++ b/cmd/pulls.go @@ -273,47 +273,63 @@ var CmdPullsCreate = cli.Command{ Action: runPullsCreate, Flags: append([]cli.Flag{ &cli.StringFlag{ - Name: "head", - Usage: "pull-request head", + Name: "head", + Usage: "Set head branch (default is current one)", }, &cli.StringFlag{ Name: "base", Aliases: []string{"b"}, - Usage: "pull-request base", + Usage: "Set base branch (default is default branch)", }, &cli.StringFlag{ Name: "title", Aliases: []string{"t"}, - Usage: "pull-request title", + Usage: "Set title of pull (default is head branch name)", }, &cli.StringFlag{ Name: "description", Aliases: []string{"d"}, - Usage: "pull-request description", + Usage: "Set body of new pull", }, }, AllDefaultFlags...), } func runPullsCreate(ctx *cli.Context) error { - login, owner, repo := initCommand() + login, ownerArg, repoArg := initCommand() + client := login.Client() - /* - Head string `json:"head" binding:"Required"` - Base string `json:"base" binding:"Required"` - Title string `json:"title" binding:"Required"` - Body string `json:"body"` - Assignee string `json:"assignee"` - Assignees []string `json:"assignees"` - Milestone int64 `json:"milestone"` - Labels []int64 `json:"labels"` - // swagger:strfmt date-time - Deadline *time.Time `json:"due_date"` - */ + repo, err := login.Client().GetRepo(ownerArg, repoArg) - pr, err := login.Client().CreatePullRequest(owner, repo, gitea.CreatePullRequestOption{ - Head: ctx.String("head"), - Base: ctx.String("base"), - Title: ctx.String("title"), + // open local git repo + localRepo, err := local_git.RepoForWorkdir() + if err != nil { + return nil + } + + base := ctx.String("base") + // default is default branch + if len(base) == 0 { + base = repo.DefaultBranch + } + + head := ctx.String("head") + // default is current one + if len(head) == 0 { + head = localRepo.TeaGetCurrentBranchName() + } + + title := ctx.String("title") + // default is head branch name + if len(title) == 0 { + title = strings.Replace(head, "-", " ", -1) + title = strings.Replace(title, "_", " ", -1) + title = strings.Title(strings.ToLower(title)) + } + + pr, err := client.CreatePullRequest(ownerArg, repoArg, gitea.CreatePullRequestOption{ + Head: head, + Base: base, + Title: title, Body: ctx.String("body"), }) @@ -321,7 +337,7 @@ func runPullsCreate(ctx *cli.Context) error { log.Fatal(err) } - fmt.Printf("#%d %s\n%s created %s\n\n%s", pr.Index, + fmt.Printf("#%d %s\n%s created %s\n\n%s\n", pr.Index, pr.Title, pr.Poster.UserName, pr.Created.Format("2006-01-02 15:04:05"), diff --git a/modules/git/branch.go b/modules/git/branch.go index 6196398..4c013ac 100644 --- a/modules/git/branch.go +++ b/modules/git/branch.go @@ -175,3 +175,16 @@ 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 { + localHead, err := r.Head() + if err != nil { + return "" + } + if localHead.Type() != git_plumbing.SymbolicReference || !localHead.Name().IsBranch() { + return "" + } + + return localHead.Name().String() +} -- 2.40.1 From b48de9049354aaa8d2bb4873bc7c0cf836ee97fc Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 14 Jul 2020 04:18:03 +0200 Subject: [PATCH 05/11] finish --- cmd/pulls.go | 19 +++++++++++++++++-- modules/git/branch.go | 13 +++++++------ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/cmd/pulls.go b/cmd/pulls.go index b1c8c71..e256fde 100644 --- a/cmd/pulls.go +++ b/cmd/pulls.go @@ -299,11 +299,14 @@ func runPullsCreate(ctx *cli.Context) error { 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 { - return nil + log.Fatal(err) } base := ctx.String("base") @@ -315,9 +318,15 @@ func runPullsCreate(ctx *cli.Context) error { head := ctx.String("head") // default is current one if len(head) == 0 { - head = localRepo.TeaGetCurrentBranchName() + head, err = localRepo.TeaGetCurrentBranchName() + if err != nil { + log.Fatal(err) + } } + // push if possible + _ = localRepo.Push(&git.PushOptions{}) + title := ctx.String("title") // default is head branch name if len(title) == 0 { @@ -325,7 +334,13 @@ func runPullsCreate(ctx *cli.Context) error { title = strings.Replace(title, "_", " ", -1) title = strings.Title(strings.ToLower(title)) } + // title is required + if len(title) == 0 { + fmt.Printf("Can't create a title has to be set") + return nil + } + fmt.Printf("Head: %s, Base: %s, Title: %s", head, base, title) pr, err := client.CreatePullRequest(ownerArg, repoArg, gitea.CreatePullRequestOption{ Head: head, Base: base, diff --git a/modules/git/branch.go b/modules/git/branch.go index 4c013ac..8732cd3 100644 --- a/modules/git/branch.go +++ b/modules/git/branch.go @@ -177,14 +177,15 @@ func (r TeaRepo) TeaFindBranchByName(branchName, repoURL string) (b *git_config. } // TeaGetCurrentBranchName return the name of the branch witch is currently active -func (r TeaRepo) TeaGetCurrentBranchName() string { +func (r TeaRepo) TeaGetCurrentBranchName() (string, error) { localHead, err := r.Head() if err != nil { - return "" - } - if localHead.Type() != git_plumbing.SymbolicReference || !localHead.Name().IsBranch() { - return "" + return "", err } - return localHead.Name().String() + if !localHead.Name().IsBranch() { + return "", fmt.Errorf("active ref is no branch") + } + + return strings.TrimLeft(localHead.Name().String(), "refs/heads/"), nil } -- 2.40.1 From ed172076e686f0e933e23f53108d1b52a2cced5d Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 14 Jul 2020 04:29:17 +0200 Subject: [PATCH 06/11] :rocket: --- cmd/pulls.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/cmd/pulls.go b/cmd/pulls.go index e256fde..a67ee30 100644 --- a/cmd/pulls.go +++ b/cmd/pulls.go @@ -330,7 +330,11 @@ func runPullsCreate(ctx *cli.Context) error { title := ctx.String("title") // default is head branch name if len(title) == 0 { - title = strings.Replace(head, "-", " ", -1) + 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)) } @@ -340,7 +344,6 @@ func runPullsCreate(ctx *cli.Context) error { return nil } - fmt.Printf("Head: %s, Base: %s, Title: %s", head, base, title) pr, err := client.CreatePullRequest(ownerArg, repoArg, gitea.CreatePullRequestOption{ Head: head, Base: base, @@ -352,12 +355,14 @@ func runPullsCreate(ctx *cli.Context) error { log.Fatal(err) } - fmt.Printf("#%d %s\n%s created %s\n\n%s\n", pr.Index, + 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"), - pr.Body, ) + if len(pr.Body) != 0 { + fmt.Printf("\n%s\n", pr.Body) + } return nil } -- 2.40.1 From 82814efb4df3971a5d7e323ee925493d73464293 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 14 Jul 2020 10:59:31 +0200 Subject: [PATCH 07/11] fix body --- cmd/pulls.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/pulls.go b/cmd/pulls.go index a67ee30..8649e6e 100644 --- a/cmd/pulls.go +++ b/cmd/pulls.go @@ -348,7 +348,7 @@ func runPullsCreate(ctx *cli.Context) error { Head: head, Base: base, Title: title, - Body: ctx.String("body"), + Body: ctx.String("description"), }) if err != nil { -- 2.40.1 From d660b4abcacdb2de8d4021b3a6e761a027864a1c Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 15 Jul 2020 17:14:03 +0200 Subject: [PATCH 08/11] inform User on push error --- cmd/pulls.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cmd/pulls.go b/cmd/pulls.go index 8649e6e..c52a355 100644 --- a/cmd/pulls.go +++ b/cmd/pulls.go @@ -324,9 +324,6 @@ func runPullsCreate(ctx *cli.Context) error { } } - // push if possible - _ = localRepo.Push(&git.PushOptions{}) - title := ctx.String("title") // default is head branch name if len(title) == 0 { @@ -344,6 +341,12 @@ func runPullsCreate(ctx *cli.Context) error { return nil } + // push if possible + err = localRepo.Push(&git.PushOptions{}) + if err != nil { + fmt.Printf("Error ocure on 'git push':\n%s\n", err.Error()) + } + pr, err := client.CreatePullRequest(ownerArg, repoArg, gitea.CreatePullRequestOption{ Head: head, Base: base, -- 2.40.1 From 38aaf74ec7d5d9183da2345dd76c17e2d35d5777 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 15 Jul 2020 17:15:19 +0200 Subject: [PATCH 09/11] change title required mesage --- cmd/pulls.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/pulls.go b/cmd/pulls.go index c52a355..c4ca5d1 100644 --- a/cmd/pulls.go +++ b/cmd/pulls.go @@ -337,7 +337,7 @@ func runPullsCreate(ctx *cli.Context) error { } // title is required if len(title) == 0 { - fmt.Printf("Can't create a title has to be set") + fmt.Printf("Title is required") return nil } -- 2.40.1 From 360a8820aacfd266766bddaf73a5849a1e478f13 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 15 Jul 2020 17:16:49 +0200 Subject: [PATCH 10/11] print pull URL --- cmd/pulls.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/pulls.go b/cmd/pulls.go index c4ca5d1..31a6170 100644 --- a/cmd/pulls.go +++ b/cmd/pulls.go @@ -366,6 +366,7 @@ func runPullsCreate(ctx *cli.Context) error { if len(pr.Body) != 0 { fmt.Printf("\n%s\n", pr.Body) } + fmt.Println(pr.HTMLURL) return nil } -- 2.40.1 From 192f7b8eac0ff5c0543a8329003ed18006138b7e Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 16 Jul 2020 09:06:40 +0200 Subject: [PATCH 11/11] wordings --- cmd/pulls.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/pulls.go b/cmd/pulls.go index 31a6170..15c3d87 100644 --- a/cmd/pulls.go +++ b/cmd/pulls.go @@ -344,7 +344,7 @@ func runPullsCreate(ctx *cli.Context) error { // push if possible err = localRepo.Push(&git.PushOptions{}) if err != nil { - fmt.Printf("Error ocure on '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{ -- 2.40.1