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

Co-authored-by: Patrick Schratz <patrick.schratz@gmail.com>
This commit is contained in:
6543 2024-11-28 19:49:00 +01:00 committed by GitHub
parent f85192ba4a
commit 50a3749b60
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 6 deletions

View file

@ -130,7 +130,7 @@ func PostRepo(c *gin.Context) {
if errors.Is(err, types.RecordNotExist) { if errors.Is(err, types.RecordNotExist) {
org, err = _forge.Org(c, user, repo.Owner) org, err = _forge.Org(c, user, repo.Owner)
if err != nil { 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) log.Error().Err(err).Msg(msg)
c.String(http.StatusInternalServerError, msg) c.String(http.StatusInternalServerError, msg)
return return
@ -139,7 +139,7 @@ func PostRepo(c *gin.Context) {
org.ForgeID = user.ForgeID org.ForgeID = user.ForgeID
err = _store.OrgCreate(org) err = _store.OrgCreate(org)
if err != nil { 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) log.Error().Err(err).Msg(msg)
c.String(http.StatusInternalServerError, msg) c.String(http.StatusInternalServerError, msg)
return return

View file

@ -753,7 +753,7 @@ func (g *GitLab) Org(ctx context.Context, u *model.User, owner string) (*model.O
groups, _, err := client.Groups.ListGroups(&gitlab.ListGroupsOptions{ groups, _, err := client.Groups.ListGroups(&gitlab.ListGroupsOptions{
ListOptions: gitlab.ListOptions{ ListOptions: gitlab.ListOptions{
Page: 1, Page: 1,
PerPage: 1, PerPage: perPage,
}, },
Search: gitlab.Ptr(owner), Search: gitlab.Ptr(owner),
}, gitlab.WithContext(ctx)) }, gitlab.WithContext(ctx))
@ -761,13 +761,21 @@ func (g *GitLab) Org(ctx context.Context, u *model.User, owner string) (*model.O
return nil, err 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 nil, fmt.Errorf("could not find org %s", owner)
} }
return &model.Org{ return &model.Org{
Name: groups[0].FullPath, Name: matchedGroup.FullPath,
Private: groups[0].Visibility != gitlab.PublicVisibility, Private: matchedGroup.Visibility != gitlab.PublicVisibility,
}, nil }, nil
} }