本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.1KB

  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. "bytes"
  8. "context"
  9. "fmt"
  10. )
  11. // Blob represents a blob object.
  12. type Blob struct {
  13. Content *string `json:"content,omitempty"`
  14. Encoding *string `json:"encoding,omitempty"`
  15. SHA *string `json:"sha,omitempty"`
  16. Size *int `json:"size,omitempty"`
  17. URL *string `json:"url,omitempty"`
  18. NodeID *string `json:"node_id,omitempty"`
  19. }
  20. // GetBlob fetches a blob from a repo given a SHA.
  21. //
  22. // GitHub API docs: https://developer.github.com/v3/git/blobs/#get-a-blob
  23. func (s *GitService) GetBlob(ctx context.Context, owner string, repo string, sha string) (*Blob, *Response, error) {
  24. u := fmt.Sprintf("repos/%v/%v/git/blobs/%v", owner, repo, sha)
  25. req, err := s.client.NewRequest("GET", u, nil)
  26. if err != nil {
  27. return nil, nil, err
  28. }
  29. blob := new(Blob)
  30. resp, err := s.client.Do(ctx, req, blob)
  31. return blob, resp, err
  32. }
  33. // GetBlobRaw fetches a blob's contents from a repo.
  34. // Unlike GetBlob, it returns the raw bytes rather than the base64-encoded data.
  35. //
  36. // GitHub API docs: https://developer.github.com/v3/git/blobs/#get-a-blob
  37. func (s *GitService) GetBlobRaw(ctx context.Context, owner, repo, sha string) ([]byte, *Response, error) {
  38. u := fmt.Sprintf("repos/%v/%v/git/blobs/%v", owner, repo, sha)
  39. req, err := s.client.NewRequest("GET", u, nil)
  40. if err != nil {
  41. return nil, nil, err
  42. }
  43. req.Header.Set("Accept", "application/vnd.github.v3.raw")
  44. var buf bytes.Buffer
  45. resp, err := s.client.Do(ctx, req, &buf)
  46. return buf.Bytes(), resp, err
  47. }
  48. // CreateBlob creates a blob object.
  49. //
  50. // GitHub API docs: https://developer.github.com/v3/git/blobs/#create-a-blob
  51. func (s *GitService) CreateBlob(ctx context.Context, owner string, repo string, blob *Blob) (*Blob, *Response, error) {
  52. u := fmt.Sprintf("repos/%v/%v/git/blobs", owner, repo)
  53. req, err := s.client.NewRequest("POST", u, blob)
  54. if err != nil {
  55. return nil, nil, err
  56. }
  57. t := new(Blob)
  58. resp, err := s.client.Do(ctx, req, t)
  59. return t, resp, err
  60. }
上海开阖软件有限公司 沪ICP备12045867号-1