feat: edit commit message after merging through PR (#43)

Closes #5
This commit is contained in:
Julian Tölle 2024-09-06 23:17:06 +02:00 committed by GitHub
parent 36a0b90bcd
commit 2effe5e72d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 420 additions and 40 deletions

View file

@ -526,9 +526,7 @@ func gitHubPRToReleasePullRequest(pr *github.PullRequest) *releasepr.ReleasePull
}
return &releasepr.ReleasePullRequest{
ID: pr.GetNumber(),
Title: pr.GetTitle(),
Description: pr.GetBody(),
PullRequest: *gitHubPRToPullRequest(pr),
Labels: labels,
Head: pr.GetHead().GetRef(),

View file

@ -39,7 +39,7 @@ func Format(input string) (string, error) {
return buf.String(), nil
}
func GetCodeBlockText(source []byte, language string, output *string) gast.Walker {
func GetCodeBlockText(source []byte, language string, output *string, found *bool) gast.Walker {
return func(n gast.Node, entering bool) (gast.WalkStatus, error) {
if !entering {
return gast.WalkContinue, nil
@ -56,6 +56,9 @@ func GetCodeBlockText(source []byte, language string, output *string) gast.Walke
}
*output = textFromLines(source, codeBlock)
if found != nil {
*found = true
}
// Stop looking after we find the first result
return gast.WalkStop, nil
}

View file

@ -51,10 +51,11 @@ func TestGetCodeBlockText(t *testing.T) {
language string
}
tests := []struct {
name string
args args
want string
wantErr assert.ErrorAssertionFunc
name string
args args
wantText string
wantFound bool
wantErr assert.ErrorAssertionFunc
}{
{
name: "no code block",
@ -62,8 +63,9 @@ func TestGetCodeBlockText(t *testing.T) {
source: []byte("# Foo"),
language: "missing",
},
want: "",
wantErr: assert.NoError,
wantText: "",
wantFound: false,
wantErr: assert.NoError,
},
{
name: "code block",
@ -71,8 +73,9 @@ func TestGetCodeBlockText(t *testing.T) {
source: []byte("```test\nContent\n```"),
language: "test",
},
want: "Content",
wantErr: assert.NoError,
wantText: "Content",
wantFound: true,
wantErr: assert.NoError,
},
{
name: "code block with other language",
@ -80,8 +83,9 @@ func TestGetCodeBlockText(t *testing.T) {
source: []byte("```unknown\nContent\n```"),
language: "test",
},
want: "",
wantErr: assert.NoError,
wantText: "",
wantFound: false,
wantErr: assert.NoError,
},
{
name: "multiple code blocks with different languages",
@ -89,8 +93,9 @@ func TestGetCodeBlockText(t *testing.T) {
source: []byte("```unknown\nContent\n```\n\n```test\n1337\n```"),
language: "test",
},
want: "1337",
wantErr: assert.NoError,
wantText: "1337",
wantFound: true,
wantErr: assert.NoError,
},
{
name: "multiple code blocks with same language returns first one",
@ -98,22 +103,25 @@ func TestGetCodeBlockText(t *testing.T) {
source: []byte("```test\nContent\n```\n\n```test\n1337\n```"),
language: "test",
},
want: "Content",
wantErr: assert.NoError,
wantText: "Content",
wantFound: true,
wantErr: assert.NoError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var got string
var gotText string
var gotFound bool
err := WalkAST(tt.args.source,
GetCodeBlockText(tt.args.source, tt.args.language, &got),
GetCodeBlockText(tt.args.source, tt.args.language, &gotText, &gotFound),
)
if !tt.wantErr(t, err) {
return
}
assert.Equal(t, tt.want, got)
assert.Equal(t, tt.wantText, gotText)
assert.Equal(t, tt.wantFound, gotFound)
})
}
}

View file

@ -28,14 +28,9 @@ func init() {
}
}
// ReleasePullRequest
//
// TODO: Reuse [git.PullRequest]
type ReleasePullRequest struct {
ID int
Title string
Description string
Labels []Label
git.PullRequest
Labels []Label
Head string
ReleaseCommit *git.Commit
@ -137,8 +132,8 @@ func (pr *ReleasePullRequest) parseDescription(overrides ReleaseOverrides) (Rele
source := []byte(pr.Description)
err := markdown.WalkAST(source,
markdown.GetCodeBlockText(source, DescriptionLanguagePrefix, &overrides.Prefix),
markdown.GetCodeBlockText(source, DescriptionLanguageSuffix, &overrides.Suffix),
markdown.GetCodeBlockText(source, DescriptionLanguagePrefix, &overrides.Prefix, nil),
markdown.GetCodeBlockText(source, DescriptionLanguageSuffix, &overrides.Suffix, nil),
)
if err != nil {
return ReleaseOverrides{}, err

View file

@ -6,6 +6,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/apricote/releaser-pleaser/internal/git"
"github.com/apricote/releaser-pleaser/internal/versioning"
)
@ -36,7 +37,9 @@ func TestReleasePullRequest_GetOverrides(t *testing.T) {
{
name: "prefix in description",
pr: ReleasePullRequest{
Description: "```rp-prefix\n## Foo\n\n- Cool thing\n```",
PullRequest: git.PullRequest{
Description: "```rp-prefix\n## Foo\n\n- Cool thing\n```",
},
},
want: ReleaseOverrides{Prefix: "## Foo\n\n- Cool thing"},
wantErr: assert.NoError,
@ -44,7 +47,9 @@ func TestReleasePullRequest_GetOverrides(t *testing.T) {
{
name: "suffix in description",
pr: ReleasePullRequest{
Description: "```rp-suffix\n## Compatibility\n\nNo compatibility guarantees.\n```",
PullRequest: git.PullRequest{
Description: "```rp-suffix\n## Compatibility\n\nNo compatibility guarantees.\n```",
},
},
want: ReleaseOverrides{Suffix: "## Compatibility\n\nNo compatibility guarantees."},
wantErr: assert.NoError,
@ -105,7 +110,9 @@ Suffix Things
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pr := &ReleasePullRequest{
Description: tt.description,
PullRequest: git.PullRequest{
Description: tt.description,
},
}
got, err := pr.ChangelogText()
if !tt.wantErr(t, err, fmt.Sprintf("ChangelogText()")) {
@ -129,7 +136,11 @@ func TestReleasePullRequest_SetTitle(t *testing.T) {
}{
{
name: "simple update",
pr: &ReleasePullRequest{Title: "foo: bar"},
pr: &ReleasePullRequest{
PullRequest: git.PullRequest{
Title: "foo: bar",
},
},
args: args{
branch: "main",
version: "v1.0.0",