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

Commit

Permalink
Use AccessLevelValue for BaseAccessLevel and add documentation comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mauamy committed Oct 30, 2023
1 parent f919fb7 commit 9d247d2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 27 deletions.
63 changes: 38 additions & 25 deletions member_roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,48 @@ import (
"net/http"
)

const (
GuestBaseLevel BaseAccessLevel = 10
)

type BaseAccessLevel int

// MemberRolesService handles communication with the member roles related methods of
// the GitLab API.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/member_roles.html
type MemberRolesService struct {
client *Client
}

// MemberRole represents a GitLab member role.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/member_roles.html
type MemberRole struct {
ID int `json:"id"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
GroupId int `json:"group_id"`
BaseAccessLevel int `json:"base_access_level"`
AdminMergeRequests bool `json:"admin_merge_requests,omitempty"`
AdminVulnerability bool `json:"admin_vulnerability,omitempty"`
ReadCode bool `json:"read_code,omitempty"`
ReadDependency bool `json:"read_dependency,omitempty"`
ReadVulnerability bool `json:"read_vulnerability,omitempty"`
ManageProjectAccessToken bool `json:"manage_project_access_token,omitempty"`
ID int `json:"id"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
GroupId int `json:"group_id"`
BaseAccessLevel AccessLevelValue `json:"base_access_level"`
AdminMergeRequests bool `json:"admin_merge_requests,omitempty"`
AdminVulnerability bool `json:"admin_vulnerability,omitempty"`
ReadCode bool `json:"read_code,omitempty"`
ReadDependency bool `json:"read_dependency,omitempty"`
ReadVulnerability bool `json:"read_vulnerability,omitempty"`
ManageProjectAccessToken bool `json:"manage_project_access_token,omitempty"`
}

// CreateMemberRoleOptions represents the available CreateMemberRole() options.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/member_roles.html#add-a-member-role-to-a-group
type CreateMemberRoleOptions struct {
Name string `json:"name,"`
BaseAccessLevel BaseAccessLevel `json:"base_access_level"`
Description string `json:"description,omitempty"`
AdminMergeRequest bool `json:"admin_merge_request,omitempty"`
AdminVulnerability bool `json:"admin_vulnerability,omitempty"`
ReadCode bool `json:"read_code,omitempty"`
ReadDependency bool `json:"read_dependency,omitempty"`
ReadVulnerability bool `json:"read_vulnerability,omitempty"`
Name string `json:"name,"`
BaseAccessLevel AccessLevelValue `json:"base_access_level"`
Description string `json:"description,omitempty"`
AdminMergeRequest bool `json:"admin_merge_request,omitempty"`
AdminVulnerability bool `json:"admin_vulnerability,omitempty"`
ReadCode bool `json:"read_code,omitempty"`
ReadDependency bool `json:"read_dependency,omitempty"`
ReadVulnerability bool `json:"read_vulnerability,omitempty"`
}

// ListMemberRoles gets a list of member roles for a specified group.
//
// Gitlab API docs: https://docs.gitlab.com/ee/api/member_roles.html#list-all-member-roles-of-a-group
func (s *MemberRolesService) ListMemberRoles(groupId int, options ...RequestOptionFunc) ([]*MemberRole, *Response, error) {
path := fmt.Sprintf("groups/%d/member_roles", groupId)
req, err := s.client.NewRequest(http.MethodGet, path, nil, options)
Expand All @@ -56,6 +63,9 @@ func (s *MemberRolesService) ListMemberRoles(groupId int, options ...RequestOpti
return memberRoles, resp, nil
}

// CreateMemberRole creates a new member role for a specified group.
//
// Gitlab API docs: https://docs.gitlab.com/ee/api/member_roles.html#add-a-member-role-to-a-group
func (s *MemberRolesService) CreateMemberRole(groupId int, opt *CreateMemberRoleOptions, options ...RequestOptionFunc) (*MemberRole, *Response, error) {
path := fmt.Sprintf("groups/%d/member_roles", groupId)
req, err := s.client.NewRequest(http.MethodPost, path, opt, options)
Expand All @@ -72,6 +82,9 @@ func (s *MemberRolesService) CreateMemberRole(groupId int, opt *CreateMemberRole
return memberRole, resp, nil
}

// DeleteMemberRole deletes a member role from a specified group.
//
// Gitlab API docs: https://docs.gitlab.com/ee/api/member_roles.html#remove-member-role-of-a-group
func (s *MemberRolesService) DeleteMemberRole(groupId, memberRoleId int, options ...RequestOptionFunc) (*Response, error) {
path := fmt.Sprintf("groups/%d/member_roles/%d", groupId, memberRoleId)
req, err := s.client.NewRequest(http.MethodDelete, path, nil, options)
Expand Down
4 changes: 2 additions & 2 deletions member_roles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestCreateMemberRole(t *testing.T) {

memberRole, _, err := client.MemberRolesService.CreateMemberRole(84, &CreateMemberRoleOptions{
Name: "Custom guest",
BaseAccessLevel: 10,
BaseAccessLevel: GuestPermissions,
Description: "a sample custom role",
AdminMergeRequest: false,
AdminVulnerability: false,
Expand All @@ -78,7 +78,7 @@ func TestCreateMemberRole(t *testing.T) {
ID: 3,
Name: "Custom guest",
Description: "a sample custom role",
BaseAccessLevel: 10,
BaseAccessLevel: GuestPermissions,
GroupId: 84,
AdminMergeRequests: false,
AdminVulnerability: false,
Expand Down

0 comments on commit 9d247d2

Please sign in to comment.