Skip to content

Commit

Permalink
Merge pull request #187 from 5ouma/feat-template-text
Browse files Browse the repository at this point in the history
Specify a template text directly in the config file
  • Loading branch information
Songmu authored Oct 27, 2024
2 parents 1877c63 + a422597 commit e7bca42
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ Flag whether or not changelog is added or changed during the release.
Command to change files just before release.

### tagpr.template (Optional)
Pull request template in go template format
Pull request template file in go template format

### tagpr.templateText (Optional)
Pull request template text in go template format

### tagpr.release (Optional)
GitHub Release creation behavior after tagging `[true, draft, false]`
Expand Down
21 changes: 20 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ const (
# Command to change files just before release.
#
# tagpr.template (Optional)
# Pull request template in go template format
# Pull request template file in go template format
#
# tagpr.templateText (Optional)
# Pull request template text in go template format
#
# tagpr.release (Optional)
# GitHub Release creation behavior after tagging [true, draft, false]
Expand All @@ -60,6 +63,7 @@ const (
envChangelog = "TAGPR_CHANGELOG"
envCommand = "TAGPR_COMMAND"
envTemplate = "TAGPR_TEMPLATE"
envTemplateText = "TAGPR_TEMPLATE_TEXT"
envRelease = "TAGPR_RELEASE"
envMajorLabels = "TAGPR_MAJOR_LABELS"
envMinorLabels = "TAGPR_MAINOR_LABELS"
Expand All @@ -69,6 +73,7 @@ const (
configChangelog = "tagpr.changelog"
configCommand = "tagpr.command"
configTemplate = "tagpr.template"
configTemplateText = "tagpr.templateText"
configRelease = "tagpr.release"
configMajorLabels = "tagpr.majorLabels"
configMinorLabels = "tagpr.minorLabels"
Expand All @@ -79,6 +84,7 @@ type config struct {
versionFile *string
command *string
template *string
templateText *string
release *string
vPrefix *bool
changelog *bool
Expand Down Expand Up @@ -165,6 +171,15 @@ func (cfg *config) Reload() error {
}
}

if tmplTxt := os.Getenv(envTemplateText); tmplTxt != "" {
cfg.templateText = github.String(tmplTxt)
} else {
tmplTxt, err := cfg.gitconfig.Get(configTemplateText)
if err == nil {
cfg.templateText = github.String(tmplTxt)
}
}

if rel := os.Getenv(envRelease); rel != "" {
cfg.release = github.String(rel)
} else {
Expand Down Expand Up @@ -279,6 +294,10 @@ func (cfg *config) Template() string {
return stringify(cfg.template)
}

func (cfg *config) TemplateText() string {
return stringify(cfg.templateText)
}

func (cfg *config) Release() bool {
rel := strings.ToLower(stringify(cfg.release))
if rel == "draft" || rel == "" {
Expand Down
7 changes: 7 additions & 0 deletions tagpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,13 @@ OUT:
} else {
log.Printf("parse configured template failed: %s\n", err)
}
} else if t := tp.cfg.TemplateText(); t != "" {
tmpTmplTxt, err := template.New("templateText").Parse(t)
if err == nil {
tmpl = tmpTmplTxt
} else {
log.Printf("parse configured template failed: %s\n", err)
}
}
pt := newPRTmpl(tmpl)
prText, err := pt.Render(&tmplArg{
Expand Down

0 comments on commit e7bca42

Please sign in to comment.