Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to protect tags with group_id or user_id #1540

Merged
merged 3 commits into from Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 15 additions & 2 deletions protected_tags.go
Expand Up @@ -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"`
}
Expand Down Expand Up @@ -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
Expand Down
24 changes: 21 additions & 3 deletions protected_tags_test.go
Expand Up @@ -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{
Expand All @@ -78,6 +78,11 @@ func TestGetProtectedTag(t *testing.T) {
AccessLevel: 30,
AccessLevelDescription: "Developers + Maintainers",
},
{
AccessLevel: 40,
GroupID: 300,
AccessLevelDescription: "Sample Group",
},
},
}

Expand All @@ -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{
Expand All @@ -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")
Expand Down