gists.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // Copyright 2013 The go-github AUTHORS. All rights reserved.
  2. //
  3. // Use of this source code is governed by BSD-style
  4. // license that can be found in the LICENSE file.
  5. package github
  6. import (
  7. "context"
  8. "fmt"
  9. "time"
  10. )
  11. // GistsService handles communication with the Gist related
  12. // methods of the GitHub API.
  13. //
  14. // GitHub API docs: https://developer.github.com/v3/gists/
  15. type GistsService service
  16. // Gist represents a GitHub's gist.
  17. type Gist struct {
  18. ID *string `json:"id,omitempty"`
  19. Description *string `json:"description,omitempty"`
  20. Public *bool `json:"public,omitempty"`
  21. Owner *User `json:"owner,omitempty"`
  22. Files map[GistFilename]GistFile `json:"files,omitempty"`
  23. Comments *int `json:"comments,omitempty"`
  24. HTMLURL *string `json:"html_url,omitempty"`
  25. GitPullURL *string `json:"git_pull_url,omitempty"`
  26. GitPushURL *string `json:"git_push_url,omitempty"`
  27. CreatedAt *time.Time `json:"created_at,omitempty"`
  28. UpdatedAt *time.Time `json:"updated_at,omitempty"`
  29. NodeID *string `json:"node_id,omitempty"`
  30. }
  31. func (g Gist) String() string {
  32. return Stringify(g)
  33. }
  34. // GistFilename represents filename on a gist.
  35. type GistFilename string
  36. // GistFile represents a file on a gist.
  37. type GistFile struct {
  38. Size *int `json:"size,omitempty"`
  39. Filename *string `json:"filename,omitempty"`
  40. Language *string `json:"language,omitempty"`
  41. Type *string `json:"type,omitempty"`
  42. RawURL *string `json:"raw_url,omitempty"`
  43. Content *string `json:"content,omitempty"`
  44. }
  45. func (g GistFile) String() string {
  46. return Stringify(g)
  47. }
  48. // GistCommit represents a commit on a gist.
  49. type GistCommit struct {
  50. URL *string `json:"url,omitempty"`
  51. Version *string `json:"version,omitempty"`
  52. User *User `json:"user,omitempty"`
  53. ChangeStatus *CommitStats `json:"change_status,omitempty"`
  54. CommittedAt *Timestamp `json:"committed_at,omitempty"`
  55. NodeID *string `json:"node_id,omitempty"`
  56. }
  57. func (gc GistCommit) String() string {
  58. return Stringify(gc)
  59. }
  60. // GistFork represents a fork of a gist.
  61. type GistFork struct {
  62. URL *string `json:"url,omitempty"`
  63. User *User `json:"user,omitempty"`
  64. ID *string `json:"id,omitempty"`
  65. CreatedAt *Timestamp `json:"created_at,omitempty"`
  66. UpdatedAt *Timestamp `json:"updated_at,omitempty"`
  67. NodeID *string `json:"node_id,omitempty"`
  68. }
  69. func (gf GistFork) String() string {
  70. return Stringify(gf)
  71. }
  72. // GistListOptions specifies the optional parameters to the
  73. // GistsService.List, GistsService.ListAll, and GistsService.ListStarred methods.
  74. type GistListOptions struct {
  75. // Since filters Gists by time.
  76. Since time.Time `url:"since,omitempty"`
  77. ListOptions
  78. }
  79. // List gists for a user. Passing the empty string will list
  80. // all public gists if called anonymously. However, if the call
  81. // is authenticated, it will returns all gists for the authenticated
  82. // user.
  83. //
  84. // GitHub API docs: https://developer.github.com/v3/gists/#list-gists
  85. func (s *GistsService) List(ctx context.Context, user string, opt *GistListOptions) ([]*Gist, *Response, error) {
  86. var u string
  87. if user != "" {
  88. u = fmt.Sprintf("users/%v/gists", user)
  89. } else {
  90. u = "gists"
  91. }
  92. u, err := addOptions(u, opt)
  93. if err != nil {
  94. return nil, nil, err
  95. }
  96. req, err := s.client.NewRequest("GET", u, nil)
  97. if err != nil {
  98. return nil, nil, err
  99. }
  100. // TODO: remove custom Accept header when this API fully launches.
  101. req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
  102. var gists []*Gist
  103. resp, err := s.client.Do(ctx, req, &gists)
  104. if err != nil {
  105. return nil, resp, err
  106. }
  107. return gists, resp, nil
  108. }
  109. // ListAll lists all public gists.
  110. //
  111. // GitHub API docs: https://developer.github.com/v3/gists/#list-gists
  112. func (s *GistsService) ListAll(ctx context.Context, opt *GistListOptions) ([]*Gist, *Response, error) {
  113. u, err := addOptions("gists/public", opt)
  114. if err != nil {
  115. return nil, nil, err
  116. }
  117. req, err := s.client.NewRequest("GET", u, nil)
  118. if err != nil {
  119. return nil, nil, err
  120. }
  121. // TODO: remove custom Accept header when this API fully launches.
  122. req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
  123. var gists []*Gist
  124. resp, err := s.client.Do(ctx, req, &gists)
  125. if err != nil {
  126. return nil, resp, err
  127. }
  128. return gists, resp, nil
  129. }
  130. // ListStarred lists starred gists of authenticated user.
  131. //
  132. // GitHub API docs: https://developer.github.com/v3/gists/#list-gists
  133. func (s *GistsService) ListStarred(ctx context.Context, opt *GistListOptions) ([]*Gist, *Response, error) {
  134. u, err := addOptions("gists/starred", opt)
  135. if err != nil {
  136. return nil, nil, err
  137. }
  138. req, err := s.client.NewRequest("GET", u, nil)
  139. if err != nil {
  140. return nil, nil, err
  141. }
  142. // TODO: remove custom Accept header when this API fully launches.
  143. req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
  144. var gists []*Gist
  145. resp, err := s.client.Do(ctx, req, &gists)
  146. if err != nil {
  147. return nil, resp, err
  148. }
  149. return gists, resp, nil
  150. }
  151. // Get a single gist.
  152. //
  153. // GitHub API docs: https://developer.github.com/v3/gists/#get-a-single-gist
  154. func (s *GistsService) Get(ctx context.Context, id string) (*Gist, *Response, error) {
  155. u := fmt.Sprintf("gists/%v", id)
  156. req, err := s.client.NewRequest("GET", u, nil)
  157. if err != nil {
  158. return nil, nil, err
  159. }
  160. // TODO: remove custom Accept header when this API fully launches.
  161. req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
  162. gist := new(Gist)
  163. resp, err := s.client.Do(ctx, req, gist)
  164. if err != nil {
  165. return nil, resp, err
  166. }
  167. return gist, resp, nil
  168. }
  169. // GetRevision gets a specific revision of a gist.
  170. //
  171. // GitHub API docs: https://developer.github.com/v3/gists/#get-a-specific-revision-of-a-gist
  172. func (s *GistsService) GetRevision(ctx context.Context, id, sha string) (*Gist, *Response, error) {
  173. u := fmt.Sprintf("gists/%v/%v", id, sha)
  174. req, err := s.client.NewRequest("GET", u, nil)
  175. if err != nil {
  176. return nil, nil, err
  177. }
  178. // TODO: remove custom Accept header when this API fully launches.
  179. req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
  180. gist := new(Gist)
  181. resp, err := s.client.Do(ctx, req, gist)
  182. if err != nil {
  183. return nil, resp, err
  184. }
  185. return gist, resp, nil
  186. }
  187. // Create a gist for authenticated user.
  188. //
  189. // GitHub API docs: https://developer.github.com/v3/gists/#create-a-gist
  190. func (s *GistsService) Create(ctx context.Context, gist *Gist) (*Gist, *Response, error) {
  191. u := "gists"
  192. req, err := s.client.NewRequest("POST", u, gist)
  193. if err != nil {
  194. return nil, nil, err
  195. }
  196. // TODO: remove custom Accept header when this API fully launches.
  197. req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
  198. g := new(Gist)
  199. resp, err := s.client.Do(ctx, req, g)
  200. if err != nil {
  201. return nil, resp, err
  202. }
  203. return g, resp, nil
  204. }
  205. // Edit a gist.
  206. //
  207. // GitHub API docs: https://developer.github.com/v3/gists/#edit-a-gist
  208. func (s *GistsService) Edit(ctx context.Context, id string, gist *Gist) (*Gist, *Response, error) {
  209. u := fmt.Sprintf("gists/%v", id)
  210. req, err := s.client.NewRequest("PATCH", u, gist)
  211. if err != nil {
  212. return nil, nil, err
  213. }
  214. // TODO: remove custom Accept header when this API fully launches.
  215. req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
  216. g := new(Gist)
  217. resp, err := s.client.Do(ctx, req, g)
  218. if err != nil {
  219. return nil, resp, err
  220. }
  221. return g, resp, nil
  222. }
  223. // ListCommits lists commits of a gist.
  224. //
  225. // GitHub API docs: https://developer.github.com/v3/gists/#list-gist-commits
  226. func (s *GistsService) ListCommits(ctx context.Context, id string, opt *ListOptions) ([]*GistCommit, *Response, error) {
  227. u := fmt.Sprintf("gists/%v/commits", id)
  228. u, err := addOptions(u, opt)
  229. if err != nil {
  230. return nil, nil, err
  231. }
  232. req, err := s.client.NewRequest("GET", u, nil)
  233. if err != nil {
  234. return nil, nil, err
  235. }
  236. // TODO: remove custom Accept header when this API fully launches.
  237. req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
  238. var gistCommits []*GistCommit
  239. resp, err := s.client.Do(ctx, req, &gistCommits)
  240. if err != nil {
  241. return nil, resp, err
  242. }
  243. return gistCommits, resp, nil
  244. }
  245. // Delete a gist.
  246. //
  247. // GitHub API docs: https://developer.github.com/v3/gists/#delete-a-gist
  248. func (s *GistsService) Delete(ctx context.Context, id string) (*Response, error) {
  249. u := fmt.Sprintf("gists/%v", id)
  250. req, err := s.client.NewRequest("DELETE", u, nil)
  251. if err != nil {
  252. return nil, err
  253. }
  254. return s.client.Do(ctx, req, nil)
  255. }
  256. // Star a gist on behalf of authenticated user.
  257. //
  258. // GitHub API docs: https://developer.github.com/v3/gists/#star-a-gist
  259. func (s *GistsService) Star(ctx context.Context, id string) (*Response, error) {
  260. u := fmt.Sprintf("gists/%v/star", id)
  261. req, err := s.client.NewRequest("PUT", u, nil)
  262. if err != nil {
  263. return nil, err
  264. }
  265. return s.client.Do(ctx, req, nil)
  266. }
  267. // Unstar a gist on a behalf of authenticated user.
  268. //
  269. // GitHub API docs: https://developer.github.com/v3/gists/#unstar-a-gist
  270. func (s *GistsService) Unstar(ctx context.Context, id string) (*Response, error) {
  271. u := fmt.Sprintf("gists/%v/star", id)
  272. req, err := s.client.NewRequest("DELETE", u, nil)
  273. if err != nil {
  274. return nil, err
  275. }
  276. return s.client.Do(ctx, req, nil)
  277. }
  278. // IsStarred checks if a gist is starred by authenticated user.
  279. //
  280. // GitHub API docs: https://developer.github.com/v3/gists/#check-if-a-gist-is-starred
  281. func (s *GistsService) IsStarred(ctx context.Context, id string) (bool, *Response, error) {
  282. u := fmt.Sprintf("gists/%v/star", id)
  283. req, err := s.client.NewRequest("GET", u, nil)
  284. if err != nil {
  285. return false, nil, err
  286. }
  287. resp, err := s.client.Do(ctx, req, nil)
  288. starred, err := parseBoolResponse(err)
  289. return starred, resp, err
  290. }
  291. // Fork a gist.
  292. //
  293. // GitHub API docs: https://developer.github.com/v3/gists/#fork-a-gist
  294. func (s *GistsService) Fork(ctx context.Context, id string) (*Gist, *Response, error) {
  295. u := fmt.Sprintf("gists/%v/forks", id)
  296. req, err := s.client.NewRequest("POST", u, nil)
  297. if err != nil {
  298. return nil, nil, err
  299. }
  300. // TODO: remove custom Accept header when this API fully launches.
  301. req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
  302. g := new(Gist)
  303. resp, err := s.client.Do(ctx, req, g)
  304. if err != nil {
  305. return nil, resp, err
  306. }
  307. return g, resp, nil
  308. }
  309. // ListForks lists forks of a gist.
  310. //
  311. // GitHub API docs: https://developer.github.com/v3/gists/#list-gist-forks
  312. func (s *GistsService) ListForks(ctx context.Context, id string) ([]*GistFork, *Response, error) {
  313. u := fmt.Sprintf("gists/%v/forks", id)
  314. req, err := s.client.NewRequest("GET", u, nil)
  315. if err != nil {
  316. return nil, nil, err
  317. }
  318. // TODO: remove custom Accept header when this API fully launches.
  319. req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
  320. var gistForks []*GistFork
  321. resp, err := s.client.Do(ctx, req, &gistForks)
  322. if err != nil {
  323. return nil, resp, err
  324. }
  325. return gistForks, resp, nil
  326. }