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 2 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 @@ -46,6 +46,8 @@ type ProtectedTag struct {
type TagAccessDescription struct {
AccessLevel AccessLevelValue `json:"access_level"`
AccessLevelDescription string `json:"access_level_description"`
UserID *int `url:"user_id,omitempty" json:"user_id,omitempty"`
GroupID *int `url:"group_id,omitempty" json:"group_id,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot see in the docs that these fields need to be here. Can you point me to somewhere (in the code) that shows these fields exist in this struct?

Additionally in a response struct like this we don't use pointers for build-in types and URL tags.

}

// ListProtectedTagsOptions represents the available ListProtectedTags()
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" json:"name"`
CreateAccessLevel *AccessLevelValue `url:"create_access_level,omitempty" json:"create_access_level,omitempty"`
AllowedToCreate *[]ProtectRepositoryTagsPermissionOptions `url:"allowed_to_create,omitempty" json:"allowed_to_create,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
AllowedToCreate *[]ProtectRepositoryTagsPermissionOptions `url:"allowed_to_create,omitempty" json:"allowed_to_create,omitempty"`
AllowedToCreate *[]*ProtectRepositoryTagsPermissionOptions `url:"allowed_to_create,omitempty" json:"allowed_to_create,omitempty"`

}

// ProtectRepositoryTagsPermissionOptions represents a protected tag permission option.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/protected_tags.html#protect-repository-tags
type ProtectRepositoryTagsPermissionOptions struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe make this just a little shorter, like TagsPermissionOptions or something?

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: Int(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: Int(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: &[]ProtectRepositoryTagsPermissionOptions{
{
GroupID: Int(300),
},
},
}
tag, _, err := client.ProtectedTags.ProtectRepositoryTags(1, opt)

assert.NoError(t, err, "failed to get response")
Expand Down