Repository: xanzy/go-gitlab
Pull request #1540 (2022-09-01 00:54:20)

Add support to protect tags with group_id or user_id
This resolves https://github.com/xanzy/go-gitlab/issues/1511 


Patch:
diff --git a/protected_tags.go b/protected_tags.go
index f53c989..4f8fb4e 100644
--- a/protected_tags.go
+++ b/protected_tags.go
@@ -44,6 +44,8 @@ type ProtectedTag struct {
 // GitLab API docs:
 // https://docs.gitlab.com/ee/api/protected_tags.html
 type TagAccessDescription struct {
+	UserID                 int              `json:"user_id"`
+	GroupID                int              `json:"group_id"`
 	AccessLevel            AccessLevelValue `json:"access_level"`
 	AccessLevelDescription string           `json:"access_level_description"`
 }
@@ -111,8 +113,19 @@ func (s *ProtectedTagsService) GetProtectedTag(pid interface{}, tag string, opti
 // GitLab API docs:
 // https://docs.gitlab.com/ee/api/protected_tags.html#protect-repository-tags
 type ProtectRepositoryTagsOptions struct {
-	Name              *string           `url:"name" json:"name"`
-	CreateAccessLevel *AccessLevelValue `url:"create_access_level,omitempty" json:"create_access_level,omitempty"`
+	Name              *string                   `url:"name,omitempty" json:"name,omitempty"`
+	CreateAccessLevel *AccessLevelValue         `url:"create_access_level,omitempty" json:"create_access_level,omitempty"`
+	AllowedToCreate   *[]*TagsPermissionOptions `url:"allowed_to_create,omitempty" json:"allowed_to_create,omitempty"`
+}
+
+// TagsPermissionOptions represents a protected tag permission option.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ee/api/protected_tags.html#protect-repository-tags
+type TagsPermissionOptions struct {
+	UserID      *int              `url:"user_id,omitempty" json:"user_id,omitempty"`
+	GroupID     *int              `url:"group_id,omitempty" json:"group_id,omitempty"`
+	AccessLevel *AccessLevelValue `url:"access_level,omitempty" json:"access_level,omitempty"`
 }
 
 // ProtectRepositoryTags protects a single repository tag or several project


Test patch:
diff --git a/protected_tags_test.go b/protected_tags_test.go
index 66337fa..94c7a1a 100644
--- a/protected_tags_test.go
+++ b/protected_tags_test.go
@@ -68,7 +68,7 @@ func TestGetProtectedTag(t *testing.T) {
 
 	mux.HandleFunc(fmt.Sprintf("/api/v4/projects/1/protected_tags/%s", tagName), func(w http.ResponseWriter, r *http.Request) {
 		testMethod(t, r, http.MethodGet)
-		fmt.Fprint(w, `{"name":"my-awesome-tag", "create_access_levels": [{"access_level": 30, "access_level_description": "Developers + Maintainers"}]}`)
+		fmt.Fprint(w, `{"name":"my-awesome-tag", "create_access_levels": [{"access_level": 30, "access_level_description": "Developers + Maintainers"},{"access_level": 40, "access_level_description": "Sample Group", "group_id": 300}]}`)
 	})
 
 	expected := &ProtectedTag{
@@ -78,6 +78,11 @@ func TestGetProtectedTag(t *testing.T) {
 				AccessLevel:            30,
 				AccessLevelDescription: "Developers + Maintainers",
 			},
+			{
+				AccessLevel:            40,
+				GroupID:                300,
+				AccessLevelDescription: "Sample Group",
+			},
 		},
 	}
 
@@ -93,7 +98,7 @@ func TestProtectRepositoryTags(t *testing.T) {
 
 	mux.HandleFunc("/api/v4/projects/1/protected_tags", func(w http.ResponseWriter, r *http.Request) {
 		testMethod(t, r, http.MethodPost)
-		fmt.Fprint(w, `{"name":"my-awesome-tag", "create_access_levels": [{"access_level": 30, "access_level_description": "Developers + Maintainers"}]}`)
+		fmt.Fprint(w, `{"name":"my-awesome-tag", "create_access_levels": [{"access_level": 30, "access_level_description": "Developers + Maintainers"},{"access_level": 40, "access_level_description": "Sample Group", "group_id": 300}]}`)
 	})
 
 	expected := &ProtectedTag{
@@ -103,10 +108,23 @@ func TestProtectRepositoryTags(t *testing.T) {
 				AccessLevel:            30,
 				AccessLevelDescription: "Developers + Maintainers",
 			},
+			{
+				AccessLevel:            40,
+				GroupID:                300,
+				AccessLevelDescription: "Sample Group",
+			},
 		},
 	}
 
-	opt := &ProtectRepositoryTagsOptions{Name: String("my-awesome-tag"), CreateAccessLevel: AccessLevel(30)}
+	opt := &ProtectRepositoryTagsOptions{
+		Name:              String("my-awesome-tag"),
+		CreateAccessLevel: AccessLevel(30),
+		AllowedToCreate: &[]*TagsPermissionOptions{
+			{
+				GroupID: Int(300),
+			},
+		},
+	}
 	tag, _, err := client.ProtectedTags.ProtectRepositoryTags(1, opt)
 
 	assert.NoError(t, err, "failed to get response")