CreateCommit creates a new commit in a repository. commit must not be nil. The commit.Committer is optional and will be filled with the commit.Author data if omitted. If the commit.Author is omitted, it will be filled in with the authenticated user’s information and the current date. GitHub API do
(ctx context.Context, owner, repo string, commit Commit, opts *CreateCommitOptions)
| 130 | // |
| 131 | //meta:operation POST /repos/{owner}/{repo}/git/commits |
| 132 | func (s *GitService) CreateCommit(ctx context.Context, owner, repo string, commit Commit, opts *CreateCommitOptions) (*Commit, *Response, error) { |
| 133 | if opts == nil { |
| 134 | opts = &CreateCommitOptions{} |
| 135 | } |
| 136 | |
| 137 | u := fmt.Sprintf("repos/%v/%v/git/commits", owner, repo) |
| 138 | |
| 139 | parents := make([]string, len(commit.Parents)) |
| 140 | for i, parent := range commit.Parents { |
| 141 | parents[i] = *parent.SHA |
| 142 | } |
| 143 | |
| 144 | body := &createCommit{ |
| 145 | Author: commit.Author, |
| 146 | Committer: commit.Committer, |
| 147 | Message: commit.Message, |
| 148 | Parents: parents, |
| 149 | } |
| 150 | if commit.Tree != nil { |
| 151 | body.Tree = commit.Tree.SHA |
| 152 | } |
| 153 | switch { |
| 154 | case commit.Verification != nil: |
| 155 | body.Signature = commit.Verification.Signature |
| 156 | case opts.Signer != nil: |
| 157 | signature, err := createSignature(opts.Signer, body) |
| 158 | if err != nil { |
| 159 | return nil, nil, err |
| 160 | } |
| 161 | body.Signature = &signature |
| 162 | } |
| 163 | |
| 164 | req, err := s.client.NewRequest(ctx, "POST", u, body) |
| 165 | if err != nil { |
| 166 | return nil, nil, err |
| 167 | } |
| 168 | |
| 169 | var c *Commit |
| 170 | resp, err := s.client.Do(req, &c) |
| 171 | if err != nil { |
| 172 | return nil, resp, err |
| 173 | } |
| 174 | |
| 175 | return c, resp, nil |
| 176 | } |
| 177 | |
| 178 | func createSignature(signer MessageSigner, commit *createCommit) (string, error) { |
| 179 | if signer == nil { |