1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-24 04:16:27 +02:00

137 lines
3.1 KiB
Go
Raw Normal View History

2017-05-13 18:09:42 -03:00
package client
import (
"bytes"
2017-09-23 19:42:07 +02:00
"net/url"
2017-05-13 18:09:42 -03:00
"os"
2017-06-22 00:09:14 -03:00
"github.com/apex/log"
2017-05-13 18:09:42 -03:00
"github.com/google/go-github/github"
"github.com/goreleaser/goreleaser/context"
"golang.org/x/oauth2"
)
type githubClient struct {
client *github.Client
}
// NewGitHub returns a github client implementation
2017-09-26 18:33:22 -03:00
func NewGitHub(ctx *context.Context) (Client, error) {
2017-05-13 18:09:42 -03:00
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: ctx.Token},
)
2017-09-23 19:42:07 +02:00
client := github.NewClient(oauth2.NewClient(ctx, ts))
2017-09-26 18:33:22 -03:00
if ctx.Config.GitHubURLs.API != "" {
api, err := url.Parse(ctx.Config.GitHubURLs.API)
2017-09-23 19:42:07 +02:00
if err != nil {
return &githubClient{}, err
}
2017-09-26 18:33:22 -03:00
upload, err := url.Parse(ctx.Config.GitHubURLs.Upload)
2017-09-23 19:42:07 +02:00
if err != nil {
return &githubClient{}, err
}
2017-09-26 18:33:22 -03:00
client.BaseURL = api
client.UploadURL = upload
2017-09-23 19:42:07 +02:00
}
return &githubClient{client}, nil
2017-05-13 18:09:42 -03:00
}
func (c *githubClient) CreateFile(
ctx *context.Context,
content bytes.Buffer,
path string,
) (err error) {
options := &github.RepositoryContentFileOptions{
Committer: &github.CommitAuthor{
Name: github.String(ctx.Config.Brew.CommitAuthor.Name),
Email: github.String(ctx.Config.Brew.CommitAuthor.Email),
2017-05-13 18:09:42 -03:00
},
Content: content.Bytes(),
Message: github.String(
2017-07-01 22:42:10 -03:00
ctx.Config.ProjectName + " version " + ctx.Git.CurrentTag,
2017-05-13 18:09:42 -03:00
),
}
file, _, res, err := c.client.Repositories.GetContents(
ctx,
ctx.Config.Brew.GitHub.Owner,
ctx.Config.Brew.GitHub.Name,
path,
&github.RepositoryContentGetOptions{},
)
if err != nil && res.StatusCode == 404 {
_, _, err = c.client.Repositories.CreateFile(
ctx,
ctx.Config.Brew.GitHub.Owner,
ctx.Config.Brew.GitHub.Name,
path,
options,
)
return
}
options.SHA = file.SHA
_, _, err = c.client.Repositories.UpdateFile(
ctx,
ctx.Config.Brew.GitHub.Owner,
ctx.Config.Brew.GitHub.Name,
path,
options,
)
return
}
func (c *githubClient) CreateRelease(ctx *context.Context, body string) (releaseID int, err error) {
var release *github.RepositoryRelease
var data = &github.RepositoryRelease{
Name: github.String(ctx.Git.CurrentTag),
TagName: github.String(ctx.Git.CurrentTag),
Body: github.String(body),
Draft: github.Bool(ctx.Config.Release.Draft),
Prerelease: github.Bool(ctx.Config.Release.Prerelease),
2017-05-13 18:09:42 -03:00
}
release, _, err = c.client.Repositories.GetReleaseByTag(
ctx,
ctx.Config.Release.GitHub.Owner,
ctx.Config.Release.GitHub.Name,
ctx.Git.CurrentTag,
)
if err != nil {
release, _, err = c.client.Repositories.CreateRelease(
ctx,
ctx.Config.Release.GitHub.Owner,
ctx.Config.Release.GitHub.Name,
data,
)
} else {
release, _, err = c.client.Repositories.EditRelease(
ctx,
ctx.Config.Release.GitHub.Owner,
ctx.Config.Release.GitHub.Name,
release.GetID(),
data,
)
}
2017-06-22 10:47:34 -03:00
log.WithField("url", release.GetHTMLURL()).Info("release updated")
2017-05-13 18:09:42 -03:00
return release.GetID(), err
}
func (c *githubClient) Upload(
ctx *context.Context,
releaseID int,
name string,
file *os.File,
) (err error) {
_, _, err = c.client.Repositories.UploadReleaseAsset(
ctx,
ctx.Config.Release.GitHub.Owner,
ctx.Config.Release.GitHub.Name,
releaseID,
&github.UploadOptions{
Name: name,
},
file,
)
return
}