git_tags.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. "strings"
  10. )
  11. // Tag represents a tag object.
  12. type Tag struct {
  13. Tag *string `json:"tag,omitempty"`
  14. SHA *string `json:"sha,omitempty"`
  15. URL *string `json:"url,omitempty"`
  16. Message *string `json:"message,omitempty"`
  17. Tagger *CommitAuthor `json:"tagger,omitempty"`
  18. Object *GitObject `json:"object,omitempty"`
  19. Verification *SignatureVerification `json:"verification,omitempty"`
  20. NodeID *string `json:"node_id,omitempty"`
  21. }
  22. // createTagRequest represents the body of a CreateTag request. This is mostly
  23. // identical to Tag with the exception that the object SHA and Type are
  24. // top-level fields, rather than being nested inside a JSON object.
  25. type createTagRequest struct {
  26. Tag *string `json:"tag,omitempty"`
  27. Message *string `json:"message,omitempty"`
  28. Object *string `json:"object,omitempty"`
  29. Type *string `json:"type,omitempty"`
  30. Tagger *CommitAuthor `json:"tagger,omitempty"`
  31. }
  32. // GetTag fetchs a tag from a repo given a SHA.
  33. //
  34. // GitHub API docs: https://developer.github.com/v3/git/tags/#get-a-tag
  35. func (s *GitService) GetTag(ctx context.Context, owner string, repo string, sha string) (*Tag, *Response, error) {
  36. u := fmt.Sprintf("repos/%v/%v/git/tags/%v", owner, repo, sha)
  37. req, err := s.client.NewRequest("GET", u, nil)
  38. if err != nil {
  39. return nil, nil, err
  40. }
  41. // TODO: remove custom Accept headers when APIs fully launch.
  42. acceptHeaders := []string{mediaTypeGitSigningPreview, mediaTypeGraphQLNodeIDPreview}
  43. req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
  44. tag := new(Tag)
  45. resp, err := s.client.Do(ctx, req, tag)
  46. return tag, resp, err
  47. }
  48. // CreateTag creates a tag object.
  49. //
  50. // GitHub API docs: https://developer.github.com/v3/git/tags/#create-a-tag-object
  51. func (s *GitService) CreateTag(ctx context.Context, owner string, repo string, tag *Tag) (*Tag, *Response, error) {
  52. u := fmt.Sprintf("repos/%v/%v/git/tags", owner, repo)
  53. // convert Tag into a createTagRequest
  54. tagRequest := &createTagRequest{
  55. Tag: tag.Tag,
  56. Message: tag.Message,
  57. Tagger: tag.Tagger,
  58. }
  59. if tag.Object != nil {
  60. tagRequest.Object = tag.Object.SHA
  61. tagRequest.Type = tag.Object.Type
  62. }
  63. req, err := s.client.NewRequest("POST", u, tagRequest)
  64. if err != nil {
  65. return nil, nil, err
  66. }
  67. // TODO: remove custom Accept header when this API fully launches.
  68. req.Header.Set("Accept", mediaTypeGraphQLNodeIDPreview)
  69. t := new(Tag)
  70. resp, err := s.client.Do(ctx, req, t)
  71. return t, resp, err
  72. }