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

136 lines
4.2KB

  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. "fmt"
  9. "time"
  10. )
  11. // SignatureVerification represents GPG signature verification.
  12. type SignatureVerification struct {
  13. Verified *bool `json:"verified,omitempty"`
  14. Reason *string `json:"reason,omitempty"`
  15. Signature *string `json:"signature,omitempty"`
  16. Payload *string `json:"payload,omitempty"`
  17. }
  18. // Commit represents a GitHub commit.
  19. type Commit struct {
  20. SHA *string `json:"sha,omitempty"`
  21. Author *CommitAuthor `json:"author,omitempty"`
  22. Committer *CommitAuthor `json:"committer,omitempty"`
  23. Message *string `json:"message,omitempty"`
  24. Tree *Tree `json:"tree,omitempty"`
  25. Parents []Commit `json:"parents,omitempty"`
  26. Stats *CommitStats `json:"stats,omitempty"`
  27. HTMLURL *string `json:"html_url,omitempty"`
  28. URL *string `json:"url,omitempty"`
  29. Verification *SignatureVerification `json:"verification,omitempty"`
  30. NodeID *string `json:"node_id,omitempty"`
  31. // CommentCount is the number of GitHub comments on the commit. This
  32. // is only populated for requests that fetch GitHub data like
  33. // Pulls.ListCommits, Repositories.ListCommits, etc.
  34. CommentCount *int `json:"comment_count,omitempty"`
  35. }
  36. func (c Commit) String() string {
  37. return Stringify(c)
  38. }
  39. // CommitAuthor represents the author or committer of a commit. The commit
  40. // author may not correspond to a GitHub User.
  41. type CommitAuthor struct {
  42. Date *time.Time `json:"date,omitempty"`
  43. Name *string `json:"name,omitempty"`
  44. Email *string `json:"email,omitempty"`
  45. // The following fields are only populated by Webhook events.
  46. Login *string `json:"username,omitempty"` // Renamed for go-github consistency.
  47. }
  48. func (c CommitAuthor) String() string {
  49. return Stringify(c)
  50. }
  51. // GetCommit fetches the Commit object for a given SHA.
  52. //
  53. // GitHub API docs: https://developer.github.com/v3/git/commits/#get-a-commit
  54. func (s *GitService) GetCommit(ctx context.Context, owner string, repo string, sha string) (*Commit, *Response, error) {
  55. u := fmt.Sprintf("repos/%v/%v/git/commits/%v", owner, repo, sha)
  56. req, err := s.client.NewRequest("GET", u, nil)
  57. if err != nil {
  58. return nil, nil, err
  59. }
  60. c := new(Commit)
  61. resp, err := s.client.Do(ctx, req, c)
  62. if err != nil {
  63. return nil, resp, err
  64. }
  65. return c, resp, nil
  66. }
  67. // createCommit represents the body of a CreateCommit request.
  68. type createCommit struct {
  69. Author *CommitAuthor `json:"author,omitempty"`
  70. Committer *CommitAuthor `json:"committer,omitempty"`
  71. Message *string `json:"message,omitempty"`
  72. Tree *string `json:"tree,omitempty"`
  73. Parents []string `json:"parents,omitempty"`
  74. Signature *string `json:"signature,omitempty"`
  75. }
  76. // CreateCommit creates a new commit in a repository.
  77. // commit must not be nil.
  78. //
  79. // The commit.Committer is optional and will be filled with the commit.Author
  80. // data if omitted. If the commit.Author is omitted, it will be filled in with
  81. // the authenticated user’s information and the current date.
  82. //
  83. // GitHub API docs: https://developer.github.com/v3/git/commits/#create-a-commit
  84. func (s *GitService) CreateCommit(ctx context.Context, owner string, repo string, commit *Commit) (*Commit, *Response, error) {
  85. if commit == nil {
  86. return nil, nil, fmt.Errorf("commit must be provided")
  87. }
  88. u := fmt.Sprintf("repos/%v/%v/git/commits", owner, repo)
  89. parents := make([]string, len(commit.Parents))
  90. for i, parent := range commit.Parents {
  91. parents[i] = *parent.SHA
  92. }
  93. body := &createCommit{
  94. Author: commit.Author,
  95. Committer: commit.Committer,
  96. Message: commit.Message,
  97. Parents: parents,
  98. }
  99. if commit.Tree != nil {
  100. body.Tree = commit.Tree.SHA
  101. }
  102. if commit.Verification != nil {
  103. body.Signature = commit.Verification.Signature
  104. }
  105. req, err := s.client.NewRequest("POST", u, body)
  106. if err != nil {
  107. return nil, nil, err
  108. }
  109. c := new(Commit)
  110. resp, err := s.client.Do(ctx, req, c)
  111. if err != nil {
  112. return nil, resp, err
  113. }
  114. return c, resp, nil
  115. }
上海开阖软件有限公司 沪ICP备12045867号-1