github.com/tomsquest/goreleaser@v0.34.3-0.20171008022654-7d6ef4d338b3/internal/client/github.go (about)

     1  package client
     2  
     3  import (
     4  	"bytes"
     5  	"net/url"
     6  	"os"
     7  
     8  	"github.com/apex/log"
     9  	"github.com/google/go-github/github"
    10  	"github.com/goreleaser/goreleaser/context"
    11  	"golang.org/x/oauth2"
    12  )
    13  
    14  type githubClient struct {
    15  	client *github.Client
    16  }
    17  
    18  // NewGitHub returns a github client implementation
    19  func NewGitHub(ctx *context.Context) (Client, error) {
    20  	ts := oauth2.StaticTokenSource(
    21  		&oauth2.Token{AccessToken: ctx.Token},
    22  	)
    23  	client := github.NewClient(oauth2.NewClient(ctx, ts))
    24  	if ctx.Config.GitHubURLs.API != "" {
    25  		api, err := url.Parse(ctx.Config.GitHubURLs.API)
    26  		if err != nil {
    27  			return &githubClient{}, err
    28  		}
    29  		upload, err := url.Parse(ctx.Config.GitHubURLs.Upload)
    30  		if err != nil {
    31  			return &githubClient{}, err
    32  		}
    33  		client.BaseURL = api
    34  		client.UploadURL = upload
    35  	}
    36  
    37  	return &githubClient{client}, nil
    38  }
    39  
    40  func (c *githubClient) CreateFile(
    41  	ctx *context.Context,
    42  	content bytes.Buffer,
    43  	path string,
    44  ) (err error) {
    45  	options := &github.RepositoryContentFileOptions{
    46  		Committer: &github.CommitAuthor{
    47  			Name:  github.String(ctx.Config.Brew.CommitAuthor.Name),
    48  			Email: github.String(ctx.Config.Brew.CommitAuthor.Email),
    49  		},
    50  		Content: content.Bytes(),
    51  		Message: github.String(
    52  			ctx.Config.ProjectName + " version " + ctx.Git.CurrentTag,
    53  		),
    54  	}
    55  
    56  	file, _, res, err := c.client.Repositories.GetContents(
    57  		ctx,
    58  		ctx.Config.Brew.GitHub.Owner,
    59  		ctx.Config.Brew.GitHub.Name,
    60  		path,
    61  		&github.RepositoryContentGetOptions{},
    62  	)
    63  	if err != nil && res.StatusCode == 404 {
    64  		_, _, err = c.client.Repositories.CreateFile(
    65  			ctx,
    66  			ctx.Config.Brew.GitHub.Owner,
    67  			ctx.Config.Brew.GitHub.Name,
    68  			path,
    69  			options,
    70  		)
    71  		return
    72  	}
    73  	options.SHA = file.SHA
    74  	_, _, err = c.client.Repositories.UpdateFile(
    75  		ctx,
    76  		ctx.Config.Brew.GitHub.Owner,
    77  		ctx.Config.Brew.GitHub.Name,
    78  		path,
    79  		options,
    80  	)
    81  	return
    82  }
    83  
    84  func (c *githubClient) CreateRelease(ctx *context.Context, body string) (releaseID int, err error) {
    85  	var release *github.RepositoryRelease
    86  	var data = &github.RepositoryRelease{
    87  		Name:       github.String(ctx.Git.CurrentTag),
    88  		TagName:    github.String(ctx.Git.CurrentTag),
    89  		Body:       github.String(body),
    90  		Draft:      github.Bool(ctx.Config.Release.Draft),
    91  		Prerelease: github.Bool(ctx.Config.Release.Prerelease),
    92  	}
    93  	release, _, err = c.client.Repositories.GetReleaseByTag(
    94  		ctx,
    95  		ctx.Config.Release.GitHub.Owner,
    96  		ctx.Config.Release.GitHub.Name,
    97  		ctx.Git.CurrentTag,
    98  	)
    99  	if err != nil {
   100  		release, _, err = c.client.Repositories.CreateRelease(
   101  			ctx,
   102  			ctx.Config.Release.GitHub.Owner,
   103  			ctx.Config.Release.GitHub.Name,
   104  			data,
   105  		)
   106  	} else {
   107  		release, _, err = c.client.Repositories.EditRelease(
   108  			ctx,
   109  			ctx.Config.Release.GitHub.Owner,
   110  			ctx.Config.Release.GitHub.Name,
   111  			release.GetID(),
   112  			data,
   113  		)
   114  	}
   115  	log.WithField("url", release.GetHTMLURL()).Info("release updated")
   116  	return release.GetID(), err
   117  }
   118  
   119  func (c *githubClient) Upload(
   120  	ctx *context.Context,
   121  	releaseID int,
   122  	name string,
   123  	file *os.File,
   124  ) (err error) {
   125  	_, _, err = c.client.Repositories.UploadReleaseAsset(
   126  		ctx,
   127  		ctx.Config.Release.GitHub.Owner,
   128  		ctx.Config.Release.GitHub.Name,
   129  		releaseID,
   130  		&github.UploadOptions{
   131  			Name: name,
   132  		},
   133  		file,
   134  	)
   135  	return
   136  }