Repository: xanzy/go-gitlab
Pull request #811 (2020-03-29 17:05:45)

Add ProtectedBranch to  ProjectApproval Rules
Add ID Field to ProtectedBranch struct and add protected branches to create/update approval rules.


Patch:
diff --git a/projects.go b/projects.go
index 7df17f3..baf8b7c 100644
--- a/projects.go
+++ b/projects.go
@@ -192,14 +192,15 @@ func (s Project) String() string {
 // GitLab API docs:
 // https://docs.gitlab.com/ee/api/merge_request_approvals.html#get-project-level-rules
 type ProjectApprovalRule struct {
-	ID                   int          `json:"id"`
-	Name                 string       `json:"name"`
-	RuleType             string       `json:"rule_type"`
-	EligibleApprovers    []*BasicUser `json:"eligible_approvers"`
-	ApprovalsRequired    int          `json:"approvals_required"`
-	Users                []*BasicUser `json:"users"`
-	Groups               []*Group     `json:"groups"`
-	ContainsHiddenGroups bool         `json:"contains_hidden_groups"`
+	ID                   int                `json:"id"`
+	Name                 string             `json:"name"`
+	RuleType             string             `json:"rule_type"`
+	EligibleApprovers    []*BasicUser       `json:"eligible_approvers"`
+	ApprovalsRequired    int                `json:"approvals_required"`
+	Users                []*BasicUser       `json:"users"`
+	Groups               []*Group           `json:"groups"`
+	ContainsHiddenGroups bool               `json:"contains_hidden_groups"`
+	ProtectedBranches    []*ProtectedBranch `json:"protected_branches"`
 }
 
 func (s ProjectApprovalRule) String() string {
@@ -1382,10 +1383,11 @@ func (s *ProjectsService) GetProjectApprovalRules(pid interface{}, options ...Op
 // GitLab API docs:
 // https://docs.gitlab.com/ee/api/merge_request_approvals.html#create-project-level-rules
 type CreateProjectLevelRuleOptions struct {
-	Name              *string `url:"name,omitempty" json:"name,omitempty"`
-	ApprovalsRequired *int    `url:"approvals_required,omitempty" json:"approvals_required,omitempty"`
-	UserIDs           []int   `url:"user_ids,omitempty" json:"user_ids,omitempty"`
-	GroupIDs          []int   `url:"group_ids,omitempty" json:"group_ids,omitempty"`
+	Name               *string `url:"name,omitempty" json:"name,omitempty"`
+	ApprovalsRequired  *int    `url:"approvals_required,omitempty" json:"approvals_required,omitempty"`
+	UserIDs            []int   `url:"user_ids,omitempty" json:"user_ids,omitempty"`
+	GroupIDs           []int   `url:"group_ids,omitempty" json:"group_ids,omitempty"`
+	ProtectedBranchIDs []int   `url:"protected_branch_ids,omitempty" json:"protected_branch_ids,omitempty"`
 }
 
 // CreateProjectApprovalRule creates a new project-level approval rule.
@@ -1419,10 +1421,11 @@ func (s *ProjectsService) CreateProjectApprovalRule(pid interface{}, opt *Create
 // GitLab API docs:
 // https://docs.gitlab.com/ee/api/merge_request_approvals.html#update-project-level-rules
 type UpdateProjectLevelRuleOptions struct {
-	Name              *string `url:"name,omitempty" json:"name,omitempty"`
-	ApprovalsRequired *int    `url:"approvals_required,omitempty" json:"approvals_required,omitempty"`
-	UserIDs           []int   `url:"user_ids,omitempty" json:"user_ids,omitempty"`
-	GroupIDs          []int   `url:"group_ids,omitempty" json:"group_ids,omitempty"`
+	Name               *string `url:"name,omitempty" json:"name,omitempty"`
+	ApprovalsRequired  *int    `url:"approvals_required,omitempty" json:"approvals_required,omitempty"`
+	UserIDs            []int   `url:"user_ids,omitempty" json:"user_ids,omitempty"`
+	GroupIDs           []int   `url:"group_ids,omitempty" json:"group_ids,omitempty"`
+	ProtectedBranchIDs []int   `url:"protected_branch_ids,omitempty" json:"protected_branch_ids,omitempty"`
 }
 
 // UpdateProjectApprovalRule updates an existing approval rule with new options.
diff --git a/protected_branches.go b/protected_branches.go
index 67a3025..069bb7f 100644
--- a/protected_branches.go
+++ b/protected_branches.go
@@ -45,6 +45,7 @@ type BranchAccessDescription struct {
 // GitLab API docs:
 // https://docs.gitlab.com/ce/api/protected_branches.html#list-protected-branches
 type ProtectedBranch struct {
+	ID                        int                        `json:"id"`
 	Name                      string                     `json:"name"`
 	PushAccessLevels          []*BranchAccessDescription `json:"push_access_levels"`
 	MergeAccessLevels         []*BranchAccessDescription `json:"merge_access_levels"`


Test patch:
diff --git a/projects_test.go b/projects_test.go
index 3406f7b..5a3b135 100644
--- a/projects_test.go
+++ b/projects_test.go
@@ -611,6 +611,31 @@ func TestGetProjectApprovalRules(t *testing.T) {
 						"ldap_access": null
 					}
 				],
+				"protected_branches": [
+					  {
+						"id": 1,
+						"name": "master",
+						"push_access_levels": [
+						  {
+							"access_level": 30,
+							"access_level_description": "Developers + Maintainers"
+						  }
+						],
+						"merge_access_levels": [
+						  {
+							"access_level": 30,
+							"access_level_description": "Developers + Maintainers"
+						  }
+						],
+						"unprotect_access_levels": [
+						  {
+							"access_level": 40,
+							"access_level_description": "Maintainers"
+						  }
+						],
+						"code_owner_approval_required": false
+					  }
+                ],
 				"contains_hidden_groups": false
 			}
 		]`)
@@ -670,6 +695,25 @@ func TestGetProjectApprovalRules(t *testing.T) {
 					FullPath:             "group1",
 				},
 			},
+			ProtectedBranches: []*ProtectedBranch{
+				{
+					ID: 1,
+					Name: "master",
+					PushAccessLevels: []*BranchAccessDescription{
+						{
+							AccessLevel:            30,
+							AccessLevelDescription: "Developers + Maintainers",
+						},
+					},
+					MergeAccessLevels: []*BranchAccessDescription{
+						{
+							AccessLevel:            30,
+							AccessLevelDescription: "Developers + Maintainers",
+						},
+					},
+					CodeOwnerApprovalRequired: false,
+				},
+			},
 		},
 	}
 
@@ -735,6 +779,31 @@ func TestCreateProjectApprovalRule(t *testing.T) {
 					"ldap_access": null
 				}
 			],
+			"protected_branches": [
+				{
+				  "id": 1,
+				  "name": "master",
+				  "push_access_levels": [
+					{
+					  "access_level": 30,
+					  "access_level_description": "Developers + Maintainers"
+					}
+				  ],
+				  "merge_access_levels": [
+					{
+					  "access_level": 30,
+					  "access_level_description": "Developers + Maintainers"
+					}
+				  ],
+				  "unprotect_access_levels": [
+					{
+					  "access_level": 40,
+					  "access_level_description": "Maintainers"
+					}
+				  ],
+				  "code_owner_approval_required": false
+				}
+			],
 			"contains_hidden_groups": false
 		}`)
 	})
