Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify a template text directly in the config file #187

Merged
merged 2 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -59,6 +62,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 @@ -68,6 +72,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 @@ -78,6 +83,7 @@ type config struct {
versionFile *string
command *string
template *string
templateText *string
release *string
vPrefix *bool
changelog *bool
Expand Down Expand Up @@ -160,6 +166,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 @@ -274,6 +289,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
Loading