From e20ec5136394326976cf58cf97b8949f0ff50611 Mon Sep 17 00:00:00 2001 From: Andrey Sobolev Date: Tue, 15 Apr 2025 17:18:49 +0700 Subject: [PATCH] UBERF-9724: Fix github functionality on 0.7 (#8554) * UBERF-9724: Fix github functionality on 0.7 Signed-off-by: Andrey Sobolev * Fix exception for no social id for system account Signed-off-by: Andrey Sobolev --------- Signed-off-by: Andrey Sobolev --- common/config/rush/command-line.json | 2 +- services/github/pod-github/src/client.ts | 3 +- services/github/pod-github/src/index.ts | 4 ++ services/github/pod-github/src/platform.ts | 80 +++++++++++++++------- services/github/pod-github/src/users.ts | 5 +- services/github/pod-github/src/worker.ts | 10 +-- 6 files changed, 69 insertions(+), 35 deletions(-) diff --git a/common/config/rush/command-line.json b/common/config/rush/command-line.json index b8a720e842..3283d419f7 100644 --- a/common/config/rush/command-line.json +++ b/common/config/rush/command-line.json @@ -109,7 +109,7 @@ "summary": "Do a format and show errors after it", "description": "Do a format and show errors after it", "safeForSimultaneousRushProcesses": true, - "shellCommand": "rush format && ./common/scripts/format-show.sh" + "shellCommand": "(rush format && true) && ./common/scripts/format-show.sh" }, { "commandKind": "phased", diff --git a/services/github/pod-github/src/client.ts b/services/github/pod-github/src/client.ts index 57b9543071..878a92cf21 100644 --- a/services/github/pod-github/src/client.ts +++ b/services/github/pod-github/src/client.ts @@ -8,7 +8,7 @@ import clientResources from '@hcengineering/client-resources' import { Client, ClientConnectEvent, systemAccountUuid, WorkspaceUuid } from '@hcengineering/core' import { setMetadata } from '@hcengineering/platform' import { getTransactorEndpoint } from '@hcengineering/server-client' -import serverToken, { generateToken } from '@hcengineering/server-token' +import { generateToken } from '@hcengineering/server-token' import WebSocket from 'ws' import config from './config' @@ -28,7 +28,6 @@ export async function createPlatformClient ( }) as never as ClientSocket }) - setMetadata(serverToken.metadata.Secret, config.ServerSecret) const token = generateToken(systemAccountUuid, workspace, { service: 'github', mode: 'github' }) setMetadata(client.metadata.UseBinaryProtocol, true) setMetadata(client.metadata.UseProtocolCompression, true) diff --git a/services/github/pod-github/src/index.ts b/services/github/pod-github/src/index.ts index afc5f9c8f7..7ea7f45d4c 100644 --- a/services/github/pod-github/src/index.ts +++ b/services/github/pod-github/src/index.ts @@ -9,9 +9,13 @@ import { initStatisticsContext, loadBrandingMap } from '@hcengineering/server-co import { join } from 'path' import config from './config' import { start } from './server' +import { setMetadata } from '@hcengineering/platform' +import serverToken from '@hcengineering/server-token' // Load and inc startID, to have easy logs. +setMetadata(serverToken.metadata.Secret, config.ServerSecret) + const metricsContext = initStatisticsContext('github', { factory: () => new MeasureMetricsContext( diff --git a/services/github/pod-github/src/platform.ts b/services/github/pod-github/src/platform.ts index 106079d12d..bddba3fb75 100644 --- a/services/github/pod-github/src/platform.ts +++ b/services/github/pod-github/src/platform.ts @@ -26,9 +26,8 @@ import core, { type Ref } from '@hcengineering/core' import github, { GithubAuthentication, makeQuery, type GithubIntegration } from '@hcengineering/github' -import { setMetadata } from '@hcengineering/platform' import { buildStorageFromConfig, storageConfigFromEnv } from '@hcengineering/server-storage' -import serverToken, { generateToken } from '@hcengineering/server-token' +import { generateToken } from '@hcengineering/server-token' import tracker from '@hcengineering/tracker' import { Installation, type InstallationCreatedEvent, type InstallationUnsuspendEvent } from '@octokit/webhooks-types' import { App, Octokit } from 'octokit' @@ -82,7 +81,6 @@ export class PlatformWorker { readonly brandingMap: BrandingMap, readonly periodicSyncInterval = 10 * 60 * 1000 // 10 minutes ) { - setMetadata(serverToken.metadata.Secret, config.ServerSecret) registerLoaders() } @@ -510,38 +508,68 @@ export class PlatformWorker { if (githubSocialId === undefined) { // We need to create a new social id for this account. - githubSocialId = await sysAccountClient.addSocialIdToPerson( - person.personUuid as PersonUuid, - SocialIdType.GITHUB, - dta?._id ?? '', - true - ) + this.ctx.info('Create social id', { + account: dta?._id, + workspace: payload.workspace, + personUuid: person.personUuid, + ids + }) + try { + const pp = await sysAccountClient.findPersonBySocialKey( + buildSocialIdString({ type: SocialIdType.GITHUB, value: dta?._id }) + ) + if (pp !== person.personUuid) { + // TODO: We need to remove old social id association + } + + githubSocialId = await sysAccountClient.addSocialIdToPerson( + person.personUuid as PersonUuid, + SocialIdType.GITHUB, + dta?._id ?? '', + true + ) + } catch (err: any) { + this.ctx.error('Failed to create social id', { + account: dta?._id, + workspace: payload.workspace, + error: err + }) + } } const socialIdentity = await client.findOne(contact.class.SocialIdentity, { _id: githubSocialId as SocialIdentityRef }) - if (socialIdentity === undefined) { + if (githubSocialId !== undefined && socialIdentity === undefined) { // We need to create a new social id for this account. // We need to create social id github account - await client.addCollection( - contact.class.SocialIdentity, - contact.space.Contacts, - person._id, - contact.class.Person, - 'socialIds', - { - type: SocialIdType.GITHUB, - value: dta._id, - key: buildSocialIdString({ + try { + await client.addCollection( + contact.class.SocialIdentity, + contact.space.Contacts, + person._id, + contact.class.Person, + 'socialIds', + { type: SocialIdType.GITHUB, - value: dta._id - }), - verifiedOn: Date.now() - }, - githubSocialId as SocialIdentityRef - ) + value: dta._id.toLowerCase(), + key: buildSocialIdString({ + type: SocialIdType.GITHUB, + value: dta._id.toLowerCase() + }), + verifiedOn: Date.now() + }, + githubSocialId as SocialIdentityRef + ) + } catch (err: any) { + this.ctx.error('Failed to create social id', { + account: dta?._id, + workspace: payload.workspace, + error: err, + githubSocialId + }) + } } } } else { diff --git a/services/github/pod-github/src/users.ts b/services/github/pod-github/src/users.ts index 0207f15d7b..72d5079b6b 100644 --- a/services/github/pod-github/src/users.ts +++ b/services/github/pod-github/src/users.ts @@ -1,5 +1,5 @@ import type { AccountClient, IntegrationSecret } from '@hcengineering/account-client' -import { systemAccountUuid, type PersonId, type WorkspaceUuid } from '@hcengineering/core' +import core, { systemAccountUuid, type PersonId, type WorkspaceUuid } from '@hcengineering/core' import { getAccountClient } from '@hcengineering/server-client' import { generateToken } from '@hcengineering/server-token' import type { GithubUserRecord } from './types' @@ -49,6 +49,9 @@ export class UserManager { if (rec !== undefined) { return rec } + if (ref === core.account.System || ref === core.account.ConfigUser) { + return undefined + } const secrets = await this.accountClient.listIntegrationsSecrets({ kind: 'github-user', socialId: ref }) if (secrets.length === 0) { diff --git a/services/github/pod-github/src/worker.ts b/services/github/pod-github/src/worker.ts index 36f54b613e..232d9c8b8b 100644 --- a/services/github/pod-github/src/worker.ts +++ b/services/github/pod-github/src/worker.ts @@ -341,7 +341,7 @@ export class GithubWorker implements IntegrationManager { // Find a local social id already existing const existingSocialId = await this._client.findOne(contact.class.SocialIdentity, { type: SocialIdType.GITHUB, - value: userInfo.login + value: userInfo.login.toLowerCase() }) if (existingSocialId !== undefined) { @@ -350,7 +350,7 @@ export class GithubWorker implements IntegrationManager { const { uuid, socialId } = await this.accountClient.ensurePerson( SocialIdType.GITHUB, - userInfo.login, + userInfo.login.toLowerCase(), userInfo.name ?? userInfo.login, '' ) @@ -366,10 +366,10 @@ export class GithubWorker implements IntegrationManager { 'socialIds', { type: SocialIdType.GITHUB, - value: userInfo.login, + value: userInfo.login.toLowerCase(), key: buildSocialIdString({ type: SocialIdType.GITHUB, - value: userInfo.login + value: userInfo.login.toLowerCase() }), verifiedOn: Date.now() }, @@ -471,7 +471,7 @@ export class GithubWorker implements IntegrationManager { if (userInfo.email != null && userInfo.email.trim().length > 0) { const personAccount = await this.client.findOne(contact.class.SocialIdentity, { type: SocialIdType.EMAIL, - value: userInfo.email + value: userInfo.email.toLowerCase() }) person = personAccount?.attachedTo }