Skip to content

Commit

Permalink
Allow creation of draft code requests (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmahsax committed Feb 8, 2024
1 parent c09e0e4 commit 78b98db
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 48 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
12 changes: 12 additions & 0 deletions cmd/codeRequest/codeRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
14 changes: 3 additions & 11 deletions internal/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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()
Expand Down
20 changes: 14 additions & 6 deletions internal/githubPullRequest/githubPullRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"],
Expand All @@ -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()
Expand Down
13 changes: 2 additions & 11 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
29 changes: 20 additions & 9 deletions internal/gitlabMergeRequest/gitlabMergeRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ 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 {
BaseBranch string
Debug bool
GitRootDir string
LocalBranch string
Draft string
LocalProject string
NewMrTitle string
}
Expand All @@ -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"],
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
)

var (
packageVersion = "0.0.2"
packageVersion = "0.0.3"
)

func main() {
Expand Down

0 comments on commit 78b98db

Please sign in to comment.