From 3dd9e61197fbedf7292202a602727572dab011e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20T=C3=B6lle?= Date: Thu, 8 Aug 2024 18:58:57 +0200 Subject: [PATCH] refactor(releasepr): rebuild pr description Build PR description from scratch and parsed values instead of copying some of the AST to next description. --- cmd/rp/cmd/run.go | 7 +++- releasepr.go | 62 +++------------------------- releasepr.md.tpl | 31 +++++++------- releasepr_test.go | 101 +++++++++++++++++----------------------------- 4 files changed, 67 insertions(+), 134 deletions(-) diff --git a/cmd/rp/cmd/run.go b/cmd/rp/cmd/run.go index 365c7e9..04bf933 100644 --- a/cmd/rp/cmd/run.go +++ b/cmd/rp/cmd/run.go @@ -352,7 +352,12 @@ func reconcileReleasePR(ctx context.Context, forge rp.Forge, changesets []rp.Cha logger.InfoContext(ctx, "opened pull request", "pr.title", pr.Title, "pr.id", pr.ID) } else { pr.SetTitle(flagBranch, nextVersion) - err = pr.SetDescription(changelogEntry) + + overrides, err := pr.GetOverrides() + if err != nil { + return err + } + err = pr.SetDescription(changelogEntry, overrides) if err != nil { return err } diff --git a/releasepr.go b/releasepr.go index eadb3b0..a0a6c0b 100644 --- a/releasepr.go +++ b/releasepr.go @@ -6,6 +6,7 @@ import ( "fmt" "log" "regexp" + "strings" "text/template" "github.com/yuin/goldmark/ast" @@ -47,7 +48,7 @@ func NewReleasePullRequest(head, branch, version, changelogEntry string) (*Relea } rp.SetTitle(branch, version) - if err := rp.SetDescription(changelogEntry); err != nil { + if err := rp.SetDescription(changelogEntry, ReleaseOverrides{}); err != nil { return nil, err } @@ -115,7 +116,6 @@ const ( ) const ( - MarkdownSectionOverrides = "overrides" MarkdownSectionChangelog = "changelog" ) @@ -190,51 +190,6 @@ func (pr *ReleasePullRequest) parseDescription(overrides ReleaseOverrides) (Rele return overrides, nil } -func (pr *ReleasePullRequest) overridesText() (string, error) { - source := []byte(pr.Description) - gm := markdown.New() - descriptionAST := gm.Parser().Parse(text.NewReader(source)) - - var section *east.Section - - err := ast.Walk(descriptionAST, func(n ast.Node, entering bool) (ast.WalkStatus, error) { - if !entering { - return ast.WalkContinue, nil - } - - if n.Type() != ast.TypeBlock || n.Kind() != east.KindSection { - return ast.WalkContinue, nil - } - - anySection, ok := n.(*east.Section) - if !ok { - return ast.WalkStop, fmt.Errorf("node has unexpected type: %T", n) - } - - if anySection.Name != MarkdownSectionOverrides { - return ast.WalkContinue, nil - } - - section = anySection - return ast.WalkStop, nil - }) - if err != nil { - return "", err - } - - if section == nil { - return "", nil - } - - outputBuffer := new(bytes.Buffer) - err = gm.Renderer().Render(outputBuffer, source, section) - if err != nil { - return "", err - } - - return outputBuffer.String(), nil -} - func (pr *ReleasePullRequest) ChangelogText() (string, error) { source := []byte(pr.Description) gm := markdown.New() @@ -289,11 +244,11 @@ func textFromLines(source []byte, n ast.Node) string { content = append(content, line.Value(source)...) } - return string(content) + return strings.TrimSpace(string(content)) } func (pr *ReleasePullRequest) SetTitle(branch, version string) { - pr.Title = fmt.Sprintf("chore(%s): release %s", branch, version) + pr.Title = fmt.Sprintf(TitleFormat, branch, version) } func (pr *ReleasePullRequest) Version() (string, error) { @@ -305,14 +260,9 @@ func (pr *ReleasePullRequest) Version() (string, error) { return matches[2], nil } -func (pr *ReleasePullRequest) SetDescription(changelogEntry string) error { - overrides, err := pr.overridesText() - if err != nil { - return err - } - +func (pr *ReleasePullRequest) SetDescription(changelogEntry string, overrides ReleaseOverrides) error { var description bytes.Buffer - err = releasePRTemplate.Execute(&description, map[string]any{ + err := releasePRTemplate.Execute(&description, map[string]any{ "Changelog": changelogEntry, "Overrides": overrides, }) diff --git a/releasepr.md.tpl b/releasepr.md.tpl index e48872b..6f74aa0 100644 --- a/releasepr.md.tpl +++ b/releasepr.md.tpl @@ -1,29 +1,32 @@ ---- - {{ .Changelog }} --- -## releaser-pleaser Instructions -{{ if .Overrides }} -{{- .Overrides -}} -{{- else }} - -> If you want to modify the proposed release, add you overrides here. You can learn more about the options in the docs. +
+

PR by releaser-pleaser 🤖

-### Prefix +If you want to modify the proposed release, add you overrides here. You can learn more about the options in the docs. + +## Release Notes + +### Prefix / Start + +This will be added to the start of the release notes. ```rp-prefix +{{- if .Overrides.Prefix }} +{{ .Overrides.Prefix }}{{ end }} ``` -### Suffix +### Suffix / End + +This will be added to the end of the release notes. ```rp-suffix +{{- if .Overrides.Suffix }} +{{ .Overrides.Suffix }}{{ end }} ``` - - -{{ end }} -#### PR by [releaser-pleaser](https://github.com/apricote/releaser-pleaser) +
diff --git a/releasepr_test.go b/releasepr_test.go index 124b063..b1ffbb2 100644 --- a/releasepr_test.go +++ b/releasepr_test.go @@ -49,121 +49,96 @@ func TestReleasePullRequest_SetDescription(t *testing.T) { tests := []struct { name string - pr *ReleasePullRequest changelogEntry string + overrides ReleaseOverrides want string wantErr assert.ErrorAssertionFunc }{ { - name: "empty description", - pr: &ReleasePullRequest{}, + name: "no overrides", changelogEntry: `## v1.0.0`, - want: `--- - - + overrides: ReleaseOverrides{}, + want: ` ## v1.0.0 --- -## releaser-pleaser Instructions +
+

PR by releaser-pleaser 🤖

- -> If you want to modify the proposed release, add you overrides here. You can learn more about the options in the docs. +If you want to modify the proposed release, add you overrides here. You can learn more about the options in the docs. -### Prefix +## Release Notes + +### Prefix / Start + +This will be added to the start of the release notes. ` + "```" + `rp-prefix ` + "```" + ` -### Suffix +### Suffix / End + +This will be added to the end of the release notes. ` + "```" + `rp-suffix ` + "```" + ` - - - -#### PR by [releaser-pleaser](https://github.com/apricote/releaser-pleaser) +
`, wantErr: assert.NoError, }, { - name: "existing overrides", - pr: &ReleasePullRequest{ - Description: `--- - - -## v0.1.0 - -### Features - -- bedazzle - - ---- - -## releaser-pleaser Instructions - - -> If you want to modify the proposed release, add you overrides here. You can learn more about the options in the docs. - -### Prefix - -` + "```" + `rp-prefix -This release is awesome! -` + "```" + ` - -### Suffix - -` + "```" + `rp-suffix -` + "```" + ` - - - -#### PR by [releaser-pleaser](https://github.com/apricote/releaser-pleaser) -`, - }, + name: "existing overrides", changelogEntry: `## v1.0.0`, - want: `--- - - + overrides: ReleaseOverrides{ + Prefix: "This release is awesome!", + Suffix: "Fooo", + }, + want: ` ## v1.0.0 --- -## releaser-pleaser Instructions +
+

PR by releaser-pleaser 🤖

- -> If you want to modify the proposed release, add you overrides here. You can learn more about the options in the docs. +If you want to modify the proposed release, add you overrides here. You can learn more about the options in the docs. -### Prefix +## Release Notes + +### Prefix / Start + +This will be added to the start of the release notes. ` + "```" + `rp-prefix This release is awesome! ` + "```" + ` -### Suffix +### Suffix / End + +This will be added to the end of the release notes. ` + "```" + `rp-suffix +Fooo ` + "```" + ` - - -#### PR by [releaser-pleaser](https://github.com/apricote/releaser-pleaser) +
`, wantErr: assert.NoError, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - err := tt.pr.SetDescription(tt.changelogEntry) + pr := &ReleasePullRequest{} + err := pr.SetDescription(tt.changelogEntry, tt.overrides) if !tt.wantErr(t, err) { return } - assert.Equal(t, tt.want, tt.pr.Description) + assert.Equal(t, tt.want, pr.Description) }) } }