diff --git a/README.md b/README.md index 33906a5..6c0ad3c 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ jobs: - `--from`: The base branch name. Required. - `--to`: The target branch name. Required. - `--labels`: Specify the labels to add to the pull request as a comma-separated list of strings. Optional. +- `--template`: Specify the Mustache template file. Optional. ## Environment Variables @@ -72,7 +73,6 @@ While inspired by git-pr-release, this tool pays homage to its predecessor yet i - Templates use Mustache files instead of ERB files. # TODO -- [ ] Support a custom template - [ ] Add more testing # Release flow diff --git a/main.go b/main.go index 28f85ff..64333e5 100644 --- a/main.go +++ b/main.go @@ -12,9 +12,10 @@ import ( type Options struct { // from flag - from string - to string - labels []string + from string + to string + labels []string + template *string // from env owner string @@ -27,6 +28,7 @@ func getOptions() (Options, error) { from := flag.String("from", "", "The base branch name.") to := flag.String("to", "", "The target branch name.") labelsFlag := flag.String("labels", "", "Specify the labels to add to the pull request as a comma-separated list of strings.") + template := flag.String("template", "", "The path to the template file.") flag.Parse() githubToken := os.Getenv("GITHUB_TOKEN") @@ -46,6 +48,7 @@ func getOptions() (Options, error) { from: *from, to: *to, labels: labels, + template: template, owner: owner, repo: repo, gitHubToken: githubToken, @@ -82,7 +85,7 @@ func run(options Options) error { currentTime := time.Now() date := currentTime.Format("2006-01-02") - data, err := RenderTemplate(nil, RenderTemplateData{pullRequests, date}) + data, err := RenderTemplate(options.template, RenderTemplateData{pullRequests, date}) parts := strings.SplitN(data, "\n", 2) title := parts[0] diff --git a/template_test.go b/template_test.go new file mode 100644 index 0000000..493eb0c --- /dev/null +++ b/template_test.go @@ -0,0 +1,74 @@ +package main + +import ( + "os" + "strings" + "testing" + + "github.com/google/go-github/v60/github" +) + +func TestRenderTemplate(t *testing.T) { + data := RenderTemplateData{ + PullRequests: []github.PullRequest{ + { + Number: github.Int(1), + }, + { + Number: github.Int(2), + }, + }, + Date: "2021-01-01", + } + + template, err := RenderTemplate(nil, data) + + if err != nil { + t.Errorf("RenderTemplate returned error: %v", err) + } + + if !strings.Contains(template, "Release 2021-01-01") { + t.Errorf("RenderTemplate returned %v, want %v", template, "2021-01-01") + } + + if !strings.Contains(template, "#1") { + t.Errorf("RenderTemplate returned %v, want %v", template, "#1") + } +} + +func TestRenderTemplateWithFilename(t *testing.T) { + data := RenderTemplateData{ + PullRequests: []github.PullRequest{ + { + Number: github.Int(1), + }, + { + Number: github.Int(2), + }, + }, + Date: "2021-01-01", + } + + tmpFile, err := os.CreateTemp("", "custom.mustache") + if err != nil { + panic(err) + } + defer os.Remove(tmpFile.Name()) + + filename := tmpFile.Name() + _, err = tmpFile.Write([]byte("This is custom template")) + if err != nil { + panic(err) + } + + template, err := RenderTemplate(&filename, data) + + if err != nil { + t.Errorf("RenderTemplate returned error: %v", err) + } + + want := "This is custom template" + if !strings.Contains(template, want) { + t.Errorf("RenderTemplate returned %v, want %v", template, want) + } +}