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.

git_tags.go 2.4KB

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