1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2026-05-22 09:35:43 +02:00
Files
goreleaser/internal/pipe/changelog/changelog.go
T

211 lines
5.0 KiB
Go
Raw Normal View History

// Package changelog provides the release changelog to goreleaser.
package changelog
import (
"errors"
"fmt"
2018-05-01 20:54:16 -07:00
"io/ioutil"
"os"
2018-05-01 20:54:16 -07:00
"path/filepath"
2017-10-17 23:45:19 -02:00
"regexp"
"sort"
"strings"
2018-05-01 20:54:16 -07:00
"github.com/apex/log"
"github.com/goreleaser/goreleaser/internal/git"
2018-09-12 14:18:01 -03:00
"github.com/goreleaser/goreleaser/internal/pipe"
2018-08-14 23:50:20 -03:00
"github.com/goreleaser/goreleaser/pkg/context"
)
// ErrInvalidSortDirection happens when the sort order is invalid
var ErrInvalidSortDirection = errors.New("invalid sort direction")
// Pipe for checksums
type Pipe struct{}
2017-12-02 19:53:19 -02:00
func (Pipe) String() string {
return "generating changelog"
}
// Run the pipe
func (Pipe) Run(ctx *context.Context) error {
2019-01-22 01:56:16 -02:00
// TODO: should probably have a different field for the filename and its
// contents.
if ctx.ReleaseNotes != "" {
2019-01-22 01:56:16 -02:00
notes, err := loadFromFile(ctx.ReleaseNotes)
if err != nil {
return err
}
log.WithField("file", ctx.ReleaseNotes).Info("loaded custom release notes")
log.WithField("file", ctx.ReleaseNotes).Debugf("custom release notes: \n%s", notes)
2019-01-22 01:56:16 -02:00
ctx.ReleaseNotes = notes
}
if ctx.Config.Changelog.Skip {
return pipe.Skip("changelog should not be built")
}
if ctx.Snapshot {
2018-09-12 14:18:01 -03:00
return pipe.Skip("not available for snapshots")
}
if ctx.ReleaseNotes != "" {
return nil
}
if ctx.ReleaseHeader != "" {
header, err := loadFromFile(ctx.ReleaseHeader)
if err != nil {
return err
}
ctx.ReleaseHeader = header
}
if ctx.ReleaseFooter != "" {
footer, err := loadFromFile(ctx.ReleaseFooter)
if err != nil {
return err
}
ctx.ReleaseFooter = footer
}
if err := checkSortDirection(ctx.Config.Changelog.Sort); err != nil {
return err
}
entries, err := buildChangelog(ctx)
if err != nil {
return err
}
2019-06-29 16:02:40 +02:00
changelogStringJoiner := "\n"
2019-08-26 10:31:38 +03:00
if ctx.TokenType == context.TokenTypeGitLab || ctx.TokenType == context.TokenTypeGitea {
// We need two or more whitespace to let markdown interpret
2019-06-29 16:02:40 +02:00
// it as newline. See https://docs.gitlab.com/ee/user/markdown.html#newlines for details
2019-08-26 10:31:38 +03:00
log.Debug("is gitlab or gitea changelog")
2019-06-29 16:02:40 +02:00
changelogStringJoiner = " \n"
}
ctx.ReleaseNotes = strings.Join(
[]string{
ctx.ReleaseHeader,
"## Changelog",
strings.Join(entries, changelogStringJoiner),
ctx.ReleaseFooter,
},
"\n\n",
)
2018-05-01 20:54:16 -07:00
var path = filepath.Join(ctx.Config.Dist, "CHANGELOG.md")
log.WithField("changelog", path).Info("writing")
return ioutil.WriteFile(path, []byte(ctx.ReleaseNotes), 0644)
}
2019-01-22 01:56:16 -02:00
func loadFromFile(file string) (string, error) {
bts, err := ioutil.ReadFile(file)
if err != nil {
return "", err
}
return string(bts), nil
}
func checkSortDirection(mode string) error {
switch mode {
case "":
fallthrough
case "asc":
fallthrough
case "desc":
return nil
}
return ErrInvalidSortDirection
}
func buildChangelog(ctx *context.Context) ([]string, error) {
log, err := getChangelog(ctx.Git.CurrentTag)
if err != nil {
return nil, err
}
var entries = strings.Split(log, "\n")
entries = entries[0 : len(entries)-1]
entries, err = filterEntries(ctx, entries)
if err != nil {
return entries, err
}
return sortEntries(ctx, entries), nil
}
func filterEntries(ctx *context.Context, entries []string) ([]string, error) {
for _, filter := range ctx.Config.Changelog.Filters.Exclude {
2017-10-17 23:45:19 -02:00
r, err := regexp.Compile(filter)
if err != nil {
return entries, err
2017-10-17 23:45:19 -02:00
}
entries = remove(r, entries)
}
return entries, nil
}
func sortEntries(ctx *context.Context, entries []string) []string {
var direction = ctx.Config.Changelog.Sort
if direction == "" {
return entries
}
var result = make([]string, len(entries))
copy(result, entries)
sort.Slice(result, func(i, j int) bool {
2019-10-09 16:07:51 -03:00
var imsg = extractCommitInfo(result[i])
var jmsg = extractCommitInfo(result[j])
if direction == "asc" {
return strings.Compare(imsg, jmsg) < 0
}
return strings.Compare(imsg, jmsg) > 0
})
return result
}
2017-10-17 23:45:19 -02:00
func remove(filter *regexp.Regexp, entries []string) (result []string) {
for _, entry := range entries {
2019-10-09 16:07:51 -03:00
if !filter.MatchString(extractCommitInfo(entry)) {
result = append(result, entry)
}
}
return result
}
2019-10-09 16:07:51 -03:00
func extractCommitInfo(line string) string {
return strings.Join(strings.Split(line, " ")[1:], " ")
2017-10-17 23:45:19 -02:00
}
func getChangelog(tag string) (string, error) {
prev, err := previous(tag)
if err != nil {
return "", err
}
2018-10-05 14:18:39 -05:00
if isSHA1(prev) {
return gitLog(prev, tag)
}
2018-10-05 14:18:39 -05:00
return gitLog(fmt.Sprintf("tags/%s..tags/%s", prev, tag))
}
func gitLog(refs ...string) (string, error) {
2018-02-20 14:23:45 -03:00
var args = []string{"log", "--pretty=oneline", "--abbrev-commit", "--no-decorate", "--no-color"}
args = append(args, refs...)
return git.Run(args...)
}
2018-10-05 14:18:39 -05:00
func previous(tag string) (result string, err error) {
if tag := os.Getenv("GORELEASER_PREVIOUS_TAG"); tag != "" {
return tag, nil
}
2018-10-05 14:18:39 -05:00
result, err = git.Clean(git.Run("describe", "--tags", "--abbrev=0", fmt.Sprintf("tags/%s^", tag)))
if err != nil {
2018-10-05 14:18:39 -05:00
result, err = git.Clean(git.Run("rev-list", "--max-parents=0", "HEAD"))
}
return
}
2018-11-07 22:04:49 -02:00
// nolint: gochecknoglobals
2018-10-05 14:18:39 -05:00
var validSHA1 = regexp.MustCompile(`^[a-fA-F0-9]{40}$`)
// isSHA1 te lets us know if the ref is a SHA1 or not
func isSHA1(ref string) bool {
return validSHA1.MatchString(ref)
}