From 2966f2579cd93bc62410f55ba6830b3925e7629d Mon Sep 17 00:00:00 2001 From: Aleksandar Mirilovic Date: Fri, 21 Apr 2017 21:11:22 +0200 Subject: [PATCH] Add admin enforcement to protected branches API. (#610) 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 #606. --- github/github-accessors.go | 8 ++++++++ github/repos.go | 10 ++++++++++ github/repos_test.go | 6 +++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/github/github-accessors.go b/github/github-accessors.go index c74c025484..794f5e2fde 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -20,6 +20,14 @@ func (a *AbuseRateLimitError) GetRetryAfter() time.Duration { return *a.RetryAfter } +// GetURL returns the URL field if it's non-nil, zero value otherwise. +func (a *AdminEnforcement) GetURL() string { + if a == nil || a.URL == nil { + return "" + } + return *a.URL +} + // GetVerifiablePasswordAuthentication returns the VerifiablePasswordAuthentication field if it's non-nil, zero value otherwise. func (a *APIMeta) GetVerifiablePasswordAuthentication() bool { if a == nil || a.VerifiablePasswordAuthentication == nil { diff --git a/github/repos.go b/github/repos.go index 058f149dda..41b72e3dc8 100644 --- a/github/repos.go +++ b/github/repos.go @@ -511,6 +511,7 @@ 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"` } @@ -518,12 +519,14 @@ type Protection struct { 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"` @@ -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 { diff --git a/github/repos_test.go b/github/repos_test.go index 4eac80253c..75204d82fd 100644 --- a/github/repos_test.go +++ b/github/repos_test.go @@ -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") @@ -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)},