Skip to content

Commit

Permalink
fix: github changeloger should use short commits
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Aug 23, 2022
1 parent ff0eeac commit fe32842
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
15 changes: 12 additions & 3 deletions internal/client/github.go
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/caarlos0/log"
"github.com/google/go-github/v47/github"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/git"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
Expand Down Expand Up @@ -63,10 +64,19 @@ func (c *githubClient) GenerateReleaseNotes(ctx *context.Context, repo Repo, pre
return notes.Body, err
}

func commitAbbrevLen(ctx *context.Context) int {
hash, err := git.Clean(git.Run(ctx, "rev-parse", "--short", "HEAD", "--quiet"))
if err != nil || len(hash) > 40 {
return 40 // max sha1 len
}
return len(hash)
}

func (c *githubClient) Changelog(ctx *context.Context, repo Repo, prev, current string) (string, error) {
var log []string

commitlen := commitAbbrevLen(ctx)
opts := &github.ListOptions{PerPage: 100}

for {
result, resp, err := c.client.Repositories.CompareCommits(ctx, repo.Owner, repo.Name, prev, current, opts)
if err != nil {
Expand All @@ -75,15 +85,14 @@ func (c *githubClient) Changelog(ctx *context.Context, repo Repo, prev, current
for _, commit := range result.Commits {
log = append(log, fmt.Sprintf(
"%s: %s (@%s)",
commit.GetSHA(),
commit.GetSHA()[0:commitlen-1],
strings.Split(commit.Commit.GetMessage(), "\n")[0],
commit.GetAuthor().GetLogin(),
))
}
if resp.NextPage == 0 {
break
}

opts.Page = resp.NextPage
}

Expand Down
2 changes: 1 addition & 1 deletion internal/client/github_test.go
Expand Up @@ -289,7 +289,7 @@ func TestChangelog(t *testing.T) {

log, err := client.Changelog(ctx, repo, "v1.0.0", "v1.1.0")
require.NoError(t, err)
require.Equal(t, "6dcb09b5b57875f334f61aebed695e2e4193db5e: Fix all the bugs (@octocat)", log)
require.Equal(t, "6dcb09b: Fix all the bugs (@octocat)", log)
}

func TestReleaseNotes(t *testing.T) {
Expand Down

0 comments on commit fe32842

Please sign in to comment.