Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

fix: don't use expired token for gitlab client init #111

Merged
merged 1 commit into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions services/GitlabService.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class GitlabService {
await svc.refreshAccessToken();
svc.#gitlab = new Gitlab({
host: config.GITLAB_API_BASE_URL,
oauthToken: user.accessToken
oauthToken: svc.#user.accessToken
});
return svc;
} catch (err) {
Expand All @@ -123,10 +123,10 @@ class GitlabService {
if (!lockedUser) {
throw new Error(`Failed to acquire lock on user ${this.#user.id} after ${tries} attempts.`);
}
logger.debug(`[Lock ID: ${lockId}] Acquired lock on user ${this.#user.id}.`);
logger.debug(`[Lock ID: ${lockId}] Acquired lock on user ${this.#user.username}.`);
if (lockedUser.accessTokenExpiration && new Date().getTime() > lockedUser.accessTokenExpiration.getTime() -
(config.GITLAB_REFRESH_TOKEN_BEFORE_EXPIRATION * MS_PER_SECOND)) {
logger.debug(`[Lock ID: ${lockId}] Refreshing access token for user ${this.#user.id}.`);
logger.debug(`[Lock ID: ${lockId}] Refreshing access token for user ${this.#user.username}.`);
const query = {
client_id: config.GITLAB_CLIENT_ID,
client_secret: config.GITLAB_CLIENT_SECRET,
Expand All @@ -147,11 +147,10 @@ class GitlabService {
_.assign(lockedUser, updates);
await dbHelper.update(models.User, lockedUser.id, updates);
}
return lockedUser;
} finally {
if (lockedUser) {
logger.debug(`[Lock ID: ${lockId}] Releasing lock on user ${this.#user.id}.`);
await dbHelper.releaseLockOnUser(this.#user.id, lockId);
logger.debug(`[Lock ID: ${lockId}] Releasing lock on user ${this.#user.username}.`);
this.#user = await dbHelper.releaseLockOnUser(this.#user.id, lockId);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions utils/db-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,15 +479,15 @@ async function acquireLockOnUser(userId, lockId, ttl) {
* @returns {Promise<Object>} The lock object
*/
async function releaseLockOnUser(id, lockId) {
const res = await models.User.update(
const user = await models.User.update(
{id},
{lockId: null, lockExpiration: null},
{
condition: 'lockId = :lockId',
conditionValues: {lockId}
},
);
return res;
return user;
}

module.exports = {
Expand Down