Skip to content

Commit

Permalink
Add admin enforcement to protected branches API. (google#610)
Browse files Browse the repository at this point in the history
This change adds support for an addition to the Protected Branches API
preview, the ability to turn on and off admin settings in one place.

This is documented at https://developer.github.com/changes/2017-03-22-protected-branches-admin-improvements/.

The IncludeAdmins fields are deprecated, but they're still required to
be sent in the request for now. A future update to API preview will
remove the IncludeAdmins fields completely.

Helps google#606.
  • Loading branch information
amirilovic authored and dmitshur committed Apr 21, 2017
1 parent a0c13e8 commit 2966f25
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
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.

10 changes: 10 additions & 0 deletions github/repos.go
Expand Up @@ -511,19 +511,22 @@ type Branch struct {
type Protection struct {
RequiredStatusChecks *RequiredStatusChecks `json:"required_status_checks"`
RequiredPullRequestReviews *RequiredPullRequestReviews `json:"required_pull_request_reviews"`
EnforceAdmins *AdminEnforcement `json:"enforce_admins"`
Restrictions *BranchRestrictions `json:"restrictions"`
}

// ProtectionRequest represents a request to create/edit a branch's protection.
type ProtectionRequest struct {
RequiredStatusChecks *RequiredStatusChecks `json:"required_status_checks"`
RequiredPullRequestReviews *RequiredPullRequestReviews `json:"required_pull_request_reviews"`
EnforceAdmins bool `json:"enforce_admins"`
Restrictions *BranchRestrictionsRequest `json:"restrictions"`
}

// RequiredStatusChecks represents the protection status of a individual branch.
type RequiredStatusChecks struct {
// Enforce required status checks for repository administrators. (Required.)
// Deprecated: Use EnforceAdmins instead.
IncludeAdmins bool `json:"include_admins"`
// Require branches to be up to date before merging. (Required.)
Strict bool `json:"strict"`
Expand All @@ -535,9 +538,16 @@ type RequiredStatusChecks struct {
// RequiredPullRequestReviews represents the protection configuration for pull requests.
type RequiredPullRequestReviews struct {
// Enforce pull request reviews for repository administrators. (Required.)
// Deprecated: Use EnforceAdmins instead.
IncludeAdmins bool `json:"include_admins"`
}

// AdminEnforcement represents the configuration to enforce required status checks for repository administrators.
type AdminEnforcement struct {
URL *string `json:"url,omitempty"`
Enabled bool `json:"enabled"`
}

// BranchRestrictions represents the restriction that only certain users or
// teams may push to a branch.
type BranchRestrictions struct {
Expand Down
6 changes: 5 additions & 1 deletion github/repos_test.go
Expand Up @@ -482,7 +482,7 @@ func TestRepositoriesService_GetBranchProtection(t *testing.T) {

testMethod(t, r, "GET")
testHeader(t, r, "Accept", mediaTypeProtectedBranchesPreview)
fmt.Fprintf(w, `{"required_status_checks":{"include_admins":true,"strict":true,"contexts":["continuous-integration"]},"required_pull_request_reviews":{"include_admins":true},"restrictions":{"users":[{"id":1,"login":"u"}],"teams":[{"id":2,"slug":"t"}]}}`)
fmt.Fprintf(w, `{"required_status_checks":{"include_admins":true,"strict":true,"contexts":["continuous-integration"]},"required_pull_request_reviews":{"include_admins":true},"enforce_admins":{"url":"/repos/o/r/branches/b/protection/enforce_admins","enabled":true},"restrictions":{"users":[{"id":1,"login":"u"}],"teams":[{"id":2,"slug":"t"}]}}`)
})

protection, _, err := client.Repositories.GetBranchProtection(context.Background(), "o", "r", "b")
Expand All @@ -499,6 +499,10 @@ func TestRepositoriesService_GetBranchProtection(t *testing.T) {
RequiredPullRequestReviews: &RequiredPullRequestReviews{
IncludeAdmins: true,
},
EnforceAdmins: &AdminEnforcement{
URL: String("/repos/o/r/branches/b/protection/enforce_admins"),
Enabled: true,
},
Restrictions: &BranchRestrictions{
Users: []*User{
{Login: String("u"), ID: Int(1)},
Expand Down

0 comments on commit 2966f25

Please sign in to comment.