2017-05-13 23:09:42 +02:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-01-17 15:25:57 +02:00
|
|
|
"crypto/tls"
|
|
|
|
"net/http"
|
2017-09-23 19:42:07 +02:00
|
|
|
"net/url"
|
2017-05-13 23:09:42 +02:00
|
|
|
"os"
|
|
|
|
|
2017-06-22 05:09:14 +02:00
|
|
|
"github.com/apex/log"
|
2017-05-13 23:09:42 +02:00
|
|
|
"github.com/google/go-github/github"
|
2018-07-09 05:47:30 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/tmpl"
|
2018-08-15 04:50:20 +02:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/config"
|
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
2017-05-13 23:09:42 +02:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type githubClient struct {
|
|
|
|
client *github.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewGitHub returns a github client implementation
|
2017-09-26 23:33:22 +02:00
|
|
|
func NewGitHub(ctx *context.Context) (Client, error) {
|
2017-05-13 23:09:42 +02:00
|
|
|
ts := oauth2.StaticTokenSource(
|
|
|
|
&oauth2.Token{AccessToken: ctx.Token},
|
|
|
|
)
|
2019-01-17 15:25:57 +02:00
|
|
|
httpClient := oauth2.NewClient(ctx, ts)
|
2019-01-17 17:59:55 +02:00
|
|
|
httpClient.Transport.(*oauth2.Transport).Base.(*http.Transport).TLSClientConfig = &tls.Config{
|
2019-01-17 15:28:10 +02:00
|
|
|
InsecureSkipVerify: ctx.Config.GitHubURLs.SkipTLSVerify,
|
2019-01-17 15:25:57 +02:00
|
|
|
}
|
|
|
|
client := github.NewClient(httpClient)
|
2017-09-26 23:33:22 +02: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 23:33:22 +02: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 23:33:22 +02:00
|
|
|
client.BaseURL = api
|
|
|
|
client.UploadURL = upload
|
2017-09-23 19:42:07 +02:00
|
|
|
}
|
|
|
|
|
2018-06-19 20:53:14 +02:00
|
|
|
return &githubClient{client: client}, nil
|
2017-05-13 23:09:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *githubClient) CreateFile(
|
|
|
|
ctx *context.Context,
|
2018-02-09 15:35:51 +02:00
|
|
|
commitAuthor config.CommitAuthor,
|
|
|
|
repo config.Repo,
|
2017-05-13 23:09:42 +02:00
|
|
|
content bytes.Buffer,
|
|
|
|
path string,
|
2018-03-10 19:13:00 +02:00
|
|
|
message string,
|
2018-02-16 14:35:44 +02:00
|
|
|
) error {
|
2017-05-13 23:09:42 +02:00
|
|
|
options := &github.RepositoryContentFileOptions{
|
|
|
|
Committer: &github.CommitAuthor{
|
2018-02-09 15:43:00 +02:00
|
|
|
Name: github.String(commitAuthor.Name),
|
|
|
|
Email: github.String(commitAuthor.Email),
|
2017-05-13 23:09:42 +02:00
|
|
|
},
|
|
|
|
Content: content.Bytes(),
|
2018-03-10 19:13:00 +02:00
|
|
|
Message: github.String(message),
|
2017-05-13 23:09:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
file, _, res, err := c.client.Repositories.GetContents(
|
|
|
|
ctx,
|
2018-02-09 15:43:00 +02:00
|
|
|
repo.Owner,
|
|
|
|
repo.Name,
|
2017-05-13 23:09:42 +02:00
|
|
|
path,
|
|
|
|
&github.RepositoryContentGetOptions{},
|
|
|
|
)
|
2018-02-10 15:14:52 +02:00
|
|
|
if err != nil && res.StatusCode != 404 {
|
2018-02-16 14:35:44 +02:00
|
|
|
return err
|
2018-02-10 15:14:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if res.StatusCode == 404 {
|
2017-05-13 23:09:42 +02:00
|
|
|
_, _, err = c.client.Repositories.CreateFile(
|
|
|
|
ctx,
|
2018-02-09 15:43:00 +02:00
|
|
|
repo.Owner,
|
|
|
|
repo.Name,
|
2017-05-13 23:09:42 +02:00
|
|
|
path,
|
|
|
|
options,
|
|
|
|
)
|
2018-02-16 14:35:44 +02:00
|
|
|
return err
|
2017-05-13 23:09:42 +02:00
|
|
|
}
|
2018-02-16 14:35:44 +02:00
|
|
|
options.SHA = file.SHA
|
|
|
|
_, _, err = c.client.Repositories.UpdateFile(
|
|
|
|
ctx,
|
|
|
|
repo.Owner,
|
|
|
|
repo.Name,
|
|
|
|
path,
|
|
|
|
options,
|
|
|
|
)
|
|
|
|
return err
|
2017-05-13 23:09:42 +02:00
|
|
|
}
|
|
|
|
|
2018-02-16 14:35:44 +02:00
|
|
|
func (c *githubClient) CreateRelease(ctx *context.Context, body string) (int64, error) {
|
2017-05-13 23:09:42 +02:00
|
|
|
var release *github.RepositoryRelease
|
2018-07-09 05:47:30 +02:00
|
|
|
title, err := tmpl.New(ctx).Apply(ctx.Config.Release.NameTemplate)
|
2017-10-07 11:31:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2018-11-29 20:42:14 +02:00
|
|
|
|
2017-05-13 23:09:42 +02:00
|
|
|
var data = &github.RepositoryRelease{
|
2018-04-26 21:27:24 +02:00
|
|
|
Name: github.String(title),
|
|
|
|
TagName: github.String(ctx.Git.CurrentTag),
|
|
|
|
Body: github.String(body),
|
|
|
|
Draft: github.Bool(ctx.Config.Release.Draft),
|
2018-11-29 20:42:14 +02:00
|
|
|
Prerelease: github.Bool(ctx.PreRelease),
|
2017-05-13 23:09:42 +02: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 15:47:34 +02:00
|
|
|
log.WithField("url", release.GetHTMLURL()).Info("release updated")
|
2017-05-13 23:09:42 +02:00
|
|
|
return release.GetID(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *githubClient) Upload(
|
|
|
|
ctx *context.Context,
|
2018-01-26 13:36:55 +02:00
|
|
|
releaseID int64,
|
2017-05-13 23:09:42 +02:00
|
|
|
name string,
|
|
|
|
file *os.File,
|
2018-02-16 14:35:44 +02:00
|
|
|
) error {
|
|
|
|
_, _, err := c.client.Repositories.UploadReleaseAsset(
|
2017-05-13 23:09:42 +02:00
|
|
|
ctx,
|
|
|
|
ctx.Config.Release.GitHub.Owner,
|
|
|
|
ctx.Config.Release.GitHub.Name,
|
|
|
|
releaseID,
|
|
|
|
&github.UploadOptions{
|
|
|
|
Name: name,
|
|
|
|
},
|
|
|
|
file,
|
|
|
|
)
|
2018-12-28 22:58:11 +02:00
|
|
|
return err
|
2017-05-13 23:09:42 +02:00
|
|
|
}
|