feat: introduced variable resolution for templates

closes #1
This commit is contained in:
Christian Schulze 2023-03-06 17:05:18 +01:00
parent 7c8014f8d8
commit 732094d79e
4 changed files with 89 additions and 8 deletions

View File

@ -78,8 +78,17 @@ func updateOrCreateDraftRelease(a *Action, cfg *config.RepoConfig) (*gitea.Relea
return nil, err
}
title := FillVariables(cfg.NameTemplate, TemplateVariables{
ReleaseVersion: nextVersion.String(),
})
// FIXME: require RESOLVED_VERSION to be set?
tag := FillVariables(cfg.TagTemplate, TemplateVariables{
ReleaseVersion: nextVersion.String(),
})
if draft != nil {
updatedDraft, err := UpdateExistingDraft(a.client, a.config.RepoOwner, a.config.RepoName, draft, nextVersion.String(), b.String())
updatedDraft, err := UpdateExistingDraft(a.client, a.config.RepoOwner, a.config.RepoName, draft, title, tag, b.String())
if err != nil {
return nil, err
}
@ -87,7 +96,7 @@ func updateOrCreateDraftRelease(a *Action, cfg *config.RepoConfig) (*gitea.Relea
return updatedDraft, nil
}
newDraft, err := CreateDraftRelease(a.client, a.config.RepoOwner, a.config.RepoName, cfg.DefaultBranch, fmt.Sprintf("v%s", nextVersion.String()), b.String())
newDraft, err := CreateDraftRelease(a.client, a.config.RepoOwner, a.config.RepoName, cfg.DefaultBranch, title, tag, b.String())
if err != nil {
return nil, err
}

View File

@ -39,11 +39,11 @@ func FindReleases(c *gitea.Client, owner string, repo string) (*gitea.Release, *
return mostRecentDraftRelease, mostRecentRelease, err
}
func CreateDraftRelease(c *gitea.Client, owner string, repo string, targetBranch string, version string, body string) (*gitea.Release, error) {
func CreateDraftRelease(c *gitea.Client, owner string, repo string, targetBranch string, title string, tag string, body string) (*gitea.Release, error) {
release, _, err := c.CreateRelease(owner, repo, gitea.CreateReleaseOption{
TagName: version,
TagName: tag,
Target: targetBranch,
Title: version,
Title: title,
Note: body,
IsDraft: true,
IsPrerelease: false,
@ -55,10 +55,10 @@ func CreateDraftRelease(c *gitea.Client, owner string, repo string, targetBranch
return release, err
}
func UpdateExistingDraft(c *gitea.Client, owner string, repo string, draft *gitea.Release, nextVersion string, body string) (*gitea.Release, error) {
func UpdateExistingDraft(c *gitea.Client, owner string, repo string, draft *gitea.Release, title string, tag string, body string) (*gitea.Release, error) {
rel, _, err := c.EditRelease(owner, repo, draft.ID, gitea.EditReleaseOption{
TagName: nextVersion,
Title: nextVersion,
TagName: tag,
Title: title,
Note: body,
})
if err != nil {

27
src/template.go Normal file
View File

@ -0,0 +1,27 @@
package src
import (
"reflect"
"strings"
)
const varTag = "var"
type TemplateVariables struct {
ReleaseVersion string `var:"$RESOLVED_VERSION"`
}
func FillVariables(str string, vars TemplateVariables) string {
t := reflect.TypeOf(vars)
v := reflect.ValueOf(vars)
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
variable := field.Tag.Get(varTag)
varVal := v.Field(i).String()
str = strings.ReplaceAll(str, variable, varVal)
}
return str
}

45
src/template_test.go Normal file
View File

@ -0,0 +1,45 @@
package src
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestReplaceVariables(t *testing.T) {
// Given
// valid variable values
v := TemplateVariables{
ReleaseVersion: "1.2.3",
}
// and a string containing all variables we have
str := "tag-v$RESOLVED_VERSION-foo"
// When
// filling the variables
res := FillVariables(str, v)
// Then
// the string should've been filled as expected
assert.Equal(t, "tag-v1.2.3-foo", res)
}
func TestReplaceVariablesShouldSkipIfVarsAreAbsent(t *testing.T) {
// Given
// valid variable values
v := TemplateVariables{
ReleaseVersion: "1.2.3",
}
// and a string containing no variables
str := "tag-v-foo"
// When
// filling the variables
res := FillVariables(str, v)
// Then
// the string should'nt have been filled
assert.Equal(t, "tag-v-foo", res)
}