Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions github/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,34 +508,42 @@ type Branch struct {

// Protection represents a repository branch's protection.
type Protection struct {
RequiredStatusChecks *RequiredStatusChecks `json:"required_status_checks"`
Restrictions *BranchRestrictions `json:"restrictions"`
RequiredStatusChecks *RequiredStatusChecks `json:"required_status_checks"`
RequiredPullRequestReviews *RequiredPullRequestReviews `json:"required_pull_request_reviews"`
Restrictions *BranchRestrictions `json:"restrictions"`
}

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

// RequiredStatusChecks represents the protection status of a individual branch.
type RequiredStatusChecks struct {
// Enforce required status checks for repository administrators.
IncludeAdmins *bool `json:"include_admins,omitempty"`
IncludeAdmins bool `json:"include_admins"`
// Require branches to be up to date before merging.
Strict *bool `json:"strict,omitempty"`
Strict bool `json:"strict"`
// The list of status checks to require in order to merge into this
// branch.
Contexts *[]string `json:"contexts,omitempty"`
Contexts []string `json:"contexts"`
}

// RequiredPullRequestReviews represents the protection configuration for pull requests.
type RequiredPullRequestReviews struct {
// Enforce pull request reviews for repository administrators.
IncludeAdmins bool `json:"include_admins"`
}

// BranchRestrictions represents the restriction that only certain users or
// teams may push to a branch.
type BranchRestrictions struct {
// The list of user logins with push access.
Users []*User `json:"users,omitempty"`
Users []*User `json:"users"`
// The list of team slugs with push access.
Teams []*Team `json:"teams,omitempty"`
Teams []*Team `json:"teams"`
}

// BranchRestrictionsRequest represents the request to create/edit the
Expand All @@ -544,9 +552,9 @@ type BranchRestrictions struct {
// different from the response structure.
type BranchRestrictionsRequest struct {
// The list of user logins with push access.
Users *[]string `json:"users,omitempty"`
Users []string `json:"users"`
// The list of team slugs with push access.
Teams *[]string `json:"teams,omitempty"`
Teams []string `json:"teams"`
}

// ListBranches lists branches for the specified repository.
Expand Down
35 changes: 22 additions & 13 deletions github/repos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,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"]},"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},"restrictions":{"users":[{"id":1,"login":"u"}],"teams":[{"id":2,"slug":"t"}]}}`)
})

protection, _, err := client.Repositories.GetBranchProtection("o", "r", "b")
Expand All @@ -491,9 +491,12 @@ func TestRepositoriesService_GetBranchProtection(t *testing.T) {

want := &Protection{
RequiredStatusChecks: &RequiredStatusChecks{
IncludeAdmins: Bool(true),
Strict: Bool(true),
Contexts: &[]string{"continuous-integration"},
IncludeAdmins: true,
Strict: true,
Contexts: []string{"continuous-integration"},
},
RequiredPullRequestReviews: &RequiredPullRequestReviews{
IncludeAdmins: true,
},
Restrictions: &BranchRestrictions{
Users: []*User{
Expand All @@ -515,13 +518,16 @@ func TestRepositoriesService_UpdateBranchProtection(t *testing.T) {

input := &ProtectionRequest{
RequiredStatusChecks: &RequiredStatusChecks{
IncludeAdmins: Bool(true),
Strict: Bool(true),
Contexts: &[]string{"continuous-integration"},
IncludeAdmins: true,
Strict: true,
Contexts: []string{"continuous-integration"},
},
RequiredPullRequestReviews: &RequiredPullRequestReviews{
IncludeAdmins: true,
},
Restrictions: &BranchRestrictionsRequest{
Users: &[]string{"u"},
Teams: &[]string{"t"},
Users: []string{"u"},
Teams: []string{"t"},
},
}

Expand All @@ -534,7 +540,7 @@ func TestRepositoriesService_UpdateBranchProtection(t *testing.T) {
t.Errorf("Request body = %+v, want %+v", v, input)
}
testHeader(t, r, "Accept", mediaTypeProtectedBranchesPreview)
fmt.Fprintf(w, `{"required_status_checks":{"include_admins":true,"strict":true,"contexts":["continuous-integration"]},"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},"restrictions":{"users":[{"id":1,"login":"u"}],"teams":[{"id":2,"slug":"t"}]}}`)
})

protection, _, err := client.Repositories.UpdateBranchProtection("o", "r", "b", input)
Expand All @@ -544,9 +550,12 @@ func TestRepositoriesService_UpdateBranchProtection(t *testing.T) {

want := &Protection{
RequiredStatusChecks: &RequiredStatusChecks{
IncludeAdmins: Bool(true),
Strict: Bool(true),
Contexts: &[]string{"continuous-integration"},
IncludeAdmins: true,
Strict: true,
Contexts: []string{"continuous-integration"},
},
RequiredPullRequestReviews: &RequiredPullRequestReviews{
IncludeAdmins: true,
},
Restrictions: &BranchRestrictions{
Users: []*User{
Expand Down