From 3de85a996c96694a00808e4d951bb99abce653e6 Mon Sep 17 00:00:00 2001 From: Emma Sax Date: Sat, 28 Oct 2023 07:54:37 -0500 Subject: [PATCH] Don't list the git root dir when listing template options (#17) --- cmd/codeRequest/codeRequest.go | 2 ++ .../githubPullRequest/githubPullRequest.go | 23 +++++++++++-------- .../githubPullRequest_test.go | 17 ++++++++++---- .../gitlabMergeRequest/gitlabMergeRequest.go | 23 +++++++++++-------- main.go | 2 +- 5 files changed, 44 insertions(+), 23 deletions(-) diff --git a/cmd/codeRequest/codeRequest.go b/cmd/codeRequest/codeRequest.go index 6172049..7c90bd4 100644 --- a/cmd/codeRequest/codeRequest.go +++ b/cmd/codeRequest/codeRequest.go @@ -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() @@ -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() diff --git a/internal/githubPullRequest/githubPullRequest.go b/internal/githubPullRequest/githubPullRequest.go index 28f87c3..904aba2 100644 --- a/internal/githubPullRequest/githubPullRequest.go +++ b/internal/githubPullRequest/githubPullRequest.go @@ -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 @@ -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"], @@ -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 @@ -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) diff --git a/internal/githubPullRequest/githubPullRequest_test.go b/internal/githubPullRequest/githubPullRequest_test.go index 6072dce..80a2074 100644 --- a/internal/githubPullRequest/githubPullRequest_test.go +++ b/internal/githubPullRequest/githubPullRequest_test.go @@ -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) @@ -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 { @@ -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) diff --git a/internal/gitlabMergeRequest/gitlabMergeRequest.go b/internal/gitlabMergeRequest/gitlabMergeRequest.go index 3554314..7789c49 100644 --- a/internal/gitlabMergeRequest/gitlabMergeRequest.go +++ b/internal/gitlabMergeRequest/gitlabMergeRequest.go @@ -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 @@ -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"], @@ -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 @@ -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) diff --git a/main.go b/main.go index 3eb812d..99bbf57 100644 --- a/main.go +++ b/main.go @@ -20,7 +20,7 @@ import ( ) var ( - packageVersion = "beta-1.0.3" + packageVersion = "beta-1.0.4" ) func main() {