diff --git a/clients/github.go b/clients/github.go new file mode 100644 index 000000000..45807ad3f --- /dev/null +++ b/clients/github.go @@ -0,0 +1,16 @@ +package clients + +import ( + "context" + + "github.com/google/go-github/github" + "golang.org/x/oauth2" +) + +func Github(token string) *github.Client { + ts := oauth2.StaticTokenSource( + &oauth2.Token{AccessToken: token}, + ) + tc := oauth2.NewClient(context.Background(), ts) + return github.NewClient(tc) +} diff --git a/pipeline/brew/brew.go b/pipeline/brew/brew.go index 9645eb857..c9a406b8a 100644 --- a/pipeline/brew/brew.go +++ b/pipeline/brew/brew.go @@ -2,16 +2,15 @@ package brew import ( "bytes" - goctx "context" "log" "path/filepath" "strings" "text/template" "github.com/google/go-github/github" + "github.com/goreleaser/releaser/clients" "github.com/goreleaser/releaser/context" "github.com/goreleaser/releaser/sha256sum" - "golang.org/x/oauth2" ) const formulae = `class {{ .Name }} < Formula @@ -51,18 +50,9 @@ func (Pipe) Run(ctx *context.Context) error { if ctx.Config.Brew.Repo == "" { return nil } - ts := oauth2.StaticTokenSource( - &oauth2.Token{AccessToken: *ctx.Token}, - ) - tc := oauth2.NewClient(goctx.Background(), ts) - client := github.NewClient(tc) + client := clients.Github(*ctx.Token) + path := filepath.Join(ctx.Config.Brew.Folder, ctx.Config.BinaryName+".rb") - owner := ctx.BrewRepo.Owner - repo := ctx.BrewRepo.Name - path := filepath.Join( - ctx.Config.Brew.Folder, - ctx.Config.BinaryName+".rb", - ) log.Println("Updating", path, "on", ctx.Config.Brew.Repo, "...") out, err := buildFormulae(ctx, client) if err != nil { @@ -80,6 +70,8 @@ func (Pipe) Run(ctx *context.Context) error { ), } + owner := ctx.BrewRepo.Owner + repo := ctx.BrewRepo.Name file, _, res, err := client.Repositories.GetContents( owner, repo, path, &github.RepositoryContentGetOptions{}, ) diff --git a/pipeline/release/release.go b/pipeline/release/release.go index d30a6250a..430eec45e 100644 --- a/pipeline/release/release.go +++ b/pipeline/release/release.go @@ -1,14 +1,13 @@ package release import ( - goctx "context" "log" "os" "os/exec" "github.com/google/go-github/github" + "github.com/goreleaser/releaser/clients" "github.com/goreleaser/releaser/context" - "golang.org/x/oauth2" "golang.org/x/sync/errgroup" ) @@ -22,11 +21,7 @@ func (Pipe) Name() string { // Run the pipe func (Pipe) Run(ctx *context.Context) error { - ts := oauth2.StaticTokenSource( - &oauth2.Token{AccessToken: *ctx.Token}, - ) - tc := oauth2.NewClient(goctx.Background(), ts) - client := github.NewClient(tc) + client := clients.Github(*ctx.Token) r, err := getOrCreateRelease(client, ctx) if err != nil {