4
0

git_blobs.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2013 The go-github AUTHORS. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. package github
  6. import (
  7. "context"
  8. "fmt"
  9. )
  10. // Blob represents a blob object.
  11. type Blob struct {
  12. Content *string `json:"content,omitempty"`
  13. Encoding *string `json:"encoding,omitempty"`
  14. SHA *string `json:"sha,omitempty"`
  15. Size *int `json:"size,omitempty"`
  16. URL *string `json:"url,omitempty"`
  17. NodeID *string `json:"node_id,omitempty"`
  18. }
  19. // GetBlob fetchs a blob from a repo given a SHA.
  20. //
  21. // GitHub API docs: https://developer.github.com/v3/git/blobs/#get-a-blob
  22. func (s *GitService) GetBlob(ctx context.Context, owner string, repo string, sha string) (*Blob, *Response, error) {
  23. u := fmt.Sprintf("repos/%v/%v/git/blobs/%v", owner, repo, sha)
  24. req, err := s.client.NewRequest("GET", u, nil)
  25. if err != nil {
  26. return nil, nil, err
  27. }
  28. // TODO: remove custom Accept header when this API fully launches.
  29. req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
  30. blob := new(Blob)
  31. resp, err := s.client.Do(ctx, req, blob)
  32. return blob, resp, err
  33. }
  34. // CreateBlob creates a blob object.
  35. //
  36. // GitHub API docs: https://developer.github.com/v3/git/blobs/#create-a-blob
  37. func (s *GitService) CreateBlob(ctx context.Context, owner string, repo string, blob *Blob) (*Blob, *Response, error) {
  38. u := fmt.Sprintf("repos/%v/%v/git/blobs", owner, repo)
  39. req, err := s.client.NewRequest("POST", u, blob)
  40. if err != nil {
  41. return nil, nil, err
  42. }
  43. // TODO: remove custom Accept header when this API fully launches.
  44. req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
  45. t := new(Blob)
  46. resp, err := s.client.Do(ctx, req, t)
  47. return t, resp, err
  48. }