1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-04-11 11:42:15 +02:00

refactor: release url (#2637)

* refactor: release url

* chore: fmt

* chore: less diffs

* docs: lowercase everything

* chore: improve fmt

* chore: revert
This commit is contained in:
Carlos Alexandro Becker 2021-11-06 16:54:01 -03:00 committed by GitHub
parent 1b775c541c
commit a2ba9cb8fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 48 additions and 41 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(ctx)
info, err := getGitInfo()
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(ctx *context.Context) (context.GitInfo, error) {
func getGitInfo() (context.GitInfo, error) {
branch, err := getBranch()
if err != nil {
return context.GitInfo{}, fmt.Errorf("couldn't get current branch: %w", err)
@ -88,18 +88,6 @@ func getGitInfo(ctx *context.Context) (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 {
@ -109,7 +97,8 @@ func getGitInfo(ctx *context.Context) (context.GitInfo, error) {
gitURL = u.String()
}
if tagErr != nil {
tag, err := getTag()
if err != nil {
return context.GitInfo{
Branch: branch,
Commit: full,
@ -117,8 +106,7 @@ func getGitInfo(ctx *context.Context) (context.GitInfo, error) {
ShortCommit: short,
CommitDate: date,
URL: gitURL,
ReleaseURL: releaseURL,
CurrentTag: tag,
CurrentTag: "v0.0.0",
}, ErrNoTag
}
return context.GitInfo{
@ -129,7 +117,6 @@ func getGitInfo(ctx *context.Context) (context.GitInfo, error) {
ShortCommit: short,
CommitDate: date,
URL: gitURL,
ReleaseURL: releaseURL,
}, nil
}
@ -219,16 +206,3 @@ 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

@ -89,7 +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.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

@ -54,6 +54,13 @@ func (Pipe) Default(ctx *context.Context) error {
}
ctx.Config.Release.GitLab = repo
}
ctx.ReleaseURL = fmt.Sprintf(
"%s/%s/%s/-/releases/%s",
ctx.Config.GitLabURLs.Download,
ctx.Config.Release.GitLab.Owner,
ctx.Config.Release.GitLab.Name,
ctx.Git.CurrentTag,
)
case context.TokenTypeGitea:
if ctx.Config.Release.Gitea.Name == "" {
repo, err := git.ExtractRepoFromConfig()
@ -62,6 +69,13 @@ func (Pipe) Default(ctx *context.Context) error {
}
ctx.Config.Release.Gitea = repo
}
ctx.ReleaseURL = fmt.Sprintf(
"%s/%s/%s/releases/tag/%s",
ctx.Config.GiteaURLs.Download,
ctx.Config.Release.Gitea.Owner,
ctx.Config.Release.Gitea.Name,
ctx.Git.CurrentTag,
)
default:
// We keep github as default for now
if ctx.Config.Release.GitHub.Name == "" {
@ -71,6 +85,13 @@ func (Pipe) Default(ctx *context.Context) error {
}
ctx.Config.Release.GitHub = repo
}
ctx.ReleaseURL = fmt.Sprintf(
"%s/%s/%s/releases/tag/%s",
ctx.Config.GitHubURLs.Download,
ctx.Config.Release.GitHub.Owner,
ctx.Config.Release.GitHub.Name,
ctx.Git.CurrentTag,
)
}
// Check if we have to check the git tag for an indicator to mark as pre release

View File

@ -317,9 +317,12 @@ func TestDefault(t *testing.T) {
ctx := context.New(config.Project{})
ctx.TokenType = context.TokenTypeGitHub
ctx.Config.GitHubURLs.Download = "https://github.com"
ctx.Git.CurrentTag = "v1.0.0"
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, "goreleaser", ctx.Config.Release.GitHub.Name)
require.Equal(t, "goreleaser", ctx.Config.Release.GitHub.Owner)
require.Equal(t, "https://github.com/goreleaser/goreleaser/releases/tag/v1.0.0", ctx.ReleaseURL)
}
func TestDefaultWithGitlab(t *testing.T) {
@ -329,9 +332,12 @@ func TestDefaultWithGitlab(t *testing.T) {
ctx := context.New(config.Project{})
ctx.TokenType = context.TokenTypeGitLab
ctx.Config.GitLabURLs.Download = "https://gitlab.com"
ctx.Git.CurrentTag = "v1.0.0"
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, "gitlabrepo", ctx.Config.Release.GitLab.Name)
require.Equal(t, "gitlabowner", ctx.Config.Release.GitLab.Owner)
require.Equal(t, "https://gitlab.com/gitlabowner/gitlabrepo/-/releases/v1.0.0", ctx.ReleaseURL)
}
func TestDefaultWithGitea(t *testing.T) {

View File

@ -85,7 +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,
releaseURL: ctx.ReleaseURL,
env: ctx.Env,
date: ctx.Date.UTC().Format(time.RFC3339),
timestamp: ctx.Date.UTC().Unix(),

View File

@ -156,6 +156,7 @@ func TestFuncMap(t *testing.T) {
require.NoError(t, err)
ctx.Git.URL = "https://github.com/foo/bar.git"
ctx.ReleaseURL = "https://github.com/foo/bar/releases/tag/v1.0.0"
ctx.Git.CurrentTag = "v1.2.4"
for _, tc := range []struct {
Template string
@ -204,6 +205,11 @@ func TestFuncMap(t *testing.T) {
Name: "trimsuffix",
Expected: "https://github.com/foo/bar",
},
{
Template: `{{ .ReleaseURL }}`,
Name: "trimsuffix",
Expected: "https://github.com/foo/bar/releases/tag/v1.0.0",
},
{
Template: `{{ toupper "test" }}`,
Name: "toupper",

View File

@ -25,7 +25,6 @@ type GitInfo struct {
FullCommit string
CommitDate time.Time
URL string
ReleaseURL string
}
// Env is the environment variables.
@ -73,6 +72,7 @@ type Context struct {
Git GitInfo
Date time.Time
Artifacts artifact.Artifacts
ReleaseURL string
ReleaseNotes string
ReleaseNotesFile string
ReleaseNotesTmpl string

View File

@ -4,8 +4,6 @@ GoReleaser can also announce new releases on social networks, chat rooms and via
It runs at the very end of the pipeline and can be skipped with the `--skip-announce` flag of the [`release`](/cmd/goreleaser_release/) command, or via the skip property:
By default, ".git" suffix is removing from `{{ .GitURL }}` variable like this `{{ trimsuffix .GitURL ".git" }}`.
```yaml
# .goreleaser.yml
announce:

View File

@ -36,25 +36,27 @@ On fields that support templating, these fields are always available:
| `incpatch "v1.2.4"` | increments the patch of the given version[^3] |
| `incminor "v1.2.4"` | increments the minor of the given version[^3] |
| `incmajor "v1.2.4"` | increments the major of the given version[^3] |
| `.ReleaseURL` | the current release download url[^4] |
[^1]: The `v` prefix is stripped and it might be changed in `snapshot` and `nightly` builds.
[^2]: Assuming `Tag` is a valid a SemVer, otherwise empty/zeroed.
[^3]: Will panic if not a semantic version.
[^4]: Composed from the current SCM's download URL and current tag. For instance, on GitHub, it'll be `https://github.com/{owner}/{repo}/releases/tag/{tag}`.
On fields that are related to a single artifact (e.g., the binary name), you
may have some extra fields:
| Key | Description |
|-----------------|---------------------------------------|
| `.Os` | `GOOS`[^4] |
| `.Arch` | `GOARCH`[^4] |
| `.Arm` | `GOARM`[^4] |
| `.Mips` | `GOMIPS`[^4] |
| `.Os` | `GOOS`[^5] |
| `.Arch` | `GOARCH`[^5] |
| `.Arm` | `GOARM`[^5] |
| `.Mips` | `GOMIPS`[^5] |
| `.Binary` | binary name |
| `.ArtifactName` | archive name |
| `.ArtifactPath` | absolute path to artifact |
[^4]: Might have been replaced by `archives.replacements`.
[^5]: Might have been replaced by `archives.replacements`.
On the NFPM name template field, you can use those extra fields as well: