Add support for Commit Search endpoint#520
Add support for Commit Search endpoint#520sahildua2305 wants to merge 8 commits intogoogle:masterfrom
Conversation
Added support for this new preview endpoint which lets users search for commits based on various conditions. GitHub announcement - https://developer.github.com/changes/2017-01-05-commit-search-api/ Docs - https://developer.github.com/v3/search/#search-commits Fixes google#508.
github/search.go
Outdated
| AuthorName *string `json:"author_name,omitempty"` | ||
| AuthorEmail *string `json:"author_email,omitempty"` | ||
| AuthorDate *string `json:"author_date,omitempty"` | ||
| CommitterID int `json:"committer_id,omitempty"` |
There was a problem hiding this comment.
Any reason these two ints (AuthorID and CommitterID) aren't pointers, like everywhere else?
There was a problem hiding this comment.
Oops! No, that's a bug, not a feature.
github/search.go
Outdated
| return result, resp, err | ||
| } | ||
|
|
||
| type SingleCommitResult struct { |
There was a problem hiding this comment.
golint issue here, exported struct SingleCommitResult should have documentation.
|
@shurcooL can you please review again? 🙂 |
dmitshur
left a comment
There was a problem hiding this comment.
I found a few opportunities to improve, and asked a few questions.
| @@ -150,6 +182,11 @@ func (s *SearchService) search(searchType string, query string, opt *SearchOptio | |||
| return nil, err | |||
There was a problem hiding this comment.
Update the documentation for this method. It currently says:
// ...
// GitHub search types (repositories, code, issues, users)
But that's no longer true after your change, there's one more.
github/search.go
Outdated
| if searchType == "commits" { | ||
| // Accept header for search commits preview endpoint | ||
| req.Header.Set("Accept", mediaTypeCommitSearchPreview) | ||
| } |
There was a problem hiding this comment.
Combine this if statement with the if opt != nil && opt.TextMatch { below. They both decide what Accept header to set, and I think it should be exclusive (i.e., it doesn't make sense to think about the case where both run, since the second one would override the first one anyway).
A switch statement that combines their logic would work great for this. Something like:
switch {
case ...:
req.Header.Set("Accept", ...)
case ...:
req.Header.Set("Accept", ...)
}That'll make the code more readable and easier to maintain.
github/search.go
Outdated
| CommitterID *int `json:"committer_id,omitempty"` | ||
| CommitterName *string `json:"committer_name,omitempty"` | ||
| CommitterEmail *string `json:"committer_email,omitempty"` | ||
| CommitterDate *string `json:"committer_date,omitempty"` |
There was a problem hiding this comment.
Are you sure AuthorDate and CommitterDate should be *string instead of *Timestamp?
There was a problem hiding this comment.
No, it should be *Timestamp only. Changed it.
github/search.go
Outdated
| } | ||
|
|
||
| // SingleCommitResult represents a commit object as returned in commit search endpoint response. | ||
| type SingleCommitResult struct { |
There was a problem hiding this comment.
What do you think of following the style set forward by CodeResult and name this CommitResult?
// CodeResult represents a single search result.
type CodeResult struct {Source:
Lines 115 to 116 in c9c37fd
The prefix Single doesn't seem to be needed, is it? Unless it's needed, it's better to remove it to have a simpler struct name.
|
Thanks @shurcooL, I will make the changes as suggested. |
- Make code consistent with the current code. - Simplified logic for setting Accept headers. - Changed AuthorDate and CommitterDate type from *string to *Timestamp.
|
@shurcooL I have made changes as you suggested. |
|
@shurcooL can you please this again? |
github/search.go
Outdated
| req.Header.Set("Accept", "application/vnd.github.v3.text-match+json") | ||
| case searchType == "commits": | ||
| // Accept header for search commits preview endpoint | ||
| req.Header.Set("Accept", mediaTypeCommitSearchPreview) |
There was a problem hiding this comment.
Two things here.
-
Add a
// TODO: remove custom Accept header when this API fully launches.comment. -
Now that these cases are in the same switch, it's a little easier to reason about them. I think we should give the
searchType == "commits"higher priority thanopt != nil && opt.TextMatch. It may not even be valid to setopt.TextMatch: truefor a commit search, but if someone does, it would fail to return any results at all because the custom accept header wouldn't be set.So, let's be safe and put the
searchType == "commits"case on top, so that the API preview header is always set when doing a commit search.
There was a problem hiding this comment.
@shurcooL made the changes. Please have a look. :)
- Move commits case above the textMatch one. - Add TODO for removing custom header when API fully launches
dmitshur
left a comment
There was a problem hiding this comment.
LGTM. Thanks @sahildua2305!
Mind looking it over @gmlewis?
gmlewis
left a comment
There was a problem hiding this comment.
Thanks, @sahildua2305!
Please address the slice-of-values vs slice-of-pointers.
@shurcooL - what do you think about the field naming questions?
github/search.go
Outdated
| type CommitsSearchResult struct { | ||
| Total *int `json:"total_count,omitempty"` | ||
| IncompleteResults *bool `json:"incomplete_results,omitempty"` | ||
| Commits []CommitResult `json:"items,omitempty"` |
There was a problem hiding this comment.
Please change this to:
Commits []*CommitResult `json:"items,omitempty"`Also, wouldn't we want Items here?
There was a problem hiding this comment.
Again, the current version of not using Items as the name, and not having pointers is consistent with other structs.
However, we can probably deviate on the pointers. What do you think?
There was a problem hiding this comment.
I definitely think the pointer version is better despite any inconsistencies, especially for new structs. See #180 for discussion.
As for Commits, that's fine with me.
There was a problem hiding this comment.
SGTM. @sahildua2305, please go ahead with this []*CommitResult change.
github/search.go
Outdated
|
|
||
| // CommitsSearchResult represents the result of a commits search. | ||
| type CommitsSearchResult struct { | ||
| Total *int `json:"total_count,omitempty"` |
There was a problem hiding this comment.
Wouldn't it reduce confusion if the Go field name matched the JSON field name?
In other words, use TotalCount here?
There was a problem hiding this comment.
In a normal situation, I would fully agree.
However, please look at the other similar structs in this file @gmlewis. See RepositoriesSearchResult, CodeSearchResult, etc.
They consistently call total_count as Total.
There was a problem hiding this comment.
If you think it's worth changing (outside this PR), we can make a separate issue/PR to discuss that. But I suspect the cost (breaking API) wouldn't be worth the small benefit (more consistency).
github/search_test.go
Outdated
| want := &CommitsSearchResult{ | ||
| Total: Int(4), | ||
| IncompleteResults: Bool(false), | ||
| Commits: []CommitResult{{Hash: String("random_hash1")}, {Hash: String("random_hash2")}}, |
gmlewis
left a comment
There was a problem hiding this comment.
LGTM. Thank you, @sahildua2305!
|
Merging. |
|
Integrated in 3b59f32 |
|
Thanks @gmlewis |
Added support for this new preview endpoint which lets users search for commits based on various conditions. GitHub announcement - https://developer.github.com/changes/2017-01-05-commit-search-api/ Docs - https://developer.github.com/v3/search/#search-commits Fixes google#508. Closes google#520. Change-Id: I2210fafe3da530716075f9113851e09e3bc671f8
Added support for this new preview endpoint which lets users
search for commits based on various conditions.
GitHub announcement -
https://developer.github.com/changes/2017-01-05-commit-search-api/
Docs - https://developer.github.com/v3/search/#search-commits
Fixes #508.
\cc @shurcooL @gmlewis