本站源代码
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.

234 lines
7.3KB

  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. "bytes"
  8. "context"
  9. "fmt"
  10. "net/url"
  11. "time"
  12. )
  13. // RepositoryCommit represents a commit in a repo.
  14. // Note that it's wrapping a Commit, so author/committer information is in two places,
  15. // but contain different details about them: in RepositoryCommit "github details", in Commit - "git details".
  16. type RepositoryCommit struct {
  17. SHA *string `json:"sha,omitempty"`
  18. Commit *Commit `json:"commit,omitempty"`
  19. Author *User `json:"author,omitempty"`
  20. Committer *User `json:"committer,omitempty"`
  21. Parents []Commit `json:"parents,omitempty"`
  22. HTMLURL *string `json:"html_url,omitempty"`
  23. URL *string `json:"url,omitempty"`
  24. CommentsURL *string `json:"comments_url,omitempty"`
  25. // Details about how many changes were made in this commit. Only filled in during GetCommit!
  26. Stats *CommitStats `json:"stats,omitempty"`
  27. // Details about which files, and how this commit touched. Only filled in during GetCommit!
  28. Files []CommitFile `json:"files,omitempty"`
  29. }
  30. func (r RepositoryCommit) String() string {
  31. return Stringify(r)
  32. }
  33. // CommitStats represents the number of additions / deletions from a file in a given RepositoryCommit or GistCommit.
  34. type CommitStats struct {
  35. Additions *int `json:"additions,omitempty"`
  36. Deletions *int `json:"deletions,omitempty"`
  37. Total *int `json:"total,omitempty"`
  38. }
  39. func (c CommitStats) String() string {
  40. return Stringify(c)
  41. }
  42. // CommitFile represents a file modified in a commit.
  43. type CommitFile struct {
  44. SHA *string `json:"sha,omitempty"`
  45. Filename *string `json:"filename,omitempty"`
  46. Additions *int `json:"additions,omitempty"`
  47. Deletions *int `json:"deletions,omitempty"`
  48. Changes *int `json:"changes,omitempty"`
  49. Status *string `json:"status,omitempty"`
  50. Patch *string `json:"patch,omitempty"`
  51. BlobURL *string `json:"blob_url,omitempty"`
  52. RawURL *string `json:"raw_url,omitempty"`
  53. ContentsURL *string `json:"contents_url,omitempty"`
  54. PreviousFilename *string `json:"previous_filename,omitempty"`
  55. }
  56. func (c CommitFile) String() string {
  57. return Stringify(c)
  58. }
  59. // CommitsComparison is the result of comparing two commits.
  60. // See CompareCommits() for details.
  61. type CommitsComparison struct {
  62. BaseCommit *RepositoryCommit `json:"base_commit,omitempty"`
  63. MergeBaseCommit *RepositoryCommit `json:"merge_base_commit,omitempty"`
  64. // Head can be 'behind' or 'ahead'
  65. Status *string `json:"status,omitempty"`
  66. AheadBy *int `json:"ahead_by,omitempty"`
  67. BehindBy *int `json:"behind_by,omitempty"`
  68. TotalCommits *int `json:"total_commits,omitempty"`
  69. Commits []RepositoryCommit `json:"commits,omitempty"`
  70. Files []CommitFile `json:"files,omitempty"`
  71. HTMLURL *string `json:"html_url,omitempty"`
  72. PermalinkURL *string `json:"permalink_url,omitempty"`
  73. DiffURL *string `json:"diff_url,omitempty"`
  74. PatchURL *string `json:"patch_url,omitempty"`
  75. URL *string `json:"url,omitempty"` // API URL.
  76. }
  77. func (c CommitsComparison) String() string {
  78. return Stringify(c)
  79. }
  80. // CommitsListOptions specifies the optional parameters to the
  81. // RepositoriesService.ListCommits method.
  82. type CommitsListOptions struct {
  83. // SHA or branch to start listing Commits from.
  84. SHA string `url:"sha,omitempty"`
  85. // Path that should be touched by the returned Commits.
  86. Path string `url:"path,omitempty"`
  87. // Author of by which to filter Commits.
  88. Author string `url:"author,omitempty"`
  89. // Since when should Commits be included in the response.
  90. Since time.Time `url:"since,omitempty"`
  91. // Until when should Commits be included in the response.
  92. Until time.Time `url:"until,omitempty"`
  93. ListOptions
  94. }
  95. // ListCommits lists the commits of a repository.
  96. //
  97. // GitHub API docs: https://developer.github.com/v3/repos/commits/#list
  98. func (s *RepositoriesService) ListCommits(ctx context.Context, owner, repo string, opt *CommitsListOptions) ([]*RepositoryCommit, *Response, error) {
  99. u := fmt.Sprintf("repos/%v/%v/commits", owner, repo)
  100. u, err := addOptions(u, opt)
  101. if err != nil {
  102. return nil, nil, err
  103. }
  104. req, err := s.client.NewRequest("GET", u, nil)
  105. if err != nil {
  106. return nil, nil, err
  107. }
  108. var commits []*RepositoryCommit
  109. resp, err := s.client.Do(ctx, req, &commits)
  110. if err != nil {
  111. return nil, resp, err
  112. }
  113. return commits, resp, nil
  114. }
  115. // GetCommit fetches the specified commit, including all details about it.
  116. //
  117. // GitHub API docs: https://developer.github.com/v3/repos/commits/#get-a-single-commit
  118. // See also: https://developer.github.com/v3/git/commits/#get-a-single-commit provides the same functionality
  119. func (s *RepositoriesService) GetCommit(ctx context.Context, owner, repo, sha string) (*RepositoryCommit, *Response, error) {
  120. u := fmt.Sprintf("repos/%v/%v/commits/%v", owner, repo, sha)
  121. req, err := s.client.NewRequest("GET", u, nil)
  122. if err != nil {
  123. return nil, nil, err
  124. }
  125. commit := new(RepositoryCommit)
  126. resp, err := s.client.Do(ctx, req, commit)
  127. if err != nil {
  128. return nil, resp, err
  129. }
  130. return commit, resp, nil
  131. }
  132. // GetCommitRaw fetches the specified commit in raw (diff or patch) format.
  133. func (s *RepositoriesService) GetCommitRaw(ctx context.Context, owner string, repo string, sha string, opt RawOptions) (string, *Response, error) {
  134. u := fmt.Sprintf("repos/%v/%v/commits/%v", owner, repo, sha)
  135. req, err := s.client.NewRequest("GET", u, nil)
  136. if err != nil {
  137. return "", nil, err
  138. }
  139. switch opt.Type {
  140. case Diff:
  141. req.Header.Set("Accept", mediaTypeV3Diff)
  142. case Patch:
  143. req.Header.Set("Accept", mediaTypeV3Patch)
  144. default:
  145. return "", nil, fmt.Errorf("unsupported raw type %d", opt.Type)
  146. }
  147. var buf bytes.Buffer
  148. resp, err := s.client.Do(ctx, req, &buf)
  149. if err != nil {
  150. return "", resp, err
  151. }
  152. return buf.String(), resp, nil
  153. }
  154. // GetCommitSHA1 gets the SHA-1 of a commit reference. If a last-known SHA1 is
  155. // supplied and no new commits have occurred, a 304 Unmodified response is returned.
  156. //
  157. // GitHub API docs: https://developer.github.com/v3/repos/commits/#get-the-sha-1-of-a-commit-reference
  158. func (s *RepositoriesService) GetCommitSHA1(ctx context.Context, owner, repo, ref, lastSHA string) (string, *Response, error) {
  159. u := fmt.Sprintf("repos/%v/%v/commits/%v", owner, repo, url.QueryEscape(ref))
  160. req, err := s.client.NewRequest("GET", u, nil)
  161. if err != nil {
  162. return "", nil, err
  163. }
  164. if lastSHA != "" {
  165. req.Header.Set("If-None-Match", `"`+lastSHA+`"`)
  166. }
  167. req.Header.Set("Accept", mediaTypeV3SHA)
  168. var buf bytes.Buffer
  169. resp, err := s.client.Do(ctx, req, &buf)
  170. if err != nil {
  171. return "", resp, err
  172. }
  173. return buf.String(), resp, nil
  174. }
  175. // CompareCommits compares a range of commits with each other.
  176. // todo: support media formats - https://github.com/google/go-github/issues/6
  177. //
  178. // GitHub API docs: https://developer.github.com/v3/repos/commits/#compare-two-commits
  179. func (s *RepositoriesService) CompareCommits(ctx context.Context, owner, repo string, base, head string) (*CommitsComparison, *Response, error) {
  180. u := fmt.Sprintf("repos/%v/%v/compare/%v...%v", owner, repo, base, head)
  181. req, err := s.client.NewRequest("GET", u, nil)
  182. if err != nil {
  183. return nil, nil, err
  184. }
  185. comp := new(CommitsComparison)
  186. resp, err := s.client.Do(ctx, req, comp)
  187. if err != nil {
  188. return nil, resp, err
  189. }
  190. return comp, resp, nil
  191. }
上海开阖软件有限公司 沪ICP备12045867号-1