-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add GitService.GetBlobRaw. #891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
Thank you, @dsymonds!
Awaiting second LGTM before merging.
github/git_blobs.go
Outdated
if err != nil { | ||
return nil, err | ||
} | ||
req.Header.Set("Accept", "application/vnd.github.v3.raw") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want to make "application/vnd.github.v3.raw"
a const
in github.go
?
No big deal... either way is fine with me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered it, but it didn't seem necessary for this initial case. If any other code starts using it then that's probably a good enough time to define it as a constant.
I was hoping that this would follow the same pattern as in the previous PRs #481 and #767, but I see now that the choice of supported media types by the blob endpoints is not the same. According to https://developer.github.com/v3/media/#commits-commit-comparison-and-pull-requests:
(It's missing JSON, but it is also supported.) But the blob endpoint are described at https://developer.github.com/v3/media/#git-blob-properties:
I was going to suggest we have a The reason to have a parameter like that is in case GitHub adds additional media types. If there's a Blob endpoints currently supports only 1 non-JSON raw type, so not having an option works today. It feels like this won't change soon, but it's hard to predict with certainty... If you think the current approach makes sense, I'm okay with this API (no |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR. Here are some comments on how I think we can improve this. Let me know what you think about them.
github/git_blobs.go
Outdated
req.Header.Set("Accept", "application/vnd.github.v3.raw") | ||
|
||
var buf bytes.Buffer | ||
if _, err = s.client.Do(ctx, req, &buf); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest having _, err = s.client.Do(ctx, req, &buf)
be on a separate line, like this:
_, err = s.client.Do(ctx, req, &buf)
if err != nil {
...
}
Making it inline in the if
statement makes it look less important, and it's not consistent with the style in the hundreds of other endpoints we have. I think it's a good idea to try to keep the code in this library as consistent as possible, and not deviate unnecessarily. Inconsistent style makes it harder for new contributors to figure out which style to use.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems like unnecessary consistency, but sure.
@@ -38,6 +39,23 @@ func (s *GitService) GetBlob(ctx context.Context, owner string, repo string, sha | |||
return blob, resp, err | |||
} | |||
|
|||
// GetBlobRaw fetches a blob's contents from a repo. | |||
// Unlike GetBlob, it returns the raw bytes rather than the base64-encoded data. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a GitHub API link. It's the same as the one above:
//
// GitHub API docs: https://developer.github.com/v3/git/blobs/#get-a-blob
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
github/git_blobs.go
Outdated
@@ -38,6 +39,23 @@ func (s *GitService) GetBlob(ctx context.Context, owner string, repo string, sha | |||
return blob, resp, err | |||
} | |||
|
|||
// GetBlobRaw fetches a blob's contents from a repo. | |||
// Unlike GetBlob, it returns the raw bytes rather than the base64-encoded data. | |||
func (s *GitService) GetBlobRaw(ctx context.Context, owner, repo, sha1 string) ([]byte, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method doesn't return *Response
. Is there a specific reason you left it out?
Unless there's a reason for it, I believe it's needed for consistency and for callers to be able to find out more details about the response.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know what one would do with the Response apart from what is in the []byte and error return values, but sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be helpful, for example, to log the response status code from GitHub API in case an error happens.
It's mostly for consistency. All other endpoints return it, so users would expect to be able to access the *github.Response
struct in this case too.
github/git_blobs.go
Outdated
@@ -38,6 +39,23 @@ func (s *GitService) GetBlob(ctx context.Context, owner string, repo string, sha | |||
return blob, resp, err | |||
} | |||
|
|||
// GetBlobRaw fetches a blob's contents from a repo. | |||
// Unlike GetBlob, it returns the raw bytes rather than the base64-encoded data. | |||
func (s *GitService) GetBlobRaw(ctx context.Context, owner, repo, sha1 string) ([]byte, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The endpoint above, and the GitHub API documentation (https://developer.github.com/v3/git/blobs/#get-a-blob) uses sha
as the name of the parameter (rather than sha1
). I suspect it's better if we stay consistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
I get the impression that GitHub is focused more upon their GraphQL API for the long-term from the way they were talking at their San Francisco get-together last year... (but that certainly doesn't mean this won't change). So I think the current approach makes sense. |
Yes, that's the main reason I'm okay going without an |
I think I addressed everything. PTAL. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
github/git_blobs_test.go
Outdated
|
||
blob, _, err := client.Git.GetBlobRaw(context.Background(), "o", "r", "s") | ||
if err != nil { | ||
t.Errorf("Git.GetBlob returned error: %v", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor, should be GetBlobRaw
in the error text.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whoops. fixed.
Thank you! |
Cool. I'm just glad to upstream some of my custom code. ;-) |
This method is similar to GitService.GetBlob, except it uses the "application/vnd.github.v3.raw" media type to fetch the blob content as raw bytes (rather than a JSON object containing a base64-encoded string and other information). GitHub API docs: - https://developer.github.com/v3/git/blobs/#get-a-blob - https://developer.github.com/v3/git/blobs/#custom-media-types Fixes google#658.
This addresses #658.
The name isn't great (I would have preferred
GetRawBlob
), but it matches the previously addedRepositoriesService.GetCommitRaw
. Happy to switch if you'd prefer the better name and don't mind the mild inconsistency.