diff --git a/README.md b/README.md index 4ab8e98..80aa749 100644 --- a/README.md +++ b/README.md @@ -159,9 +159,11 @@ After setup is complete, you can call the command like this: git-helper code-request ``` -The command will provide an autogenerated code request title based on your branch name. It will separate the branch name by `'_'` if underscores are in the branch, or `'-'` if dashes are present. Then it will join the list of words together by spaces. If there's a pattern in the form of `jira-123` or `jira_123` in the first part of the branch name, then it'll add `JIRA-123` to the first part of the code request. You can respond `y` or `n`. If you respond `n`, you can provide your own code request title. +The command will provide an autogenerated code request title based on your branch name. It will separate the branch name by `'_'` if underscores are in the branch, or `'-'` if dashes are present. Then it will join the list of words together by spaces. If there's a pattern in the form of `jira-123` or `jira_123` in the first part of the branch name, then it'll add `JIRA-123` to the first part of the code request. You can choose whether to accept this title or not. If the title's declined, you can provide your own code request title. -The command will also ask you if the default branch of the repository is the proper base branch to use. You can say `y` or `n`. If you respond `n`, then you can give the command your chosen base base. +The command will also ask you if the default branch of the repository is the proper base branch to use. You can say whether that is correct or not, and if it's incorrect, you can give the command your chosen base branch. + +The command will also ask you whether you'd like to mark the new code request as a draft or not. If your project uses GitLab, the command will automatically set the merge request to delete the source branch upon merge. The value can later be changed for a specific MR either over the API or in the browser. The command also automatically sets the merge request to squash, and this will be the setting on the MR if the project allows, encourages, or requires squashing. If the project doesn't allow squashing at all, then that option will be voided, and the MR will not be squashed. Depending on the project's settings, the value can later be changed for a specific MR over the API or in the browser. diff --git a/cmd/codeRequest/codeRequest.go b/cmd/codeRequest/codeRequest.go index b552a47..7ce4ab9 100644 --- a/cmd/codeRequest/codeRequest.go +++ b/cmd/codeRequest/codeRequest.go @@ -68,6 +68,7 @@ func (cr *CodeRequest) askForClarification() { func (cr *CodeRequest) createGitHub() { options := make(map[string]string) options["baseBranch"] = cr.baseBranch() + options["draft"] = cr.draft() options["newPrTitle"] = cr.newPrTitle() g := git.NewGit(cr.Debug) options["gitRootDir"] = g.GetGitRootDir() @@ -79,6 +80,7 @@ func (cr *CodeRequest) createGitHub() { func (cr *CodeRequest) createGitLab() { options := make(map[string]string) options["baseBranch"] = cr.baseBranch() + options["draft"] = cr.draft() options["newMrTitle"] = cr.newMrTitle() g := git.NewGit(cr.Debug) options["gitRootDir"] = g.GetGitRootDir() @@ -98,6 +100,16 @@ func (cr *CodeRequest) baseBranch() string { } } +func (cr *CodeRequest) draft() string { + answer := commandline.AskYesNoQuestion("Create a draft code request?") + + if answer { + return "true" + } else { + return "false" + } +} + func (cr *CodeRequest) newMrTitle() string { return cr.newPrTitle() } diff --git a/go.mod b/go.mod index 8bf9f1d..509b6e7 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,12 @@ module github.com/emmahsax/go-git-helper go 1.21 require ( - github.com/google/go-github/v56 v56.0.0 + github.com/google/go-github/v58 v58.0.0 github.com/pterm/pterm v0.12.73 github.com/spf13/cobra v1.8.0 github.com/stretchr/testify v1.8.4 github.com/tidwall/gjson v1.17.0 - github.com/xanzy/go-gitlab v0.95.2 + github.com/xanzy/go-gitlab v0.97.0 golang.org/x/oauth2 v0.6.0 gopkg.in/yaml.v3 v3.0.1 ) diff --git a/go.sum b/go.sum index b946bd4..3c3c554 100644 --- a/go.sum +++ b/go.sum @@ -28,10 +28,10 @@ github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= -github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/google/go-github/v56 v56.0.0 h1:TysL7dMa/r7wsQi44BjqlwaHvwlFlqkK8CtBWCX3gb4= -github.com/google/go-github/v56 v56.0.0/go.mod h1:D8cdcX98YWJvi7TLo7zM4/h8ZTx6u6fwGEkCdisopo0= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-github/v58 v58.0.0 h1:Una7GGERlF/37XfkPwpzYJe0Vp4dt2k1kCjlxwjIvzw= +github.com/google/go-github/v58 v58.0.0/go.mod h1:k4hxDKEfoWpSqFlc8LTpGd9fu2KrV1YAa6Hi6FmDNY4= github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= github.com/gookit/color v1.4.2/go.mod h1:fqRyamkC1W8uxl+lxCQxOT09l/vYfZ+QeiX3rKQHCoQ= @@ -95,8 +95,8 @@ github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= -github.com/xanzy/go-gitlab v0.95.2 h1:4p0IirHqEp5f0baK/aQqr4TR57IsD+8e4fuyAA1yi88= -github.com/xanzy/go-gitlab v0.95.2/go.mod h1:ETg8tcj4OhrB84UEgeE8dSuV/0h4BBL1uOV/qK0vlyI= +github.com/xanzy/go-gitlab v0.97.0 h1:StMqJ1Kvt00X43pYIBBjj52dFlghwSeBhRDRfzaZ7xY= +github.com/xanzy/go-gitlab v0.97.0/go.mod h1:ETg8tcj4OhrB84UEgeE8dSuV/0h4BBL1uOV/qK0vlyI= github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs= github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= diff --git a/internal/github/github.go b/internal/github/github.go index 1a4cf2a..cc8573f 100644 --- a/internal/github/github.go +++ b/internal/github/github.go @@ -6,7 +6,7 @@ import ( "runtime/debug" "github.com/emmahsax/go-git-helper/internal/configfile" - "github.com/google/go-github/v56/github" + "github.com/google/go-github/v58/github" "golang.org/x/oauth2" ) @@ -25,16 +25,8 @@ func NewGitHub(debugB bool) *GitHub { } } -func (c *GitHub) CreatePullRequest(owner, repo string, options map[string]string) (*github.PullRequest, error) { - createOpts := &github.NewPullRequest{ - Base: github.String(options["base"]), - Body: github.String(options["body"]), - Head: github.String(options["head"]), - MaintainerCanModify: github.Bool(true), - Title: github.String(options["title"]), - } - - pr, _, err := c.Client.PullRequests.Create(context.Background(), owner, repo, createOpts) +func (c *GitHub) CreatePullRequest(owner, repo string, options *github.NewPullRequest) (*github.PullRequest, error) { + pr, _, err := c.Client.PullRequests.Create(context.Background(), owner, repo, options) if err != nil { if c.Debug { debug.PrintStack() diff --git a/internal/githubPullRequest/githubPullRequest.go b/internal/githubPullRequest/githubPullRequest.go index 95455ee..afeb4c9 100644 --- a/internal/githubPullRequest/githubPullRequest.go +++ b/internal/githubPullRequest/githubPullRequest.go @@ -6,15 +6,18 @@ import ( "os" "path/filepath" "runtime/debug" + "strconv" "strings" "github.com/emmahsax/go-git-helper/internal/commandline" "github.com/emmahsax/go-git-helper/internal/github" + go_github "github.com/google/go-github/v58/github" ) type GitHubPullRequest struct { BaseBranch string Debug bool + Draft string GitRootDir string LocalBranch string LocalRepo string @@ -25,6 +28,7 @@ func NewGitHubPullRequest(options map[string]string, debug bool) *GitHubPullRequ return &GitHubPullRequest{ BaseBranch: options["baseBranch"], Debug: debug, + Draft: options["draft"], GitRootDir: options["gitRootDir"], LocalBranch: options["localBranch"], LocalRepo: options["localRepo"], @@ -33,16 +37,20 @@ func NewGitHubPullRequest(options map[string]string, debug bool) *GitHubPullRequ } func (pr *GitHubPullRequest) Create() { - optionsMap := map[string]string{ - "base": pr.BaseBranch, - "body": pr.newPrBody(), - "head": pr.LocalBranch, - "title": pr.NewPrTitle, + d, _ := strconv.ParseBool(pr.Draft) + options := go_github.NewPullRequest{ + Base: go_github.String(pr.BaseBranch), + Body: go_github.String(pr.newPrBody()), + Draft: go_github.Bool(d), + Head: go_github.String(pr.LocalBranch), + MaintainerCanModify: go_github.Bool(true), + Title: go_github.String(pr.NewPrTitle), } + repo := strings.Split(pr.LocalRepo, "/") fmt.Println("Creating pull request:", pr.NewPrTitle) - resp, err := pr.github().CreatePullRequest(repo[0], repo[1], optionsMap) + resp, err := pr.github().CreatePullRequest(repo[0], repo[1], &options) if err != nil { if pr.Debug { debug.PrintStack() diff --git a/internal/gitlab/gitlab.go b/internal/gitlab/gitlab.go index cc8b019..a51c5ec 100644 --- a/internal/gitlab/gitlab.go +++ b/internal/gitlab/gitlab.go @@ -30,17 +30,8 @@ func NewGitLab(debugB bool) *GitLab { } } -func (c *GitLab) CreateMergeRequest(projectName string, options map[string]string) (*gitlab.MergeRequest, error) { - createOpts := &gitlab.CreateMergeRequestOptions{ - Description: gitlab.Ptr(options["description"]), - RemoveSourceBranch: gitlab.Ptr(true), - SourceBranch: gitlab.Ptr(options["source_branch"]), - Squash: gitlab.Ptr(true), - TargetBranch: gitlab.Ptr(options["target_branch"]), - Title: gitlab.Ptr(options["title"]), - } - - mr, _, err := c.Client.MergeRequests.CreateMergeRequest(projectName, createOpts) +func (c *GitLab) CreateMergeRequest(projectName string, options *gitlab.CreateMergeRequestOptions) (*gitlab.MergeRequest, error) { + mr, _, err := c.Client.MergeRequests.CreateMergeRequest(projectName, options) if err != nil { if c.Debug { debug.PrintStack() diff --git a/internal/gitlabMergeRequest/gitlabMergeRequest.go b/internal/gitlabMergeRequest/gitlabMergeRequest.go index 17b0b83..0bc3276 100644 --- a/internal/gitlabMergeRequest/gitlabMergeRequest.go +++ b/internal/gitlabMergeRequest/gitlabMergeRequest.go @@ -6,10 +6,12 @@ import ( "os" "path/filepath" "runtime/debug" + "strconv" "strings" "github.com/emmahsax/go-git-helper/internal/commandline" "github.com/emmahsax/go-git-helper/internal/gitlab" + go_github "github.com/xanzy/go-gitlab" ) type GitLabMergeRequest struct { @@ -17,6 +19,7 @@ type GitLabMergeRequest struct { Debug bool GitRootDir string LocalBranch string + Draft string LocalProject string NewMrTitle string } @@ -25,6 +28,7 @@ func NewGitLabMergeRequest(options map[string]string, debug bool) *GitLabMergeRe return &GitLabMergeRequest{ BaseBranch: options["baseBranch"], Debug: debug, + Draft: options["draft"], GitRootDir: options["gitRootDir"], LocalBranch: options["localBranch"], LocalProject: options["localProject"], @@ -33,17 +37,24 @@ func NewGitLabMergeRequest(options map[string]string, debug bool) *GitLabMergeRe } func (mr *GitLabMergeRequest) Create() { - optionsMap := map[string]string{ - "description": mr.newMrBody(), - "remove_source_branch": "true", - "squash": "true", - "source_branch": mr.LocalBranch, - "target_branch": mr.BaseBranch, - "title": mr.NewMrTitle, + var t string + if d, _ := strconv.ParseBool(mr.Draft); d { + t = "Draft: " + mr.NewMrTitle + } else { + t = mr.NewMrTitle + } + + options := go_github.CreateMergeRequestOptions{ + Description: go_github.Ptr(mr.newMrBody()), + RemoveSourceBranch: go_github.Ptr(true), + SourceBranch: go_github.Ptr(mr.LocalBranch), + Squash: go_github.Ptr(true), + TargetBranch: go_github.Ptr(mr.BaseBranch), + Title: go_github.Ptr(t), } - fmt.Println("Creating merge request:", mr.NewMrTitle) - resp, err := mr.gitlab().CreateMergeRequest(mr.LocalProject, optionsMap) + fmt.Println("Creating merge request:", t) + resp, err := mr.gitlab().CreateMergeRequest(mr.LocalProject, &options) if err != nil { if mr.Debug { debug.PrintStack() diff --git a/main.go b/main.go index 1eccd29..8ac61d5 100644 --- a/main.go +++ b/main.go @@ -20,7 +20,7 @@ import ( ) var ( - packageVersion = "0.0.2" + packageVersion = "0.0.3" ) func main() {