123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- package github
- import (
- "context"
- "fmt"
- "strings"
- "time"
- )
- type SignatureVerification struct {
- Verified *bool `json:"verified,omitempty"`
- Reason *string `json:"reason,omitempty"`
- Signature *string `json:"signature,omitempty"`
- Payload *string `json:"payload,omitempty"`
- }
- type Commit struct {
- SHA *string `json:"sha,omitempty"`
- Author *CommitAuthor `json:"author,omitempty"`
- Committer *CommitAuthor `json:"committer,omitempty"`
- Message *string `json:"message,omitempty"`
- Tree *Tree `json:"tree,omitempty"`
- Parents []Commit `json:"parents,omitempty"`
- Stats *CommitStats `json:"stats,omitempty"`
- HTMLURL *string `json:"html_url,omitempty"`
- URL *string `json:"url,omitempty"`
- Verification *SignatureVerification `json:"verification,omitempty"`
- NodeID *string `json:"node_id,omitempty"`
-
-
-
- CommentCount *int `json:"comment_count,omitempty"`
- }
- func (c Commit) String() string {
- return Stringify(c)
- }
- type CommitAuthor struct {
- Date *time.Time `json:"date,omitempty"`
- Name *string `json:"name,omitempty"`
- Email *string `json:"email,omitempty"`
-
- Login *string `json:"username,omitempty"`
- }
- func (c CommitAuthor) String() string {
- return Stringify(c)
- }
- func (s *GitService) GetCommit(ctx context.Context, owner string, repo string, sha string) (*Commit, *Response, error) {
- u := fmt.Sprintf("repos/%v/%v/git/commits/%v", owner, repo, sha)
- req, err := s.client.NewRequest("GET", u, nil)
- if err != nil {
- return nil, nil, err
- }
-
- acceptHeaders := []string{mediaTypeGitSigningPreview, mediaTypeGraphQLNodeIDPreview}
- req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
- c := new(Commit)
- resp, err := s.client.Do(ctx, req, c)
- if err != nil {
- return nil, resp, err
- }
- return c, resp, nil
- }
- type createCommit struct {
- Author *CommitAuthor `json:"author,omitempty"`
- Committer *CommitAuthor `json:"committer,omitempty"`
- Message *string `json:"message,omitempty"`
- Tree *string `json:"tree,omitempty"`
- Parents []string `json:"parents,omitempty"`
- }
- func (s *GitService) CreateCommit(ctx context.Context, owner string, repo string, commit *Commit) (*Commit, *Response, error) {
- if commit == nil {
- return nil, nil, fmt.Errorf("commit must be provided")
- }
- u := fmt.Sprintf("repos/%v/%v/git/commits", owner, repo)
- parents := make([]string, len(commit.Parents))
- for i, parent := range commit.Parents {
- parents[i] = *parent.SHA
- }
- body := &createCommit{
- Author: commit.Author,
- Committer: commit.Committer,
- Message: commit.Message,
- Parents: parents,
- }
- if commit.Tree != nil {
- body.Tree = commit.Tree.SHA
- }
- req, err := s.client.NewRequest("POST", u, body)
- if err != nil {
- return nil, nil, err
- }
-
- req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
- c := new(Commit)
- resp, err := s.client.Do(ctx, req, c)
- if err != nil {
- return nil, resp, err
- }
- return c, resp, nil
- }
|