mirror of
https://github.com/hcengineering/platform.git
synced 2026-08-25 05:42:23 +02:00
@@ -11,6 +11,7 @@ import core, {
|
||||
ClientConnectEvent,
|
||||
DocumentUpdate,
|
||||
isActiveMode,
|
||||
isDeletingMode,
|
||||
MeasureContext,
|
||||
RateLimiter,
|
||||
Ref,
|
||||
@@ -712,33 +713,36 @@ export class PlatformWorker {
|
||||
return Array.from(workspaces)
|
||||
}
|
||||
|
||||
async checkWorkspaceIsActive (token: string, workspace: string): Promise<ClientWorkspaceInfo | undefined> {
|
||||
async checkWorkspaceIsActive (
|
||||
token: string,
|
||||
workspace: string
|
||||
): Promise<{ workspaceInfo: ClientWorkspaceInfo | undefined, needRecheck: boolean }> {
|
||||
let workspaceInfo: ClientWorkspaceInfo | undefined
|
||||
try {
|
||||
workspaceInfo = await getWorkspaceInfo(token)
|
||||
} catch (err: any) {
|
||||
this.ctx.error('Workspace not found:', { workspace })
|
||||
return
|
||||
return { workspaceInfo: undefined, needRecheck: false }
|
||||
}
|
||||
if (workspaceInfo?.workspace === undefined) {
|
||||
this.ctx.error('No workspace exists for workspaceId', { workspace })
|
||||
return
|
||||
return { workspaceInfo: undefined, needRecheck: false }
|
||||
}
|
||||
if (workspaceInfo?.disabled === true || isDeletingMode(workspaceInfo?.mode)) {
|
||||
this.ctx.warn('Workspace is disabled', { workspace })
|
||||
return { workspaceInfo: undefined, needRecheck: false }
|
||||
}
|
||||
if (!isActiveMode(workspaceInfo?.mode)) {
|
||||
this.ctx.warn('Workspace is in maitenance, skipping for now.', { workspace })
|
||||
return
|
||||
}
|
||||
if (workspaceInfo?.disabled === true) {
|
||||
this.ctx.warn('Workspace is disabled', { workspace })
|
||||
return
|
||||
this.ctx.warn('Workspace is in maitenance, skipping for now.', { workspace, mode: workspaceInfo?.mode })
|
||||
return { workspaceInfo: undefined, needRecheck: true }
|
||||
}
|
||||
const lastVisit = (Date.now() - workspaceInfo.lastVisit) / (3600 * 24 * 1000) // In days
|
||||
|
||||
if (config.WorkspaceInactivityInterval > 0 && lastVisit > config.WorkspaceInactivityInterval) {
|
||||
this.ctx.warn('Workspace is inactive for too long, skipping for now.', { workspace })
|
||||
return
|
||||
return { workspaceInfo: undefined, needRecheck: true }
|
||||
}
|
||||
return workspaceInfo
|
||||
return { workspaceInfo, needRecheck: true }
|
||||
}
|
||||
|
||||
private async checkWorkspaces (): Promise<boolean> {
|
||||
@@ -781,9 +785,11 @@ export class PlatformWorker {
|
||||
},
|
||||
{ mode: 'github' }
|
||||
)
|
||||
const workspaceInfo = await this.checkWorkspaceIsActive(token, workspace)
|
||||
const { workspaceInfo, needRecheck } = await this.checkWorkspaceIsActive(token, workspace)
|
||||
if (workspaceInfo === undefined) {
|
||||
errors++
|
||||
if (needRecheck) {
|
||||
errors++
|
||||
}
|
||||
return
|
||||
}
|
||||
try {
|
||||
|
||||
@@ -497,7 +497,15 @@ export abstract class IssueSyncManagerBase {
|
||||
return (pField.node.options ?? []).find((it) => it.id === field.optionId)
|
||||
}
|
||||
|
||||
findOptionId (container: ContainerFocus, fieldId: string, value: string, target: IssueSyncTarget): string | undefined {
|
||||
findOptionId (
|
||||
container: ContainerFocus,
|
||||
fieldId: string,
|
||||
value: string | null,
|
||||
target: IssueSyncTarget
|
||||
): string | undefined {
|
||||
if (value == null) {
|
||||
return
|
||||
}
|
||||
const structure = container.container.projectStructure.get(target.target._id)
|
||||
if (structure === undefined) {
|
||||
return
|
||||
@@ -506,7 +514,7 @@ export abstract class IssueSyncManagerBase {
|
||||
if (pField === undefined) {
|
||||
return undefined
|
||||
}
|
||||
return (pField.node.options ?? []).find((it) => it.name.toLowerCase() === value.toLowerCase())?.id
|
||||
return (pField.node.options ?? []).find((it) => it.name?.toLowerCase() === value.toLowerCase())?.id
|
||||
}
|
||||
|
||||
async toPlatformField (
|
||||
|
||||
@@ -344,7 +344,7 @@ export class ProjectsSyncManager implements DocSyncManager {
|
||||
derivedClient: TxOperations,
|
||||
deleteExisting: boolean
|
||||
): Promise<boolean> {
|
||||
return false
|
||||
return true
|
||||
}
|
||||
|
||||
async externalSync (
|
||||
|
||||
@@ -127,6 +127,9 @@ export async function getSinceRaw (
|
||||
export function gqlp (params: Record<string, string | number | string[] | undefined>): string {
|
||||
let result = ''
|
||||
let first = true
|
||||
function escape (str: string): string {
|
||||
return str.replace(/"/g, '\\"')
|
||||
}
|
||||
for (const [k, v] of Object.entries(params)) {
|
||||
if (v !== undefined) {
|
||||
if (!first) {
|
||||
@@ -136,9 +139,9 @@ export function gqlp (params: Record<string, string | number | string[] | undefi
|
||||
if (typeof v === 'number') {
|
||||
result += `${k}: ${v}`
|
||||
} else if (Array.isArray(v)) {
|
||||
result += `${k}: [${v.map((it) => `"${it}"`).join(', ')}]`
|
||||
result += `${k}: [${v.map((it) => `"${escape(it)}"`).join(', ')}]`
|
||||
} else {
|
||||
result += `${k}: "${v}"`
|
||||
result += `${k}: "${escape(v)}"`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -226,7 +226,10 @@ export class GithubWorker implements IntegrationManager {
|
||||
}
|
||||
}
|
||||
|
||||
async getAccountU (user: User): Promise<PersonAccount | undefined> {
|
||||
async getAccountU (user?: User): Promise<PersonAccount | undefined> {
|
||||
if (user == null) {
|
||||
return undefined
|
||||
}
|
||||
return await this.getAccount({
|
||||
id: user.node_id,
|
||||
login: user.login,
|
||||
@@ -1101,38 +1104,41 @@ export class GithubWorker implements IntegrationManager {
|
||||
this.ctx.error('Failed to perform full sync', { error: err })
|
||||
})
|
||||
}
|
||||
|
||||
const { projects, repositories } = await this.collectActiveProjects()
|
||||
if (projects.length === 0 && repositories.length === 0) {
|
||||
await this.waitChanges()
|
||||
continue
|
||||
}
|
||||
|
||||
// Check if we have documents with external sync request's pending.
|
||||
const hadExternalChanges = await this.performExternalSync(
|
||||
projects,
|
||||
repositories,
|
||||
'externalVersion',
|
||||
githubExternalSyncVersion
|
||||
)
|
||||
const hadSyncChanges = await this.performSync(projects, repositories)
|
||||
|
||||
// Perform derived operations
|
||||
// Sync derived external data, like pull request reviews, files etc.
|
||||
const hadDerivedChanges = await this.performExternalSync(
|
||||
projects,
|
||||
repositories,
|
||||
'derivedVersion',
|
||||
githubDerivedSyncVersion
|
||||
)
|
||||
|
||||
if (!hadExternalChanges && !hadSyncChanges && !hadDerivedChanges) {
|
||||
if (this.previousWait !== 0) {
|
||||
this.ctx.info('Wait for changes:', { previousWait: this.previousWait, workspace: this.workspace.name })
|
||||
this.previousWait = 0
|
||||
try {
|
||||
const { projects, repositories } = await this.collectActiveProjects()
|
||||
if (projects.length === 0 && repositories.length === 0) {
|
||||
await this.waitChanges()
|
||||
continue
|
||||
}
|
||||
// Wait until some sync documents will be modified, updated.
|
||||
await this.waitChanges()
|
||||
|
||||
// Check if we have documents with external sync request's pending.
|
||||
const hadExternalChanges = await this.performExternalSync(
|
||||
projects,
|
||||
repositories,
|
||||
'externalVersion',
|
||||
githubExternalSyncVersion
|
||||
)
|
||||
const hadSyncChanges = await this.performSync(projects, repositories)
|
||||
|
||||
// Perform derived operations
|
||||
// Sync derived external data, like pull request reviews, files etc.
|
||||
const hadDerivedChanges = await this.performExternalSync(
|
||||
projects,
|
||||
repositories,
|
||||
'derivedVersion',
|
||||
githubDerivedSyncVersion
|
||||
)
|
||||
|
||||
if (!hadExternalChanges && !hadSyncChanges && !hadDerivedChanges) {
|
||||
if (this.previousWait !== 0) {
|
||||
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.
|
||||
await this.waitChanges()
|
||||
}
|
||||
} catch (err: any) {
|
||||
this.ctx.error('failed to perform sync', { err, workspace: this.workspace.name })
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1327,25 +1333,30 @@ export class GithubWorker implements IntegrationManager {
|
||||
const targetProject = await this.client.findOne(github.mixin.GithubProject, {
|
||||
_id: existing.space as Ref<GithubProject>
|
||||
})
|
||||
if (await mapper.handleDelete(existing, info, derivedClient, false, parent)) {
|
||||
const h = this._client.getHierarchy()
|
||||
await derivedClient.remove(info)
|
||||
if (h.hasMixin(existing, github.mixin.GithubIssue)) {
|
||||
const mixinData = this._client.getHierarchy().as(existing, github.mixin.GithubIssue)
|
||||
await this._client.update<GithubIssue>(
|
||||
mixinData,
|
||||
{
|
||||
url: '',
|
||||
githubNumber: 0,
|
||||
repository: '' as Ref<GithubIntegrationRepository>
|
||||
},
|
||||
false,
|
||||
Date.now(),
|
||||
existing.modifiedBy
|
||||
)
|
||||
try {
|
||||
if (await mapper.handleDelete(existing, info, derivedClient, false, parent)) {
|
||||
const h = this._client.getHierarchy()
|
||||
await derivedClient.remove(info)
|
||||
if (h.hasMixin(existing, github.mixin.GithubIssue)) {
|
||||
const mixinData = this._client.getHierarchy().as(existing, github.mixin.GithubIssue)
|
||||
await this._client.update<GithubIssue>(
|
||||
mixinData,
|
||||
{
|
||||
url: '',
|
||||
githubNumber: 0,
|
||||
repository: '' as Ref<GithubIntegrationRepository>
|
||||
},
|
||||
false,
|
||||
Date.now(),
|
||||
existing.modifiedBy
|
||||
)
|
||||
}
|
||||
return
|
||||
}
|
||||
return
|
||||
} catch (err: any) {
|
||||
this.ctx.error('failed to handle delete', { err })
|
||||
}
|
||||
|
||||
if (targetProject !== undefined) {
|
||||
// We need to sync into new project.
|
||||
await derivedClient.update<DocSyncInfo>(info, {
|
||||
@@ -1361,8 +1372,12 @@ export class GithubWorker implements IntegrationManager {
|
||||
}
|
||||
|
||||
if (info.deleted === true) {
|
||||
if (await mapper.handleDelete(existing, info, derivedClient, true)) {
|
||||
await derivedClient.remove(info)
|
||||
try {
|
||||
if (await mapper.handleDelete(existing, info, derivedClient, true)) {
|
||||
await derivedClient.remove(info)
|
||||
}
|
||||
} catch (err: any) {
|
||||
this.ctx.error('failed to handle delete', { err })
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user