Skip to content

Commit e8c5163

Browse files
committed
fix: 修复 gitlab token 未刷新的问题
1 parent da5e5eb commit e8c5163

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

backend/biz/git/usecase/identity.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func (u *GitIdentityUsecase) List(ctx context.Context, uid uuid.UUID) ([]*domain
5757
repos, err := client.Repositories(ctx, &domain.RepositoryOptions{
5858
Token: token,
5959
InstallID: identity.InstallationID,
60+
IsOAuth: identity.OauthRefreshToken != "",
6061
})
6162
if err != nil {
6263
u.logger.WarnContext(ctx, "failed to get authorized repositories", "error", err, "platform", identity.Platform, "identity_id", identity.ID)
@@ -73,7 +74,7 @@ func (u *GitIdentityUsecase) gitClienter(identity *db.GitIdentity) domain.GitCli
7374
case consts.GitPlatformGithub:
7475
return github.NewGithub(u.logger, u.cfg)
7576
case consts.GitPlatformGitLab:
76-
return gitlab.NewGitlab(identity.BaseURL, identity.AccessToken, u.logger)
77+
return gitlab.NewGitlabForBaseURL(identity.BaseURL, u.logger)
7778
case consts.GitPlatformGitea:
7879
return gitea.NewGitea(u.logger, identity.BaseURL)
7980
case consts.GitPlatformGitee:
@@ -104,6 +105,7 @@ func (u *GitIdentityUsecase) Get(ctx context.Context, uid uuid.UUID, id uuid.UUI
104105
repos, err := client.Repositories(ctx, &domain.RepositoryOptions{
105106
Token: token,
106107
InstallID: identity.InstallationID,
108+
IsOAuth: identity.OauthRefreshToken != "",
107109
})
108110
if err != nil {
109111
u.logger.WarnContext(ctx, "failed to get authorized repositories", "error", err, "platform", identity.Platform, "identity_id", id)
@@ -209,8 +211,13 @@ func (u *GitIdentityUsecase) ListBranches(ctx context.Context, uid uuid.UUID, id
209211
return nil, errcode.ErrInvalidPlatform
210212
}
211213

214+
token, err := u.tokenProvider.GetToken(ctx, identity.ID)
215+
if err != nil {
216+
return nil, fmt.Errorf("get token: %w", err)
217+
}
218+
212219
branches, err := client.Branches(ctx, &domain.BranchesOptions{
213-
Token: identity.AccessToken, Owner: owner, Repo: repo,
220+
Token: token, Owner: owner, Repo: repo,
214221
Page: page, PerPage: perPage,
215222
InstallID: identity.InstallationID, IsOAuth: identity.OauthRefreshToken != "",
216223
})

backend/pkg/git/gitlab/operation.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,11 @@ func (g *Gitlab) UserInfo(ctx context.Context, token string) (*domain.PlatformUs
369369

370370
// Repositories 实现 GitPlatformClient 接口
371371
func (g *Gitlab) Repositories(ctx context.Context, opts *domain.RepositoryOptions) ([]domain.AuthRepository, error) {
372-
return g.GetAuthorizedRepositories(ctx, opts.Token)
372+
client, err := g.newClientWithToken(opts.Token, opts.IsOAuth)
373+
if err != nil {
374+
return nil, fmt.Errorf("new client: %w", err)
375+
}
376+
return g.listProjects(ctx, client)
373377
}
374378

375379
// Tree 实现 GitPlatformClient 接口
@@ -433,7 +437,11 @@ func (g *Gitlab) Archive(ctx context.Context, opts *domain.ArchiveOptions) (*dom
433437

434438
// Branches 实现 GitPlatformClient 接口
435439
func (g *Gitlab) Branches(ctx context.Context, opts *domain.BranchesOptions) ([]*domain.BranchInfo, error) {
436-
resp, err := g.ListBranches(ctx, opts.Token, opts.Owner, opts.IsOAuth, opts.Page, opts.PerPage)
440+
projectPath := opts.Owner
441+
if opts.Repo != "" {
442+
projectPath = opts.Owner + "/" + opts.Repo
443+
}
444+
resp, err := g.ListBranches(ctx, opts.Token, projectPath, opts.IsOAuth, opts.Page, opts.PerPage)
437445
if err != nil {
438446
return nil, err
439447
}

0 commit comments

Comments
 (0)