repos_releases.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // Copyright 2013 The go-github AUTHORS. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. package github
  6. import (
  7. "context"
  8. "errors"
  9. "fmt"
  10. "io"
  11. "mime"
  12. "net/http"
  13. "os"
  14. "path/filepath"
  15. "strings"
  16. )
  17. // RepositoryRelease represents a GitHub release in a repository.
  18. type RepositoryRelease struct {
  19. ID *int64 `json:"id,omitempty"`
  20. TagName *string `json:"tag_name,omitempty"`
  21. TargetCommitish *string `json:"target_commitish,omitempty"`
  22. Name *string `json:"name,omitempty"`
  23. Body *string `json:"body,omitempty"`
  24. Draft *bool `json:"draft,omitempty"`
  25. Prerelease *bool `json:"prerelease,omitempty"`
  26. CreatedAt *Timestamp `json:"created_at,omitempty"`
  27. PublishedAt *Timestamp `json:"published_at,omitempty"`
  28. URL *string `json:"url,omitempty"`
  29. HTMLURL *string `json:"html_url,omitempty"`
  30. AssetsURL *string `json:"assets_url,omitempty"`
  31. Assets []ReleaseAsset `json:"assets,omitempty"`
  32. UploadURL *string `json:"upload_url,omitempty"`
  33. ZipballURL *string `json:"zipball_url,omitempty"`
  34. TarballURL *string `json:"tarball_url,omitempty"`
  35. Author *User `json:"author,omitempty"`
  36. NodeID *string `json:"node_id,omitempty"`
  37. }
  38. func (r RepositoryRelease) String() string {
  39. return Stringify(r)
  40. }
  41. // ReleaseAsset represents a GitHub release asset in a repository.
  42. type ReleaseAsset struct {
  43. ID *int64 `json:"id,omitempty"`
  44. URL *string `json:"url,omitempty"`
  45. Name *string `json:"name,omitempty"`
  46. Label *string `json:"label,omitempty"`
  47. State *string `json:"state,omitempty"`
  48. ContentType *string `json:"content_type,omitempty"`
  49. Size *int `json:"size,omitempty"`
  50. DownloadCount *int `json:"download_count,omitempty"`
  51. CreatedAt *Timestamp `json:"created_at,omitempty"`
  52. UpdatedAt *Timestamp `json:"updated_at,omitempty"`
  53. BrowserDownloadURL *string `json:"browser_download_url,omitempty"`
  54. Uploader *User `json:"uploader,omitempty"`
  55. NodeID *string `json:"node_id,omitempty"`
  56. }
  57. func (r ReleaseAsset) String() string {
  58. return Stringify(r)
  59. }
  60. // ListReleases lists the releases for a repository.
  61. //
  62. // GitHub API docs: https://developer.github.com/v3/repos/releases/#list-releases-for-a-repository
  63. func (s *RepositoriesService) ListReleases(ctx context.Context, owner, repo string, opt *ListOptions) ([]*RepositoryRelease, *Response, error) {
  64. u := fmt.Sprintf("repos/%s/%s/releases", owner, repo)
  65. u, err := addOptions(u, opt)
  66. if err != nil {
  67. return nil, nil, err
  68. }
  69. req, err := s.client.NewRequest("GET", u, nil)
  70. if err != nil {
  71. return nil, nil, err
  72. }
  73. // TODO: remove custom Accept header when this API fully launches.
  74. req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
  75. var releases []*RepositoryRelease
  76. resp, err := s.client.Do(ctx, req, &releases)
  77. if err != nil {
  78. return nil, resp, err
  79. }
  80. return releases, resp, nil
  81. }
  82. // GetRelease fetches a single release.
  83. //
  84. // GitHub API docs: https://developer.github.com/v3/repos/releases/#get-a-single-release
  85. func (s *RepositoriesService) GetRelease(ctx context.Context, owner, repo string, id int64) (*RepositoryRelease, *Response, error) {
  86. u := fmt.Sprintf("repos/%s/%s/releases/%d", owner, repo, id)
  87. return s.getSingleRelease(ctx, u)
  88. }
  89. // GetLatestRelease fetches the latest published release for the repository.
  90. //
  91. // GitHub API docs: https://developer.github.com/v3/repos/releases/#get-the-latest-release
  92. func (s *RepositoriesService) GetLatestRelease(ctx context.Context, owner, repo string) (*RepositoryRelease, *Response, error) {
  93. u := fmt.Sprintf("repos/%s/%s/releases/latest", owner, repo)
  94. return s.getSingleRelease(ctx, u)
  95. }
  96. // GetReleaseByTag fetches a release with the specified tag.
  97. //
  98. // GitHub API docs: https://developer.github.com/v3/repos/releases/#get-a-release-by-tag-name
  99. func (s *RepositoriesService) GetReleaseByTag(ctx context.Context, owner, repo, tag string) (*RepositoryRelease, *Response, error) {
  100. u := fmt.Sprintf("repos/%s/%s/releases/tags/%s", owner, repo, tag)
  101. return s.getSingleRelease(ctx, u)
  102. }
  103. func (s *RepositoriesService) getSingleRelease(ctx context.Context, url string) (*RepositoryRelease, *Response, error) {
  104. req, err := s.client.NewRequest("GET", url, nil)
  105. if err != nil {
  106. return nil, nil, err
  107. }
  108. // TODO: remove custom Accept header when this API fully launches.
  109. req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
  110. release := new(RepositoryRelease)
  111. resp, err := s.client.Do(ctx, req, release)
  112. if err != nil {
  113. return nil, resp, err
  114. }
  115. return release, resp, nil
  116. }
  117. // CreateRelease adds a new release for a repository.
  118. //
  119. // GitHub API docs: https://developer.github.com/v3/repos/releases/#create-a-release
  120. func (s *RepositoriesService) CreateRelease(ctx context.Context, owner, repo string, release *RepositoryRelease) (*RepositoryRelease, *Response, error) {
  121. u := fmt.Sprintf("repos/%s/%s/releases", owner, repo)
  122. req, err := s.client.NewRequest("POST", u, release)
  123. if err != nil {
  124. return nil, nil, err
  125. }
  126. // TODO: remove custom Accept header when this API fully launches.
  127. req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
  128. r := new(RepositoryRelease)
  129. resp, err := s.client.Do(ctx, req, r)
  130. if err != nil {
  131. return nil, resp, err
  132. }
  133. return r, resp, nil
  134. }
  135. // EditRelease edits a repository release.
  136. //
  137. // GitHub API docs: https://developer.github.com/v3/repos/releases/#edit-a-release
  138. func (s *RepositoriesService) EditRelease(ctx context.Context, owner, repo string, id int64, release *RepositoryRelease) (*RepositoryRelease, *Response, error) {
  139. u := fmt.Sprintf("repos/%s/%s/releases/%d", owner, repo, id)
  140. req, err := s.client.NewRequest("PATCH", u, release)
  141. if err != nil {
  142. return nil, nil, err
  143. }
  144. // TODO: remove custom Accept header when this API fully launches.
  145. req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
  146. r := new(RepositoryRelease)
  147. resp, err := s.client.Do(ctx, req, r)
  148. if err != nil {
  149. return nil, resp, err
  150. }
  151. return r, resp, nil
  152. }
  153. // DeleteRelease delete a single release from a repository.
  154. //
  155. // GitHub API docs: https://developer.github.com/v3/repos/releases/#delete-a-release
  156. func (s *RepositoriesService) DeleteRelease(ctx context.Context, owner, repo string, id int64) (*Response, error) {
  157. u := fmt.Sprintf("repos/%s/%s/releases/%d", owner, repo, id)
  158. req, err := s.client.NewRequest("DELETE", u, nil)
  159. if err != nil {
  160. return nil, err
  161. }
  162. return s.client.Do(ctx, req, nil)
  163. }
  164. // ListReleaseAssets lists the release's assets.
  165. //
  166. // GitHub API docs: https://developer.github.com/v3/repos/releases/#list-assets-for-a-release
  167. func (s *RepositoriesService) ListReleaseAssets(ctx context.Context, owner, repo string, id int64, opt *ListOptions) ([]*ReleaseAsset, *Response, error) {
  168. u := fmt.Sprintf("repos/%s/%s/releases/%d/assets", owner, repo, id)
  169. u, err := addOptions(u, opt)
  170. if err != nil {
  171. return nil, nil, err
  172. }
  173. req, err := s.client.NewRequest("GET", u, nil)
  174. if err != nil {
  175. return nil, nil, err
  176. }
  177. // TODO: remove custom Accept header when this API fully launches.
  178. req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
  179. var assets []*ReleaseAsset
  180. resp, err := s.client.Do(ctx, req, &assets)
  181. if err != nil {
  182. return nil, resp, err
  183. }
  184. return assets, resp, nil
  185. }
  186. // GetReleaseAsset fetches a single release asset.
  187. //
  188. // GitHub API docs: https://developer.github.com/v3/repos/releases/#get-a-single-release-asset
  189. func (s *RepositoriesService) GetReleaseAsset(ctx context.Context, owner, repo string, id int64) (*ReleaseAsset, *Response, error) {
  190. u := fmt.Sprintf("repos/%s/%s/releases/assets/%d", owner, repo, id)
  191. req, err := s.client.NewRequest("GET", u, nil)
  192. if err != nil {
  193. return nil, nil, err
  194. }
  195. // TODO: remove custom Accept header when this API fully launches.
  196. req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
  197. asset := new(ReleaseAsset)
  198. resp, err := s.client.Do(ctx, req, asset)
  199. if err != nil {
  200. return nil, resp, err
  201. }
  202. return asset, resp, nil
  203. }
  204. // DownloadReleaseAsset downloads a release asset or returns a redirect URL.
  205. //
  206. // DownloadReleaseAsset returns an io.ReadCloser that reads the contents of the
  207. // specified release asset. It is the caller's responsibility to close the ReadCloser.
  208. // If a redirect is returned, the redirect URL will be returned as a string instead
  209. // of the io.ReadCloser. Exactly one of rc and redirectURL will be zero.
  210. //
  211. // GitHub API docs: https://developer.github.com/v3/repos/releases/#get-a-single-release-asset
  212. func (s *RepositoriesService) DownloadReleaseAsset(ctx context.Context, owner, repo string, id int64) (rc io.ReadCloser, redirectURL string, err error) {
  213. u := fmt.Sprintf("repos/%s/%s/releases/assets/%d", owner, repo, id)
  214. req, err := s.client.NewRequest("GET", u, nil)
  215. if err != nil {
  216. return nil, "", err
  217. }
  218. req.Header.Set("Accept", defaultMediaType)
  219. s.client.clientMu.Lock()
  220. defer s.client.clientMu.Unlock()
  221. var loc string
  222. saveRedirect := s.client.client.CheckRedirect
  223. s.client.client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
  224. loc = req.URL.String()
  225. return errors.New("disable redirect")
  226. }
  227. defer func() { s.client.client.CheckRedirect = saveRedirect }()
  228. req = withContext(ctx, req)
  229. resp, err := s.client.client.Do(req)
  230. if err != nil {
  231. if !strings.Contains(err.Error(), "disable redirect") {
  232. return nil, "", err
  233. }
  234. return nil, loc, nil // Intentionally return no error with valid redirect URL.
  235. }
  236. if err := CheckResponse(resp); err != nil {
  237. resp.Body.Close()
  238. return nil, "", err
  239. }
  240. return resp.Body, "", nil
  241. }
  242. // EditReleaseAsset edits a repository release asset.
  243. //
  244. // GitHub API docs: https://developer.github.com/v3/repos/releases/#edit-a-release-asset
  245. func (s *RepositoriesService) EditReleaseAsset(ctx context.Context, owner, repo string, id int64, release *ReleaseAsset) (*ReleaseAsset, *Response, error) {
  246. u := fmt.Sprintf("repos/%s/%s/releases/assets/%d", owner, repo, id)
  247. req, err := s.client.NewRequest("PATCH", u, release)
  248. if err != nil {
  249. return nil, nil, err
  250. }
  251. // TODO: remove custom Accept header when this API fully launches.
  252. req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
  253. asset := new(ReleaseAsset)
  254. resp, err := s.client.Do(ctx, req, asset)
  255. if err != nil {
  256. return nil, resp, err
  257. }
  258. return asset, resp, nil
  259. }
  260. // DeleteReleaseAsset delete a single release asset from a repository.
  261. //
  262. // GitHub API docs: https://developer.github.com/v3/repos/releases/#delete-a-release-asset
  263. func (s *RepositoriesService) DeleteReleaseAsset(ctx context.Context, owner, repo string, id int64) (*Response, error) {
  264. u := fmt.Sprintf("repos/%s/%s/releases/assets/%d", owner, repo, id)
  265. req, err := s.client.NewRequest("DELETE", u, nil)
  266. if err != nil {
  267. return nil, err
  268. }
  269. return s.client.Do(ctx, req, nil)
  270. }
  271. // UploadReleaseAsset creates an asset by uploading a file into a release repository.
  272. // To upload assets that cannot be represented by an os.File, call NewUploadRequest directly.
  273. //
  274. // GitHub API docs: https://developer.github.com/v3/repos/releases/#upload-a-release-asset
  275. func (s *RepositoriesService) UploadReleaseAsset(ctx context.Context, owner, repo string, id int64, opt *UploadOptions, file *os.File) (*ReleaseAsset, *Response, error) {
  276. u := fmt.Sprintf("repos/%s/%s/releases/%d/assets", owner, repo, id)
  277. u, err := addOptions(u, opt)
  278. if err != nil {
  279. return nil, nil, err
  280. }
  281. stat, err := file.Stat()
  282. if err != nil {
  283. return nil, nil, err
  284. }
  285. if stat.IsDir() {
  286. return nil, nil, errors.New("the asset to upload can't be a directory")
  287. }
  288. mediaType := mime.TypeByExtension(filepath.Ext(file.Name()))
  289. req, err := s.client.NewUploadRequest(u, file, stat.Size(), mediaType)
  290. if err != nil {
  291. return nil, nil, err
  292. }
  293. // TODO: remove custom Accept header when this API fully launches.
  294. req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
  295. asset := new(ReleaseAsset)
  296. resp, err := s.client.Do(ctx, req, asset)
  297. if err != nil {
  298. return nil, resp, err
  299. }
  300. return asset, resp, nil
  301. }