From d0375201056f073f72b2904abf1da99a5265c9da Mon Sep 17 00:00:00 2001 From: Florian Porrmann Date: Mon, 3 Aug 2026 17:55:42 +0200 Subject: [PATCH] GitLab Synchronization: Changed getUserToken to just return the token instead of the entire object, making the code more compatible with the Github integration --- .../gitlab-sync/app/src/GitLabSyncHandler.mjs | 38 ++++++++----------- .../modules/gitlab-sync/app/src/GitMerge.mjs | 5 +-- .../gitlab-sync/app/src/TokenManager.mjs | 14 +++---- 3 files changed, 25 insertions(+), 32 deletions(-) diff --git a/services/web/modules/gitlab-sync/app/src/GitLabSyncHandler.mjs b/services/web/modules/gitlab-sync/app/src/GitLabSyncHandler.mjs index 64427d2443..026ad93b04 100644 --- a/services/web/modules/gitlab-sync/app/src/GitLabSyncHandler.mjs +++ b/services/web/modules/gitlab-sync/app/src/GitLabSyncHandler.mjs @@ -18,11 +18,10 @@ import { InvalidTokenError, NotFoundError } from './GitSyncErrors.mjs' async function getGitConnState(userId) { try { - const tokens = await TokenManager.getUserToken(userId) - if (!tokens) return false; - const { token } = tokens + const token = await TokenManager.getUserToken(userId) + if (!token) return false; - await api.getUser(token) + await api.getUser(token) return true } catch (err) { if (err instanceof InvalidTokenError) { @@ -52,9 +51,9 @@ async function getProjectState(userId, projectId) { } let canPush try { - const tokens = await TokenManager.getUserToken(userId) - if (!tokens) throw new InvalidTokenError('no user token', { userId, status: 400 }); - const { token } = tokens + const token = await TokenManager.getUserToken(userId) + if (!token) throw new InvalidTokenError('no user token', { userId, status: 400 }); + canPush = await api.getPushPermission(token, pss.repoFullName) } catch (err) { if ((err instanceof NotFoundError) || @@ -97,16 +96,14 @@ async function unlinkRepo(userId, projectId) { async function listUserRepos(userId) { - const tokens = await TokenManager.getUserToken(userId) - if (!tokens) throw new InvalidTokenError('no user token', { userId, status: 400 }); - const { token } = tokens + const token = await TokenManager.getUserToken(userId) + if (!token) throw new InvalidTokenError('no user token', { userId, status: 400 }); return await api.listUserRepos(token) } async function getUserAndOrgs(userId) { - const tokens = await TokenManager.getUserToken(userId) - if (!tokens) throw new InvalidTokenError('no user token', { userId, status: 400 }); - const { token } = tokens + const token = await TokenManager.getUserToken(userId) + if (!token) throw new InvalidTokenError('no user token', { userId, status: 400 }); return await api.getUserAndOrgs(token) } @@ -117,9 +114,8 @@ async function getMergeOverview(userId, projectId) { if (mergeStatus === 'conflict') return null - const tokens = await TokenManager.getUserToken(userId) - if (!tokens) throw new InvalidTokenError('no user token', { userId, status: 400 }); - const { token } = tokens + const token = await TokenManager.getUserToken(userId) + if (!token) throw new InvalidTokenError('no user token', { userId, status: 400 }); const currentVersion = await HistoryManager.latestVersion(projectId.toString()) const isProjectUpdated = currentVersion !== lastSyncVersion try { @@ -148,9 +144,8 @@ async function getMergeOverview(userId, projectId) { } async function importRepo(userId, projectName, repoFullName, defaultBranchName) { - const tokens = await TokenManager.getUserToken(userId) - if (!tokens) throw new InvalidTokenError('no user token', { userId, status: 400 }); - const { token } = tokens + const token = await TokenManager.getUserToken(userId) + if (!token) throw new InvalidTokenError('no user token', { userId, status: 400 }); const defaultBranchHead = await api.getBranchHead(token, repoFullName, defaultBranchName) const fsPath = Path.join(Settings.path.dumpFolder, `gitlab_import_${crypto.randomUUID()}`) @@ -203,9 +198,8 @@ async function exportProject(userId, projectId, repoOptions) { throw new OError('Project is already linked to Git server', { projectId }) } - const tokens = await TokenManager.getUserToken(userId) - if (!tokens) throw new InvalidTokenError('no user token', { userId, status: 400 }); - const { token } = tokens + const token = await TokenManager.getUserToken(userId) + if (!token) throw new InvalidTokenError('no user token', { userId, status: 400 }); const { path_with_namespace: repoFullName, default_branch: defaultBranchName } = await api.createRepo(token, repoOptions) diff --git a/services/web/modules/gitlab-sync/app/src/GitMerge.mjs b/services/web/modules/gitlab-sync/app/src/GitMerge.mjs index 5ec205bd82..63a50e9ec0 100644 --- a/services/web/modules/gitlab-sync/app/src/GitMerge.mjs +++ b/services/web/modules/gitlab-sync/app/src/GitMerge.mjs @@ -40,9 +40,8 @@ async function doGitMergeWithoutLock(userId, projectId, message, claimConflictIs claimConflictIsResolved && mergeStatus !== 'conflict' ) return { mergeStatus, repoFullName, unmergedBranchName } - const tokens = await TokenManager.getUserToken(userId) - if (!tokens) throw new InvalidTokenError('no user token', { userId, status: 400 }); - const { token } = tokens + const token = await TokenManager.getUserToken(userId) + if (!token) throw new InvalidTokenError('no user token', { userId, status: 400 }); await DocumentUpdaterHandler.promises.flushProjectToMongo(projectId) diff --git a/services/web/modules/gitlab-sync/app/src/TokenManager.mjs b/services/web/modules/gitlab-sync/app/src/TokenManager.mjs index ff59e79eba..10c54cea93 100644 --- a/services/web/modules/gitlab-sync/app/src/TokenManager.mjs +++ b/services/web/modules/gitlab-sync/app/src/TokenManager.mjs @@ -24,10 +24,10 @@ async function decryptAccessToken(tokenEncrypted) { } } -async function refreshToken(userId, tokens) { +async function refreshToken(userId, decData) { logger.info({ userId }, "refreshing GitLab token") - if (!tokens.refresh_token) { + if (!decData.refresh_token) { logger.error("refresh_token invalid") return false } @@ -37,7 +37,7 @@ async function refreshToken(userId, tokens) { let refresh_timestamp try { - [token, refresh_token, refresh_timestamp] = await api.refreshToken(tokens.refresh_token) + [token, refresh_token, refresh_timestamp] = await api.refreshToken(decData.refresh_token) if (!token || !refresh_token) { HttpErrorHandler.badRequest(req, res, 'Failed to refresh access token') return false @@ -68,17 +68,17 @@ async function refreshToken(userId, tokens) { async function getUserToken(userId) { const credentials = await GitLabSyncUserCredentials.findOne(normalizeQuery({ userId })) if (!credentials) throw new InvalidTokenError('no user token', { userId, status: 400 }) - let tokens = await decryptAccessToken(credentials.gitlab) + let decData = await decryptAccessToken(credentials.gitlab) const now = Math.floor(Date.now() / 1000); - if (!tokens.refresh_timestamp || tokens.refresh_timestamp < now) { - const refreshed = await refreshToken(userId, tokens) + if (!decData.refresh_timestamp || decData.refresh_timestamp < now) { + const refreshed = await refreshToken(userId, decData) if (refreshed) { return await getUserToken(userId) } } - return tokens + return decData.token } async function saveUserToken(userId, accessToken) {