xanzy / go-gitlab

GitLab Go SDK
Apache License 2.0
2.43k stars 961 forks source link

Error on Groups.ListMembershipsForBillableGroupMember #2045

Closed brturner-rivian closed 1 month ago

brturner-rivian commented 1 month ago

I'm using most recent 'verified' at e4b083f042189e61b547bf3d967027bd6e31bd8e which includes recent changes to ListMembershipsForBillableGroupMemberOptions.

Calls to ListMembershipsForBillableGroupMember fail with message : invalid ID (int or string). (A curl request with identical user ID works).

The error is consistent regardless of wether user ID is valid or not.

Error message produced is

 %!(EXTRA *errors.errorString=invalid ID type &gitlab.Client{client:(*retryablehttp.Client)(0x14000162280), baseURL:(*url.URL)(0x1400017e090), disableRetries:false, configureLimiterOnce:sync.Once{done:atomic.Uint32{_:atomic.noCopy{}, v:0x1}, m:sync.Mutex{state:0, sema:0x0}}, limiter:(*rate.Limiter)(0x14000166140), authType:3, username:"", password:"", token:"[redacted]", tokenLock:sync.RWMutex{w:sync.Mutex{state:0, sema:0x0}, writerSem:0x0, readerSem:0x0, readerCount:atomic.Int32{_:atomic.noCopy{}, v:0}, readerWait:atomic.Int32{_:atomic.noCopy{}, v:0}}, defaultRequestOptions:[]gitlab.RequestOptionFunc(nil), UserAgent:"go-gitlab", AccessRequests:(*gitlab.AccessRequestsService)(0x14000114068), Appearance:(*gitlab.AppearanceService)(0x14000114070), Applications:(*gitlab.ApplicationsService)(0x14000114078), AuditEvents:(*gitlab.AuditEventsService)(0x14000114080), Avatar:(*gitlab.AvatarRequestsService)(0x14000114088), AwardEmoji:(*gitlab.AwardEmojiService)(0x14000114090), Boards:(*gitlab.IssueBoardsService)(0x14000114098), Branches:(*gitlab.BranchesService)(0x140001140a0), BroadcastMessage:(*gitlab.BroadcastMessagesService)(0x140001140a8), CIYMLTemplate:(*gitlab.CIYMLTemplatesService)(0x140001140b0), ClusterAgents:(*gitlab.ClusterAgentsService)(0x140001140b8), Commits:(*gitlab.CommitsService)(0x140001140c0), ContainerRegistry:(*gitlab.ContainerRegistryService)(0x140001140c8), CustomAttribute:(*gitlab.CustomAttributesService)(0x140001140d0), DeployKeys:(*gitlab.DeployKeysService)(0x140001140d8), DeployTokens:(*gitlab.DeployTokensService)(0x140001140e0), DeploymentMergeRequests:(*gitlab.DeploymentMergeRequestsService)(0x140001140e8), Deployments:(*gitlab.DeploymentsService)(0x140001140f0), Discussions:(*gitlab.DiscussionsService)(0x140001140f8), DockerfileTemplate:(*gitlab.DockerfileTemplatesService)(0x14000114100), DORAMetrics:(*gitlab.DORAMetricsService)(0x14000114108), DraftNotes:(*gitlab.DraftNotesService)(0x14000114110), Environments:(*gitlab.EnvironmentsService)(0x14000114118), EpicIssues:(*gitlab.EpicIssuesService)(0x14000114120), Epics:(*gitlab.EpicsService)(0x14000114128), ErrorTracking:(*gitlab.ErrorTrackingService)(0x14000114130), Events:(*gitlab.EventsService)(0x14000114138), ExternalStatusChecks:(*gitlab.ExternalStatusChecksService)(0x14000114140), Features:(*gitlab.FeaturesService)(0x14000114148), FreezePeriods:(*gitlab.FreezePeriodsService)(0x14000114150), GenericPackages:(*gitlab.GenericPackagesService)(0x14000114158), GeoNodes:(*gitlab.GeoNodesService)(0x14000114160), GitIgnoreTemplates:(*gitlab.GitIgnoreTemplatesService)(0x14000114168), GroupAccessTokens:(*gitlab.GroupAccessTokensService)(0x14000114170), GroupBadges:(*gitlab.GroupBadgesService)(0x14000114178), GroupCluster:(*gitlab.GroupClustersService)(0x14000114180), GroupEpicBoards:(*gitlab.GroupEpicBoardsService)(0x14000114188), GroupImportExport:(*gitlab.GroupImportExportService)(0x14000114190), GroupIssueBoards:(*gitlab.GroupIssueBoardsService)(0x14000114198), GroupIterations:(*gitlab.GroupIterationsService)(0x140001141a0), GroupLabels:(*gitlab.GroupLabelsService)(0x140001141a8), GroupMembers:(*gitlab.GroupMembersService)(0x140001141b0), GroupMilestones:(*gitlab.GroupMilestonesService)(0x140001141b8), GroupProtectedEnvironments:(*gitlab.GroupProtectedEnvironmentsService)(0x140001141c0), GroupRepositoryStorageMove:(*gitlab.GroupRepositoryStorageMoveService)(0x140001141c8), GroupSSHCertificates:(*gitlab.GroupSSHCertificatesService)(0x140001141d0), GroupVariables:(*gitlab.GroupVariablesService)(0x140001141d8), GroupWikis:(*gitlab.GroupWikisService)(0x140001141e0), Groups:(*gitlab.GroupsService)(0x140001141e8), Import:(*gitlab.ImportService)(0x140001141f0), InstanceCluster:(*gitlab.InstanceClustersService)(0x140001141f8), InstanceVariables:(*gitlab.InstanceVariablesService)(0x14000114200), Invites:(*gitlab.InvitesService)(0x14000114208), IssueLinks:(*gitlab.IssueLinksService)(0x14000114210), Issues:(*gitlab.IssuesService)(0x14000112200), IssuesStatistics:(*gitlab.IssuesStatisticsService)(0x14000114218), Jobs:(*gitlab.JobsService)(0x14000114220), JobTokenScope:(*gitlab.JobTokenScopeService)(0x14000114228), Keys:(*gitlab.KeysService)(0x14000114230), Labels:(*gitlab.LabelsService)(0x14000114238), License:(*gitlab.LicenseService)(0x14000114240), LicenseTemplates:(*gitlab.LicenseTemplatesService)(0x14000114248), ManagedLicenses:(*gitlab.ManagedLicensesService)(0x14000114250), Markdown:(*gitlab.MarkdownService)(0x14000114258), MemberRolesService:(*gitlab.MemberRolesService)(0x14000114260), MergeRequestApprovals:(*gitlab.MergeRequestApprovalsService)(0x14000114268), MergeRequests:(*gitlab.MergeRequestsService)(0x14000112210), MergeTrains:(*gitlab.MergeTrainsService)(0x14000114270), Metadata:(*gitlab.MetadataService)(0x14000114278), Milestones:(*gitlab.MilestonesService)(0x14000114280), Namespaces:(*gitlab.NamespacesService)(0x14000114288), Notes:(*gitlab.NotesService)(0x14000114290), NotificationSettings:(*gitlab.NotificationSettingsService)(0x14000114298), Packages:(*gitlab.PackagesService)(0x140001142a0), Pages:(*gitlab.PagesService)(0x140001142a8), PagesDomains:(*gitlab.PagesDomainsService)(0x140001142b0), PersonalAccessTokens:(*gitlab.PersonalAccessTokensService)(0x140001142b8), PipelineSchedules:(*gitlab.PipelineSchedulesService)(0x140001142c0), PipelineTriggers:(*gitlab.PipelineTriggersService)(0x140001142c8), Pipelines:(*gitlab.PipelinesService)(0x140001142d0), PlanLimits:(*gitlab.PlanLimitsService)(0x140001142d8), ProjectAccessTokens:(*gitlab.ProjectAccessTokensService)(0x140001142e0), ProjectBadges:(*gitlab.ProjectBadgesService)(0x140001142e8), ProjectCluster:(*gitlab.ProjectClustersService)(0x140001142f0), ProjectFeatureFlags:(*gitlab.ProjectFeatureFlagService)(0x140001142f8), ProjectImportExport:(*gitlab.ProjectImportExportService)(0x14000114300), ProjectIterations:(*gitlab.ProjectIterationsService)(0x14000114308), ProjectMembers:(*gitlab.ProjectMembersService)(0x14000114310), ProjectMirrors:(*gitlab.ProjectMirrorService)(0x14000114318), ProjectRepositoryStorageMove:(*gitlab.ProjectRepositoryStorageMoveService)(0x14000114320), ProjectSnippets:(*gitlab.ProjectSnippetsService)(0x14000114328), ProjectTemplates:(*gitlab.ProjectTemplatesService)(0x14000114330), ProjectVariables:(*gitlab.ProjectVariablesService)(0x14000114338), ProjectVulnerabilities:(*gitlab.ProjectVulnerabilitiesService)(0x14000114340), Projects:(*gitlab.ProjectsService)(0x14000114348), ProtectedBranches:(*gitlab.ProtectedBranchesService)(0x14000114350), ProtectedEnvironments:(*gitlab.ProtectedEnvironmentsService)(0x14000114358), ProtectedTags:(*gitlab.ProtectedTagsService)(0x14000114360), ReleaseLinks:(*gitlab.ReleaseLinksService)(0x14000114368), Releases:(*gitlab.ReleasesService)(0x14000114370), Repositories:(*gitlab.RepositoriesService)(0x14000114378), RepositoryFiles:(*gitlab.RepositoryFilesService)(0x14000114380), RepositorySubmodules:(*gitlab.RepositorySubmodulesService)(0x14000114388), ResourceGroup:(*gitlab.ResourceGroupService)(0x14000114390), ResourceIterationEvents:(*gitlab.ResourceIterationEventsService)(0x14000114398), ResourceLabelEvents:(*gitlab.ResourceLabelEventsService)(0x140001143a0), ResourceMilestoneEvents:(*gitlab.ResourceMilestoneEventsService)(0x140001143a8), ResourceStateEvents:(*gitlab.ResourceStateEventsService)(0x140001143b0), ResourceWeightEvents:(*gitlab.ResourceWeightEventsService)(0x140001143b8), Runners:(*gitlab.RunnersService)(0x140001143c0), Search:(*gitlab.SearchService)(0x140001143c8), Services:(*gitlab.ServicesService)(0x140001143d0), Settings:(*gitlab.SettingsService)(0x140001143d8), Sidekiq:(*gitlab.SidekiqService)(0x140001143e0), SnippetRepositoryStorageMove:(*gitlab.SnippetRepositoryStorageMoveService)(0x140001143f0), Snippets:(*gitlab.SnippetsService)(0x140001143e8), SystemHooks:(*gitlab.SystemHooksService)(0x140001143f8), Tags:(*gitlab.TagsService)(0x14000114400), Todos:(*gitlab.TodosService)(0x14000114408), Topics:(*gitlab.TopicsService)(0x14000114410), Users:(*gitlab.UsersService)(0x14000114418), Validate:(*gitlab.ValidateService)(0x14000114420), Version:(*gitlab.VersionService)(0x14000114428), Wikis:(*gitlab.WikisService)(0x14000114430)}, the ID must be an int or a string)
