git_commits.go 4.4 KB

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