You've already forked goreleaser
mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-07-17 01:42:37 +02:00
refactor: turned changelog generation into a pipe
I turned myself into a pipe morty! PipeRick!!! refs #284
This commit is contained in:
committed by
Carlos Alexandro Becker
parent
b585ac8b0e
commit
87d269dc45
@ -1,12 +0,0 @@
|
||||
package git
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/goreleaser/goreleaser/internal/git"
|
||||
)
|
||||
|
||||
func cleanGit(args ...string) (output string, err error) {
|
||||
output, err = git.Run(args...)
|
||||
return strings.Replace(strings.Split(output, "\n")[0], "'", "", -1), err
|
||||
}
|
@ -4,7 +4,6 @@ package git
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"text/template"
|
||||
@ -39,9 +38,6 @@ func (Pipe) Run(ctx *context.Context) (err error) {
|
||||
Commit: commit,
|
||||
}
|
||||
log.Infof("releasing %s, commit %s", tag, commit)
|
||||
if err = setLog(ctx, tag, commit); err != nil {
|
||||
return
|
||||
}
|
||||
if err = setVersion(ctx, tag, commit); err != nil {
|
||||
return
|
||||
}
|
||||
@ -65,23 +61,6 @@ func setVersion(ctx *context.Context, tag, commit string) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func setLog(ctx *context.Context, tag, commit string) (err error) {
|
||||
if ctx.ReleaseNotes != "" {
|
||||
return
|
||||
}
|
||||
var log string
|
||||
if tag == "" {
|
||||
log, err = getChangelog(commit)
|
||||
} else {
|
||||
log, err = getChangelog(tag)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx.ReleaseNotes = fmt.Sprintf("## Changelog\n\n%v", log)
|
||||
return nil
|
||||
}
|
||||
|
||||
type snapshotNameData struct {
|
||||
Commit string
|
||||
Tag string
|
||||
@ -114,50 +93,18 @@ func validate(ctx *context.Context, commit, tag string) error {
|
||||
if !regexp.MustCompile("^[0-9.]+").MatchString(ctx.Version) {
|
||||
return ErrInvalidVersionFormat{ctx.Version}
|
||||
}
|
||||
_, err = cleanGit("describe", "--exact-match", "--tags", "--match", tag)
|
||||
_, err = git.Clean(git.Run("describe", "--exact-match", "--tags", "--match", tag))
|
||||
if err != nil {
|
||||
return ErrWrongRef{commit, tag}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getChangelog(tag string) (string, error) {
|
||||
prev, err := previous(tag)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !prev.Tag {
|
||||
return gitLog(prev.SHA, tag)
|
||||
}
|
||||
return gitLog(fmt.Sprintf("%v..%v", prev.SHA, tag))
|
||||
}
|
||||
|
||||
func gitLog(refs ...string) (string, error) {
|
||||
var args = []string{"log", "--pretty=oneline", "--abbrev-commit"}
|
||||
args = append(args, refs...)
|
||||
return git.Run(args...)
|
||||
}
|
||||
|
||||
func getInfo() (tag, commit string, err error) {
|
||||
tag, err = cleanGit("describe", "--tags", "--abbrev=0")
|
||||
tag, err = git.Clean(git.Run("describe", "--tags", "--abbrev=0"))
|
||||
if err != nil {
|
||||
log.WithError(err).Info("failed to retrieve current tag")
|
||||
}
|
||||
commit, err = cleanGit("show", "--format='%H'", "HEAD")
|
||||
commit, err = git.Clean(git.Run("show", "--format='%H'", "HEAD"))
|
||||
return
|
||||
}
|
||||
|
||||
func previous(tag string) (result ref, err error) {
|
||||
result.Tag = true
|
||||
result.SHA, err = cleanGit("describe", "--tags", "--abbrev=0", tag+"^")
|
||||
if err != nil {
|
||||
result.Tag = false
|
||||
result.SHA, err = cleanGit("rev-list", "--max-parents=0", "HEAD")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
type ref struct {
|
||||
Tag bool
|
||||
SHA string
|
||||
}
|
||||
|
@ -167,8 +167,6 @@ func TestValidState(t *testing.T) {
|
||||
}
|
||||
assert.NoError(t, Pipe{}.Run(ctx))
|
||||
assert.Equal(t, "v0.0.2", ctx.Git.CurrentTag)
|
||||
assert.NotContains(t, "commit4", ctx.ReleaseNotes)
|
||||
assert.NotContains(t, "commit3", ctx.ReleaseNotes)
|
||||
}
|
||||
|
||||
func TestNoValidate(t *testing.T) {
|
||||
@ -186,62 +184,16 @@ func TestNoValidate(t *testing.T) {
|
||||
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
|
||||
}
|
||||
|
||||
func TestChangelog(t *testing.T) {
|
||||
func TestSnapshot(t *testing.T) {
|
||||
_, back := testlib.Mktmp(t)
|
||||
defer back()
|
||||
testlib.GitInit(t)
|
||||
testlib.GitCommit(t, "first")
|
||||
testlib.GitTag(t, "v0.0.1")
|
||||
testlib.GitCommit(t, "added feature 1")
|
||||
testlib.GitCommit(t, "fixed bug 2")
|
||||
testlib.GitTag(t, "v0.0.2")
|
||||
testlib.GitAdd(t)
|
||||
testlib.GitCommit(t, "whatever")
|
||||
var ctx = &context.Context{
|
||||
Config: config.Project{},
|
||||
Config: config.Project{},
|
||||
Validate: true,
|
||||
Snapshot: true,
|
||||
}
|
||||
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
|
||||
assert.Equal(t, "v0.0.2", ctx.Git.CurrentTag)
|
||||
assert.Contains(t, ctx.ReleaseNotes, "## Changelog")
|
||||
assert.NotContains(t, ctx.ReleaseNotes, "first")
|
||||
assert.Contains(t, ctx.ReleaseNotes, "added feature 1")
|
||||
assert.Contains(t, ctx.ReleaseNotes, "fixed bug 2")
|
||||
}
|
||||
|
||||
func TestChangelogOfFirstRelease(t *testing.T) {
|
||||
_, back := testlib.Mktmp(t)
|
||||
defer back()
|
||||
testlib.GitInit(t)
|
||||
var msgs = []string{
|
||||
"initial commit",
|
||||
"another one",
|
||||
"one more",
|
||||
"and finally this one",
|
||||
}
|
||||
for _, msg := range msgs {
|
||||
testlib.GitCommit(t, msg)
|
||||
}
|
||||
testlib.GitTag(t, "v0.0.1")
|
||||
var ctx = &context.Context{
|
||||
Config: config.Project{},
|
||||
}
|
||||
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
|
||||
assert.Equal(t, "v0.0.1", ctx.Git.CurrentTag)
|
||||
assert.Contains(t, ctx.ReleaseNotes, "## Changelog")
|
||||
for _, msg := range msgs {
|
||||
assert.Contains(t, ctx.ReleaseNotes, msg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCustomReleaseNotes(t *testing.T) {
|
||||
_, back := testlib.Mktmp(t)
|
||||
defer back()
|
||||
testlib.GitInit(t)
|
||||
testlib.GitCommit(t, "first")
|
||||
testlib.GitTag(t, "v0.0.1")
|
||||
var ctx = &context.Context{
|
||||
Config: config.Project{},
|
||||
ReleaseNotes: "custom",
|
||||
}
|
||||
testlib.AssertSkipped(t, Pipe{}.Run(ctx))
|
||||
assert.Equal(t, "v0.0.1", ctx.Git.CurrentTag)
|
||||
assert.Equal(t, ctx.ReleaseNotes, "custom")
|
||||
assert.NoError(t, Pipe{}.Run(ctx))
|
||||
}
|
||||
|
Reference in New Issue
Block a user