mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-03-17 20:47:50 +02:00
context everywhere
This commit is contained in:
parent
7c315e8bd4
commit
523431bd0d
@ -1,17 +1,15 @@
|
||||
package clients
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/go-github/github"
|
||||
"github.com/goreleaser/goreleaser/context"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
// GitHub client for the given token
|
||||
func GitHub(token string) *github.Client {
|
||||
func GitHub(ctx *context.Context) *github.Client {
|
||||
ts := oauth2.StaticTokenSource(
|
||||
&oauth2.Token{AccessToken: token},
|
||||
&oauth2.Token{AccessToken: ctx.Token},
|
||||
)
|
||||
tc := oauth2.NewClient(context.Background(), ts)
|
||||
return github.NewClient(tc)
|
||||
return github.NewClient(oauth2.NewClient(ctx, ts))
|
||||
}
|
||||
|
@ -1,6 +1,10 @@
|
||||
package context
|
||||
|
||||
import "github.com/goreleaser/goreleaser/config"
|
||||
import (
|
||||
ctx "context"
|
||||
|
||||
"github.com/goreleaser/goreleaser/config"
|
||||
)
|
||||
|
||||
// GitInfo includes tags and diffs used in some point
|
||||
type GitInfo struct {
|
||||
@ -16,6 +20,7 @@ type Repo struct {
|
||||
|
||||
// Context carries along some data through the pipes
|
||||
type Context struct {
|
||||
ctx.Context
|
||||
Config config.Project
|
||||
Token string
|
||||
Git GitInfo
|
||||
@ -28,6 +33,7 @@ type Context struct {
|
||||
// New context
|
||||
func New(config config.Project) *Context {
|
||||
return &Context{
|
||||
Context: ctx.Background(),
|
||||
Config: config,
|
||||
Archives: map[string]string{},
|
||||
}
|
||||
|
@ -88,7 +88,7 @@ func (Pipe) Run(ctx *context.Context) error {
|
||||
if ctx.Config.Brew.Repo == "" {
|
||||
return nil
|
||||
}
|
||||
client := clients.GitHub(ctx.Token)
|
||||
client := clients.GitHub(ctx)
|
||||
path := filepath.Join(
|
||||
ctx.Config.Brew.Folder, ctx.Config.Build.BinaryName+".rb",
|
||||
)
|
||||
@ -113,14 +113,16 @@ 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{},
|
||||
ctx, owner, repo, path, &github.RepositoryContentGetOptions{},
|
||||
)
|
||||
if err != nil && res.StatusCode == 404 {
|
||||
_, _, err = client.Repositories.CreateFile(owner, repo, path, options)
|
||||
_, _, err = client.Repositories.CreateFile(
|
||||
ctx, owner, repo, path, options,
|
||||
)
|
||||
return err
|
||||
}
|
||||
options.SHA = file.SHA
|
||||
_, _, err = client.Repositories.UpdateFile(owner, repo, path, options)
|
||||
_, _, err = client.Repositories.UpdateFile(ctx, owner, repo, path, options)
|
||||
return err
|
||||
}
|
||||
|
||||
@ -142,10 +144,14 @@ func doBuildFormula(data templateData) (bytes.Buffer, error) {
|
||||
return out, err
|
||||
}
|
||||
|
||||
func dataFor(ctx *context.Context, client *github.Client) (result templateData, err error) {
|
||||
func dataFor(
|
||||
ctx *context.Context, client *github.Client,
|
||||
) (result templateData, err error) {
|
||||
var homepage string
|
||||
var description string
|
||||
rep, _, err := client.Repositories.Get(ctx.ReleaseRepo.Owner, ctx.ReleaseRepo.Name)
|
||||
rep, _, err := client.Repositories.Get(
|
||||
ctx, ctx.ReleaseRepo.Owner, ctx.ReleaseRepo.Name,
|
||||
)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ func (Pipe) Description() string {
|
||||
|
||||
// Run the pipe
|
||||
func (Pipe) Run(ctx *context.Context) error {
|
||||
client := clients.GitHub(ctx.Token)
|
||||
client := clients.GitHub(ctx)
|
||||
|
||||
r, err := getOrCreateRelease(client, ctx)
|
||||
if err != nil {
|
||||
@ -52,14 +52,14 @@ func getOrCreateRelease(client *github.Client, ctx *context.Context) (*github.Re
|
||||
TagName: github.String(ctx.Git.CurrentTag),
|
||||
Body: github.String(description(ctx.Git.Diff)),
|
||||
}
|
||||
r, _, err := client.Repositories.GetReleaseByTag(owner, repo, ctx.Git.CurrentTag)
|
||||
r, _, err := client.Repositories.GetReleaseByTag(ctx, owner, repo, ctx.Git.CurrentTag)
|
||||
if err != nil {
|
||||
log.Println("Creating release", ctx.Git.CurrentTag, "on", ctx.Config.Release.Repo)
|
||||
r, _, err = client.Repositories.CreateRelease(owner, repo, data)
|
||||
r, _, err = client.Repositories.CreateRelease(ctx, owner, repo, data)
|
||||
return r, err
|
||||
}
|
||||
log.Println("Updating existing release", ctx.Git.CurrentTag, "on", ctx.Config.Release.Repo)
|
||||
r, _, err = client.Repositories.EditRelease(owner, repo, *r.ID, data)
|
||||
r, _, err = client.Repositories.EditRelease(ctx, owner, repo, *r.ID, data)
|
||||
return r, err
|
||||
}
|
||||
|
||||
@ -93,6 +93,7 @@ func upload(ctx *context.Context, client *github.Client, releaseID int, archive,
|
||||
defer func() { _ = file.Close() }()
|
||||
log.Println("Uploading", file.Name())
|
||||
_, _, err = client.Repositories.UploadReleaseAsset(
|
||||
ctx,
|
||||
ctx.ReleaseRepo.Owner,
|
||||
ctx.ReleaseRepo.Name,
|
||||
releaseID,
|
||||
|
Loading…
x
Reference in New Issue
Block a user