UBERF-9724: Fix github functionality on 0.7 (#8554)

* UBERF-9724: Fix github functionality on 0.7

Signed-off-by: Andrey Sobolev <haiodo@gmail.com>

* Fix exception for no social id for system account

Signed-off-by: Andrey Sobolev <haiodo@gmail.com>

---------

Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
This commit is contained in:
Andrey Sobolev
2025-04-15 17:18:49 +07:00
committed by GitHub
parent a8d6eaca92
commit e20ec51363
6 changed files with 69 additions and 35 deletions
+1 -2
View File
@@ -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)
+4
View File
@@ -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(
+54 -26
View File
@@ -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 {
+4 -1
View File
@@ -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) {
+5 -5
View File
@@ -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
}