2017-02-07 21:33:23 +00:00
|
|
|
// Copyright 2013 The go-github AUTHORS. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package github
|
|
|
|
|
|
|
|
import (
|
2017-04-11 15:10:46 +00:00
|
|
|
"context"
|
2017-02-07 21:33:23 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"mime"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RepositoryRelease represents a GitHub release in a repository.
|
|
|
|
type RepositoryRelease struct {
|
|
|
|
ID *int `json:"id,omitempty"`
|
|
|
|
TagName *string `json:"tag_name,omitempty"`
|
|
|
|
TargetCommitish *string `json:"target_commitish,omitempty"`
|
|
|
|
Name *string `json:"name,omitempty"`
|
|
|
|
Body *string `json:"body,omitempty"`
|
|
|
|
Draft *bool `json:"draft,omitempty"`
|
|
|
|
Prerelease *bool `json:"prerelease,omitempty"`
|
|
|
|
CreatedAt *Timestamp `json:"created_at,omitempty"`
|
|
|
|
PublishedAt *Timestamp `json:"published_at,omitempty"`
|
|
|
|
URL *string `json:"url,omitempty"`
|
|
|
|
HTMLURL *string `json:"html_url,omitempty"`
|
|
|
|
AssetsURL *string `json:"assets_url,omitempty"`
|
|
|
|
Assets []ReleaseAsset `json:"assets,omitempty"`
|
|
|
|
UploadURL *string `json:"upload_url,omitempty"`
|
|
|
|
ZipballURL *string `json:"zipball_url,omitempty"`
|
|
|
|
TarballURL *string `json:"tarball_url,omitempty"`
|
|
|
|
Author *User `json:"author,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r RepositoryRelease) String() string {
|
|
|
|
return Stringify(r)
|
|
|
|
}
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
// ReleaseAsset represents a GitHub release asset in a repository.
|
2017-02-07 21:33:23 +00:00
|
|
|
type ReleaseAsset struct {
|
|
|
|
ID *int `json:"id,omitempty"`
|
|
|
|
URL *string `json:"url,omitempty"`
|
|
|
|
Name *string `json:"name,omitempty"`
|
|
|
|
Label *string `json:"label,omitempty"`
|
|
|
|
State *string `json:"state,omitempty"`
|
|
|
|
ContentType *string `json:"content_type,omitempty"`
|
|
|
|
Size *int `json:"size,omitempty"`
|
|
|
|
DownloadCount *int `json:"download_count,omitempty"`
|
|
|
|
CreatedAt *Timestamp `json:"created_at,omitempty"`
|
|
|
|
UpdatedAt *Timestamp `json:"updated_at,omitempty"`
|
|
|
|
BrowserDownloadURL *string `json:"browser_download_url,omitempty"`
|
|
|
|
Uploader *User `json:"uploader,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r ReleaseAsset) String() string {
|
|
|
|
return Stringify(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListReleases lists the releases for a repository.
|
|
|
|
//
|
2017-04-11 15:10:46 +00:00
|
|
|
// GitHub API docs: https://developer.github.com/v3/repos/releases/#list-releases-for-a-repository
|
|
|
|
func (s *RepositoriesService) ListReleases(ctx context.Context, owner, repo string, opt *ListOptions) ([]*RepositoryRelease, *Response, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
u := fmt.Sprintf("repos/%s/%s/releases", owner, repo)
|
|
|
|
u, err := addOptions(u, opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest("GET", u, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
var releases []*RepositoryRelease
|
|
|
|
resp, err := s.client.Do(ctx, req, &releases)
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
2017-04-11 15:10:46 +00:00
|
|
|
return releases, resp, nil
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRelease fetches a single release.
|
|
|
|
//
|
2017-04-11 15:10:46 +00:00
|
|
|
// GitHub API docs: https://developer.github.com/v3/repos/releases/#get-a-single-release
|
|
|
|
func (s *RepositoriesService) GetRelease(ctx context.Context, owner, repo string, id int) (*RepositoryRelease, *Response, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
u := fmt.Sprintf("repos/%s/%s/releases/%d", owner, repo, id)
|
2017-04-11 15:10:46 +00:00
|
|
|
return s.getSingleRelease(ctx, u)
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetLatestRelease fetches the latest published release for the repository.
|
|
|
|
//
|
|
|
|
// GitHub API docs: https://developer.github.com/v3/repos/releases/#get-the-latest-release
|
2017-04-11 15:10:46 +00:00
|
|
|
func (s *RepositoriesService) GetLatestRelease(ctx context.Context, owner, repo string) (*RepositoryRelease, *Response, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
u := fmt.Sprintf("repos/%s/%s/releases/latest", owner, repo)
|
2017-04-11 15:10:46 +00:00
|
|
|
return s.getSingleRelease(ctx, u)
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetReleaseByTag fetches a release with the specified tag.
|
|
|
|
//
|
|
|
|
// GitHub API docs: https://developer.github.com/v3/repos/releases/#get-a-release-by-tag-name
|
2017-04-11 15:10:46 +00:00
|
|
|
func (s *RepositoriesService) GetReleaseByTag(ctx context.Context, owner, repo, tag string) (*RepositoryRelease, *Response, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
u := fmt.Sprintf("repos/%s/%s/releases/tags/%s", owner, repo, tag)
|
2017-04-11 15:10:46 +00:00
|
|
|
return s.getSingleRelease(ctx, u)
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
func (s *RepositoriesService) getSingleRelease(ctx context.Context, url string) (*RepositoryRelease, *Response, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
req, err := s.client.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
release := new(RepositoryRelease)
|
2017-04-11 15:10:46 +00:00
|
|
|
resp, err := s.client.Do(ctx, req, release)
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
2017-04-11 15:10:46 +00:00
|
|
|
return release, resp, nil
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateRelease adds a new release for a repository.
|
|
|
|
//
|
2017-04-11 15:10:46 +00:00
|
|
|
// GitHub API docs: https://developer.github.com/v3/repos/releases/#create-a-release
|
|
|
|
func (s *RepositoriesService) CreateRelease(ctx context.Context, owner, repo string, release *RepositoryRelease) (*RepositoryRelease, *Response, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
u := fmt.Sprintf("repos/%s/%s/releases", owner, repo)
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest("POST", u, release)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
r := new(RepositoryRelease)
|
2017-04-11 15:10:46 +00:00
|
|
|
resp, err := s.client.Do(ctx, req, r)
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
2017-04-11 15:10:46 +00:00
|
|
|
return r, resp, nil
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EditRelease edits a repository release.
|
|
|
|
//
|
2017-04-11 15:10:46 +00:00
|
|
|
// GitHub API docs: https://developer.github.com/v3/repos/releases/#edit-a-release
|
|
|
|
func (s *RepositoriesService) EditRelease(ctx context.Context, owner, repo string, id int, release *RepositoryRelease) (*RepositoryRelease, *Response, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
u := fmt.Sprintf("repos/%s/%s/releases/%d", owner, repo, id)
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest("PATCH", u, release)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
r := new(RepositoryRelease)
|
2017-04-11 15:10:46 +00:00
|
|
|
resp, err := s.client.Do(ctx, req, r)
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
2017-04-11 15:10:46 +00:00
|
|
|
return r, resp, nil
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteRelease delete a single release from a repository.
|
|
|
|
//
|
2017-04-11 15:10:46 +00:00
|
|
|
// GitHub API docs: https://developer.github.com/v3/repos/releases/#delete-a-release
|
|
|
|
func (s *RepositoriesService) DeleteRelease(ctx context.Context, owner, repo string, id int) (*Response, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
u := fmt.Sprintf("repos/%s/%s/releases/%d", owner, repo, id)
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest("DELETE", u, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-11 15:10:46 +00:00
|
|
|
return s.client.Do(ctx, req, nil)
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListReleaseAssets lists the release's assets.
|
|
|
|
//
|
2017-04-11 15:10:46 +00:00
|
|
|
// GitHub API docs: https://developer.github.com/v3/repos/releases/#list-assets-for-a-release
|
|
|
|
func (s *RepositoriesService) ListReleaseAssets(ctx context.Context, owner, repo string, id int, opt *ListOptions) ([]*ReleaseAsset, *Response, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
u := fmt.Sprintf("repos/%s/%s/releases/%d/assets", owner, repo, id)
|
|
|
|
u, err := addOptions(u, opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest("GET", u, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
var assets []*ReleaseAsset
|
|
|
|
resp, err := s.client.Do(ctx, req, &assets)
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
2017-04-11 15:10:46 +00:00
|
|
|
return nil, resp, err
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
2017-04-11 15:10:46 +00:00
|
|
|
return assets, resp, nil
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetReleaseAsset fetches a single release asset.
|
|
|
|
//
|
2017-04-11 15:10:46 +00:00
|
|
|
// GitHub API docs: https://developer.github.com/v3/repos/releases/#get-a-single-release-asset
|
|
|
|
func (s *RepositoriesService) GetReleaseAsset(ctx context.Context, owner, repo string, id int) (*ReleaseAsset, *Response, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
u := fmt.Sprintf("repos/%s/%s/releases/assets/%d", owner, repo, id)
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest("GET", u, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
asset := new(ReleaseAsset)
|
2017-04-11 15:10:46 +00:00
|
|
|
resp, err := s.client.Do(ctx, req, asset)
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
2017-04-11 15:10:46 +00:00
|
|
|
return nil, resp, err
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
2017-04-11 15:10:46 +00:00
|
|
|
return asset, resp, nil
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DownloadReleaseAsset downloads a release asset or returns a redirect URL.
|
|
|
|
//
|
|
|
|
// DownloadReleaseAsset returns an io.ReadCloser that reads the contents of the
|
|
|
|
// specified release asset. It is the caller's responsibility to close the ReadCloser.
|
|
|
|
// If a redirect is returned, the redirect URL will be returned as a string instead
|
|
|
|
// of the io.ReadCloser. Exactly one of rc and redirectURL will be zero.
|
|
|
|
//
|
2017-04-11 15:10:46 +00:00
|
|
|
// GitHub API docs: https://developer.github.com/v3/repos/releases/#get-a-single-release-asset
|
|
|
|
func (s *RepositoriesService) DownloadReleaseAsset(ctx context.Context, owner, repo string, id int) (rc io.ReadCloser, redirectURL string, err error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
u := fmt.Sprintf("repos/%s/%s/releases/assets/%d", owner, repo, id)
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest("GET", u, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
req.Header.Set("Accept", defaultMediaType)
|
|
|
|
|
|
|
|
s.client.clientMu.Lock()
|
|
|
|
defer s.client.clientMu.Unlock()
|
|
|
|
|
|
|
|
var loc string
|
|
|
|
saveRedirect := s.client.client.CheckRedirect
|
|
|
|
s.client.client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
|
|
|
|
loc = req.URL.String()
|
|
|
|
return errors.New("disable redirect")
|
|
|
|
}
|
|
|
|
defer func() { s.client.client.CheckRedirect = saveRedirect }()
|
|
|
|
|
2017-04-11 15:10:46 +00:00
|
|
|
ctx, req = withContext(ctx, req)
|
2017-02-07 21:33:23 +00:00
|
|
|
resp, err := s.client.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
if !strings.Contains(err.Error(), "disable redirect") {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
2017-04-11 15:10:46 +00:00
|
|
|
return nil, loc, nil // Intentionally return no error with valid redirect URL.
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := CheckResponse(resp); err != nil {
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Body, "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EditReleaseAsset edits a repository release asset.
|
|
|
|
//
|
2017-04-11 15:10:46 +00:00
|
|
|
// GitHub API docs: https://developer.github.com/v3/repos/releases/#edit-a-release-asset
|
|
|
|
func (s *RepositoriesService) EditReleaseAsset(ctx context.Context, owner, repo string, id int, release *ReleaseAsset) (*ReleaseAsset, *Response, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
u := fmt.Sprintf("repos/%s/%s/releases/assets/%d", owner, repo, id)
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest("PATCH", u, release)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
asset := new(ReleaseAsset)
|
2017-04-11 15:10:46 +00:00
|
|
|
resp, err := s.client.Do(ctx, req, asset)
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
2017-04-11 15:10:46 +00:00
|
|
|
return asset, resp, nil
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteReleaseAsset delete a single release asset from a repository.
|
|
|
|
//
|
2017-04-11 15:10:46 +00:00
|
|
|
// GitHub API docs: https://developer.github.com/v3/repos/releases/#delete-a-release-asset
|
|
|
|
func (s *RepositoriesService) DeleteReleaseAsset(ctx context.Context, owner, repo string, id int) (*Response, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
u := fmt.Sprintf("repos/%s/%s/releases/assets/%d", owner, repo, id)
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest("DELETE", u, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-11 15:10:46 +00:00
|
|
|
return s.client.Do(ctx, req, nil)
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UploadReleaseAsset creates an asset by uploading a file into a release repository.
|
|
|
|
// To upload assets that cannot be represented by an os.File, call NewUploadRequest directly.
|
|
|
|
//
|
2017-04-11 15:10:46 +00:00
|
|
|
// GitHub API docs: https://developer.github.com/v3/repos/releases/#upload-a-release-asset
|
|
|
|
func (s *RepositoriesService) UploadReleaseAsset(ctx context.Context, owner, repo string, id int, opt *UploadOptions, file *os.File) (*ReleaseAsset, *Response, error) {
|
2017-02-07 21:33:23 +00:00
|
|
|
u := fmt.Sprintf("repos/%s/%s/releases/%d/assets", owner, repo, id)
|
|
|
|
u, err := addOptions(u, opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
stat, err := file.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if stat.IsDir() {
|
|
|
|
return nil, nil, errors.New("the asset to upload can't be a directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
mediaType := mime.TypeByExtension(filepath.Ext(file.Name()))
|
|
|
|
req, err := s.client.NewUploadRequest(u, file, stat.Size(), mediaType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
asset := new(ReleaseAsset)
|
2017-04-11 15:10:46 +00:00
|
|
|
resp, err := s.client.Do(ctx, req, asset)
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
2017-04-11 15:10:46 +00:00
|
|
|
return asset, resp, nil
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|