fix: native changeloger without previous tag (#3668)

closes #3662

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
This commit is contained in:
Carlos Alexandro Becker
2022-12-29 10:41:59 -03:00
committed by GitHub
parent e7a2361be1
commit da2335791a
7 changed files with 105 additions and 38 deletions
+17 -33
View File
@@ -220,7 +220,11 @@ func checkSortDirection(mode string) error {
}
func buildChangelog(ctx *context.Context) ([]string, error) {
log, err := getChangelog(ctx, ctx.Git.CurrentTag)
l, err := getChangeloger(ctx)
if err != nil {
return nil, err
}
log, err := l.Log(ctx)
if err != nil {
return nil, err
}
@@ -280,27 +284,6 @@ func extractCommitInfo(line string) string {
return strings.Join(strings.Split(line, " ")[1:], " ")
}
func getChangelog(ctx *context.Context, tag string) (string, error) {
prev := ctx.Git.PreviousTag
if prev == "" {
// get first commit
result, err := git.Clean(git.Run(ctx, "rev-list", "--max-parents=0", "HEAD"))
if err != nil {
return "", err
}
prev = result
}
return doGetChangelog(ctx, prev, tag)
}
func doGetChangelog(ctx *context.Context, prev, tag string) (string, error) {
l, err := getChangeloger(ctx)
if err != nil {
return "", err
}
return l.Log(ctx, prev, tag)
}
func getChangeloger(ctx *context.Context) (changeloger, error) {
switch ctx.Config.Changelog.Use {
case useGit:
@@ -387,19 +370,17 @@ func loadContent(ctx *context.Context, fileName, tmplName string) (string, error
}
type changeloger interface {
Log(ctx *context.Context, prev, current string) (string, error)
Log(ctx *context.Context) (string, error)
}
type gitChangeloger struct{}
var validSHA1 = regexp.MustCompile(`^[a-fA-F0-9]{40}$`)
func (g gitChangeloger) Log(ctx *context.Context, prev, current string) (string, error) {
func (g gitChangeloger) Log(ctx *context.Context) (string, error) {
args := []string{"log", "--pretty=oneline", "--abbrev-commit", "--no-decorate", "--no-color"}
if validSHA1.MatchString(prev) {
args = append(args, prev, current)
if ctx.Git.PreviousTag == "" {
args = append(args, ctx.Git.FirstCommit, ctx.Git.CurrentTag)
} else {
args = append(args, fmt.Sprintf("tags/%s..tags/%s", prev, current))
args = append(args, fmt.Sprintf("tags/%s..tags/%s", ctx.Git.PreviousTag, ctx.Git.CurrentTag))
}
return git.Run(ctx, args...)
}
@@ -409,8 +390,11 @@ type scmChangeloger struct {
repo client.Repo
}
func (c *scmChangeloger) Log(ctx *context.Context, prev, current string) (string, error) {
return c.client.Changelog(ctx, c.repo, prev, current)
func (c *scmChangeloger) Log(ctx *context.Context) (string, error) {
if ctx.Git.PreviousTag == "" {
return c.client.Changelog(ctx, c.repo, ctx.Git.FirstCommit, ctx.Git.PreviousTag)
}
return c.client.Changelog(ctx, c.repo, ctx.Git.PreviousTag, ctx.Git.PreviousTag)
}
type githubNativeChangeloger struct {
@@ -418,6 +402,6 @@ type githubNativeChangeloger struct {
repo client.Repo
}
func (c *githubNativeChangeloger) Log(ctx *context.Context, prev, current string) (string, error) {
return c.client.GenerateReleaseNotes(ctx, c.repo, prev, current)
func (c *githubNativeChangeloger) Log(ctx *context.Context) (string, error) {
return c.client.GenerateReleaseNotes(ctx, c.repo, ctx.Git.PreviousTag, ctx.Git.CurrentTag)
}
+68 -2
View File
@@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/goreleaser/goreleaser/internal/client"
"github.com/goreleaser/goreleaser/internal/git"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
@@ -51,6 +52,7 @@ func TestTemplatedChangelogProvidedViaFlag(t *testing.T) {
ctx.ReleaseNotesFile = "testdata/changes.md"
ctx.ReleaseNotesTmpl = "testdata/changes-templated.md"
ctx.Git.CurrentTag = "v0.0.1"
ctx.Git.FirstCommit = firstCommit(t)
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "c0ff33 coffeee v0.0.1\n", ctx.ReleaseNotes)
}
@@ -59,6 +61,7 @@ func TestTemplatedChangelogProvidedViaFlagResultIsEmpty(t *testing.T) {
ctx := context.New(config.Project{})
ctx.ReleaseNotesTmpl = "testdata/changes-templated-empty.md"
ctx.Git.CurrentTag = "v0.0.1"
ctx.Git.FirstCommit = firstCommit(t)
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "\n\n", ctx.ReleaseNotes)
}
@@ -260,6 +263,7 @@ func TestChangelogOfFirstRelease(t *testing.T) {
testlib.GitTag(t, "v0.0.1")
ctx := context.New(config.Project{})
ctx.Git.CurrentTag = "v0.0.1"
ctx.Git.FirstCommit = firstCommit(t)
require.NoError(t, Pipe{}.Run(ctx))
require.Contains(t, ctx.ReleaseNotes, "## Changelog")
for _, msg := range msgs {
@@ -313,6 +317,7 @@ func TestChangelogOnBranchWithSameNameAsTag(t *testing.T) {
testlib.GitCheckoutBranch(t, "v0.0.1")
ctx := context.New(config.Project{})
ctx.Git.CurrentTag = "v0.0.1"
ctx.Git.FirstCommit = firstCommit(t)
require.NoError(t, Pipe{}.Run(ctx))
require.Contains(t, ctx.ReleaseNotes, "## Changelog")
for _, msg := range msgs {
@@ -339,6 +344,7 @@ func TestChangeLogWithReleaseHeader(t *testing.T) {
testlib.GitCheckoutBranch(t, "v0.0.1")
ctx := context.New(config.Project{})
ctx.Git.CurrentTag = "v0.0.1"
ctx.Git.FirstCommit = firstCommit(t)
ctx.ReleaseHeaderFile = "testdata/release-header.md"
require.NoError(t, Pipe{}.Run(ctx))
require.Contains(t, ctx.ReleaseNotes, "## Changelog")
@@ -364,6 +370,7 @@ func TestChangeLogWithTemplatedReleaseHeader(t *testing.T) {
testlib.GitCheckoutBranch(t, "v0.0.1")
ctx := context.New(config.Project{})
ctx.Git.CurrentTag = "v0.0.1"
ctx.Git.FirstCommit = firstCommit(t)
ctx.ReleaseHeaderTmpl = "testdata/release-header-templated.md"
require.NoError(t, Pipe{}.Run(ctx))
require.Contains(t, ctx.ReleaseNotes, "## Changelog")
@@ -389,6 +396,7 @@ func TestChangeLogWithReleaseFooter(t *testing.T) {
testlib.GitCheckoutBranch(t, "v0.0.1")
ctx := context.New(config.Project{})
ctx.Git.CurrentTag = "v0.0.1"
ctx.Git.FirstCommit = firstCommit(t)
ctx.ReleaseFooterFile = "testdata/release-footer.md"
require.NoError(t, Pipe{}.Run(ctx))
require.Contains(t, ctx.ReleaseNotes, "## Changelog")
@@ -415,6 +423,7 @@ func TestChangeLogWithTemplatedReleaseFooter(t *testing.T) {
testlib.GitCheckoutBranch(t, "v0.0.1")
ctx := context.New(config.Project{})
ctx.Git.CurrentTag = "v0.0.1"
ctx.Git.FirstCommit = firstCommit(t)
ctx.ReleaseFooterTmpl = "testdata/release-footer-templated.md"
require.NoError(t, Pipe{}.Run(ctx))
require.Contains(t, ctx.ReleaseNotes, "## Changelog")
@@ -441,6 +450,7 @@ func TestChangeLogWithoutReleaseFooter(t *testing.T) {
testlib.GitCheckoutBranch(t, "v0.0.1")
ctx := context.New(config.Project{})
ctx.Git.CurrentTag = "v0.0.1"
ctx.Git.FirstCommit = firstCommit(t)
require.NoError(t, Pipe{}.Run(ctx))
require.Contains(t, ctx.ReleaseNotes, "## Changelog")
require.Equal(t, rune(ctx.ReleaseNotes[len(ctx.ReleaseNotes)-1]), '\n')
@@ -463,7 +473,12 @@ func TestGetChangelogGitHub(t *testing.T) {
Name: "goreleaser",
},
}
log, err := l.Log(ctx, "v0.180.1", "v0.180.2")
ctx.Git = context.GitInfo{
CurrentTag: "v0.180.2",
PreviousTag: "v0.180.1",
}
log, err := l.Log(ctx)
require.NoError(t, err)
require.Equal(t, expected, log)
}
@@ -490,9 +505,45 @@ func TestGetChangelogGitHubNative(t *testing.T) {
Name: "goreleaser",
},
}
log, err := l.Log(ctx, "v0.180.1", "v0.180.2")
ctx.Git = context.GitInfo{
CurrentTag: "v0.180.2",
PreviousTag: "v0.180.1",
}
log, err := l.Log(ctx)
require.NoError(t, err)
require.Equal(t, expected, log)
require.Equal(t, []string{"v0.180.1", "v0.180.2"}, mock.ReleaseNotesParams)
}
func TestGetChangelogGitHubNativeFirstRelease(t *testing.T) {
ctx := context.New(config.Project{
Changelog: config.Changelog{
Use: useGitHubNative,
},
})
expected := `## What's changed
* Foo bar test
**Full Changelog**: https://github.com/gorelease/goreleaser/commits/v0.1.0
`
mock := client.NewMock()
mock.ReleaseNotes = expected
l := githubNativeChangeloger{
client: mock,
repo: client.Repo{
Owner: "goreleaser",
Name: "goreleaser",
},
}
ctx.Git = context.GitInfo{
CurrentTag: "v0.1.0",
}
log, err := l.Log(ctx)
require.NoError(t, err)
require.Equal(t, expected, log)
require.Equal(t, []string{"", "v0.1.0"}, mock.ReleaseNotesParams)
}
func TestGetChangeloger(t *testing.T) {
@@ -659,6 +710,7 @@ func TestGroup(t *testing.T) {
},
})
ctx.Git.CurrentTag = "v0.0.2"
ctx.Git.FirstCommit = firstCommit(t)
require.NoError(t, Pipe{}.Run(ctx))
require.Contains(t, ctx.ReleaseNotes, "## Changelog")
require.Contains(t, ctx.ReleaseNotes, "### Bots")
@@ -686,6 +738,7 @@ func TestGroupBadRegex(t *testing.T) {
},
})
ctx.Git.CurrentTag = "v0.0.2"
ctx.Git.FirstCommit = firstCommit(t)
require.EqualError(t, Pipe{}.Run(ctx), "failed to group into \"Something\": error parsing regexp: missing closing ]: `[a-z`")
}
@@ -796,6 +849,8 @@ func TestAbbrev(t *testing.T) {
Changelog: config.Changelog{},
})
ctx.Git.CurrentTag = "v0.0.2"
ctx.Git.FirstCommit = firstCommit(t)
require.NoError(t, Pipe{}.Run(ctx))
ensureCommitHashLen(t, ctx.ReleaseNotes, 7)
})
@@ -808,6 +863,7 @@ func TestAbbrev(t *testing.T) {
},
})
ctx.Git.CurrentTag = "v0.0.2"
ctx.Git.FirstCommit = firstCommit(t)
require.NoError(t, Pipe{}.Run(ctx))
})
@@ -819,6 +875,7 @@ func TestAbbrev(t *testing.T) {
},
})
ctx.Git.CurrentTag = "v0.0.2"
ctx.Git.FirstCommit = firstCommit(t)
require.NoError(t, Pipe{}.Run(ctx))
ensureCommitHashLen(t, ctx.ReleaseNotes, 3)
})
@@ -831,6 +888,7 @@ func TestAbbrev(t *testing.T) {
},
})
ctx.Git.CurrentTag = "v0.0.2"
ctx.Git.FirstCommit = firstCommit(t)
require.NoError(t, Pipe{}.Run(ctx))
ensureCommitHashLen(t, ctx.ReleaseNotes, 7)
})
@@ -843,6 +901,7 @@ func TestAbbrev(t *testing.T) {
},
})
ctx.Git.CurrentTag = "v0.0.2"
ctx.Git.FirstCommit = firstCommit(t)
require.NoError(t, Pipe{}.Run(ctx))
ensureCommitHashLen(t, ctx.ReleaseNotes, 7)
})
@@ -859,3 +918,10 @@ func ensureCommitHashLen(tb testing.TB, log string, l int) {
require.Len(tb, commit, l)
}
}
func firstCommit(tb testing.TB) string {
tb.Helper()
s, err := git.Clean(git.Run(context.New(config.Project{}), "rev-list", "--max-parents=0", "HEAD"))
require.NoError(tb, err)
return s
}
+11 -1
View File
@@ -58,7 +58,7 @@ var fakeInfo = context.GitInfo{
func getInfo(ctx *context.Context) (context.GitInfo, error) {
if !git.IsRepo(ctx) && ctx.Snapshot {
log.Warn("accepting to run without a git repo because this is a snapshot")
log.Warn("accepting to run without a git repository because this is a snapshot")
return fakeInfo, nil
}
if !git.IsRepo(ctx) {
@@ -88,6 +88,10 @@ func getGitInfo(ctx *context.Context) (context.GitInfo, error) {
if err != nil {
return context.GitInfo{}, fmt.Errorf("couldn't get current commit: %w", err)
}
first, err := getFirstCommit(ctx)
if err != nil {
return context.GitInfo{}, fmt.Errorf("couldn't get first commit: %w", err)
}
date, err := getCommitDate(ctx)
if err != nil {
return context.GitInfo{}, fmt.Errorf("couldn't get commit date: %w", err)
@@ -117,6 +121,7 @@ func getGitInfo(ctx *context.Context) (context.GitInfo, error) {
Commit: full,
FullCommit: full,
ShortCommit: short,
FirstCommit: first,
CommitDate: date,
URL: gitURL,
CurrentTag: "v0.0.0",
@@ -152,6 +157,7 @@ func getGitInfo(ctx *context.Context) (context.GitInfo, error) {
Commit: full,
FullCommit: full,
ShortCommit: short,
FirstCommit: first,
CommitDate: date,
URL: gitURL,
Summary: summary,
@@ -221,6 +227,10 @@ func getFullCommit(ctx *context.Context) (string, error) {
return git.Clean(git.Run(ctx, "show", "--format=%H", "HEAD", "--quiet"))
}
func getFirstCommit(ctx *context.Context) (string, error) {
return git.Clean(git.Run(ctx, "rev-list", "--max-parents=0", "HEAD"))
}
func getSummary(ctx *context.Context) (string, error) {
return git.Clean(git.Run(ctx, "describe", "--always", "--dirty", "--tags"))
}
+4
View File
@@ -34,6 +34,7 @@ func TestSingleCommit(t *testing.T) {
require.Equal(t, "v0.0.1", ctx.Git.Summary)
require.Equal(t, "commit1", ctx.Git.TagSubject)
require.Equal(t, "commit1", ctx.Git.TagContents)
require.NotEmpty(t, ctx.Git.FirstCommit)
}
func TestAnnotatedTags(t *testing.T) {
@@ -218,6 +219,7 @@ func TestValidState(t *testing.T) {
require.Equal(t, "v0.0.2", ctx.Git.PreviousTag)
require.Equal(t, "v0.0.3", ctx.Git.CurrentTag)
require.Equal(t, "git@github.com:foo/bar.git", ctx.Git.URL)
require.NotEmpty(t, ctx.Git.FirstCommit)
}
func TestSnapshotNoTags(t *testing.T) {
@@ -231,6 +233,7 @@ func TestSnapshotNoTags(t *testing.T) {
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
require.Equal(t, fakeInfo.CurrentTag, ctx.Git.CurrentTag)
require.Empty(t, ctx.Git.PreviousTag)
require.NotEmpty(t, ctx.Git.FirstCommit)
}
func TestSnapshotNoCommits(t *testing.T) {
@@ -316,6 +319,7 @@ func TestNoPreviousTag(t *testing.T) {
require.NoError(t, Pipe{}.Run(ctx))
require.Equal(t, "v0.0.1", ctx.Git.CurrentTag)
require.Empty(t, ctx.Git.PreviousTag, "should be empty")
require.NotEmpty(t, ctx.Git.FirstCommit, "should not be empty")
}
func TestPreviousTagFromCI(t *testing.T) {