本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

359 lines
9.5KB

  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. var gists []*Gist
  101. resp, err := s.client.Do(ctx, req, &gists)
  102. if err != nil {
  103. return nil, resp, err
  104. }
  105. return gists, resp, nil
  106. }
  107. // ListAll lists all public gists.
  108. //
  109. // GitHub API docs: https://developer.github.com/v3/gists/#list-gists
  110. func (s *GistsService) ListAll(ctx context.Context, opt *GistListOptions) ([]*Gist, *Response, error) {
  111. u, err := addOptions("gists/public", opt)
  112. if err != nil {
  113. return nil, nil, err
  114. }
  115. req, err := s.client.NewRequest("GET", u, nil)
  116. if err != nil {
  117. return nil, nil, err
  118. }
  119. var gists []*Gist
  120. resp, err := s.client.Do(ctx, req, &gists)
  121. if err != nil {
  122. return nil, resp, err
  123. }
  124. return gists, resp, nil
  125. }
  126. // ListStarred lists starred gists of authenticated user.
  127. //
  128. // GitHub API docs: https://developer.github.com/v3/gists/#list-gists
  129. func (s *GistsService) ListStarred(ctx context.Context, opt *GistListOptions) ([]*Gist, *Response, error) {
  130. u, err := addOptions("gists/starred", opt)
  131. if err != nil {
  132. return nil, nil, err
  133. }
  134. req, err := s.client.NewRequest("GET", u, nil)
  135. if err != nil {
  136. return nil, nil, err
  137. }
  138. var gists []*Gist
  139. resp, err := s.client.Do(ctx, req, &gists)
  140. if err != nil {
  141. return nil, resp, err
  142. }
  143. return gists, resp, nil
  144. }
  145. // Get a single gist.
  146. //
  147. // GitHub API docs: https://developer.github.com/v3/gists/#get-a-single-gist
  148. func (s *GistsService) Get(ctx context.Context, id string) (*Gist, *Response, error) {
  149. u := fmt.Sprintf("gists/%v", id)
  150. req, err := s.client.NewRequest("GET", u, nil)
  151. if err != nil {
  152. return nil, nil, err
  153. }
  154. gist := new(Gist)
  155. resp, err := s.client.Do(ctx, req, gist)
  156. if err != nil {
  157. return nil, resp, err
  158. }
  159. return gist, resp, nil
  160. }
  161. // GetRevision gets a specific revision of a gist.
  162. //
  163. // GitHub API docs: https://developer.github.com/v3/gists/#get-a-specific-revision-of-a-gist
  164. func (s *GistsService) GetRevision(ctx context.Context, id, sha string) (*Gist, *Response, error) {
  165. u := fmt.Sprintf("gists/%v/%v", id, sha)
  166. req, err := s.client.NewRequest("GET", u, nil)
  167. if err != nil {
  168. return nil, nil, err
  169. }
  170. gist := new(Gist)
  171. resp, err := s.client.Do(ctx, req, gist)
  172. if err != nil {
  173. return nil, resp, err
  174. }
  175. return gist, resp, nil
  176. }
  177. // Create a gist for authenticated user.
  178. //
  179. // GitHub API docs: https://developer.github.com/v3/gists/#create-a-gist
  180. func (s *GistsService) Create(ctx context.Context, gist *Gist) (*Gist, *Response, error) {
  181. u := "gists"
  182. req, err := s.client.NewRequest("POST", u, gist)
  183. if err != nil {
  184. return nil, nil, err
  185. }
  186. g := new(Gist)
  187. resp, err := s.client.Do(ctx, req, g)
  188. if err != nil {
  189. return nil, resp, err
  190. }
  191. return g, resp, nil
  192. }
  193. // Edit a gist.
  194. //
  195. // GitHub API docs: https://developer.github.com/v3/gists/#edit-a-gist
  196. func (s *GistsService) Edit(ctx context.Context, id string, gist *Gist) (*Gist, *Response, error) {
  197. u := fmt.Sprintf("gists/%v", id)
  198. req, err := s.client.NewRequest("PATCH", u, gist)
  199. if err != nil {
  200. return nil, nil, err
  201. }
  202. g := new(Gist)
  203. resp, err := s.client.Do(ctx, req, g)
  204. if err != nil {
  205. return nil, resp, err
  206. }
  207. return g, resp, nil
  208. }
  209. // ListCommits lists commits of a gist.
  210. //
  211. // GitHub API docs: https://developer.github.com/v3/gists/#list-gist-commits
  212. func (s *GistsService) ListCommits(ctx context.Context, id string, opt *ListOptions) ([]*GistCommit, *Response, error) {
  213. u := fmt.Sprintf("gists/%v/commits", id)
  214. u, err := addOptions(u, opt)
  215. if err != nil {
  216. return nil, nil, err
  217. }
  218. req, err := s.client.NewRequest("GET", u, nil)
  219. if err != nil {
  220. return nil, nil, err
  221. }
  222. var gistCommits []*GistCommit
  223. resp, err := s.client.Do(ctx, req, &gistCommits)
  224. if err != nil {
  225. return nil, resp, err
  226. }
  227. return gistCommits, resp, nil
  228. }
  229. // Delete a gist.
  230. //
  231. // GitHub API docs: https://developer.github.com/v3/gists/#delete-a-gist
  232. func (s *GistsService) Delete(ctx context.Context, id string) (*Response, error) {
  233. u := fmt.Sprintf("gists/%v", id)
  234. req, err := s.client.NewRequest("DELETE", u, nil)
  235. if err != nil {
  236. return nil, err
  237. }
  238. return s.client.Do(ctx, req, nil)
  239. }
  240. // Star a gist on behalf of authenticated user.
  241. //
  242. // GitHub API docs: https://developer.github.com/v3/gists/#star-a-gist
  243. func (s *GistsService) Star(ctx context.Context, id string) (*Response, error) {
  244. u := fmt.Sprintf("gists/%v/star", id)
  245. req, err := s.client.NewRequest("PUT", u, nil)
  246. if err != nil {
  247. return nil, err
  248. }
  249. return s.client.Do(ctx, req, nil)
  250. }
  251. // Unstar a gist on a behalf of authenticated user.
  252. //
  253. // GitHub API docs: https://developer.github.com/v3/gists/#unstar-a-gist
  254. func (s *GistsService) Unstar(ctx context.Context, id string) (*Response, error) {
  255. u := fmt.Sprintf("gists/%v/star", id)
  256. req, err := s.client.NewRequest("DELETE", u, nil)
  257. if err != nil {
  258. return nil, err
  259. }
  260. return s.client.Do(ctx, req, nil)
  261. }
  262. // IsStarred checks if a gist is starred by authenticated user.
  263. //
  264. // GitHub API docs: https://developer.github.com/v3/gists/#check-if-a-gist-is-starred
  265. func (s *GistsService) IsStarred(ctx context.Context, id string) (bool, *Response, error) {
  266. u := fmt.Sprintf("gists/%v/star", id)
  267. req, err := s.client.NewRequest("GET", u, nil)
  268. if err != nil {
  269. return false, nil, err
  270. }
  271. resp, err := s.client.Do(ctx, req, nil)
  272. starred, err := parseBoolResponse(err)
  273. return starred, resp, err
  274. }
  275. // Fork a gist.
  276. //
  277. // GitHub API docs: https://developer.github.com/v3/gists/#fork-a-gist
  278. func (s *GistsService) Fork(ctx context.Context, id string) (*Gist, *Response, error) {
  279. u := fmt.Sprintf("gists/%v/forks", id)
  280. req, err := s.client.NewRequest("POST", u, nil)
  281. if err != nil {
  282. return nil, nil, err
  283. }
  284. g := new(Gist)
  285. resp, err := s.client.Do(ctx, req, g)
  286. if err != nil {
  287. return nil, resp, err
  288. }
  289. return g, resp, nil
  290. }
  291. // ListForks lists forks of a gist.
  292. //
  293. // GitHub API docs: https://developer.github.com/v3/gists/#list-gist-forks
  294. func (s *GistsService) ListForks(ctx context.Context, id string) ([]*GistFork, *Response, error) {
  295. u := fmt.Sprintf("gists/%v/forks", id)
  296. req, err := s.client.NewRequest("GET", u, nil)
  297. if err != nil {
  298. return nil, nil, err
  299. }
  300. var gistForks []*GistFork
  301. resp, err := s.client.Do(ctx, req, &gistForks)
  302. if err != nil {
  303. return nil, resp, err
  304. }
  305. return gistForks, resp, nil
  306. }
上海开阖软件有限公司 沪ICP备12045867号-1