diff --git a/src/action.go b/src/action.go index 6ff8e99..cbc43de 100644 --- a/src/action.go +++ b/src/action.go @@ -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 } diff --git a/src/gitea.go b/src/gitea.go index 31e4384..d690888 100644 --- a/src/gitea.go +++ b/src/gitea.go @@ -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 { diff --git a/src/template.go b/src/template.go new file mode 100644 index 0000000..4bdf9ed --- /dev/null +++ b/src/template.go @@ -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 +} diff --git a/src/template_test.go b/src/template_test.go new file mode 100644 index 0000000..54a3480 --- /dev/null +++ b/src/template_test.go @@ -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) +}