-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add tag protection #2424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add tag protection #2424
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
add tag protection api
- Loading branch information
commit 3507843dab2671c8246795a338134b9f7d82dd6e
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2022 The go-github AUTHORS. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// The tagprotection command demonstrates the functionality that | ||
// prompts the user for GitHub owner, repo, tag protection pattern and token | ||
// it will create new tag protection if the user enter pattern in prompt | ||
gmlewis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// otherwise it will just list all existing tag protection | ||
gmlewis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
package main | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"syscall" | ||
|
||
"github.com/google/go-github/v45/github" | ||
"golang.org/x/crypto/ssh/terminal" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
func main() { | ||
// read github owner, repo, token from standard input | ||
r := bufio.NewReader(os.Stdin) | ||
fmt.Print("GitHub Org/User name: ") | ||
owner, _ := r.ReadString('\n') | ||
owner = strings.TrimSpace(owner) | ||
|
||
fmt.Print("GitHub repo name: ") | ||
repo, _ := r.ReadString('\n') | ||
repo = strings.TrimSpace(repo) | ||
|
||
fmt.Print("Tag pattern(leave blank to not create new tag protection): ") | ||
pattern, _ := r.ReadString('\n') | ||
pattern = strings.TrimSpace(pattern) | ||
|
||
fmt.Print("GitHub Token: ") | ||
byteToken, _ := terminal.ReadPassword(int(syscall.Stdin)) | ||
println() | ||
token := string(byteToken) | ||
|
||
ctx := context.Background() | ||
ts := oauth2.StaticTokenSource( | ||
&oauth2.Token{AccessToken: token}, | ||
) | ||
tc := oauth2.NewClient(ctx, ts) | ||
|
||
client := github.NewClient(tc) | ||
|
||
// create new tag protection | ||
if pattern != "" { | ||
tagProtection, _, err := client.Repositories.CreateTagProtection(context.Background(), owner, repo, pattern) | ||
gmlewis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
fmt.Printf("Error: %v\n", err) | ||
return | ||
gmlewis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
println() | ||
fmt.Printf("New tag protection created in github.com/%v/%v\n", owner, repo) | ||
tp, _ := json.Marshal(tagProtection) | ||
fmt.Println(string(tp)) | ||
} | ||
|
||
// list all tag protection | ||
println() | ||
fmt.Printf("List all tag protection in github.com/%v/%v\n", owner, repo) | ||
tagProtections, _, err := client.Repositories.ListTagProtection(context.Background(), owner, repo) | ||
gmlewis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
fmt.Printf("Error: %v\n", err) | ||
return | ||
gmlewis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
results, _ := json.Marshal(tagProtections) | ||
fmt.Println(string(results)) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2022 The go-github AUTHORS. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package github | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
) | ||
|
||
// TagProtection represents a repository tag protection. | ||
type TagProtection struct { | ||
ID *int64 `json:"id"` | ||
Pattern *string `json:"pattern"` | ||
} | ||
|
||
// TagProtectionRequest represents a request to create tag protection | ||
gmlewis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
type TagProtectionRequest struct { | ||
gmlewis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// An optional glob pattern to match against when enforcing tag protection. | ||
Pattern string `json:"pattern"` | ||
} | ||
|
||
// ListTagProtection lists tag protection of the specified repository. | ||
// | ||
// GitHub API docs: https://docs.github.com/en/rest/repos/tags#list-tag-protection-states-for-a-repository | ||
func (s *RepositoriesService) ListTagProtection(ctx context.Context, owner, repo string) ([]*TagProtection, *Response, error) { | ||
u := fmt.Sprintf("repos/%v/%v/tags/protection", owner, repo) | ||
|
||
req, err := s.client.NewRequest("GET", u, nil) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
var tagProtections []*TagProtection | ||
resp, err := s.client.Do(ctx, req, &tagProtections) | ||
if err != nil { | ||
return nil, resp, err | ||
} | ||
|
||
return tagProtections, resp, nil | ||
} | ||
|
||
// CreateTagProtection creates the tag protection of the specified repository. | ||
// | ||
// GitHub API docs: https://docs.github.com/en/rest/repos/tags#create-a-tag-protection-state-for-a-repository | ||
func (s *RepositoriesService) CreateTagProtection(ctx context.Context, owner, repo, pattern string) (*TagProtection, *Response, error) { | ||
u := fmt.Sprintf("repos/%v/%v/tags/protection", owner, repo) | ||
r := &TagProtectionRequest{Pattern: pattern} | ||
req, err := s.client.NewRequest("POST", u, r) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
tagProtection := new(TagProtection) | ||
resp, err := s.client.Do(ctx, req, tagProtection) | ||
if err != nil { | ||
return nil, resp, err | ||
} | ||
|
||
return tagProtection, resp, nil | ||
} | ||
|
||
// DeleteTagProtection deletes a tag protection from the specified repository. | ||
// | ||
// GitHub API docs: https://docs.github.com/en/rest/repos/tags#delete-a-tag-protection-state-for-a-repository | ||
func (s *RepositoriesService) DeleteTagProtection(ctx context.Context, owner, repo string, tag_protection_id int64) (*Response, error) { | ||
gmlewis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
u := fmt.Sprintf("repos/%v/%v/tags/protection/%v", owner, repo, tag_protection_id) | ||
req, err := s.client.NewRequest("DELETE", u, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return s.client.Do(ctx, req, nil) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// Copyright 2022 The go-github AUTHORS. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package github | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestRepositoriesService_ListTagProtection(t *testing.T) { | ||
client, mux, _, teardown := setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/repos/o/r/tags/protection", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "GET") | ||
|
||
fmt.Fprint(w, `[{"id":1, "pattern":"tag1"},{"id":2, "pattern":"tag2"}]`) | ||
}) | ||
|
||
ctx := context.Background() | ||
tagProtections, _, err := client.Repositories.ListTagProtection(ctx, "o", "r") | ||
if err != nil { | ||
t.Errorf("Repositories.ListTagProtection returned error: %v", err) | ||
} | ||
|
||
want := []*TagProtection{{ID: Int64(1), Pattern: String("tag1")}, {ID: Int64(2), Pattern: String("tag2")}} | ||
if !cmp.Equal(tagProtections, want) { | ||
t.Errorf("Repositories.ListTagProtection returned %+v, want %+v", tagProtections, want) | ||
} | ||
|
||
const methodName = "ListTagProtection" | ||
testBadOptions(t, methodName, func() (err error) { | ||
_, _, err = client.Repositories.ListTagProtection(ctx, "\n", "\n") | ||
return err | ||
}) | ||
|
||
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { | ||
got, resp, err := client.Repositories.ListTagProtection(ctx, "o", "r") | ||
if got != nil { | ||
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) | ||
} | ||
return resp, err | ||
}) | ||
} | ||
|
||
func TestRepositoriesService_ListTagProtection_invalidOwner(t *testing.T) { | ||
client, _, _, teardown := setup() | ||
defer teardown() | ||
|
||
ctx := context.Background() | ||
_, _, err := client.Repositories.ListTagProtection(ctx, "%", "r") | ||
testURLParseError(t, err) | ||
} | ||
|
||
func TestRepositoriesService_CreateTagProtection(t *testing.T) { | ||
client, mux, _, teardown := setup() | ||
defer teardown() | ||
|
||
pattern := "tag*" | ||
|
||
mux.HandleFunc("/repos/o/r/tags/protection", func(w http.ResponseWriter, r *http.Request) { | ||
v := new(TagProtectionRequest) | ||
json.NewDecoder(r.Body).Decode(v) | ||
|
||
testMethod(t, r, "POST") | ||
want := &TagProtectionRequest{Pattern: "tag*"} | ||
if !cmp.Equal(v, want) { | ||
t.Errorf("Request body = %+v, want %+v", v, want) | ||
} | ||
|
||
fmt.Fprint(w, `{"id":1,"pattern":"tag*"}`) | ||
}) | ||
|
||
ctx := context.Background() | ||
got, _, err := client.Repositories.CreateTagProtection(ctx, "o", "r", pattern) | ||
if err != nil { | ||
t.Errorf("Repositories.CreateTagProtection returned error: %v", err) | ||
} | ||
|
||
want := &TagProtection{ID: Int64(1), Pattern: String("tag*")} | ||
if !cmp.Equal(got, want) { | ||
t.Errorf("Repositories.CreateTagProtection returned %+v, want %+v", got, want) | ||
} | ||
|
||
const methodName = "CreateTagProtection" | ||
testBadOptions(t, methodName, func() (err error) { | ||
_, _, err = client.Repositories.CreateTagProtection(ctx, "\n", "\n", pattern) | ||
return err | ||
}) | ||
|
||
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { | ||
got, resp, err := client.Repositories.CreateTagProtection(ctx, "o", "r", pattern) | ||
if got != nil { | ||
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) | ||
} | ||
return resp, err | ||
}) | ||
} | ||
|
||
func TestRepositoriesService_DeleteTagProtection(t *testing.T) { | ||
client, mux, _, teardown := setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/repos/o/r/tags/protection/1", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "DELETE") | ||
w.WriteHeader(http.StatusNoContent) | ||
}) | ||
|
||
ctx := context.Background() | ||
_, err := client.Repositories.DeleteTagProtection(ctx, "o", "r", 1) | ||
if err != nil { | ||
t.Errorf("Repositories.DeleteTagProtection returned error: %v", err) | ||
} | ||
|
||
const methodName = "DeleteTagProtection" | ||
testBadOptions(t, methodName, func() (err error) { | ||
_, err = client.Repositories.DeleteTagProtection(ctx, "\n", "\n", 1) | ||
return err | ||
}) | ||
|
||
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { | ||
return client.Repositories.DeleteTagProtection(ctx, "o", "r", 1) | ||
}) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.