exit status 1
brturner-rivian commented 1 month ago

btw - the go-gitlab package is amazingly helpful

svanharmelen commented 1 month ago

Can you share an example (code) of how you're making this call as well as what the cURL command look like?

biltirner commented 1 month ago

The generic curl would be curl --request GET --header "PRIVATE-TOKEN: [redacted]" "https://gitlab.com/api/v4/groups/{group id}/billable_members/{user ID}/memberships"

in my case curl --request GET --header "PRIVATE-TOKEN: [redacted]" "https://gitlab.com/api/v4/groups/1891092/billable_members/10024951/memberships"

the payload returned (sanitized) looks like

[{"id":43867499,"source_id":1891092,"source_full_name":"rootgroup","source_members_url":"https://gitlab.com/groups/rootgroup/-/group_members","created_at":"2021-10-20T20:23:07.538Z","expires_at":null,"access_level":{"string_value":"Minimal Access","integer_value":5,"custom_role":null}},{"id":43868462,"source_id":8320573,"source_full_name":"rootgroup / group subgroup","source_members_url":"https://gitlab.com/groups/rootgroup/dc/-/group_members","created_at":"2021-10-20T21:13:35.699Z","expires_at":null,"access_level":{"string_value":"Developer","integer_value":30,"custom_role":null}},{"id":43868701,"source_id":8362854,"source_full_name":"rootgroup / Project Templates","source_members_url":"https://gitlab.com/groups/rootgroup/project-templates/-/group_members","created_at":"2021-10-20T21:24:09.416Z","expires_at":null,"access_level":{"string_value":"Developer","integer_value":30,"custom_role":null}},{"id":43868861,"source_id":19534091,"source_full_name":"rootgroup / File Templates","source_members_url":"https://gitlab.com/rootgroup/file-templates/-/project_members","created_at":"2021-10-20T21:34:31.194Z","expires_at":null,"access_level":{"string_value":"Developer","integer_value":30,"custom_role":null}},{"id":81092669,"source_id":67944258,"source_full_name":"rootgroup / group subgroup / team subgroup / Diagnostics / Fleet Management / YMS","source_members_url":"https://gitlab.com/groups/rootgroup/dc/team-subgroup/diagnostics/team-applications/yms/-/group_members","created_at":"2023-06-30T16:10:17.565Z","expires_at":null,"access_level":{"string_value":"Maintainer","integer_value":40,"custom_role":null}},{"id":83345375,"source_id":43600229,"source_full_name":"rootgroup / group subgroup2 / platform team / Data atlas dbt","source_members_url":"https://gitlab.com/rootgroup/vt/platform-team/data-atlas-dbt/-/project_members","created_at":"2023-08-09T19:11:12.101Z","expires_at":null,"access_level":{"string_value":"Developer","integer_value":30,"custom_role":null}},{"id":88817886,"source_id":45282881,"source_full_name":"rootgroup / group subgroup / team subgroup / Diagnostics / ABC Cloud / cloud-abc-app","source_members_url":"https://gitlab.com/rootgroup/dc/team-subgroup/diagnostics/abc-cloud/cloud-abc-app/-/project_members","created_at":"2023-10-06T17:22:29.508Z","expires_at":null,"access_level":{"string_value":"Maintainer","integer_value":40,"custom_role":null}},{"id":97276664,"source_id":24876491,"source_full_name":"rootgroup / group subgroup2 / platform team / data-ai-hub","source_members_url":"https://gitlab.com/rootgroup/vt/platform-team/data-atlas-terraform/-/project_members","created_at":"2024-03-19T17:01:18.267Z","expires_at":null,"access_level":{"string_value":"Maintainer","integer_value":40,"custom_role":null}}]      
platform team
biltirner commented 1 month ago

aannddd.... in putting together the go script for this issue I discovered my mistake (passing an invalid group ID). I thought the call required the git client address.

Corrected script: package main

import ( "fmt" "github.com/xanzy/go-gitlab" "log" "time" )

func changeUserPermissions(git *gitlab.Client, userID int, reportOnly bool) { // get all memberships for this user

listMemberOptions := &gitlab.ListMembershipsForBillableGroupMemberOptions{
    PerPage: 100,
    Page:    1,
}

// Mistake: Group ID, NOT git client address memberships, _, err := git.Groups.ListMembershipsForBillableGroupMember(1891092, userID, listMemberOptions) if err != nil { log.Fatalf("\n \n ", err) } else { fmt.Printf("\n \n Success! : %v", memberships) }

}

func main() { fmt.Printf("\n Running - Change Inactive Users to Minimal Access \n") fmt.Printf("\n Look Back Date : %v \n", time.Now().AddDate(0, 0, -60)) // Create Client baseURL := "https://gitlab.com/" glToken := "[redacted]" reportOnly := true git, err := gitlab.NewClient(glToken, gitlab.WithBaseURL(baseURL)) if err != nil { fmt.Printf("\n fatal ") fmt.Println(git) log.Fatalf("\n Failed to create client: %v", err) } changeUserPermissions(git, 10024951, reportOnly) }

Apologies, please close the issue and thanks for your time

svanharmelen commented 1 month ago

All good, glad it works as expected.