mirror of
https://github.com/hcengineering/platform.git
synced 2026-08-25 22:02:23 +02:00
UBERF-7924: Fix workspace variable in logs + reuse installation account (#6376)
This commit is contained in:
@@ -31,6 +31,7 @@ import core, {
|
||||
concatLink,
|
||||
generateId,
|
||||
groupByArray,
|
||||
reduceCalls,
|
||||
toIdMap,
|
||||
type Blob,
|
||||
type MigrationState
|
||||
@@ -189,9 +190,11 @@ export class GithubWorker implements IntegrationManager {
|
||||
if (v.octokit === undefined) {
|
||||
continue
|
||||
}
|
||||
const project = await this.liveQuery.findOne<GithubProject>(github.mixin.GithubProject, {
|
||||
_id: space as Ref<GithubProject>
|
||||
})
|
||||
const project = (
|
||||
await this.liveQuery.queryFind<GithubProject>(github.mixin.GithubProject, {
|
||||
_id: space as Ref<GithubProject>
|
||||
})
|
||||
).shift()
|
||||
if (project !== undefined) {
|
||||
const repositories = await this.liveQuery.queryFind<GithubIntegrationRepository>(
|
||||
github.class.GithubIntegrationRepository,
|
||||
@@ -299,7 +302,7 @@ export class GithubWorker implements IntegrationManager {
|
||||
person,
|
||||
role: AccountRole.User
|
||||
})
|
||||
const acc = await this._client.findOne(contact.class.PersonAccount, { _id: id })
|
||||
const acc = await this.liveQuery.findOne(contact.class.PersonAccount, { _id: id })
|
||||
return acc
|
||||
}
|
||||
}
|
||||
@@ -391,12 +394,12 @@ export class GithubWorker implements IntegrationManager {
|
||||
let person: Ref<Person> | undefined
|
||||
// try to find by account.
|
||||
if (userInfo.email != null) {
|
||||
const personAccount = await this.client.findOne(contact.class.PersonAccount, { email: userInfo.email })
|
||||
const personAccount = await this.liveQuery.findOne(contact.class.PersonAccount, { email: userInfo.email })
|
||||
person = personAccount?.person
|
||||
}
|
||||
|
||||
if (person === undefined) {
|
||||
const channel = await this.client.findOne(contact.class.Channel, {
|
||||
const channel = await this.liveQuery.findOne(contact.class.Channel, {
|
||||
provider: contact.channelProvider.GitHub,
|
||||
value: userInfo.login
|
||||
})
|
||||
@@ -475,14 +478,19 @@ export class GithubWorker implements IntegrationManager {
|
||||
|
||||
async syncUserData (ctx: MeasureContext, users: GithubUserRecord[]): Promise<void> {
|
||||
// Let's sync information about users and send some details
|
||||
const accounts = await this._client.findAll(contact.class.PersonAccount, {
|
||||
email: { $in: users.map((it) => `github:${it._id}`) }
|
||||
})
|
||||
const userAuths = await this._client.findAll(github.class.GithubAuthentication, {})
|
||||
const persons = await this._client.findAll(contact.class.Person, { _id: { $in: accounts.map((it) => it.person) } })
|
||||
for (const record of users) {
|
||||
if (record.error !== undefined) {
|
||||
// Skip accounts with error
|
||||
continue
|
||||
}
|
||||
const account = await this._client.findOne(contact.class.PersonAccount, { email: `github:${record._id}` })
|
||||
const userAuth = await this._client.findOne(github.class.GithubAuthentication, { login: record._id })
|
||||
const person = await this._client.findOne(contact.class.Person, { _id: account?.person })
|
||||
const account = accounts.find((it) => it.email === `github:${record._id}`)
|
||||
const userAuth = userAuths.find((it) => it.login === record._id)
|
||||
const person = persons.find((it) => account?.person)
|
||||
if (account === undefined || userAuth === undefined || person === undefined) {
|
||||
continue
|
||||
}
|
||||
@@ -517,12 +525,11 @@ export class GithubWorker implements IntegrationManager {
|
||||
let record = await this.platform.getAccountByRef(this.workspace.name, account)
|
||||
|
||||
// const accountRef = this.accounts.find((it) => it._id === account)
|
||||
const accountRef = await this._client.findOne(contact.class.PersonAccount, { _id: account })
|
||||
const accountRef = await this.liveQuery.findOne(contact.class.PersonAccount, { _id: account })
|
||||
if (record === undefined) {
|
||||
if (accountRef !== undefined) {
|
||||
const accounts = await this._client.findAll(contact.class.PersonAccount, {})
|
||||
const allAccounts = accounts.filter((it) => it.person === accountRef.person)
|
||||
for (const aa of allAccounts) {
|
||||
const accounts = this._client.getModel().getAccountByPersonId(accountRef.person)
|
||||
for (const aa of accounts) {
|
||||
record = await this.platform.getAccountByRef(this.workspace.name, aa._id)
|
||||
if (record !== undefined) {
|
||||
break
|
||||
@@ -532,7 +539,7 @@ export class GithubWorker implements IntegrationManager {
|
||||
}
|
||||
// Check and refresh token if required.
|
||||
if (record !== undefined) {
|
||||
this.ctx.info('get octokit', { account, recordId: record._id })
|
||||
this.ctx.info('get octokit', { account, recordId: record._id, workspace: this.workspace.name })
|
||||
await this.platform.checkRefreshToken(record)
|
||||
return new Octokit({
|
||||
auth: record.token,
|
||||
@@ -543,9 +550,9 @@ export class GithubWorker implements IntegrationManager {
|
||||
|
||||
// We need to inform user, he need to authorize this account with github.
|
||||
if (accountRef !== undefined) {
|
||||
const person = await this.client.findOne(contact.class.Person, { _id: accountRef.person })
|
||||
const person = await this.liveQuery.findOne(contact.class.Person, { _id: accountRef.person })
|
||||
if (person !== undefined) {
|
||||
const personSpace = await this.client.findOne(contact.class.PersonSpace, { person: person._id })
|
||||
const personSpace = await this.liveQuery.findOne(contact.class.PersonSpace, { person: person._id })
|
||||
if (personSpace !== undefined) {
|
||||
await createNotification(this._client, person, {
|
||||
user: account,
|
||||
@@ -556,17 +563,16 @@ export class GithubWorker implements IntegrationManager {
|
||||
}
|
||||
}
|
||||
}
|
||||
this.ctx.info('get octokit: return bot', { account })
|
||||
this.ctx.info('get octokit: return bot', { account, workspace: this.workspace.name })
|
||||
}
|
||||
|
||||
async isPlatformUser (account: Ref<PersonAccount>): Promise<boolean> {
|
||||
let record = await this.platform.getAccountByRef(this.workspace.name, account)
|
||||
const accountRef = await this._client.findOne(contact.class.PersonAccount, { _id: account })
|
||||
const accountRef = await this.liveQuery.findOne(contact.class.PersonAccount, { _id: account })
|
||||
if (record === undefined) {
|
||||
if (accountRef !== undefined) {
|
||||
const accounts = await this._client.findAll(contact.class.PersonAccount, {})
|
||||
const allAccounts = accounts.filter((it) => it.person === accountRef.person)
|
||||
for (const aa of allAccounts) {
|
||||
const accounts = this._client.getModel().getAccountByPersonId(accountRef.person)
|
||||
for (const aa of accounts) {
|
||||
record = await this.platform.getAccountByRef(this.workspace.name, aa._id)
|
||||
if (record !== undefined) {
|
||||
break
|
||||
@@ -588,11 +594,11 @@ export class GithubWorker implements IntegrationManager {
|
||||
integrationsRaw: GithubIntegration[] = []
|
||||
|
||||
async getProjectType (type: Ref<ProjectType>): Promise<ProjectType | undefined> {
|
||||
return await this._client.findOne(task.class.ProjectType, { _id: type })
|
||||
return (await this.liveQuery.queryFind(task.class.ProjectType, { _id: type })).shift()
|
||||
}
|
||||
|
||||
async getTaskType (type: Ref<TaskType>): Promise<TaskType | undefined> {
|
||||
return await this._client.findOne(task.class.TaskType, { _id: type })
|
||||
return (await this.liveQuery.queryFind(task.class.TaskType, { _id: type })).shift()
|
||||
}
|
||||
|
||||
async getTaskTypeOf (project: Ref<ProjectType>, ofClass: Ref<Class<Doc>>): Promise<TaskType | undefined> {
|
||||
@@ -687,13 +693,16 @@ export class GithubWorker implements IntegrationManager {
|
||||
})
|
||||
})
|
||||
|
||||
const userRecords = await this.platform.getUsers(this.workspace.name)
|
||||
|
||||
await this.syncUserData(this.ctx, userRecords)
|
||||
|
||||
this.triggerRequests = 1
|
||||
this.updateRequests = 1
|
||||
this.syncPromise = this.syncAndWait()
|
||||
|
||||
const userRecords = await this.platform.getUsers(this.workspace.name)
|
||||
try {
|
||||
await this.syncUserData(this.ctx, userRecords)
|
||||
} catch (err: any) {
|
||||
Analytics.handleError(err)
|
||||
}
|
||||
}
|
||||
|
||||
projects: GithubProject[] = []
|
||||
@@ -756,7 +765,7 @@ export class GithubWorker implements IntegrationManager {
|
||||
syncLock: new Map()
|
||||
}
|
||||
this.integrations.set(it.installationId, current)
|
||||
await this.repositoryManager.reloadRepositories(current)
|
||||
await this.repositoryManager.reloadRepositories(current, inst.repositories)
|
||||
} catch (err: any) {
|
||||
Analytics.handleError(err)
|
||||
this.ctx.error('Error', { err })
|
||||
@@ -768,7 +777,7 @@ export class GithubWorker implements IntegrationManager {
|
||||
continue
|
||||
}
|
||||
current.integration = it
|
||||
await this.repositoryManager.reloadRepositories(current)
|
||||
await this.repositoryManager.reloadRepositories(current, inst.repositories)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -919,7 +928,14 @@ export class GithubWorker implements IntegrationManager {
|
||||
continue
|
||||
}
|
||||
|
||||
this.ctx.info('External Syncing', { name: repo.name, prj: prj.name, field, version, docs: docs.length })
|
||||
this.ctx.info('External Syncing', {
|
||||
name: repo.name,
|
||||
prj: prj.name,
|
||||
field,
|
||||
version,
|
||||
docs: docs.length,
|
||||
workspace: this.workspace.name
|
||||
})
|
||||
|
||||
const byClass = this.groupByClass(docs)
|
||||
for (const [_class, _docs] of byClass.entries()) {
|
||||
@@ -1043,7 +1059,7 @@ export class GithubWorker implements IntegrationManager {
|
||||
if (this.updateRequests > 0) {
|
||||
this.updateRequests = 0 // Just in case
|
||||
await this.updateIntegrations()
|
||||
await this.performFullSync()
|
||||
void this.performFullSync()
|
||||
}
|
||||
|
||||
const { projects, repositories } = await this.collectActiveProjects()
|
||||
@@ -1072,7 +1088,7 @@ export class GithubWorker implements IntegrationManager {
|
||||
|
||||
if (!hadExternalChanges && !hadSyncChanges && !hadDerivedChanges) {
|
||||
if (this.previousWait !== 0) {
|
||||
this.ctx.info('Wait for changes:', { previousWait: this.previousWait })
|
||||
this.ctx.info('Wait for changes:', { previousWait: this.previousWait, workspace: this.workspace.name })
|
||||
this.previousWait = 0
|
||||
}
|
||||
// Wait until some sync documents will be modified, updated.
|
||||
@@ -1125,7 +1141,7 @@ export class GithubWorker implements IntegrationManager {
|
||||
|
||||
if (docs.length > 0) {
|
||||
this.previousWait += docs.length
|
||||
this.ctx.info('Syncing', { docs: docs.length })
|
||||
this.ctx.info('Syncing', { docs: docs.length, workspace: this.workspace.name })
|
||||
|
||||
await this.doSyncFor(docs)
|
||||
}
|
||||
@@ -1239,7 +1255,7 @@ export class GithubWorker implements IntegrationManager {
|
||||
const existing = externalDocs.find((it) => it._id === info._id)
|
||||
const mapper = this.mappers.find((it) => it._class.includes(info.objectClass))?.mapper
|
||||
if (mapper === undefined) {
|
||||
this.ctx.info('No mapper for class', { objectClass: info.objectClass })
|
||||
this.ctx.info('No mapper for class', { objectClass: info.objectClass, workspace: this.workspace.name })
|
||||
await derivedClient.update<DocSyncInfo>(info, {
|
||||
needSync: githubSyncVersion
|
||||
})
|
||||
@@ -1333,7 +1349,11 @@ export class GithubWorker implements IntegrationManager {
|
||||
|
||||
private async waitChanges (): Promise<void> {
|
||||
if (this.triggerRequests > 0 || this.updateRequests > 0) {
|
||||
this.ctx.info('Trigger check pending:', { requests: this.triggerRequests, updates: this.updateRequests })
|
||||
this.ctx.info('Trigger check pending:', {
|
||||
requests: this.triggerRequests,
|
||||
updates: this.updateRequests,
|
||||
workspace: this.workspace.name
|
||||
})
|
||||
this.triggerRequests = 0
|
||||
return
|
||||
}
|
||||
@@ -1347,7 +1367,7 @@ export class GithubWorker implements IntegrationManager {
|
||||
triggerTimeout = setTimeout(() => {
|
||||
triggerTimeout = undefined
|
||||
if (was0) {
|
||||
this.ctx.info('Sync triggered', { request: this.triggerRequests })
|
||||
this.ctx.info('Sync triggered', { request: this.triggerRequests, workspace: this.workspace.name })
|
||||
}
|
||||
resolve()
|
||||
}, 50) // Small timeout to aggregate few bulk changes.
|
||||
@@ -1361,7 +1381,7 @@ export class GithubWorker implements IntegrationManager {
|
||||
updateTimeout = setTimeout(() => {
|
||||
updateTimeout = undefined
|
||||
if (was0) {
|
||||
this.ctx.info('Sync update triggered', { requests: this.updateRequests })
|
||||
this.ctx.info('Sync update triggered', { requests: this.updateRequests, workspace: this.workspace.name })
|
||||
}
|
||||
resolve()
|
||||
}, 50) // Small timeout to aggregate few bulk changes.
|
||||
@@ -1378,7 +1398,11 @@ export class GithubWorker implements IntegrationManager {
|
||||
this.triggerSync()
|
||||
}
|
||||
|
||||
async performFullSync (): Promise<void> {
|
||||
performFullSync = reduceCalls(async () => {
|
||||
await this._performFullSync()
|
||||
})
|
||||
|
||||
async _performFullSync (): Promise<void> {
|
||||
// Wait previous active sync
|
||||
for (const integration of this.integrations.values()) {
|
||||
await this.ctx.withLog('external sync', { installation: integration.installationName }, async () => {
|
||||
|
||||
Reference in New Issue
Block a user