Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 364
feat(ee): GitLab permission syncing#585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
26a7555bad775705c3d1379697e045bac0f067668bFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -6,10 +6,13 @@ import { Redis } from "ioredis"; | ||
| import { PERMISSION_SYNC_SUPPORTED_CODE_HOST_TYPES } from "../constants.js"; | ||
| import { env } from "../env.js"; | ||
| import { createOctokitFromToken, getReposForAuthenticatedUser } from "../github.js"; | ||
| import { createGitLabFromOAuthToken, getProjectsForAuthenticatedUser } from "../gitlab.js"; | ||
| import { hasEntitlement } from "@sourcebot/shared"; | ||
| import { Settings } from "../types.js"; | ||
| const logger = createLogger('user-permission-syncer'); | ||
| const LOG_TAG = 'user-permission-syncer'; | ||
| const logger = createLogger(LOG_TAG); | ||
| const createJobLogger = (jobId: string) => createLogger(`${LOG_TAG}:job:${jobId}`); | ||
| const QUEUE_NAME = 'userPermissionSyncQueue'; | ||
| @@ -110,28 +113,31 @@ export class UserPermissionSyncer { | ||
| } | ||
| private async schedulePermissionSync(users: User[]) { | ||
| await this.db.$transaction(async (tx) => { | ||
| const jobs = await tx.userPermissionSyncJob.createManyAndReturn({ | ||
| data: users.map(user => ({ | ||
| userId: user.id, | ||
| })), | ||
| }); | ||
| await this.queue.addBulk(jobs.map((job) => ({ | ||
| name: 'userPermissionSyncJob', | ||
| data: { | ||
| jobId: job.id, | ||
| }, | ||
| opts: { | ||
| removeOnComplete: env.REDIS_REMOVE_ON_COMPLETE, | ||
| removeOnFail: env.REDIS_REMOVE_ON_FAIL, | ||
| } | ||
| }))) | ||
| // @note: we don't perform this in a transaction because | ||
| // we want to avoid the situation where a job is created and run | ||
| // prior to the transaction being committed. | ||
| const jobs = await this.db.userPermissionSyncJob.createManyAndReturn({ | ||
| data: users.map(user => ({ | ||
| userId: user.id, | ||
| })), | ||
| }); | ||
| await this.queue.addBulk(jobs.map((job) => ({ | ||
| name: 'userPermissionSyncJob', | ||
| data: { | ||
| jobId: job.id, | ||
| }, | ||
| opts: { | ||
| removeOnComplete: env.REDIS_REMOVE_ON_COMPLETE, | ||
| removeOnFail: env.REDIS_REMOVE_ON_FAIL, | ||
| } | ||
| }))) | ||
| } | ||
brendan-kellam marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| private async runJob(job: Job<UserPermissionSyncJob>) { | ||
| const id = job.data.jobId; | ||
| const logger = createJobLogger(id); | ||
| const { user } = await this.db.userPermissionSyncJob.update({ | ||
| where: { | ||
| id, | ||
| @@ -183,6 +189,37 @@ export class UserPermissionSyncer { | ||
| } | ||
| }); | ||
| repos.forEach(repo => aggregatedRepoIds.add(repo.id)); | ||
| } else if (account.provider === 'gitlab') { | ||
| if (!account.access_token) { | ||
| throw new Error(`User '${user.email}' does not have a GitLab OAuth access token associated with their GitLab account.`); | ||
| } | ||
| const api = await createGitLabFromOAuthToken({ | ||
| oauthToken: account.access_token, | ||
| url: env.AUTH_EE_GITLAB_BASE_URL, | ||
| }); | ||
| // @note: we only care about the private and internal repos since we don't need to build a mapping | ||
brendan-kellam marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| // for public repos. | ||
| // @see: packages/web/src/prisma.ts | ||
| const privateGitLabProjects = await getProjectsForAuthenticatedUser('private', api); | ||
| const internalGitLabProjects = await getProjectsForAuthenticatedUser('internal', api); | ||
| const gitLabProjectIds = [ | ||
| ...privateGitLabProjects, | ||
| ...internalGitLabProjects, | ||
| ].map(project => project.id.toString()); | ||
| const repos = await this.db.repo.findMany({ | ||
| where: { | ||
| external_codeHostType: 'gitlab', | ||
| external_id: { | ||
| in: gitLabProjectIds, | ||
| } | ||
| } | ||
| }); | ||
| repos.forEach(repo => aggregatedRepoIds.add(repo.id)); | ||
| } | ||
| } | ||
| @@ -212,6 +249,8 @@ export class UserPermissionSyncer { | ||
| } | ||
| private async onJobCompleted(job: Job<UserPermissionSyncJob>) { | ||
| const logger = createJobLogger(job.data.jobId); | ||
| const { user } = await this.db.userPermissionSyncJob.update({ | ||
| where: { | ||
| id: job.data.jobId, | ||
| @@ -234,6 +273,8 @@ export class UserPermissionSyncer { | ||
| } | ||
| private async onJobFailed(job: Job<UserPermissionSyncJob> | undefined, err: Error) { | ||
| const logger = createJobLogger(job?.data.jobId ?? 'unknown'); | ||
| Sentry.captureException(err, { | ||
| tags: { | ||
| jobId: job?.data.jobId, | ||
| @@ -260,7 +301,7 @@ export class UserPermissionSyncer { | ||
| logger.error(errorMessage(user.email ?? user.id)); | ||
| } else { | ||
| logger.error(errorMessage('unknown user (id not found)')); | ||
| logger.error(errorMessage('unknown job (id not found)')); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.