|
| 1 | +import core, { |
| 2 | + buildSocialIdString, |
| 3 | + DOMAIN_MODEL_TX, |
| 4 | + systemAccountUuid, |
| 5 | + TxProcessor, |
| 6 | + type BackupClient, |
| 7 | + type Client, |
| 8 | + type Doc, |
| 9 | + type Ref, |
| 10 | + type TxCUD, |
| 11 | + type WorkspaceUuid |
| 12 | +} from '@hcengineering/core' |
| 13 | +import { getAccountsFromTxes, getSocialKeyByOldEmail } from '@hcengineering/model-core' |
| 14 | +import { createClient, getAccountClient, getTransactorEndpoint } from '@hcengineering/server-client' |
| 15 | +import { generateToken } from '@hcengineering/server-token' |
| 16 | +import type { Db } from 'mongodb' |
| 17 | + |
| 18 | +/** |
| 19 | + * @public |
| 20 | + */ |
| 21 | +export interface GithubIntegrationRecord { |
| 22 | + installationId: number |
| 23 | + workspace: string |
| 24 | + accountId: string // Ref<Account> |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * @public |
| 29 | + */ |
| 30 | +export interface GithubUserRecord { |
| 31 | + _id: string // login |
| 32 | + code?: string | null |
| 33 | + token?: string |
| 34 | + expiresIn?: number | null // seconds |
| 35 | + refreshToken?: string | null |
| 36 | + refreshTokenExpiresIn?: number | null |
| 37 | + authorized?: boolean |
| 38 | + state?: string |
| 39 | + scope?: string |
| 40 | + error?: string | null |
| 41 | + |
| 42 | + accounts: Record<string, string /* Ref<Account> */> |
| 43 | +} |
| 44 | + |
| 45 | +export async function performGithubAccountMigrations (db: Db, region: string | null): Promise<void> { |
| 46 | + const token = generateToken(systemAccountUuid, '' as WorkspaceUuid, { service: 'admin', admin: 'true' }) |
| 47 | + const githubToken = generateToken(systemAccountUuid, '' as WorkspaceUuid, { service: 'github' }) |
| 48 | + const accountClient = getAccountClient(token) |
| 49 | + |
| 50 | + const githubAccountClient = getAccountClient(githubToken) |
| 51 | + |
| 52 | + const usersCollection = db.collection<GithubUserRecord>('users') |
| 53 | + |
| 54 | + const integrationCollection = db.collection<GithubIntegrationRecord>('installations') |
| 55 | + |
| 56 | + const integrations = await integrationCollection.find({}).toArray() |
| 57 | + // Check and apply migrations |
| 58 | + // We need to update all workspace information accordingly |
| 59 | + |
| 60 | + const allWorkpaces = await accountClient.listWorkspaces(region) |
| 61 | + const byId = new Map(allWorkpaces.map((it) => [it.uuid, it])) |
| 62 | + const oldNewIds = new Map(allWorkpaces.map((it) => [it.dataId ?? it.uuid, it])) |
| 63 | + |
| 64 | + const allAuthorizations = await usersCollection.find({}).toArray() |
| 65 | + |
| 66 | + const wsToAuth = new Map<WorkspaceUuid, GithubUserRecord[]>() |
| 67 | + |
| 68 | + for (const it of allAuthorizations) { |
| 69 | + for (const ws of Object.keys(it.accounts)) { |
| 70 | + const wsId = oldNewIds.get(ws as WorkspaceUuid) ?? byId.get(ws as WorkspaceUuid) |
| 71 | + if (wsId !== undefined) { |
| 72 | + wsToAuth.set(wsId.uuid, (wsToAuth.get(wsId.uuid) ?? []).concat(it)) |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + const processed = new Set<string>() |
| 77 | + |
| 78 | + const replaces = new Map<string, WorkspaceUuid>() |
| 79 | + for (const it of integrations) { |
| 80 | + const ws = oldNewIds.get(it.workspace as any) ?? byId.get(it.workspace as any) |
| 81 | + if (ws != null) { |
| 82 | + // Need to connect to workspace to get account mapping |
| 83 | + |
| 84 | + it.workspace = ws.uuid |
| 85 | + replaces.set(it.workspace, ws.uuid) |
| 86 | + |
| 87 | + const wsToken = generateToken(systemAccountUuid, ws.uuid, { service: 'github', mode: 'backup' }) |
| 88 | + const endpoint = await getTransactorEndpoint(wsToken, 'external') |
| 89 | + const client = (await createClient(endpoint, wsToken)) as BackupClient & Client |
| 90 | + |
| 91 | + const systemAccounts = [core.account.System, core.account.ConfigUser] |
| 92 | + const accountsTxes: TxCUD<Doc>[] = [] |
| 93 | + |
| 94 | + let idx: number | undefined |
| 95 | + |
| 96 | + while (true) { |
| 97 | + const info = await client.loadChunk(DOMAIN_MODEL_TX, idx) |
| 98 | + idx = info.idx |
| 99 | + const ids = Array.from(info.docs.map((it) => it.id as Ref<Doc>)) |
| 100 | + const docs = (await client.loadDocs(DOMAIN_MODEL_TX, ids)).filter((it) => |
| 101 | + TxProcessor.isExtendsCUD(it._class) |
| 102 | + ) as TxCUD<Doc>[] |
| 103 | + accountsTxes.push(...docs) |
| 104 | + if (info.finished && idx !== undefined) { |
| 105 | + await client.closeChunk(info.idx) |
| 106 | + break |
| 107 | + } |
| 108 | + } |
| 109 | + await client.close() |
| 110 | + |
| 111 | + // await client.loadChunk(DOMAIN_MODEL_TX, { |
| 112 | + // objectClass: { $in: ['core:class:Account', 'contact:class:PersonAccount'] as Ref<Class<Doc>>[] } |
| 113 | + // }) |
| 114 | + const accounts: (Doc & { email?: string })[] = getAccountsFromTxes(accountsTxes) |
| 115 | + |
| 116 | + const socialKeyByAccount: Record<string, string> = {} |
| 117 | + for (const account of accounts) { |
| 118 | + if (account.email === undefined) { |
| 119 | + continue |
| 120 | + } |
| 121 | + |
| 122 | + if (systemAccounts.includes(account._id as any)) { |
| 123 | + ;(socialKeyByAccount as any)[account._id] = account._id |
| 124 | + } else { |
| 125 | + socialKeyByAccount[account._id] = buildSocialIdString(getSocialKeyByOldEmail(account.email)) as any |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + const sid = socialKeyByAccount[it.accountId] |
| 130 | + |
| 131 | + const person = sid !== undefined ? await accountClient.findSocialIdBySocialKey(sid) : undefined |
| 132 | + if (person !== undefined) { |
| 133 | + // Check/create integeration in account |
| 134 | + |
| 135 | + const existing = await githubAccountClient.getIntegration({ |
| 136 | + kind: 'github', |
| 137 | + workspaceUuid: ws?.uuid, |
| 138 | + socialId: person |
| 139 | + }) |
| 140 | + |
| 141 | + if (existing == null) { |
| 142 | + await githubAccountClient.createIntegration({ |
| 143 | + kind: 'github', |
| 144 | + workspaceUuid: ws?.uuid, |
| 145 | + socialId: person, |
| 146 | + data: { |
| 147 | + installationId: it.installationId |
| 148 | + } |
| 149 | + }) |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + const users = wsToAuth.get(ws.uuid) |
| 154 | + for (const u of users ?? []) { |
| 155 | + if (processed.has(u._id)) { |
| 156 | + continue |
| 157 | + } |
| 158 | + processed.add(u._id) |
| 159 | + |
| 160 | + const sid = socialKeyByAccount[u.accounts[ws.dataId ?? ws.uuid]] |
| 161 | + if (sid !== undefined) { |
| 162 | + const person = await accountClient.findSocialIdBySocialKey(sid) |
| 163 | + if (person !== undefined) { |
| 164 | + const { _id, accounts, ...data } = u |
| 165 | + |
| 166 | + const existing = await githubAccountClient.getIntegration({ |
| 167 | + kind: 'github-user', |
| 168 | + workspaceUuid: null, |
| 169 | + socialId: person |
| 170 | + }) |
| 171 | + |
| 172 | + if (existing == null) { |
| 173 | + await githubAccountClient.createIntegration({ |
| 174 | + kind: 'github-user', |
| 175 | + workspaceUuid: null, |
| 176 | + socialId: person, |
| 177 | + data: { |
| 178 | + login: u._id |
| 179 | + } |
| 180 | + }) |
| 181 | + // Check/create integeration in account |
| 182 | + await githubAccountClient.addIntegrationSecret({ |
| 183 | + kind: 'github-user', |
| 184 | + workspaceUuid: null, |
| 185 | + socialId: person, |
| 186 | + key: u._id, // github login |
| 187 | + secret: JSON.stringify(data) |
| 188 | + }) |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments