Skip to content

Commit

Permalink
UBERF-9137: Fix Support for suspended installations
Browse files Browse the repository at this point in the history
Signed-off-by: Andrey Sobolev <[email protected]>
  • Loading branch information
haiodo committed Jan 14, 2025
1 parent e81100c commit 022e284
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 23 deletions.
20 changes: 14 additions & 6 deletions services/github/pod-github/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,23 @@ Analytics.setTag('application', 'github-service')

let doOnClose: () => Promise<void> = async () => {}

void start(metricsContext, loadBrandingMap(config.BrandingPath)).then((r) => {
doOnClose = r
})
void start(metricsContext, loadBrandingMap(config.BrandingPath))
.then((r) => {
doOnClose = r
})
.catch((err) => {
metricsContext.error('Error', { error: err })
})

const onClose = (): void => {
metricsContext.info('Closed')
void doOnClose().then((r) => {
process.exit(0)
})
void doOnClose()
.then((r) => {
process.exit(0)
})
.catch((err) => {
metricsContext.error('Error', { error: err })
})
}

process.on('uncaughtException', (e) => {
Expand Down
41 changes: 29 additions & 12 deletions services/github/pod-github/src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export interface InstallationRecord {
repositories?: InstallationCreatedEvent['repositories'] | InstallationUnsuspendEvent['repositories']
type: 'Bot' | 'User' | 'Organization'
octokit: Octokit
suspended: boolean
}

export class PlatformWorker {
Expand Down Expand Up @@ -593,7 +594,8 @@ export class PlatformWorker {
login: tinst.account.login,
loginNodeId: tinst.account.node_id,
type: tinst.account?.type ?? 'User',
installationName: `${tinst.account?.html_url ?? ''}`
installationName: `${tinst.account?.html_url ?? ''}`,
suspended: install.data.suspended_at != null
}
this.updateInstallationRecord(installationId, val)
}
Expand Down Expand Up @@ -625,7 +627,8 @@ export class PlatformWorker {
login: tinst.account.login,
loginNodeId: tinst.account.node_id,
type: tinst.account?.type ?? 'User',
installationName: `${tinst.account?.html_url ?? ''}`
installationName: `${tinst.account?.html_url ?? ''}`,
suspended: install.installation.suspended_at != null
}
this.updateInstallationRecord(install.installation.id, val)
ctx.info('Found installation', {
Expand All @@ -650,7 +653,8 @@ export class PlatformWorker {
type: install.account?.type ?? 'User',
loginNodeId: install.account.node_id,
installationName: iName,
repositories
repositories,
suspended: !enabled
})

const worker = this.getWorker(install.id)
Expand Down Expand Up @@ -810,17 +814,28 @@ export class PlatformWorker {
this.storageAdapter,
(workspace, event) => {
if (event === ClientConnectEvent.Refresh || event === ClientConnectEvent.Upgraded) {
void this.clients.get(workspace)?.refreshClient(event === ClientConnectEvent.Upgraded)
void this.clients
.get(workspace)
?.refreshClient(event === ClientConnectEvent.Upgraded)
?.catch((err) => {
workerCtx.error('Failed to refresh', { error: err })
})
}
if (initialized) {
// We need to check if workspace is inactive
void this.checkWorkspaceIsActive(token, workspace).then((res) => {
if (res === undefined) {
this.ctx.warn('Workspace is inactive, removing from clients list.', { workspace })
this.clients.delete(workspace)
void worker?.close()
}
})
void this.checkWorkspaceIsActive(token, workspace)
.then((res) => {
if (res === undefined) {
this.ctx.warn('Workspace is inactive, removing from clients list.', { workspace })
this.clients.delete(workspace)
void worker?.close().catch((err) => {
this.ctx.error('Failed to close workspace', { workspace, error: err })
})
}
})
.catch((err) => {
this.ctx.error('Failed to check workspace is active', { workspace, error: err })
})
}
}
)
Expand Down Expand Up @@ -879,7 +894,9 @@ export class PlatformWorker {
try {
this.ctx.info('workspace removed from tracking list', { workspace: deleted })
this.clients.delete(deleted)
void ws.close()
void ws.close().catch((err) => {
this.ctx.error('Error', { error: err })
})
} catch (err: any) {
Analytics.handleError(err)
errors++
Expand Down
27 changes: 22 additions & 5 deletions services/github/pod-github/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,11 @@ export class GithubWorker implements IntegrationManager {
const ops = new TxOperations(this.client, accountRef)
await syncUser(ctx, record, userAuth, ops, accountRef)
} catch (err: any) {
await this.platform.revokeUserAuth(record)
try {
await this.platform.revokeUserAuth(record)
} catch (err: any) {
ctx.error(`Failed to revoke user ${record._id}`, err)
}
if (err.response?.data?.message !== 'Bad credentials') {
ctx.error(`Failed to sync user ${record._id}`, err)
Analytics.handleError(err)
Expand Down Expand Up @@ -796,7 +800,7 @@ export class GithubWorker implements IntegrationManager {
loginNodeId: inst.loginNodeId ?? '',
type: inst.type ?? 'User',
installationName: inst?.installationName ?? '',
enabled: true,
enabled: !inst.suspended,
synchronized: new Set(),
projectStructure: new Map(),
syncLock: new Map()
Expand Down Expand Up @@ -1112,7 +1116,9 @@ export class GithubWorker implements IntegrationManager {
if (this.updateRequests > 0) {
this.updateRequests = 0 // Just in case
await this.updateIntegrations()
void this.performFullSync()
void this.performFullSync().catch((err) => {
this.ctx.error('Failed to perform full sync', { error: err })
})
}

const { projects, repositories } = await this.collectActiveProjects()
Expand Down Expand Up @@ -1212,7 +1218,12 @@ export class GithubWorker implements IntegrationManager {

for (const it of Array.from(this.integrations.values())) {
if (it.enabled) {
const _projects = allProjects.filter((p) => !syncConfig.MainProject || it.projectStructure.has(p._id))
const _projects = []
for (const p of allProjects) {
if (p.integration === it.integration._id && (!syncConfig.MainProject || it.projectStructure.has(p._id))) {
_projects.push(p)
}
}

const prjIds = new Set(_projects.map((it) => it._id))

Expand Down Expand Up @@ -1591,7 +1602,13 @@ export class GithubWorker implements IntegrationManager {
branding
)
ctx.info('Init worker', { workspace: workspace.workspaceUrl, workspaceId: workspace.workspaceName })
void worker.init()
void worker.init().catch((err) => {
ctx.error('Failed to init worker', {
workspace: workspace.workspaceUrl,
workspaceId: workspace.workspaceName,
error: err
})
})
return worker
} catch (err: any) {
ctx.error('timeout during to connect', { workspace, error: err })
Expand Down

0 comments on commit 022e284

Please sign in to comment.