Skip to content
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

Add Protection to Branch struct #3095

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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