@@ -799,6 +868,25 @@ func TestCreateProjectApprovalRule(t *testing.T) {
 				FullPath:             "group1",
 			},
 		},
+		ProtectedBranches: []*ProtectedBranch{
+			{
+				ID: 1,
+				Name: "master",
+				PushAccessLevels: []*BranchAccessDescription{
+					{
+						AccessLevel:            30,
+						AccessLevelDescription: "Developers + Maintainers",
+					},
+				},
+				MergeAccessLevels: []*BranchAccessDescription{
+					{
+						AccessLevel:            30,
+						AccessLevelDescription: "Developers + Maintainers",
+					},
+				},
+				CodeOwnerApprovalRequired: false,
+			},
+		},
 	}
 
 	if !reflect.DeepEqual(want, rule) {
diff --git a/protected_branches_test.go b/protected_branches_test.go
index 31c9131..ba2a545 100644
--- a/protected_branches_test.go
+++ b/protected_branches_test.go
@@ -33,6 +33,7 @@ func TestListProtectedBranches(t *testing.T) {
 	}
 	want := []*ProtectedBranch{
 		{
+			ID:   1,
 			Name: "master",
 			PushAccessLevels: []*BranchAccessDescription{
 				{
@@ -50,7 +51,7 @@ func TestListProtectedBranches(t *testing.T) {
 		},
 	}
 	if !reflect.DeepEqual(want, protectedBranches) {
-		t.Errorf("Projects.ListProjects returned %+v, want %+v", protectedBranches, want)
+		t.Errorf("ProtectedBranches.ListProtectedBranches returned %+v, want %+v", protectedBranches, want)
 	}
 }
 
@@ -79,6 +80,7 @@ func TestListProtectedBranchesWithoutCodeOwnerApproval(t *testing.T) {
 	}
 	want := []*ProtectedBranch{
 		{
+			ID:   1,
 			Name: "master",
 			PushAccessLevels: []*BranchAccessDescription{
 				{