1
2
3
4
5
6 package github
7
8 import (
9 "bytes"
10 "context"
11 "fmt"
12 "time"
13 )
14
15
16
17
18 type RepositoryCommit struct {
19 NodeID *string `json:"node_id,omitempty"`
20 SHA *string `json:"sha,omitempty"`
21 Commit *Commit `json:"commit,omitempty"`
22 Author *User `json:"author,omitempty"`
23 Committer *User `json:"committer,omitempty"`
24 Parents []*Commit `json:"parents,omitempty"`
25 HTMLURL *string `json:"html_url,omitempty"`
26 URL *string `json:"url,omitempty"`
27 CommentsURL *string `json:"comments_url,omitempty"`
28
29
30 Stats *CommitStats `json:"stats,omitempty"`
31
32 Files []*CommitFile `json:"files,omitempty"`
33 }
34
35 func (r RepositoryCommit) String() string {
36 return Stringify(r)
37 }
38
39
40 type CommitStats struct {
41 Additions *int `json:"additions,omitempty"`
42 Deletions *int `json:"deletions,omitempty"`
43 Total *int `json:"total,omitempty"`
44 }
45
46 func (c CommitStats) String() string {
47 return Stringify(c)
48 }
49
50
51 type CommitFile struct {
52 SHA *string `json:"sha,omitempty"`
53 Filename *string `json:"filename,omitempty"`
54 Additions *int `json:"additions,omitempty"`
55 Deletions *int `json:"deletions,omitempty"`
56 Changes *int `json:"changes,omitempty"`
57 Status *string `json:"status,omitempty"`
58 Patch *string `json:"patch,omitempty"`
59 BlobURL *string `json:"blob_url,omitempty"`
60 RawURL *string `json:"raw_url,omitempty"`
61 ContentsURL *string `json:"contents_url,omitempty"`
62 PreviousFilename *string `json:"previous_filename,omitempty"`
63 }
64
65 func (c CommitFile) String() string {
66 return Stringify(c)
67 }
68
69
70
71 type CommitsComparison struct {
72 BaseCommit *RepositoryCommit `json:"base_commit,omitempty"`
73 MergeBaseCommit *RepositoryCommit `json:"merge_base_commit,omitempty"`
74
75
76 Status *string `json:"status,omitempty"`
77 AheadBy *int `json:"ahead_by,omitempty"`
78 BehindBy *int `json:"behind_by,omitempty"`
79 TotalCommits *int `json:"total_commits,omitempty"`
80
81 Commits []*RepositoryCommit `json:"commits,omitempty"`
82
83 Files []*CommitFile `json:"files,omitempty"`
84
85 HTMLURL *string `json:"html_url,omitempty"`
86 PermalinkURL *string `json:"permalink_url,omitempty"`
87 DiffURL *string `json:"diff_url,omitempty"`
88 PatchURL *string `json:"patch_url,omitempty"`
89 URL *string `json:"url,omitempty"`
90 }
91
92 func (c CommitsComparison) String() string {
93 return Stringify(c)
94 }
95
96
97
98 type CommitsListOptions struct {
99
100 SHA string `url:"sha,omitempty"`
101
102
103 Path string `url:"path,omitempty"`
104
105
106 Author string `url:"author,omitempty"`
107
108
109 Since time.Time `url:"since,omitempty"`
110
111
112 Until time.Time `url:"until,omitempty"`
113
114 ListOptions
115 }
116
117
118 type BranchCommit struct {
119 Name *string `json:"name,omitempty"`
120 Commit *Commit `json:"commit,omitempty"`
121 Protected *bool `json:"protected,omitempty"`
122 }
123
124
125
126
127 func (s *RepositoriesService) ListCommits(ctx context.Context, owner, repo string, opts *CommitsListOptions) ([]*RepositoryCommit, *Response, error) {
128 u := fmt.Sprintf("repos/%v/%v/commits", owner, repo)
129 u, err := addOptions(u, opts)
130 if err != nil {
131 return nil, nil, err
132 }
133
134 req, err := s.client.NewRequest("GET", u, nil)
135 if err != nil {
136 return nil, nil, err
137 }
138
139 var commits []*RepositoryCommit
140 resp, err := s.client.Do(ctx, req, &commits)
141 if err != nil {
142 return nil, resp, err
143 }
144
145 return commits, resp, nil
146 }
147
148
149
150
151
152 func (s *RepositoriesService) GetCommit(ctx context.Context, owner, repo, sha string) (*RepositoryCommit, *Response, error) {
153 u := fmt.Sprintf("repos/%v/%v/commits/%v", owner, repo, sha)
154
155 req, err := s.client.NewRequest("GET", u, nil)
156 if err != nil {
157 return nil, nil, err
158 }
159
160 commit := new(RepositoryCommit)
161 resp, err := s.client.Do(ctx, req, commit)
162 if err != nil {
163 return nil, resp, err
164 }
165
166 return commit, resp, nil
167 }
168
169
170
171
172 func (s *RepositoriesService) GetCommitRaw(ctx context.Context, owner string, repo string, sha string, opts RawOptions) (string, *Response, error) {
173 u := fmt.Sprintf("repos/%v/%v/commits/%v", owner, repo, sha)
174 req, err := s.client.NewRequest("GET", u, nil)
175 if err != nil {
176 return "", nil, err
177 }
178
179 switch opts.Type {
180 case Diff:
181 req.Header.Set("Accept", mediaTypeV3Diff)
182 case Patch:
183 req.Header.Set("Accept", mediaTypeV3Patch)
184 default:
185 return "", nil, fmt.Errorf("unsupported raw type %d", opts.Type)
186 }
187
188 var buf bytes.Buffer
189 resp, err := s.client.Do(ctx, req, &buf)
190 if err != nil {
191 return "", resp, err
192 }
193
194 return buf.String(), resp, nil
195 }
196
197
198
199
200
201 func (s *RepositoriesService) GetCommitSHA1(ctx context.Context, owner, repo, ref, lastSHA string) (string, *Response, error) {
202 u := fmt.Sprintf("repos/%v/%v/commits/%v", owner, repo, refURLEscape(ref))
203
204 req, err := s.client.NewRequest("GET", u, nil)
205 if err != nil {
206 return "", nil, err
207 }
208 if lastSHA != "" {
209 req.Header.Set("If-None-Match", `"`+lastSHA+`"`)
210 }
211
212 req.Header.Set("Accept", mediaTypeV3SHA)
213
214 var buf bytes.Buffer
215 resp, err := s.client.Do(ctx, req, &buf)
216 if err != nil {
217 return "", resp, err
218 }
219
220 return buf.String(), resp, nil
221 }
222
223
224
225
226 func (s *RepositoriesService) CompareCommits(ctx context.Context, owner, repo string, base, head string) (*CommitsComparison, *Response, error) {
227 u := fmt.Sprintf("repos/%v/%v/compare/%v...%v", owner, repo, base, head)
228
229 req, err := s.client.NewRequest("GET", u, nil)
230 if err != nil {
231 return nil, nil, err
232 }
233
234 comp := new(CommitsComparison)
235 resp, err := s.client.Do(ctx, req, comp)
236 if err != nil {
237 return nil, resp, err
238 }
239
240 return comp, resp, nil
241 }
242
243
244
245
246
247
248
249
250 func (s *RepositoriesService) CompareCommitsRaw(ctx context.Context, owner, repo, base, head string, opts RawOptions) (string, *Response, error) {
251 u := fmt.Sprintf("repos/%v/%v/compare/%v...%v", owner, repo, base, head)
252 req, err := s.client.NewRequest("GET", u, nil)
253 if err != nil {
254 return "", nil, err
255 }
256
257 switch opts.Type {
258 case Diff:
259 req.Header.Set("Accept", mediaTypeV3Diff)
260 case Patch:
261 req.Header.Set("Accept", mediaTypeV3Patch)
262 default:
263 return "", nil, fmt.Errorf("unsupported raw type %d", opts.Type)
264 }
265
266 var buf bytes.Buffer
267 resp, err := s.client.Do(ctx, req, &buf)
268 if err != nil {
269 return "", resp, err
270 }
271
272 return buf.String(), resp, nil
273 }
274
275
276
277
278
279 func (s *RepositoriesService) ListBranchesHeadCommit(ctx context.Context, owner, repo, sha string) ([]*BranchCommit, *Response, error) {
280 u := fmt.Sprintf("repos/%v/%v/commits/%v/branches-where-head", owner, repo, sha)
281
282 req, err := s.client.NewRequest("GET", u, nil)
283 if err != nil {
284 return nil, nil, err
285 }
286
287
288 req.Header.Set("Accept", mediaTypeListPullsOrBranchesForCommitPreview)
289 var branchCommits []*BranchCommit
290 resp, err := s.client.Do(ctx, req, &branchCommits)
291 if err != nil {
292 return nil, resp, err
293 }
294
295 return branchCommits, resp, nil
296 }
297
View as plain text