| 1 |
// Copyright 2022 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 |
// TagProtection represents a repository tag protection. |
| 14 |
type TagProtection struct { |
| 15 |
ID *int64 `json:"id"` |
| 16 |
Pattern *string `json:"pattern"` |
| 17 |
} |
| 18 |
|
| 19 |
// tagProtectionRequest represents a request to create tag protection. |
| 20 |
type tagProtectionRequest struct { |
| 21 |
// An optional glob pattern to match against when enforcing tag protection. |
| 22 |
Pattern string `json:"pattern"` |
| 23 |
} |
| 24 |
|
| 25 |
// ListTagProtection lists tag protection of the specified repository. |
| 26 |
// |
| 27 |
// GitHub API docs: https://docs.github.com/rest/repos/tags#list-tag-protection-states-for-a-repository |
| 28 |
// |
| 29 |
//meta:operation GET /repos/{owner}/{repo}/tags/protection |
| 30 |
func (s *RepositoriesService) ListTagProtection(ctx context.Context, owner, repo string) ([]*TagProtection, *Response, error) { |
| 31 |
u := fmt.Sprintf("repos/%v/%v/tags/protection", owner, repo) |
| 32 |
|
| 33 |
req, err := s.client.NewRequest("GET", u, nil) |
| 34 |
if err != nil { |
| 35 |
return nil, nil, err |
| 36 |
} |
| 37 |
|
| 38 |
var tagProtections []*TagProtection |
| 39 |
resp, err := s.client.Do(ctx, req, &tagProtections) |
| 40 |
if err != nil { |
| 41 |
return nil, resp, err |
| 42 |
} |
| 43 |
|
| 44 |
return tagProtections, resp, nil |
| 45 |
} |
| 46 |
|
| 47 |
// CreateTagProtection creates the tag protection of the specified repository. |
| 48 |
// |
| 49 |
// GitHub API docs: https://docs.github.com/rest/repos/tags#create-a-tag-protection-state-for-a-repository |
| 50 |
// |
| 51 |
//meta:operation POST /repos/{owner}/{repo}/tags/protection |
| 52 |
func (s *RepositoriesService) CreateTagProtection(ctx context.Context, owner, repo, pattern string) (*TagProtection, *Response, error) { |
| 53 |
u := fmt.Sprintf("repos/%v/%v/tags/protection", owner, repo) |
| 54 |
r := &tagProtectionRequest{Pattern: pattern} |
| 55 |
req, err := s.client.NewRequest("POST", u, r) |
| 56 |
if err != nil { |
| 57 |
return nil, nil, err |
| 58 |
} |
| 59 |
|
| 60 |
tagProtection := new(TagProtection) |
| 61 |
resp, err := s.client.Do(ctx, req, tagProtection) |
| 62 |
if err != nil { |
| 63 |
return nil, resp, err |
| 64 |
} |
| 65 |
|
| 66 |
return tagProtection, resp, nil |
| 67 |
} |
| 68 |
|
| 69 |
// DeleteTagProtection deletes a tag protection from the specified repository. |
| 70 |
// |
| 71 |
// GitHub API docs: https://docs.github.com/rest/repos/tags#delete-a-tag-protection-state-for-a-repository |
| 72 |
// |
| 73 |
//meta:operation DELETE /repos/{owner}/{repo}/tags/protection/{tag_protection_id} |
| 74 |
func (s *RepositoriesService) DeleteTagProtection(ctx context.Context, owner, repo string, tagProtectionID int64) (*Response, error) { |
| 75 |
u := fmt.Sprintf("repos/%v/%v/tags/protection/%v", owner, repo, tagProtectionID) |
| 76 |
req, err := s.client.NewRequest("DELETE", u, nil) |
| 77 |
if err != nil { |
| 78 |
return nil, err |
| 79 |
} |
| 80 |
|
| 81 |
return s.client.Do(ctx, req, nil) |
| 82 |
} |