From a0d38bd5b48f7d0f1dbc1a371eddbbdcc7ea3c51 Mon Sep 17 00:00:00 2001 From: Amar Takhar Date: Sat, 16 Nov 2024 04:08:19 -0500 Subject: [PATCH] Add a workaround for organisations in GitLab This fixes Contributors. Currently organisations are an experimental features in GitLab this change makes it use the users email domain as a pseudo organisation. The domains are anonymised as these are printed as part of the report and shouldn't be exposed. It's uncertain if the uptake in organisation will be large considering GitLab users are used to using groups for this purpose. Signed-off-by: Amar Takhar --- clients/gitlabrepo/contributors.go | 37 +++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/clients/gitlabrepo/contributors.go b/clients/gitlabrepo/contributors.go index 8959df39055..190805b73b0 100644 --- a/clients/gitlabrepo/contributors.go +++ b/clients/gitlabrepo/contributors.go @@ -84,7 +84,34 @@ func (handler *contributorsHandler) retrieveUsers(queryName string) ([]*gitlab.U return users, nil } +// Orginisation is an experimental feature in GitLab. Takes a slice +// pointer to maintain list of domains and email returns an anonymised +// string with the slice index as a unique identifier. +// Pointer is required as we are modifying the slice length. +func getOrginisation(orgDomain *[]string, orgReal string) string { + // Some repositories date before email was widely used. + emailSplit := strings.Split(orgReal, "@") + var orgName string // Orginisation name + if len(emailSplit) > 1 { + orgName = emailSplit[1] + } else { + orgName = emailSplit[0] // Not an "email" + } + + *orgDomain = append(*orgDomain, orgName) + + // Search for domain and use index as unique marker + for i, domain := range *orgDomain { + if domain == orgName { + return fmt.Sprint("GitLab-", i) + } + } + return "GitLab" +} + func (handler *contributorsHandler) setup() error { + var orgDomain []string // Slice of email domains + handler.once.Do(func() { if !strings.EqualFold(handler.repourl.commitSHA, clients.HeadSHA) { handler.errSetup = fmt.Errorf("%w: ListContributors only supported for HEAD queries", @@ -126,9 +153,17 @@ func (handler *contributorsHandler) setup() error { user = users[0] } + // In case someone is using the experimental feature. + var orgName string + if user.Organization == "" { + orgName = getOrginisation(&orgDomain, contrib.Email) + } else { + orgName = user.Organization + } + contributor := clients.User{ Login: contrib.Email, - Companies: []string{user.Organization}, + Companies: []string{orgName}, NumContributions: contrib.Commits, ID: int64(user.ID), IsBot: user.Bot,