Skip to content

Commit

Permalink
Add apps to restriction rules in branch protection (#2509)
Browse files Browse the repository at this point in the history
Fixes: #2506.
  • Loading branch information
jporzucek committed Oct 20, 2022
1 parent 3dcfcf6 commit fce4030
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 10 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.

10 changes: 10 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.

16 changes: 10 additions & 6 deletions github/repos.go
Expand Up @@ -1019,7 +1019,7 @@ type RequiredStatusCheck struct {
type PullRequestReviewsEnforcement struct {
// Allow specific users, teams, or apps to bypass pull request requirements.
BypassPullRequestAllowances *BypassPullRequestAllowances `json:"bypass_pull_request_allowances,omitempty"`
// Specifies which users and teams can dismiss pull request reviews.
// Specifies which users, teams and apps can dismiss pull request reviews.
DismissalRestrictions *DismissalRestrictions `json:"dismissal_restrictions,omitempty"`
// Specifies if approved reviews are dismissed automatically, when a new commit is pushed.
DismissStaleReviews bool `json:"dismiss_stale_reviews"`
Expand All @@ -1036,8 +1036,8 @@ type PullRequestReviewsEnforcement struct {
type PullRequestReviewsEnforcementRequest struct {
// Allow specific users, teams, or apps to bypass pull request requirements.
BypassPullRequestAllowancesRequest *BypassPullRequestAllowancesRequest `json:"bypass_pull_request_allowances,omitempty"`
// Specifies which users and teams should be allowed to dismiss pull request reviews.
// User and team dismissal restrictions are only available for
// Specifies which users, teams and apps should be allowed to dismiss pull request reviews.
// User, team and app dismissal restrictions are only available for
// organization-owned repositories. Must be nil for personal repositories.
DismissalRestrictionsRequest *DismissalRestrictionsRequest `json:"dismissal_restrictions,omitempty"`
// Specifies if approved reviews can be dismissed automatically, when a new commit is pushed. (Required)
Expand All @@ -1055,7 +1055,7 @@ type PullRequestReviewsEnforcementRequest struct {
type PullRequestReviewsEnforcementUpdate struct {
// Allow specific users, teams, or apps to bypass pull request requirements.
BypassPullRequestAllowancesRequest *BypassPullRequestAllowancesRequest `json:"bypass_pull_request_allowances,omitempty"`
// Specifies which users and teams can dismiss pull request reviews. Can be omitted.
// Specifies which users, teams and apps can dismiss pull request reviews. Can be omitted.
DismissalRestrictionsRequest *DismissalRestrictionsRequest `json:"dismissal_restrictions,omitempty"`
// Specifies if approved reviews can be dismissed automatically, when a new commit is pushed. Can be omitted.
DismissStaleReviews *bool `json:"dismiss_stale_reviews,omitempty"`
Expand Down Expand Up @@ -1113,7 +1113,7 @@ type BranchRestrictionsRequest struct {
// The list of team slugs with push access. (Required; use []string{} instead of nil for empty list.)
Teams []string `json:"teams"`
// The list of app slugs with push access.
Apps []string `json:"apps,omitempty"`
Apps []string `json:"apps"`
}

// BypassPullRequestAllowances represents the people, teams, or apps who are allowed to bypass required pull requests.
Expand Down Expand Up @@ -1145,10 +1145,12 @@ type DismissalRestrictions struct {
Users []*User `json:"users"`
// The list of teams which can dismiss pull request reviews.
Teams []*Team `json:"teams"`
// The list of apps which can dismiss pull request reviews.
Apps []*App `json:"apps"`
}

// DismissalRestrictionsRequest represents the request to create/edit the
// restriction to allows only specific users or teams to dimiss pull request reviews. It is
// restriction to allows only specific users, teams or apps to dimiss pull request reviews. It is
// separate from DismissalRestrictions above because the request structure is
// different from the response structure.
// Note: Both Users and Teams must be nil, or both must be non-nil.
Expand All @@ -1157,6 +1159,8 @@ type DismissalRestrictionsRequest struct {
Users *[]string `json:"users,omitempty"`
// The list of team slugs which can dismiss pull request reviews. (Required; use nil to disable dismissal_restrictions or &[]string{} otherwise.)
Teams *[]string `json:"teams,omitempty"`
// The list of apps which can dismiss pull request reviews. (Required; use nil to disable dismissal_restrictions or &[]string{} otherwise.)
Apps *[]string `json:"apps,omitempty"`
}

// SignaturesProtectedBranch represents the protection status of an individual branch.
Expand Down
53 changes: 49 additions & 4 deletions github/repos_test.go
Expand Up @@ -1074,6 +1074,10 @@ func TestRepositoriesService_GetBranchProtection(t *testing.T) {
"teams":[{
"id":4,
"slug":"t"
}],
"apps":[{
"id":5,
"slug":"a"
}]
},
"dismiss_stale_reviews":true,
Expand All @@ -1086,7 +1090,8 @@ func TestRepositoriesService_GetBranchProtection(t *testing.T) {
},
"restrictions":{
"users":[{"id":1,"login":"u"}],
"teams":[{"id":2,"slug":"t"}]
"teams":[{"id":2,"slug":"t"}],
"apps":[{"id":3,"slug":"a"}]
},
"required_conversation_resolution": {
"enabled": true
Expand Down Expand Up @@ -1119,6 +1124,9 @@ func TestRepositoriesService_GetBranchProtection(t *testing.T) {
Teams: []*Team{
{Slug: String("t"), ID: Int64(4)},
},
Apps: []*App{
{Slug: String("a"), ID: Int64(5)},
},
},
RequireCodeOwnerReviews: true,
RequiredApprovingReviewCount: 1,
Expand All @@ -1134,6 +1142,9 @@ func TestRepositoriesService_GetBranchProtection(t *testing.T) {
Teams: []*Team{
{Slug: String("t"), ID: Int64(2)},
},
Apps: []*App{
{Slug: String("a"), ID: Int64(3)},
},
},
RequiredConversationResolution: &RequiredConversationResolution{
Enabled: true,
Expand Down Expand Up @@ -1273,6 +1284,7 @@ func TestRepositoriesService_UpdateBranchProtection_Contexts(t *testing.T) {
DismissalRestrictionsRequest: &DismissalRestrictionsRequest{
Users: &[]string{"uu"},
Teams: &[]string{"tt"},
Apps: &[]string{"aa"},
},
BypassPullRequestAllowancesRequest: &BypassPullRequestAllowancesRequest{
Users: []string{"uuu"},
Expand Down Expand Up @@ -1318,6 +1330,10 @@ func TestRepositoriesService_UpdateBranchProtection_Contexts(t *testing.T) {
"teams":[{
"id":4,
"slug":"tt"
}],
"apps":[{
"id":5,
"slug":"aa"
}]
},
"dismiss_stale_reviews":true,
Expand Down Expand Up @@ -1361,6 +1377,9 @@ func TestRepositoriesService_UpdateBranchProtection_Contexts(t *testing.T) {
Teams: []*Team{
{Slug: String("tt"), ID: Int64(4)},
},
Apps: []*App{
{Slug: String("aa"), ID: Int64(5)},
},
},
RequireCodeOwnerReviews: true,
BypassPullRequestAllowances: &BypassPullRequestAllowances{
Expand Down Expand Up @@ -1424,6 +1443,7 @@ func TestRepositoriesService_UpdateBranchProtection_Checks(t *testing.T) {
DismissalRestrictionsRequest: &DismissalRestrictionsRequest{
Users: &[]string{"uu"},
Teams: &[]string{"tt"},
Apps: &[]string{"aa"},
},
BypassPullRequestAllowancesRequest: &BypassPullRequestAllowancesRequest{
Users: []string{"uuu"},
Expand Down Expand Up @@ -1469,6 +1489,10 @@ func TestRepositoriesService_UpdateBranchProtection_Checks(t *testing.T) {
"teams":[{
"id":4,
"slug":"tt"
}],
"apps":[{
"id":5,
"slug":"aa"
}]
},
"dismiss_stale_reviews":true,
Expand Down Expand Up @@ -1512,6 +1536,9 @@ func TestRepositoriesService_UpdateBranchProtection_Checks(t *testing.T) {
Teams: []*Team{
{Slug: String("tt"), ID: Int64(4)},
},
Apps: []*App{
{Slug: String("aa"), ID: Int64(5)},
},
},
RequireCodeOwnerReviews: true,
BypassPullRequestAllowances: &BypassPullRequestAllowances{
Expand Down Expand Up @@ -1557,6 +1584,7 @@ func TestRepositoriesService_UpdateBranchProtection_StrictNoChecks(t *testing.T)
DismissalRestrictionsRequest: &DismissalRestrictionsRequest{
Users: &[]string{"uu"},
Teams: &[]string{"tt"},
Apps: &[]string{"aa"},
},
BypassPullRequestAllowancesRequest: &BypassPullRequestAllowancesRequest{
Users: []string{"uuu"},
Expand Down Expand Up @@ -1597,6 +1625,10 @@ func TestRepositoriesService_UpdateBranchProtection_StrictNoChecks(t *testing.T)
"teams":[{
"id":4,
"slug":"tt"
}],
"apps":[{
"id":5,
"slug":"aa"
}]
},
"dismiss_stale_reviews":true,
Expand Down Expand Up @@ -1636,6 +1668,9 @@ func TestRepositoriesService_UpdateBranchProtection_StrictNoChecks(t *testing.T)
Teams: []*Team{
{Slug: String("tt"), ID: Int64(4)},
},
Apps: []*App{
{Slug: String("aa"), ID: Int64(5)},
},
},
RequireCodeOwnerReviews: true,
BypassPullRequestAllowances: &BypassPullRequestAllowances{
Expand Down Expand Up @@ -2088,7 +2123,8 @@ func TestRepositoriesService_GetPullRequestReviewEnforcement(t *testing.T) {
fmt.Fprintf(w, `{
"dismissal_restrictions":{
"users":[{"id":1,"login":"u"}],
"teams":[{"id":2,"slug":"t"}]
"teams":[{"id":2,"slug":"t"}],
"apps":[{"id":3,"slug":"a"}]
},
"dismiss_stale_reviews":true,
"require_code_owner_reviews":true,
Expand All @@ -2111,6 +2147,9 @@ func TestRepositoriesService_GetPullRequestReviewEnforcement(t *testing.T) {
Teams: []*Team{
{Slug: String("t"), ID: Int64(2)},
},
Apps: []*App{
{Slug: String("a"), ID: Int64(3)},
},
},
RequireCodeOwnerReviews: true,
RequiredApprovingReviewCount: 1,
Expand Down Expand Up @@ -2143,6 +2182,7 @@ func TestRepositoriesService_UpdatePullRequestReviewEnforcement(t *testing.T) {
DismissalRestrictionsRequest: &DismissalRestrictionsRequest{
Users: &[]string{"u"},
Teams: &[]string{"t"},
Apps: &[]string{"a"},
},
}

Expand All @@ -2159,7 +2199,8 @@ func TestRepositoriesService_UpdatePullRequestReviewEnforcement(t *testing.T) {
fmt.Fprintf(w, `{
"dismissal_restrictions":{
"users":[{"id":1,"login":"u"}],
"teams":[{"id":2,"slug":"t"}]
"teams":[{"id":2,"slug":"t"}],
"apps":[{"id":3,"slug":"a"}]
},
"dismiss_stale_reviews":true,
"require_code_owner_reviews":true,
Expand All @@ -2182,6 +2223,9 @@ func TestRepositoriesService_UpdatePullRequestReviewEnforcement(t *testing.T) {
Teams: []*Team{
{Slug: String("t"), ID: Int64(2)},
},
Apps: []*App{
{Slug: String("a"), ID: Int64(3)},
},
},
RequireCodeOwnerReviews: true,
RequiredApprovingReviewCount: 3,
Expand Down Expand Up @@ -2515,6 +2559,7 @@ func TestPullRequestReviewsEnforcementRequest_MarshalJSON_nilDismissalRestirctio
DismissalRestrictionsRequest: &DismissalRestrictionsRequest{
Users: &[]string{},
Teams: &[]string{},
Apps: &[]string{},
},
}

Expand All @@ -2523,7 +2568,7 @@ func TestPullRequestReviewsEnforcementRequest_MarshalJSON_nilDismissalRestirctio
t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned error: %v", err)
}

want = `{"dismissal_restrictions":{"users":[],"teams":[]},"dismiss_stale_reviews":false,"require_code_owner_reviews":false,"required_approving_review_count":0}`
want = `{"dismissal_restrictions":{"users":[],"teams":[],"apps":[]},"dismiss_stale_reviews":false,"require_code_owner_reviews":false,"required_approving_review_count":0}`
if want != string(got) {
t.Errorf("PullRequestReviewsEnforcementRequest.MarshalJSON returned %+v, want %+v", string(got), want)
}
Expand Down

0 comments on commit fce4030

Please sign in to comment.