Skip to content

Commit

Permalink
Add Protection to Branch struct (#3095)
Browse files Browse the repository at this point in the history
  • Loading branch information
jscaltreto committed Mar 11, 2024
1 parent 0e3ab58 commit 2c1b36c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
8 changes: 8 additions & 0 deletions github/github-accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions github/github-accessors_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions github/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,14 @@ type Branch struct {
Name *string `json:"name,omitempty"`
Commit *RepositoryCommit `json:"commit,omitempty"`
Protected *bool `json:"protected,omitempty"`

// Protection will always be included in APIs which return the
// 'Branch With Protection' schema such as 'Get a branch', but may
// not be included in APIs that return the `Short Branch` schema
// such as 'List branches'. In such cases, if branch protection is
// enabled, Protected will be `true` but this will be nil, and
// additional protection details can be obtained by calling GetBranch().
Protection *Protection `json:"protection,omitempty"`
}

// Protection represents a repository branch's protection.
Expand Down
14 changes: 12 additions & 2 deletions github/repos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ func TestRepositoriesService_GetBranch(t *testing.T) {
for _, test := range tests {
mux.HandleFunc(test.urlPath, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{"name":"n", "commit":{"sha":"s","commit":{"message":"m"}}, "protected":true}`)
fmt.Fprint(w, `{"name":"n", "commit":{"sha":"s","commit":{"message":"m"}}, "protected":true, "protection":{"required_status_checks":{"contexts":["c"]}}}`)
})

ctx := context.Background()
Expand All @@ -948,6 +948,11 @@ func TestRepositoriesService_GetBranch(t *testing.T) {
},
},
Protected: Bool(true),
Protection: &Protection{
RequiredStatusChecks: &RequiredStatusChecks{
Contexts: &[]string{"c"},
},
},
}

if !cmp.Equal(branch, want) {
Expand Down Expand Up @@ -1000,7 +1005,7 @@ func TestRepositoriesService_GetBranch_StatusMovedPermanently_followRedirects(t
})
mux.HandleFunc("/repos/o/r/branches/br", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{"name":"n", "commit":{"sha":"s","commit":{"message":"m"}}, "protected":true}`)
fmt.Fprint(w, `{"name":"n", "commit":{"sha":"s","commit":{"message":"m"}}, "protected":true, "protection":{"required_status_checks":{"contexts":["c"]}}}`)
})
ctx := context.Background()
branch, resp, err := client.Repositories.GetBranch(ctx, "o", "r", "b", 1)
Expand All @@ -1020,6 +1025,11 @@ func TestRepositoriesService_GetBranch_StatusMovedPermanently_followRedirects(t
},
},
Protected: Bool(true),
Protection: &Protection{
RequiredStatusChecks: &RequiredStatusChecks{
Contexts: &[]string{"c"},
},
},
}
if !cmp.Equal(branch, want) {
t.Errorf("Repositories.GetBranch returned %+v, want %+v", branch, want)
Expand Down

0 comments on commit 2c1b36c

Please sign in to comment.