Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce exact matching for GitLab groups (#4473) #4474

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions server/api/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func PostRepo(c *gin.Context) {
if errors.Is(err, types.RecordNotExist) {
org, err = _forge.Org(c, user, repo.Owner)
if err != nil {
msg := "could not fetch organization from forge."
msg := fmt.Sprintf("Organization %s not found in DB. Attempting to create new one.", repo.Owner)
log.Error().Err(err).Msg(msg)
c.String(http.StatusInternalServerError, msg)
return
Expand All @@ -139,7 +139,7 @@ func PostRepo(c *gin.Context) {
org.ForgeID = user.ForgeID
err = _store.OrgCreate(org)
if err != nil {
msg := "could not create organization in store."
msg := fmt.Sprintf("Failed to create organization %s.", repo.Owner)
log.Error().Err(err).Msg(msg)
c.String(http.StatusInternalServerError, msg)
return
Expand Down
16 changes: 12 additions & 4 deletions server/forge/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -753,21 +753,29 @@ func (g *GitLab) Org(ctx context.Context, u *model.User, owner string) (*model.O
groups, _, err := client.Groups.ListGroups(&gitlab.ListGroupsOptions{
ListOptions: gitlab.ListOptions{
Page: 1,
PerPage: 1,
PerPage: perPage,
},
Search: gitlab.Ptr(owner),
}, gitlab.WithContext(ctx))
if err != nil {
return nil, err
}

if len(groups) != 1 {
var matchedGroup *gitlab.Group
for _, group := range groups {
if group.FullPath == owner {
matchedGroup = group
break
}
}

if matchedGroup == nil {
return nil, fmt.Errorf("could not find org %s", owner)
}

return &model.Org{
Name: groups[0].FullPath,
Private: groups[0].Visibility != gitlab.PublicVisibility,
Name: matchedGroup.FullPath,
Private: matchedGroup.Visibility != gitlab.PublicVisibility,
}, nil
}

Expand Down