Skip to content

Commit

Permalink
Don't list the git root dir when listing template options (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmahsax authored Oct 28, 2023
1 parent ba03e47 commit 3de85a9
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 23 deletions.
2 changes: 2 additions & 0 deletions cmd/codeRequest/codeRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func (cr *CodeRequest) createGitHub() {
options["baseBranch"] = cr.baseBranch()
options["newPrTitle"] = cr.newPrTitle()
g := git.NewGit(cr.Debug)
options["gitRootDir"] = g.GetGitRootDir()
options["localBranch"] = g.CurrentBranch()
options["localRepo"] = g.RepoName()
githubPullRequest.NewGitHubPullRequest(options, cr.Debug).Create()
Expand All @@ -80,6 +81,7 @@ func (cr *CodeRequest) createGitLab() {
options["baseBranch"] = cr.baseBranch()
options["newMrTitle"] = cr.newMrTitle()
g := git.NewGit(cr.Debug)
options["gitRootDir"] = g.GetGitRootDir()
options["localBranch"] = g.CurrentBranch()
options["localProject"] = g.RepoName()
gitlabMergeRequest.NewGitLabMergeRequest(options, cr.Debug).Create()
Expand Down
23 changes: 14 additions & 9 deletions internal/githubPullRequest/githubPullRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import (
"os"
"path/filepath"
"runtime/debug"
"strings"

"github.com/emmahsax/go-git-helper/internal/commandline"
"github.com/emmahsax/go-git-helper/internal/git"
"github.com/emmahsax/go-git-helper/internal/github"
)

type GitHubPullRequest struct {
BaseBranch string
Debug bool
GitRootDir string
LocalBranch string
LocalRepo string
NewPrTitle string
Expand All @@ -24,6 +25,7 @@ func NewGitHubPullRequest(options map[string]string, debug bool) *GitHubPullRequ
return &GitHubPullRequest{
BaseBranch: options["baseBranch"],
Debug: debug,
GitRootDir: options["gitRootDir"],
LocalBranch: options["localBranch"],
LocalRepo: options["localRepo"],
NewPrTitle: options["newPrTitle"],
Expand Down Expand Up @@ -81,14 +83,20 @@ func (pr *GitHubPullRequest) templateNameToApply() string {
func (pr *GitHubPullRequest) determineTemplate() string {
if len(pr.prTemplateOptions()) == 1 {
applySingleTemplate := commandline.AskYesNoQuestion(
fmt.Sprintf("Apply the pull request template from %s?", pr.prTemplateOptions()[0]),
fmt.Sprintf("Apply the pull request template from %s?", strings.TrimPrefix(pr.prTemplateOptions()[0], pr.GitRootDir+"/")),
)
if applySingleTemplate {
return pr.prTemplateOptions()[0]
}
} else {
temp := []string{}
for _, str := range pr.prTemplateOptions() {
modifiedStr := strings.TrimPrefix(str, pr.GitRootDir+"/")
temp = append(temp, modifiedStr)
}

response := commandline.AskMultipleChoice(
"Which pull request template should be applied?", append(pr.prTemplateOptions(), "None"),
"Which pull request template should be applied?", append(temp, "None"),
)
if response != "None" {
return response
Expand All @@ -105,16 +113,13 @@ func (pr *GitHubPullRequest) prTemplateOptions() []string {
"nonNestedFileName": "pull_request_template",
}

g := git.NewGit(pr.Debug)
rootDir := g.GetGitRootDir()

nestedTemplates, _ := filepath.Glob(
filepath.Join(rootDir, identifiers["templateDir"], identifiers["nestedDirName"], "*.md"),
filepath.Join(pr.GitRootDir, identifiers["templateDir"], identifiers["nestedDirName"], "*.md"),
)
nonNestedTemplates, _ := filepath.Glob(
filepath.Join(rootDir, identifiers["templateDir"], identifiers["nonNestedFileName"]+".md"),
filepath.Join(pr.GitRootDir, identifiers["templateDir"], identifiers["nonNestedFileName"]+".md"),
)
rootTemplates, _ := filepath.Glob(filepath.Join(rootDir, identifiers["nonNestedFileName"]+".md"))
rootTemplates, _ := filepath.Glob(filepath.Join(pr.GitRootDir, identifiers["nonNestedFileName"]+".md"))

allTemplates := append(append(nestedTemplates, nonNestedTemplates...), rootTemplates...)
uniqueTemplates := make(map[string]bool)
Expand Down
17 changes: 13 additions & 4 deletions internal/githubPullRequest/githubPullRequest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ import (
)

func TestNewPrBody(t *testing.T) {
g := git.NewGit(true)
rootDir := g.GetGitRootDir()

options := make(map[string]string)
options["baseBranch"] = "main"
options["newPrTitle"] = "Example PR Title"
options["gitRootDir"] = rootDir
options["localBranch"] = "feature-branch"
options["localRepo"] = "test-repo"
pr := NewGitHubPullRequest(options, true)
body := pr.newPrBody()
g := git.NewGit(pr.Debug)
rootDir := g.GetGitRootDir()

realTemplate := rootDir + "/.github/pull_request_template.md"
content, _ := os.ReadFile(realTemplate)
realBody := string(content)
Expand All @@ -27,15 +30,17 @@ func TestNewPrBody(t *testing.T) {
}

func TestTemplateNameToApply(t *testing.T) {
g := git.NewGit(true)
rootDir := g.GetGitRootDir()

options := make(map[string]string)
options["baseBranch"] = "main"
options["newPrTitle"] = "Example PR Title"
options["gitRootDir"] = rootDir
options["localBranch"] = "feature-branch"
options["localRepo"] = "test-repo"
pr := NewGitHubPullRequest(options, true)
template := pr.templateNameToApply()
g := git.NewGit(pr.Debug)
rootDir := g.GetGitRootDir()
realTemplate := rootDir + "/.github/pull_request_template.md"

if template != realTemplate {
Expand All @@ -44,9 +49,13 @@ func TestTemplateNameToApply(t *testing.T) {
}

func TestPrTemplateOptions(t *testing.T) {
g := git.NewGit(true)
rootDir := g.GetGitRootDir()

options := make(map[string]string)
options["baseBranch"] = "main"
options["newPrTitle"] = "Example PR Title"
options["gitRootDir"] = rootDir
options["localBranch"] = "feature-branch"
options["localRepo"] = "test-repo"
pr := NewGitHubPullRequest(options, true)
Expand Down
23 changes: 14 additions & 9 deletions internal/gitlabMergeRequest/gitlabMergeRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import (
"os"
"path/filepath"
"runtime/debug"
"strings"

"github.com/emmahsax/go-git-helper/internal/commandline"
"github.com/emmahsax/go-git-helper/internal/git"
"github.com/emmahsax/go-git-helper/internal/gitlab"
)

type GitLabMergeRequest struct {
BaseBranch string
Debug bool
GitRootDir string
LocalBranch string
LocalProject string
NewMrTitle string
Expand All @@ -24,6 +25,7 @@ func NewGitLabMergeRequest(options map[string]string, debug bool) *GitLabMergeRe
return &GitLabMergeRequest{
BaseBranch: options["baseBranch"],
Debug: debug,
GitRootDir: options["gitRootDir"],
LocalBranch: options["localBranch"],
LocalProject: options["localProject"],
NewMrTitle: options["newMrTitle"],
Expand Down Expand Up @@ -83,14 +85,20 @@ func (mr *GitLabMergeRequest) templateNameToApply() string {
func (mr *GitLabMergeRequest) determineTemplate() string {
if len(mr.mrTemplateOptions()) == 1 {
applySingleTemplate := commandline.AskYesNoQuestion(
fmt.Sprintf("Apply the merge request template from %s?", mr.mrTemplateOptions()[0]),
fmt.Sprintf("Apply the merge request template from %s?", strings.TrimPrefix(mr.mrTemplateOptions()[0], mr.GitRootDir+"/")),
)
if applySingleTemplate {
return mr.mrTemplateOptions()[0]
}
} else {
temp := []string{}
for _, str := range mr.mrTemplateOptions() {
modifiedStr := strings.TrimPrefix(str, mr.GitRootDir+"/")
temp = append(temp, modifiedStr)
}

response := commandline.AskMultipleChoice(
"Which merge request template should be applied?", append(mr.mrTemplateOptions(), "None"),
"Which merge request template should be applied?", append(temp, "None"),
)
if response != "None" {
return response
Expand All @@ -107,16 +115,13 @@ func (mr *GitLabMergeRequest) mrTemplateOptions() []string {
"nonNestedFileName": "merge_request_template",
}

g := git.NewGit(mr.Debug)
rootDir := g.GetGitRootDir()

nestedTemplates, _ := filepath.Glob(
filepath.Join(rootDir, identifiers["templateDir"], identifiers["nestedDirName"], "*.md"),
filepath.Join(mr.GitRootDir, identifiers["templateDir"], identifiers["nestedDirName"], "*.md"),
)
nonNestedTemplates, _ := filepath.Glob(
filepath.Join(rootDir, identifiers["templateDir"], identifiers["nonNestedFileName"]+".md"),
filepath.Join(mr.GitRootDir, identifiers["templateDir"], identifiers["nonNestedFileName"]+".md"),
)
rootTemplates, _ := filepath.Glob(filepath.Join(rootDir, identifiers["nonNestedFileName"]+".md"))
rootTemplates, _ := filepath.Glob(filepath.Join(mr.GitRootDir, identifiers["nonNestedFileName"]+".md"))

allTemplates := append(append(nestedTemplates, nonNestedTemplates...), rootTemplates...)
uniqueTemplates := make(map[string]bool)
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 = "beta-1.0.3"
packageVersion = "beta-1.0.4"
)

func main() {
Expand Down

0 comments on commit 3de85a9

Please sign in to comment.