1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-11 14:39:28 +02:00

feat: add message template support for git providers (#2626)

* feat: add message template support for git providers

Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
Signed-off-by: Batuhan Apaydın <batuhan.apaydin@trendyol.com>
Co-authored-by: Furkan Türkal <furkan.turkal@trendyol.com>
Co-authored-by: Erkan Zileli <erkan.zileli@trendyol.com>
Signed-off-by: Batuhan Apaydın <batuhan.apaydin@trendyol.com>

* Apply suggestions from code review

Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
Co-authored-by: Furkan Türkal <furkan.turkal@trendyol.com>
Co-authored-by: Erkan Zileli <erkan.zileli@trendyol.com>
This commit is contained in:
Batuhan Apaydın 2021-11-06 21:39:43 +03:00 committed by GitHub
parent 143b955f1b
commit b99ccc029c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 49 additions and 17 deletions

View File

@ -55,7 +55,7 @@ func getInfo(ctx *context.Context) (context.GitInfo, error) {
if !git.IsRepo() {
return context.GitInfo{}, ErrNotRepository
}
info, err := getGitInfo()
info, err := getGitInfo(ctx)
if err != nil && ctx.Snapshot {
log.WithError(err).Warn("ignoring errors because this is a snapshot")
if info.Commit == "" {
@ -66,7 +66,7 @@ func getInfo(ctx *context.Context) (context.GitInfo, error) {
return info, err
}
func getGitInfo() (context.GitInfo, error) {
func getGitInfo(ctx *context.Context) (context.GitInfo, error) {
branch, err := getBranch()
if err != nil {
return context.GitInfo{}, fmt.Errorf("couldn't get current branch: %w", err)
@ -88,6 +88,18 @@ func getGitInfo() (context.GitInfo, error) {
return context.GitInfo{}, fmt.Errorf("couldn't get remote URL: %w", err)
}
var tagErr error
tag, err := getTag()
if err != nil {
tagErr = err
tag = "v0.0.0"
}
releaseURL, err := getReleaseURL(ctx.TokenType, gitURL, tag)
if err != nil {
return context.GitInfo{}, fmt.Errorf("couldn't get release URL: %w", err)
}
if strings.HasPrefix(gitURL, "https://") {
u, err := url.Parse(gitURL)
if err != nil {
@ -97,8 +109,7 @@ func getGitInfo() (context.GitInfo, error) {
gitURL = u.String()
}
tag, err := getTag()
if err != nil {
if tagErr != nil {
return context.GitInfo{
Branch: branch,
Commit: full,
@ -106,7 +117,8 @@ func getGitInfo() (context.GitInfo, error) {
ShortCommit: short,
CommitDate: date,
URL: gitURL,
CurrentTag: "v0.0.0",
ReleaseURL: releaseURL,
CurrentTag: tag,
}, ErrNoTag
}
return context.GitInfo{
@ -117,6 +129,7 @@ func getGitInfo() (context.GitInfo, error) {
ShortCommit: short,
CommitDate: date,
URL: gitURL,
ReleaseURL: releaseURL,
}, nil
}
@ -206,3 +219,16 @@ func getTag() (string, error) {
func getURL() (string, error) {
return git.Clean(git.Run("ls-remote", "--get-url"))
}
func getReleaseURL(tokenType context.TokenType, url, tag string) (string, error) {
switch tokenType {
case context.TokenTypeGitHub:
return fmt.Sprintf("%s/releases/tag/%s", url, tag), nil
case context.TokenTypeGitLab:
return fmt.Sprintf("%s/-/releases/%s", url, tag), nil
case context.TokenTypeGitea:
return fmt.Sprintf("%s/releases/tag/%s", url, tag), nil
default:
return "", fmt.Errorf("invalid client token type: %q", tokenType)
}
}

View File

@ -18,7 +18,7 @@ import (
const (
defaultColor = "#2D313E"
defaultUsername = `GoReleaser`
defaultMessageTemplate = `{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ trimsuffix .GitURL ".git" }}/releases/tag/{{ .Tag }}`
defaultMessageTemplate = `{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}`
defaultMessageTitle = `{{ .ProjectName }} {{ .Tag }} is out!`
)
@ -35,6 +35,7 @@ func (Pipe) Default(ctx *context.Context) error {
if ctx.Config.Announce.Mattermost.MessageTemplate == "" {
ctx.Config.Announce.Mattermost.MessageTemplate = defaultMessageTemplate
}
if ctx.Config.Announce.Mattermost.TitleTemplate == "" {
ctx.Config.Announce.Mattermost.TitleTemplate = defaultMessageTitle
}

View File

@ -89,6 +89,7 @@ func TestPostWebhook(t *testing.T) {
})
ctx.Git.CurrentTag = "v1.0.0"
ctx.Git.ReleaseURL = "https://github.com/honk/honk/releases/tag/v1.0.0"
ctx.Git.URL = "https://github.com/honk/honk"
os.Setenv("MATTERMOST_WEBHOOK", ts.URL)

View File

@ -12,7 +12,7 @@ import (
const (
defaultTitleTemplate = `{{ .ProjectName }} {{ .Tag }} is out!`
defaultURLTemplate = `{{ trimsuffix .GitURL ".git" }}/releases/tag/{{ .Tag }}`
defaultURLTemplate = `{{ .ReleaseURL }}`
)
type Pipe struct{}
@ -31,7 +31,7 @@ func (Pipe) Default(ctx *context.Context) error {
}
if ctx.Config.Announce.Reddit.URLTemplate == "" {
ctx.Config.Announce.Reddit.URLTemplate = defaultURLTemplate
ctx.Config.Announce.Reddit.URLTemplate = defaultURLTemplate
}
return nil

View File

@ -12,7 +12,7 @@ import (
const (
defaultUsername = `GoReleaser`
defaultMessageTemplate = `{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ trimsuffix .GitURL ".git" }}/releases/tag/{{ .Tag }}`
defaultMessageTemplate = `{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}`
)
type Pipe struct{}

View File

@ -13,7 +13,7 @@ import (
const (
defaultSubjectTemplate = `{{ .ProjectName }} {{ .Tag }} is out!`
defaultBodyTemplate = `You can view details from: {{ trimsuffix .GitURL ".git" }}/releases/tag/{{ .Tag }}`
defaultBodyTemplate = `You can view details from: {{ .ReleaseURL }}`
)
type Pipe struct{}
@ -29,14 +29,14 @@ type Config struct {
}
func (Pipe) Default(ctx *context.Context) error {
if ctx.Config.Announce.SMTP.SubjectTemplate == "" {
ctx.Config.Announce.SMTP.SubjectTemplate = defaultSubjectTemplate
}
if ctx.Config.Announce.SMTP.BodyTemplate == "" {
ctx.Config.Announce.SMTP.BodyTemplate = defaultBodyTemplate
}
if ctx.Config.Announce.SMTP.SubjectTemplate == "" {
ctx.Config.Announce.SMTP.SubjectTemplate = defaultSubjectTemplate
}
return nil
}

View File

@ -13,7 +13,7 @@ import (
const (
defaultColor = "#2D313E"
defaultIcon = "https://goreleaser.com/static/avatar.png"
defaultMessageTemplate = `{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ trimsuffix .GitURL ".git" }}/releases/tag/{{ .Tag }}`
defaultMessageTemplate = `{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}`
defaultMessageTitle = `{{ .ProjectName }} {{ .Tag }} is out!`
)

View File

@ -10,7 +10,7 @@ import (
"github.com/goreleaser/goreleaser/pkg/context"
)
const defaultMessageTemplate = `{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ trimsuffix .GitURL ".git" }}/releases/tag/{{ .Tag }}`
const defaultMessageTemplate = `{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}`
type Pipe struct{}

View File

@ -11,7 +11,7 @@ import (
"github.com/goreleaser/goreleaser/pkg/context"
)
const defaultMessageTemplate = `{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ trimsuffix .GitURL ".git" }}/releases/tag/{{ .Tag }}`
const defaultMessageTemplate = `{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}`
type Pipe struct{}

View File

@ -11,6 +11,7 @@ import (
"time"
"github.com/Masterminds/semver/v3"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/pkg/build"
"github.com/goreleaser/goreleaser/pkg/context"
@ -37,6 +38,7 @@ const (
commitDate = "CommitDate"
commitTimestamp = "CommitTimestamp"
gitURL = "GitURL"
releaseURL = "ReleaseURL"
major = "Major"
minor = "Minor"
patch = "Patch"
@ -83,6 +85,7 @@ func New(ctx *context.Context) *Template {
commitDate: ctx.Git.CommitDate.UTC().Format(time.RFC3339),
commitTimestamp: ctx.Git.CommitDate.UTC().Unix(),
gitURL: ctx.Git.URL,
releaseURL: ctx.Git.ReleaseURL,
env: ctx.Env,
date: ctx.Date.UTC().Format(time.RFC3339),
timestamp: ctx.Date.UTC().Unix(),

View File

@ -25,6 +25,7 @@ type GitInfo struct {
FullCommit string
CommitDate time.Time
URL string
ReleaseURL string
}
// Env is the environment variables.