@@ -21,6 +21,7 @@ type SearchService service
21
21
type SearchOptions struct {
22
22
// How to sort the search results. Possible values are:
23
23
// - for repositories: stars, fork, updated
24
+ // - for commits: author-date, committer-date
24
25
// - for code: indexed
25
26
// - for issues: comments, created, updated
26
27
// - for users: followers, repositories, joined
@@ -54,6 +55,37 @@ func (s *SearchService) Repositories(query string, opt *SearchOptions) (*Reposit
54
55
return result , resp , err
55
56
}
56
57
58
+ // CommitsSearchResult represents the result of a commits search.
59
+ type CommitsSearchResult struct {
60
+ Total * int `json:"total_count,omitempty"`
61
+ IncompleteResults * bool `json:"incomplete_results,omitempty"`
62
+ Commits []* CommitResult `json:"items,omitempty"`
63
+ }
64
+
65
+ // CommitResult represents a commit object as returned in commit search endpoint response.
66
+ type CommitResult struct {
67
+ Hash * string `json:"hash,omitempty"`
68
+ Message * string `json:"message,omitempty"`
69
+ AuthorID * int `json:"author_id,omitempty"`
70
+ AuthorName * string `json:"author_name,omitempty"`
71
+ AuthorEmail * string `json:"author_email,omitempty"`
72
+ AuthorDate * Timestamp `json:"author_date,omitempty"`
73
+ CommitterID * int `json:"committer_id,omitempty"`
74
+ CommitterName * string `json:"committer_name,omitempty"`
75
+ CommitterEmail * string `json:"committer_email,omitempty"`
76
+ CommitterDate * Timestamp `json:"committer_date,omitempty"`
77
+ Repository * Repository `json:"repository,omitempty"`
78
+ }
79
+
80
+ // Commits searches commits via various criteria.
81
+ //
82
+ // GitHub API Docs: https://developer.github.com/v3/search/#search-commits
83
+ func (s * SearchService ) Commits (query string , opt * SearchOptions ) (* CommitsSearchResult , * Response , error ) {
84
+ result := new (CommitsSearchResult )
85
+ resp , err := s .search ("commits" , query , opt , result )
86
+ return result , resp , err
87
+ }
88
+
57
89
// IssuesSearchResult represents the result of an issues search.
58
90
type IssuesSearchResult struct {
59
91
Total * int `json:"total_count,omitempty"`
@@ -136,7 +168,7 @@ func (s *SearchService) Code(query string, opt *SearchOptions) (*CodeSearchResul
136
168
}
137
169
138
170
// Helper function that executes search queries against different
139
- // GitHub search types (repositories, code, issues, users)
171
+ // GitHub search types (repositories, commits, code, issues, users)
140
172
func (s * SearchService ) search (searchType string , query string , opt * SearchOptions , result interface {}) (* Response , error ) {
141
173
params , err := qs .Values (opt )
142
174
if err != nil {
@@ -150,7 +182,12 @@ func (s *SearchService) search(searchType string, query string, opt *SearchOptio
150
182
return nil , err
151
183
}
152
184
153
- if opt != nil && opt .TextMatch {
185
+ switch {
186
+ case searchType == "commits" :
187
+ // Accept header for search commits preview endpoint
188
+ // TODO: remove custom Accept header when this API fully launches.
189
+ req .Header .Set ("Accept" , mediaTypeCommitSearchPreview )
190
+ case opt != nil && opt .TextMatch :
154
191
// Accept header defaults to "application/vnd.github.v3+json"
155
192
// We change it here to fetch back text-match metadata
156
193
req .Header .Set ("Accept" , "application/vnd.github.v3.text-match+json" )
0 commit comments