Skip to content

Commit 71bd14c

Browse files
feat: GetSignaturesProtectedBranch returns ErrBranchNotProtected when branch is not protected (#3896)
1 parent 5dc0050 commit 71bd14c

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

github/repos.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,6 +1629,9 @@ func (s *RepositoriesService) GetSignaturesProtectedBranch(ctx context.Context,
16291629
p := new(SignaturesProtectedBranch)
16301630
resp, err := s.client.Do(ctx, req, p)
16311631
if err != nil {
1632+
if isBranchNotProtected(err) {
1633+
err = ErrBranchNotProtected
1634+
}
16321635
return nil, resp, err
16331636
}
16341637

github/repos_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3242,6 +3242,45 @@ func TestRepositoriesService_GetSignaturesProtectedBranch(t *testing.T) {
32423242
}
32433243
}
32443244

3245+
func TestRepositoriesService_GetSignaturesProtectedBranch_branchNotProtected(t *testing.T) {
3246+
t.Parallel()
3247+
tests := []struct {
3248+
branch string
3249+
urlPath string
3250+
}{
3251+
{branch: "b", urlPath: "/repos/o/r/branches/b/protection/required_signatures"},
3252+
{branch: "feat/branch-50%", urlPath: "/repos/o/r/branches/feat%2fbranch-50%25/protection/required_signatures"},
3253+
}
3254+
3255+
for _, test := range tests {
3256+
t.Run(test.branch, func(t *testing.T) {
3257+
t.Parallel()
3258+
client, mux, _ := setup(t)
3259+
3260+
mux.HandleFunc(test.urlPath, func(w http.ResponseWriter, r *http.Request) {
3261+
testMethod(t, r, "GET")
3262+
3263+
w.WriteHeader(http.StatusBadRequest)
3264+
fmt.Fprintf(w, `{
3265+
"message": %q,
3266+
"documentation_url": "https://docs.github.com/rest/repos#get-branch-protection"
3267+
}`, githubBranchNotProtected)
3268+
})
3269+
3270+
ctx := t.Context()
3271+
checks, _, err := client.Repositories.GetSignaturesProtectedBranch(ctx, "o", "r", test.branch)
3272+
3273+
if checks != nil {
3274+
t.Error("Repositories.GetSignaturesProtectedBranch returned non-nil status-checks data")
3275+
}
3276+
3277+
if !errors.Is(err, ErrBranchNotProtected) {
3278+
t.Errorf("Repositories.GetSignaturesProtectedBranch returned an invalid error: %v", err)
3279+
}
3280+
})
3281+
}
3282+
}
3283+
32453284
func TestRepositoriesService_RequireSignaturesOnProtectedBranch(t *testing.T) {
32463285
t.Parallel()
32473286
tests := []struct {

0 commit comments

Comments
 (0)