From ff2841d43c6d1950d093cfac42c359aa30c13ab6 Mon Sep 17 00:00:00 2001 From: Rune Philosof Date: Thu, 3 Aug 2023 11:57:47 +0200 Subject: [PATCH 1/2] Forge Github Org: Use `login` instead of `name` For "woodpecker-ci" the `name` is "Woodpecker CI" and the `login` is "woodpecker-ci" Fixes #2092 This was causing the organization lookup to fail, because it looks up using the `login`, when it did not find the organization, it would try to create it. The creation would fail, because it uses the `name`, and an organization with that `name` already exists. Resulting in ``` pq: duplicate key value violates unique constraint "UQE_orgs_name" ``` --- server/forge/github/github.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/server/forge/github/github.go b/server/forge/github/github.go index 73c22d26b29..7e009bf8a53 100644 --- a/server/forge/github/github.go +++ b/server/forge/github/github.go @@ -354,20 +354,22 @@ func (c *client) Org(ctx context.Context, u *model.User, owner string) (*model.O client := c.newClientToken(ctx, u.Token) user, _, err := client.Users.Get(ctx, owner) + log.Trace().Msgf("Github user for owner %s = %v", owner, user) if user != nil && err == nil { return &model.Org{ - Name: user.GetName(), + Name: user.GetLogin(), IsUser: true, }, nil } org, _, err := client.Organizations.Get(ctx, owner) + log.Trace().Msgf("Github organization for owner %s = %v", owner, org) if err != nil { return nil, err } return &model.Org{ - Name: org.GetName(), + Name: org.GetLogin(), }, nil } From abe63d36117f4b096219abe501788095e642cc05 Mon Sep 17 00:00:00 2001 From: Rune Philosof Date: Thu, 3 Aug 2023 14:00:33 +0200 Subject: [PATCH 2/2] Forge Github Org: Use `FullPath` instead of `Name` `Name` is the human readable name, which could be "Woodpecker CI" for the `Path` "woodpecker-ci". Gitlab supports subgroups, so we need to use the FullPath, otherwise we would only get `sub-group` when owner is `parent-group/sub-group`. Fixes #2092 when using Gitlab. This was causing the organization lookup to fail, because it looks up using the `login`, when it did not find the organization, it would try to create it. The creation would fail, because it uses the `name`, and an organization with that `name` already exists. Resulting in ``` pq: duplicate key value violates unique constraint "UQE_orgs_name" ``` --- server/forge/gitlab/gitlab.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/forge/gitlab/gitlab.go b/server/forge/gitlab/gitlab.go index 5f5ec2ab375..a90d1b11893 100644 --- a/server/forge/gitlab/gitlab.go +++ b/server/forge/gitlab/gitlab.go @@ -717,7 +717,7 @@ func (g *GitLab) Org(ctx context.Context, u *model.User, owner string) (*model.O } return &model.Org{ - Name: groups[0].Name, + Name: groups[0].FullPath, Private: groups[0].Visibility != gitlab.PublicVisibility, }, nil }