Skip to content
This repository was archived by the owner on Dec 10, 2024. It is now read-only.
This repository was archived by the owner on Dec 10, 2024. It is now read-only.

Feature Request: Get Groups by Custom Attribute #1617

@adrianliechti

Description

@adrianliechti

According the GitLab Docs, there is a handy query to get groups by custom attributes:
You can filter by custom attributes](https://docs.gitlab.com/ee/api/custom_attributes.html with:
GET /groups?custom_attributes[key]=value&custom_attributes[other_key]=other_value

I have not found this option in the current version (of this amazing lib).
Could you consider an option to do it? Maybe using a map on the ListGroupsOptions? or by having a RequestOptionFunc WithCustomQuery(key, value)

In the meantime, I used this workaround:

func (g *GitLab) groupByAttribute(ctx context.Context, key, value string) (*gitlab.Group, error) {
	opt := &gitlab.ListGroupsOptions{
		WithCustomAttributes: gitlab.Bool(true),
	}

	withCustomAttribute := func() gitlab.RequestOptionFunc {
		return func(req *retryablehttp.Request) error {
			query := fmt.Sprintf("custom_attributes[%s]=%s", key, value)

			if req.URL.RawQuery != "" {
				query += "&" + req.URL.RawQuery
			}

			req.URL.RawQuery = query

			return nil
		}
	}

	groups, _, err := g.client.Groups.ListGroups(opt, gitlab.WithContext(ctx), withCustomAttribute())

	if err != nil {
		return nil, err
	}

	if len(groups) == 0 {
		return nil, errors.New("no group found with key: " + key)
	}

	if len(groups) > 1 {
		return nil, errors.New("multiple groups found with key: " + key)
	}

	return groups[0], nil
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions