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

Add support to Groups APIs for DefaultBranchDefaults #1971

Merged

Conversation

RicePatrick
Copy link
Contributor

This Pull Request adds support for the new default_branch_protection_defaults attribute to the Group APIs. These were added in GitLab 17.0, replacing the default_branch_protection integer, so that attribute is now marked as deprecated as well.

Documentation: https://docs.gitlab.com/ee/api/groups.html#options-for-default_branch_protection_defaults

Note: The structure of the API requests is weird. Even though allowed_to_push and allowed_to_merge only expect 1 access level, they do in fact require an array of objects with "access_level" being the key.

Here is an example request and responss to show the API fingerprint, since I don't think it's exactly clear from the documentation how it's supposed to work 😆 :

image

{"default_branch_protection_defaults":{"allowed_to_push":[{"access_level": 40}], "allow_force_push":false, "allowed_to_merge":[{"access_level":30}], "developer_can_initial_push": true}}
{"id":3,"web_url":"http://dcfd377d91d2/groups/test","name":"test","path":"test","description":"","visibility":"public","share_with_group_lock":false,"require_two_factor_authentication":false,"two_factor_grace_period":48,"project_creation_level":"developer","auto_devops_enabled":null,"subgroup_creation_level":"maintainer","emails_disabled":false,"emails_enabled":true,"mentions_disabled":null,"lfs_enabled":true,"math_rendering_limits_enabled":true,"lock_math_rendering_limits_enabled":false,"default_branch":null,"default_branch_protection":2,"default_branch_protection_defaults":{"allowed_to_push":[{"access_level":40}],"allow_force_push":false,"allowed_to_merge":[{"access_level":30}],"developer_can_initial_push":true},"avatar_url":null,"request_access_enabled":true,"full_name":"test","full_path":"test","created_at":"2024-07-12T18:58:09.554Z","parent_id":null,"organization_id":1,"shared_runners_setting":"enabled","ldap_cn":null,"ldap_access":null,"marked_for_deletion_on":null,"wiki_access_level":"enabled","repository_storage":null,"duo_features_enabled":true,"lock_duo_features_enabled":false,"shared_with_groups":[],"runners_token":"GR13489413zpxPbiz-s5TLjsMNi7x","enabled_git_access_protocol":"all","prevent_sharing_groups_outside_hierarchy":false,"projects":[],"shared_projects":[],"shared_runners_minutes_limit":null,"extra_shared_runners_minutes_limit":null,"prevent_forking_outside_group":false,"service_access_tokens_expiration_enforced":true,"membership_lock":false,"ip_restriction_ranges":null}

@RicePatrick
Copy link
Contributor Author

@svanharmelen - This should be ready for review. Let me know what you think about the naming for GroupAccessLevel, or if there's a better struct already for that use-case. It's an odd structure, so I was having trouble naming it 😄

Copy link
Member

@svanharmelen svanharmelen left a comment

Choose a reason for hiding this comment

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

Have a few suggestion, but other then that I'm good with this...

@@ -90,6 +95,16 @@ type Group struct {

// Deprecated: Use EmailsEnabled instead
EmailsDisabled bool `json:"emails_disabled"`

// Deprecated: Use DefaultBranchProtectionDefaults instead
DefaultBranchProtection int `json:"default_branch_protection"`
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is deprecated looking at the docs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@svanharmelen - Hm, some of the conversations I've had with the GitLab employees certainly seem to insinuate it is, so I never thought about the fact that it isn't documented that way 🤔

I'll talk with them on Monday and confirm it's status, then I'll either update the documentation or update this PR. Thanks for the great catch!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

groups.go Show resolved Hide resolved
groups.go Outdated
DefaultBranchProtection int `json:"default_branch_protection"`
}

// GroupAccessLevel represents an entry in a Group defining the access level for default protections
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
// GroupAccessLevel represents an entry in a Group defining the access level for default protections
// GroupAccessLevel represents default branch protection defaults access levels.

Still not great, but close enough.

groups.go Outdated

// GroupAccessLevel represents an entry in a Group defining the access level for default protections
//
// GitLab API docs: https://docs.gitlab.com/ee/api/groups.html#options-for-default_branch_protection_defaults
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
// GitLab API docs: https://docs.gitlab.com/ee/api/groups.html#options-for-default_branch_protection_defaults
// GitLab API docs:
// https://docs.gitlab.com/ee/api/groups.html#options-for-default_branch_protection_defaults


