Skip to content

Commit

Permalink
gitlab: support nested repos (#2981)
Browse files Browse the repository at this point in the history
close  #2311
  • Loading branch information
6543 authored Dec 20, 2023
1 parent 2cf873f commit 65c62e9
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions server/forge/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,16 @@ 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, owner, name string) (*gitlab.Project, error) {
repo, _, err := client.Projects.GetProject(fmt.Sprintf("%s/%s", owner, name), nil, gitlab.WithContext(ctx))
func (g *GitLab) getProject(ctx context.Context, client *gitlab.Client, mr *model.Repo, 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
}
Expand All @@ -254,7 +262,7 @@ func (g *GitLab) Repo(ctx context.Context, user *model.User, remoteID model.Forg
return g.convertGitLabRepo(_repo)
}

_repo, err := g.getProject(ctx, client, owner, name)
_repo, err := g.getProject(ctx, client, nil, owner, name)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -309,7 +317,7 @@ func (g *GitLab) PullRequests(ctx context.Context, u *model.User, r *model.Repo,
return nil, err
}

_repo, err := g.getProject(ctx, client, r.Owner, r.Name)
_repo, err := g.getProject(ctx, client, r, r.Owner, r.Name)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -339,7 +347,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.Owner, repo.Name)
_repo, err := g.getProject(ctx, client, repo, repo.Owner, repo.Name)
if err != nil {
return nil, err
}
Expand All @@ -355,10 +363,11 @@ 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.Owner, repo.Name)
_repo, err := g.getProject(ctx, client, repo, repo.Owner, repo.Name)
if err != nil {
return nil, err
}

opts := &gitlab.ListTreeOptions{
ListOptions: gitlab.ListOptions{PerPage: perPage},
Path: &path,
Expand Down Expand Up @@ -402,7 +411,7 @@ func (g *GitLab) Status(ctx context.Context, user *model.User, repo *model.Repo,
return err
}

_repo, err := g.getProject(ctx, client, repo.Owner, repo.Name)
_repo, err := g.getProject(ctx, client, repo, repo.Owner, repo.Name)
if err != nil {
return err
}
Expand Down Expand Up @@ -459,7 +468,7 @@ func (g *GitLab) Activate(ctx context.Context, user *model.User, repo *model.Rep
return err
}

_repo, err := g.getProject(ctx, client, repo.Owner, repo.Name)
_repo, err := g.getProject(ctx, client, repo, repo.Owner, repo.Name)
if err != nil {
return err
}
Expand Down Expand Up @@ -494,7 +503,7 @@ func (g *GitLab) Deactivate(ctx context.Context, user *model.User, repo *model.R
return err
}

_repo, err := g.getProject(ctx, client, repo.Owner, repo.Name)
_repo, err := g.getProject(ctx, client, repo, repo.Owner, repo.Name)
if err != nil {
return err
}
Expand Down Expand Up @@ -548,7 +557,7 @@ func (g *GitLab) Branches(ctx context.Context, user *model.User, repo *model.Rep
return nil, err
}

_repo, err := g.getProject(ctx, client, repo.Owner, repo.Name)
_repo, err := g.getProject(ctx, client, repo, repo.Owner, repo.Name)
if err != nil {
return nil, err
}
Expand All @@ -575,7 +584,7 @@ func (g *GitLab) BranchHead(ctx context.Context, u *model.User, r *model.Repo, b
return "", err
}

_repo, err := g.getProject(ctx, client, r.Owner, r.Name)
_repo, err := g.getProject(ctx, client, r, r.Owner, r.Name)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -744,7 +753,7 @@ func (g *GitLab) loadChangedFilesFromMergeRequest(ctx context.Context, tmpRepo *
return nil, err
}

_repo, err := g.getProject(ctx, client, repo.Owner, repo.Name)
_repo, err := g.getProject(ctx, client, repo, repo.Owner, repo.Name)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 65c62e9

Please sign in to comment.