From e5dbbd92c72b828532b9ac6d1a610398da550fa4 Mon Sep 17 00:00:00 2001 From: Anbraten Date: Sat, 23 Dec 2023 09:48:16 +0100 Subject: [PATCH] Fix gitlab project fetching (#3000) --- server/forge/gitlab/gitlab.go | 52 ++++++++++++++--------------------- 1 file changed, 21 insertions(+), 31 deletions(-) diff --git a/server/forge/gitlab/gitlab.go b/server/forge/gitlab/gitlab.go index 649f1c978..30292b4cc 100644 --- a/server/forge/gitlab/gitlab.go +++ b/server/forge/gitlab/gitlab.go @@ -228,21 +228,23 @@ func (g *GitLab) Teams(ctx context.Context, user *model.User) ([]*model.Team, er } // getProject fetches the named repository from the forge. -func (g *GitLab) getProject(ctx context.Context, client *gitlab.Client, mr *model.Repo, owner, name string) (*gitlab.Project, error) { +func (g *GitLab) getProject(ctx context.Context, client *gitlab.Client, forgeRemoteID model.ForgeRemoteID, owner, name string) (*gitlab.Project, error) { var ( repo *gitlab.Project err error ) - if mr != nil && mr.ForgeRemoteID.IsValid() { - repo, _, err = client.Projects.GetProject(mr.ForgeRemoteID, nil, gitlab.WithContext(ctx)) - } else { - repo, _, err = client.Projects.GetProject(fmt.Sprintf("%s/%s", owner, name), nil, gitlab.WithContext(ctx)) - } - if err != nil { - return nil, err + + if forgeRemoteID.IsValid() { + intID, err := strconv.Atoi(string(forgeRemoteID)) + if err != nil { + return nil, err + } + repo, _, err = client.Projects.GetProject(intID, nil, gitlab.WithContext(ctx)) + return repo, err } - return repo, nil + repo, _, err = client.Projects.GetProject(fmt.Sprintf("%s/%s", owner, name), nil, gitlab.WithContext(ctx)) + return repo, err } // Repo fetches the repository from the forge. @@ -252,19 +254,7 @@ func (g *GitLab) Repo(ctx context.Context, user *model.User, remoteID model.Forg return nil, err } - if remoteID.IsValid() { - intID, err := strconv.ParseInt(string(remoteID), 10, 64) - if err != nil { - return nil, err - } - _repo, _, err := client.Projects.GetProject(int(intID), nil, gitlab.WithContext(ctx)) - if err != nil { - return nil, err - } - return g.convertGitLabRepo(_repo) - } - - _repo, err := g.getProject(ctx, client, nil, owner, name) + _repo, err := g.getProject(ctx, client, remoteID, owner, name) if err != nil { return nil, err } @@ -319,7 +309,7 @@ func (g *GitLab) PullRequests(ctx context.Context, u *model.User, r *model.Repo, return nil, err } - _repo, err := g.getProject(ctx, client, r, r.Owner, r.Name) + _repo, err := g.getProject(ctx, client, r.ForgeRemoteID, r.Owner, r.Name) if err != nil { return nil, err } @@ -349,7 +339,7 @@ func (g *GitLab) File(ctx context.Context, user *model.User, repo *model.Repo, p if err != nil { return nil, err } - _repo, err := g.getProject(ctx, client, repo, repo.Owner, repo.Name) + _repo, err := g.getProject(ctx, client, repo.ForgeRemoteID, repo.Owner, repo.Name) if err != nil { return nil, err } @@ -368,7 +358,7 @@ func (g *GitLab) Dir(ctx context.Context, user *model.User, repo *model.Repo, pi } files := make([]*forge_types.FileMeta, 0, perPage) - _repo, err := g.getProject(ctx, client, repo, repo.Owner, repo.Name) + _repo, err := g.getProject(ctx, client, repo.ForgeRemoteID, repo.Owner, repo.Name) if err != nil { return nil, err } @@ -419,7 +409,7 @@ func (g *GitLab) Status(ctx context.Context, user *model.User, repo *model.Repo, return err } - _repo, err := g.getProject(ctx, client, repo, repo.Owner, repo.Name) + _repo, err := g.getProject(ctx, client, repo.ForgeRemoteID, repo.Owner, repo.Name) if err != nil { return err } @@ -476,7 +466,7 @@ func (g *GitLab) Activate(ctx context.Context, user *model.User, repo *model.Rep return err } - _repo, err := g.getProject(ctx, client, repo, repo.Owner, repo.Name) + _repo, err := g.getProject(ctx, client, repo.ForgeRemoteID, repo.Owner, repo.Name) if err != nil { return err } @@ -511,7 +501,7 @@ func (g *GitLab) Deactivate(ctx context.Context, user *model.User, repo *model.R return err } - _repo, err := g.getProject(ctx, client, repo, repo.Owner, repo.Name) + _repo, err := g.getProject(ctx, client, repo.ForgeRemoteID, repo.Owner, repo.Name) if err != nil { return err } @@ -565,7 +555,7 @@ func (g *GitLab) Branches(ctx context.Context, user *model.User, repo *model.Rep return nil, err } - _repo, err := g.getProject(ctx, client, repo, repo.Owner, repo.Name) + _repo, err := g.getProject(ctx, client, repo.ForgeRemoteID, repo.Owner, repo.Name) if err != nil { return nil, err } @@ -592,7 +582,7 @@ func (g *GitLab) BranchHead(ctx context.Context, u *model.User, r *model.Repo, b return "", err } - _repo, err := g.getProject(ctx, client, r, r.Owner, r.Name) + _repo, err := g.getProject(ctx, client, r.ForgeRemoteID, r.Owner, r.Name) if err != nil { return "", err } @@ -761,7 +751,7 @@ func (g *GitLab) loadChangedFilesFromMergeRequest(ctx context.Context, tmpRepo * return nil, err } - _repo, err := g.getProject(ctx, client, repo, repo.Owner, repo.Name) + _repo, err := g.getProject(ctx, client, repo.ForgeRemoteID, repo.Owner, repo.Name) if err != nil { return nil, err }