1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-09-16 09:26:52 +02:00

feat: Adding gitlab api changelog (#2567)

Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
This commit is contained in:
Drew Stinnett
2021-10-10 09:52:46 -04:00
committed by GitHub
parent a393262eb4
commit 03b6805715
5 changed files with 92 additions and 3 deletions

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"os"
"strings"
"github.com/apex/log"
"github.com/goreleaser/goreleaser/internal/artifact"
@@ -50,7 +51,26 @@ func NewGitLab(ctx *context.Context, token string) (Client, error) {
}
func (c *gitlabClient) Changelog(ctx *context.Context, repo Repo, prev, current string) (string, error) {
return "", ErrNotImplemented
cmpOpts := &gitlab.CompareOptions{
From: &prev,
To: &current,
}
result, _, err := c.client.Repositories.Compare(repo.String(), cmpOpts)
var log []string
if err != nil {
return "", err
}
for _, commit := range result.Commits {
log = append(log, fmt.Sprintf(
"%s: %s (%s <%s>)",
commit.ShortID,
strings.Split(commit.Message, "\n")[0],
commit.AuthorName,
commit.AuthorEmail,
))
}
return strings.Join(log, "\n"), nil
}
// GetDefaultBranch get the default branch

View File

@@ -398,6 +398,13 @@ func TestGitlabGetDefaultBranchErr(t *testing.T) {
func TestGitlabChangelog(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "projects/someone/something/repository/compare") {
r, err := os.Open("testdata/gitlab/compare.json")
require.NoError(t, err)
_, err = io.Copy(w, r)
require.NoError(t, err)
return
}
defer r.Body.Close()
}))
defer srv.Close()
@@ -415,6 +422,7 @@ func TestGitlabChangelog(t *testing.T) {
Branch: "somebranch",
}
_, err = client.Changelog(ctx, repo, "v1.0.0", "v1.1.0")
require.EqualError(t, err, ErrNotImplemented.Error())
log, err := client.Changelog(ctx, repo, "v1.0.0", "v1.1.0")
require.NoError(t, err)
require.Equal(t, "6dcb09b5: Fix all the bugs (Joey User <joey@user.edu>)", log)
}

View File

@@ -0,0 +1,40 @@
{
"commit": {
"id": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
"short_id": "6dcb09b5",
"created_at": "2021-09-30T20:05:21.000-04:00",
"parent_ids": [
"133bd0aeb6202b229d3dbf316c82c2935e5e3e93"
],
"title": "ding",
"message": "Fix all the bugs\nlalalal",
"author_name": "Joey User",
"author_email": "joey@user.edu",
"authored_date": "2021-09-30T20:05:21.000-04:00",
"committer_name": "Joey User",
"committer_email": "joey@user.edu",
"committed_date": "2021-09-30T20:05:21.000-04:00",
"trailers": {},
"web_url": "https://gitlab.com/somenamespace/someproject/-/commit/09df0b820664240198f7b70887cac139db1af610"
},
"commits": [
{
"id": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
"short_id": "6dcb09b5",
"created_at": "2021-09-30T20:05:21.000-04:00",
"parent_ids": [
"133bd0aeb6202b229d3dbf316c82c2935e5e3e93"
],
"title": "ding",
"message": "Fix all the bugs\nlalalal",
"author_name": "Joey User",
"author_email": "joey@user.edu",
"authored_date": "2021-09-30T20:05:21.000-04:00",
"committer_name": "Joey User",
"committer_email": "joey@user.edu",
"committed_date": "2021-09-30T20:05:21.000-04:00",
"trailers": {},
"web_url": "https://gitlab.com/somenamespace/someproject/-/commit/09df0b820664240198f7b70887cac139db1af610"
}
]
}

View File

@@ -188,6 +188,8 @@ func getChangeloger(ctx *context.Context) (changeloger, error) {
return gitChangeloger{}, nil
case "github":
return newGitHubChangeloger(ctx)
case "gitlab":
return newGitLabChangeloger(ctx)
default:
return nil, fmt.Errorf("invalid changelog.use: %q", ctx.Config.Changelog.Use)
}
@@ -211,6 +213,24 @@ func newGitHubChangeloger(ctx *context.Context) (changeloger, error) {
}, nil
}
func newGitLabChangeloger(ctx *context.Context) (changeloger, error) {
cli, err := client.New(ctx)
if err != nil {
return nil, err
}
repo, err := git.ExtractRepoFromConfig()
if err != nil {
return nil, err
}
return &scmChangeloger{
client: cli,
repo: client.Repo{
Owner: repo.Owner,
Name: repo.Name,
},
}, nil
}
func previous(tag string) (result string, err error) {
if tag := os.Getenv("GORELEASER_PREVIOUS_TAG"); tag != "" {
return tag, nil

View File

@@ -14,6 +14,7 @@ changelog:
# Valid options are:
# - `git`: uses `git log`;
# - `github`: uses the compare GitHub API, appending the author login to the changelog.
# - `gitlab`: uses the compare GitLab API, appending the author name and email to the changelog.
#
# Defaults to `git`.
use: github