1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-07-15 01:34:21 +02:00

feat: support custom tokens in Homebrew & Scoop (#1650)

This commit is contained in:
Radek Simko
2020-07-06 21:12:41 +01:00
committed by GitHub
parent 0d4f605388
commit ab8bb7f2f3
18 changed files with 258 additions and 75 deletions

View File

@ -88,11 +88,13 @@ func (Pipe) Default(ctx *context.Context) error {
}
if brew.GitHub.String() != "" {
deprecate.Notice(ctx, "brews.github")
brew.Tap = brew.GitHub
brew.Tap.Owner = brew.GitHub.Owner
brew.Tap.Name = brew.GitHub.Name
}
if brew.GitLab.String() != "" {
deprecate.Notice(ctx, "brews.gitlab")
brew.Tap = brew.GitLab
brew.Tap.Owner = brew.GitLab.Owner
brew.Tap.Name = brew.GitLab.Name
}
if brew.CommitAuthor.Name == "" {
brew.CommitAuthor.Name = "goreleaserbot"
@ -129,11 +131,24 @@ func contains(ss []string, s string) bool {
return false
}
func doRun(ctx *context.Context, brew config.Homebrew, client client.Client) error {
func doRun(ctx *context.Context, brew config.Homebrew, cl client.Client) error {
if brew.Tap.Name == "" {
return pipe.Skip("brew section is not configured")
}
if brew.Tap.Token != "" {
token, err := tmpl.New(ctx).ApplySingleEnvOnly(brew.Tap.Token)
if err != nil {
return err
}
log.Debug("using custom token to publish homebrew formula")
c, err := client.NewWithToken(ctx, token)
if err != nil {
return err
}
cl = c
}
// TODO: properly cover this with tests
var filters = []artifact.Filter{
artifact.Or(
@ -160,7 +175,7 @@ func doRun(ctx *context.Context, brew config.Homebrew, client client.Client) err
return ErrNoArchivesFound
}
content, err := buildFormula(ctx, brew, client, archives)
content, err := buildFormula(ctx, brew, cl, archives)
if err != nil {
return err
}
@ -169,7 +184,7 @@ func doRun(ctx *context.Context, brew config.Homebrew, client client.Client) err
var path = filepath.Join(ctx.Config.Dist, filename)
log.WithField("formula", path).Info("writing")
if err := ioutil.WriteFile(path, []byte(content), 0644); err != nil { //nolint: gosec
return errors.Wrap(err, "failed to write brew tap")
return errors.Wrap(err, "failed to write brew formula")
}
if strings.TrimSpace(brew.SkipUpload) == "true" {
@ -182,7 +197,7 @@ func doRun(ctx *context.Context, brew config.Homebrew, client client.Client) err
return pipe.Skip("prerelease detected with 'auto' upload, skipping homebrew publish")
}
repo := brew.Tap
repo := client.RepoFromRef(brew.Tap)
var gpath = buildFormulaPath(brew.Folder, filename)
log.WithField("formula", gpath).
@ -190,7 +205,7 @@ func doRun(ctx *context.Context, brew config.Homebrew, client client.Client) err
Info("pushing")
var msg = fmt.Sprintf("Brew formula update for %s version %s", ctx.Config.ProjectName, ctx.Git.CurrentTag)
return client.CreateFile(ctx, brew.CommitAuthor, repo, []byte(content), gpath, msg)
return cl.CreateFile(ctx, brew.CommitAuthor, repo, []byte(content), gpath, msg)
}
func buildFormulaPath(folder, filename string) string {

View File

@ -269,7 +269,7 @@ func TestRunPipeForMultipleArmVersions(t *testing.T) {
Dependencies: []config.HomebrewDependency{{Name: "zsh"}, {Name: "bash", Type: "recommended"}},
Conflicts: []string{"gtk+", "qt"},
Install: `bin.install "{{ .ProjectName }}"`,
Tap: config.Repo{
Tap: config.RepoRef{
Owner: "test",
Name: "test",
},
@ -365,7 +365,7 @@ func TestRunPipeNoDarwin64Build(t *testing.T) {
Config: config.Project{
Brews: []config.Homebrew{
{
Tap: config.Repo{
Tap: config.RepoRef{
Owner: "test",
Name: "test",
},
@ -383,7 +383,7 @@ func TestRunPipeMultipleArchivesSameOsBuild(t *testing.T) {
config.Project{
Brews: []config.Homebrew{
{
Tap: config.Repo{
Tap: config.RepoRef{
Owner: "test",
Name: "test",
},
@ -537,7 +537,7 @@ func TestRunPipeBinaryRelease(t *testing.T) {
config.Project{
Brews: []config.Homebrew{
{
Tap: config.Repo{
Tap: config.RepoRef{
Owner: "test",
Name: "test",
},
@ -566,7 +566,7 @@ func TestRunPipeNoUpload(t *testing.T) {
Release: config.Release{},
Brews: []config.Homebrew{
{
Tap: config.Repo{
Tap: config.RepoRef{
Owner: "test",
Name: "test",
},
@ -618,7 +618,7 @@ func TestRunEmptyTokenType(t *testing.T) {
Release: config.Release{},
Brews: []config.Homebrew{
{
Tap: config.Repo{
Tap: config.RepoRef{
Owner: "test",
Name: "test",
},
@ -653,7 +653,7 @@ func TestRunTokenTypeNotImplementedForBrew(t *testing.T) {
Release: config.Release{},
Brews: []config.Homebrew{
{
Tap: config.Repo{
Tap: config.RepoRef{
Owner: "test",
Name: "test",
},
@ -743,7 +743,7 @@ func (dc *DummyClient) ReleaseURLTemplate(ctx *context.Context) (string, error)
return "https://dummyhost/download/{{ .Tag }}/{{ .ArtifactName }}", nil
}
func (dc *DummyClient) CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo config.Repo, content []byte, path, msg string) (err error) {
func (dc *DummyClient) CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo client.Repo, content []byte, path, msg string) (err error) {
dc.CreatedFile = true
dc.Content = string(content)
return

View File

@ -550,7 +550,7 @@ func (c *DummyClient) ReleaseURLTemplate(ctx *context.Context) (string, error) {
return "", nil
}
func (c *DummyClient) CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo config.Repo, content []byte, path, msg string) (err error) {
func (c *DummyClient) CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo client.Repo, content []byte, path, msg string) (err error) {
return
}
@ -570,7 +570,7 @@ func (c *DummyClient) Upload(ctx *context.Context, releaseID string, artifact *a
}
if c.FailFirstUpload {
c.FailFirstUpload = false
return client.RetriableError{errors.New("upload failed, should retry")}
return client.RetriableError{Err: errors.New("upload failed, should retry")}
}
c.UploadedFile = true
c.UploadedFileNames = append(c.UploadedFileNames, artifact.Name)

View File

@ -34,6 +34,7 @@ func (Pipe) Publish(ctx *context.Context) error {
if ctx.SkipPublish {
return pipe.ErrSkipPublishEnabled
}
client, err := client.New(ctx)
if err != nil {
return err
@ -60,15 +61,24 @@ func (Pipe) Default(ctx *context.Context) error {
return nil
}
func doRun(ctx *context.Context, client client.Client) error {
if ctx.Config.Scoop.Bucket.Name == "" {
func doRun(ctx *context.Context, cl client.Client) error {
scoop := ctx.Config.Scoop
if scoop.Bucket.Name == "" {
return pipe.Skip("scoop section is not configured")
}
// TODO mavogel: in another PR
// check if release pipe is not configured!
// if ctx.Config.Release.Disable {
// }
if scoop.Bucket.Token != "" {
token, err := tmpl.New(ctx).ApplySingleEnvOnly(scoop.Bucket.Token)
if err != nil {
return err
}
log.Debug("using custom token to publish scoop manifest")
c, err := client.NewWithToken(ctx, token)
if err != nil {
return err
}
cl = c
}
// TODO: multiple archives
if ctx.Config.Archives[0].Format == "binary" {
@ -85,9 +95,9 @@ func doRun(ctx *context.Context, client client.Client) error {
return ErrNoWindows
}
var path = ctx.Config.Scoop.Name + ".json"
var path = scoop.Name + ".json"
data, err := dataFor(ctx, client, archives)
data, err := dataFor(ctx, cl, archives)
if err != nil {
return err
}
@ -99,10 +109,10 @@ func doRun(ctx *context.Context, client client.Client) error {
if ctx.SkipPublish {
return pipe.ErrSkipPublishEnabled
}
if strings.TrimSpace(ctx.Config.Scoop.SkipUpload) == "true" {
if strings.TrimSpace(scoop.SkipUpload) == "true" {
return pipe.Skip("scoop.skip_upload is true")
}
if strings.TrimSpace(ctx.Config.Scoop.SkipUpload) == "auto" && ctx.Semver.Prerelease != "" {
if strings.TrimSpace(scoop.SkipUpload) == "auto" && ctx.Semver.Prerelease != "" {
return pipe.Skip("release is prerelease")
}
if ctx.Config.Release.Draft {
@ -113,15 +123,16 @@ func doRun(ctx *context.Context, client client.Client) error {
}
commitMessage, err := tmpl.New(ctx).
Apply(ctx.Config.Scoop.CommitMessageTemplate)
Apply(scoop.CommitMessageTemplate)
if err != nil {
return err
}
return client.CreateFile(
repo := client.RepoFromRef(scoop.Bucket)
return cl.CreateFile(
ctx,
ctx.Config.Scoop.CommitAuthor,
ctx.Config.Scoop.Bucket,
scoop.CommitAuthor,
repo,
content.Bytes(),
path,
commitMessage,

View File

@ -113,7 +113,7 @@ func Test_doRun(t *testing.T) {
},
},
Scoop: config.Scoop{
Bucket: config.Repo{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
},
@ -156,7 +156,7 @@ func Test_doRun(t *testing.T) {
},
},
Scoop: config.Scoop{
Bucket: config.Repo{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
},
@ -216,7 +216,7 @@ func Test_doRun(t *testing.T) {
},
},
Scoop: config.Scoop{
Bucket: config.Repo{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
},
@ -259,7 +259,7 @@ func Test_doRun(t *testing.T) {
},
},
Scoop: config.Scoop{
Bucket: config.Repo{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
},
@ -319,7 +319,7 @@ func Test_doRun(t *testing.T) {
},
},
Scoop: config.Scoop{
Bucket: config.Repo{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
},
@ -377,7 +377,7 @@ func Test_doRun(t *testing.T) {
},
},
Scoop: config.Scoop{
Bucket: config.Repo{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
},
@ -420,7 +420,7 @@ func Test_doRun(t *testing.T) {
},
},
Scoop: config.Scoop{
Bucket: config.Repo{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
},
@ -498,7 +498,7 @@ func Test_doRun(t *testing.T) {
},
},
Scoop: config.Scoop{
Bucket: config.Repo{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
},
@ -539,7 +539,7 @@ func Test_doRun(t *testing.T) {
Draft: true,
},
Scoop: config.Scoop{
Bucket: config.Repo{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
},
@ -589,7 +589,7 @@ func Test_doRun(t *testing.T) {
},
Scoop: config.Scoop{
SkipUpload: "auto",
Bucket: config.Repo{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
},
@ -633,7 +633,7 @@ func Test_doRun(t *testing.T) {
},
Scoop: config.Scoop{
SkipUpload: "true",
Bucket: config.Repo{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
},
@ -673,7 +673,7 @@ func Test_doRun(t *testing.T) {
Disable: true,
},
Scoop: config.Scoop{
Bucket: config.Repo{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
},
@ -713,7 +713,7 @@ func Test_doRun(t *testing.T) {
Draft: true,
},
Scoop: config.Scoop{
Bucket: config.Repo{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
},
@ -780,7 +780,7 @@ func Test_buildManifest(t *testing.T) {
},
},
Scoop: config.Scoop{
Bucket: config.Repo{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
},
@ -820,7 +820,7 @@ func Test_buildManifest(t *testing.T) {
},
},
Scoop: config.Scoop{
Bucket: config.Repo{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
},
@ -862,7 +862,7 @@ func Test_buildManifest(t *testing.T) {
},
},
Scoop: config.Scoop{
Bucket: config.Repo{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
},
@ -968,7 +968,7 @@ func TestWrapInDirectory(t *testing.T) {
},
},
Scoop: config.Scoop{
Bucket: config.Repo{
Bucket: config.RepoRef{
Owner: "test",
Name: "test",
},
@ -1034,7 +1034,7 @@ func (dc *DummyClient) ReleaseURLTemplate(ctx *context.Context) (string, error)
return "", nil
}
func (dc *DummyClient) CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo config.Repo, content []byte, path, msg string) (err error) {
func (dc *DummyClient) CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo client.Repo, content []byte, path, msg string) (err error) {
dc.CreatedFile = true
dc.Content = string(content)
return