// Deprecated: Use EmailsEnabled instead
EmailsDisabled *bool `url:"emails_disabled,omitempty" json:"emails_disabled,omitempty"`

// Deprecated: User DefaultBranchProtectionDefaults instead
DefaultBranchProtection *int `url:"default_branch_protection,omitempty" json:"default_branch_protection,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

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

Same for this one of course (re the deprecation).

groups.go Outdated
DefaultBranchProtection *int `url:"default_branch_protection,omitempty" json:"default_branch_protection,omitempty"`
}

// DefaultBranchProtectionDefaultsOptions represents the available options for using default_branch_protection_defaults in CreateGroup() or UpdateGroup()
Copy link
Member

Choose a reason for hiding this comment

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

Please keep comments below 80 chars when possible...

Suggested change
// DefaultBranchProtectionDefaultsOptions represents the available options for using default_branch_protection_defaults in CreateGroup() or UpdateGroup()
// DefaultBranchProtectionDefaultsOptions represents the available options for
// using default_branch_protection_defaults in CreateGroup() or UpdateGroup()

groups.go Outdated

// DefaultBranchProtectionDefaultsOptions represents the available options for using default_branch_protection_defaults in CreateGroup() or UpdateGroup()
//
// GitLab API docs: https://docs.gitlab.com/ee/api/groups.html#options-for-default_branch_protection_defaults
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
// GitLab API docs: https://docs.gitlab.com/ee/api/groups.html#options-for-default_branch_protection_defaults
// GitLab API docs:
// https://docs.gitlab.com/ee/api/groups.html#options-for-default_branch_protection_defaults

groups.go Outdated
Comment on lines 391 to 394
AllowedToPush []GroupAccessLevel `url:"allowed_to_push,omitempty" json:"allowed_to_push,omitempty"`
AllowForcePush bool `url:"allow_force_push,omitempty" json:"allow_force_push,omitempty"`
AllowedToMerge []GroupAccessLevel `url:"allowed_to_merge.omitempty" json:"allowed_to_merge.omitempty"`
DeveloperCanInitialPush bool `url:"developer_can_initial_push,omitempty" json:"developer_can_initial_push,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

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

However weird it may be, these should all be pointers as this is an options struct...

Suggested change
AllowedToPush []GroupAccessLevel `url:"allowed_to_push,omitempty" json:"allowed_to_push,omitempty"`
AllowForcePush bool `url:"allow_force_push,omitempty" json:"allow_force_push,omitempty"`
AllowedToMerge []GroupAccessLevel `url:"allowed_to_merge.omitempty" json:"allowed_to_merge.omitempty"`
DeveloperCanInitialPush bool `url:"developer_can_initial_push,omitempty" json:"developer_can_initial_push,omitempty"`
AllowedToPush *[]*GroupAccessLevel `url:"allowed_to_push,omitempty" json:"allowed_to_push,omitempty"`
AllowForcePush *bool `url:"allow_force_push,omitempty" json:"allow_force_push,omitempty"`
AllowedToMerge *[]*GroupAccessLevel `url:"allowed_to_merge.omitempty" json:"allowed_to_merge.omitempty"`
DeveloperCanInitialPush *bool `url:"developer_can_initial_push,omitempty" json:"developer_can_initial_push,omitempty"`


// Deprecated: Use EmailsEnabled instead
EmailsDisabled *bool `url:"emails_disabled,omitempty" json:"emails_disabled,omitempty"`

// Deprecated: Use DefaultBranchProtectionDefaults instead
DefaultBranchProtection *int `url:"default_branch_protection,omitempty" json:"default_branch_protection,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

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

Same...

@RicePatrick
Copy link
Contributor Author

Everything other than the deprecation notices has been fixed; will update Deprecation noticed in either direction on Monday and let you know!

@RicePatrick
Copy link
Contributor Author

@svanharmelen - Documentation is updated and now shows the fields as Deprecated, so having them labelled as deprecated is correct: https://docs.gitlab.com/ee/api/groups.html#new-group

Reference from conversations with GitLab: https://gitlab.com/gitlab-org/gitlab/-/issues/462164#note_1999148308

@svanharmelen svanharmelen merged commit 32b7839 into xanzy:main Jul 16, 2024
3 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants