mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-12 13:13:45 +01:00
GitLab Synchronization: Changed getUserToken to just return the token instead of the entire object, making the code more compatible with the Github integration
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user