Skip to content

Commit

Permalink
support custom template
Browse files Browse the repository at this point in the history
  • Loading branch information
odanado committed Mar 24, 2024
1 parent c232a0b commit 5de1a71
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
11 changes: 7 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -46,6 +48,7 @@ func getOptions() (Options, error) {
from: *from,
to: *to,
labels: labels,
template: template,
owner: owner,
repo: repo,
gitHubToken: githubToken,
Expand Down Expand Up @@ -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]
Expand Down
74 changes: 74 additions & 0 deletions template_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 5de1a71

Please sign in to comment.