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