Repository: goreleaser/goreleaser
Pull request #2661 (2021-11-10 18:42:23)

fix: [Bug]: changelog is too long
Signed-off-by: Engin Diri <engin.diri@mail.schwarz>

fixes: #2647 

codecov[bot]: # [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/2661?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) Report
> Merging [#2661](https://codecov.io/gh/goreleaser/goreleaser/pull/2661?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) (b3bd6a9) into [master](https://codecov.io/gh/goreleaser/goreleaser/commit/9af17a49adde674d73c429daec7775d6156495a0?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) (9af17a4) will **increase** coverage by `0.01%`.
> The diff coverage is `100.00%`.

> :exclamation: Current head b3bd6a9 differs from pull request most recent head 272e561. Consider uploading reports for the commit 272e561 to get more accurate results
[![Impacted file tree graph](https://codecov.io/gh/goreleaser/goreleaser/pull/2661/graphs/tree.svg?width=650&height=150&src=pr&token=3bJvv6b7hN&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)](https://codecov.io/gh/goreleaser/goreleaser/pull/2661?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)

```diff
@@            Coverage Diff             @@
##           master    #2661      +/-   ##
==========================================
+ Coverage   84.50%   84.51%   +0.01%     
==========================================
  Files         102      102              
  Lines        7482     7487       +5     
==========================================
+ Hits         6323     6328       +5     
  Misses        951      951              
  Partials      208      208              
```


| [Impacted Files](https://codecov.io/gh/goreleaser/goreleaser/pull/2661?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) | Coverage Δ | |
|---|---|---|
| [internal/pipe/release/body.go](https://codecov.io/gh/goreleaser/goreleaser/pull/2661/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser#diff-aW50ZXJuYWwvcGlwZS9yZWxlYXNlL2JvZHkuZ28=) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/2661?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/2661?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser). Last update [9af17a4...272e561](https://codecov.io/gh/goreleaser/goreleaser/pull/2661?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser).


Patch:
diff --git a/internal/client/client.go b/internal/client/client.go
index fa111de7..8f738da8 100644
--- a/internal/client/client.go
+++ b/internal/client/client.go
@@ -12,6 +12,13 @@ import (
 	"github.com/goreleaser/goreleaser/pkg/context"
 )
 
+const (
+	// maxReleaseBodyLength defines the max characters size of the body
+	maxReleaseBodyLength = 125000
+	// ellipsis to be used when release notes body is too long
+	ellipsis = "..."
+)
+
 // ErrNotImplemented is returned when a client does not implement certain feature.
 var ErrNotImplemented = fmt.Errorf("not implemented")
 
@@ -83,6 +90,13 @@ func NewIfToken(ctx *context.Context, cli Client, token string) (Client, error)
 	return newWithToken(ctx, token)
 }
 
+func truncateReleaseBody(body string) string {
+	if len(body) > maxReleaseBodyLength {
+		body = body[1:(maxReleaseBodyLength-len(ellipsis))] + ellipsis
+	}
+	return body
+}
+
 // ErrNoMilestoneFound is an error when no milestone is found.
 type ErrNoMilestoneFound struct {
 	Title string
diff --git a/internal/client/github.go b/internal/client/github.go
index a759dc86..febce1e7 100644
--- a/internal/client/github.go
+++ b/internal/client/github.go
@@ -207,6 +207,9 @@ func (c *githubClient) CreateRelease(ctx *context.Context, body string) (string,
 		return "", err
 	}
 
+	// Truncate the release notes if it's too long (github doesn't allow more than 125000 characters)
+	body = truncateReleaseBody(body)
+
 	data := &github.RepositoryRelease{
 		Name:       github.String(title),
 		TagName:    github.String(ctx.Git.CurrentTag),


Test patch:
diff --git a/internal/client/client_test.go b/internal/client/client_test.go
index c7462396..fcea6b85 100644
--- a/internal/client/client_test.go
+++ b/internal/client/client_test.go
@@ -1,6 +1,7 @@
 package client
 
 import (
+	"math/rand"
 	"testing"
 
 	"github.com/goreleaser/goreleaser/pkg/config"
@@ -59,6 +60,18 @@ func TestClientNewGitLab(t *testing.T) {
 	require.True(t, ok)
 }
 
+func TestCheckBodyMaxLength(t *testing.T) {
+	letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
+	b := make([]rune, maxReleaseBodyLength)
+	for i := range b {
+		b[i] = letters[rand.Intn(len(letters))]
+	}
+	ctx := context.New(config.Project{})
+	ctx.ReleaseNotes = string(b)
+	out := truncateReleaseBody(string(b))
+	require.Len(t, out, maxReleaseBodyLength)
+}
+
 func TestNewIfToken(t *testing.T) {
 	t.Run("valid", func(t *testing.T) {
 		ctx := &context.Context{