|
1 | | -import { proxyActivities } from '@temporalio/workflow' |
| 1 | +import { ParentClosePolicy, executeChild, proxyActivities, startChild } from '@temporalio/workflow' |
2 | 2 |
|
3 | | -import * as activities from '../activities/nangoActivities' |
| 3 | +import type * as activities from '../activities/nangoActivities' |
4 | 4 | import { ISyncGithubIntegrationArguments } from '../types' |
5 | 5 |
|
| 6 | +import { deleteDuplicateGithubConnection } from './deleteDuplicateGithubConnection' |
| 7 | +import { deleteGithubRepoConnection } from './deleteGithubRepoConnection' |
| 8 | +import { syncGithubRepo } from './syncGithubRepo' |
| 9 | + |
6 | 10 | const activity = proxyActivities<typeof activities>({ |
7 | 11 | startToCloseTimeout: '2 hour', |
8 | 12 | retry: { maximumAttempts: 20, backoffCoefficient: 2 }, |
9 | 13 | }) |
10 | 14 |
|
11 | 15 | export async function syncGithubIntegration(args: ISyncGithubIntegrationArguments): Promise<void> { |
12 | | - const integrationId = args.integrationId |
| 16 | + const { integrationId } = args |
13 | 17 |
|
14 | 18 | const result = await activity.analyzeGithubIntegration(integrationId) |
15 | 19 |
|
16 | | - // delete connections that are no longer needed |
| 20 | + // Delete connections that are no longer needed - fire and forget (parallel) |
17 | 21 | for (const repo of result.reposToDelete) { |
18 | | - // delete nango connection |
19 | | - await activity.deleteConnection(integrationId, result.providerConfigKey, repo.connectionId) |
20 | | - |
21 | | - // delete connection from integrations.settings.nangoMapping object |
22 | | - await activity.removeGithubConnection(integrationId, repo.connectionId) |
23 | | - |
24 | | - // delete from public.repositories |
25 | | - await activity.unmapGithubRepo(integrationId, repo.repo) |
| 22 | + await startChild(deleteGithubRepoConnection, { |
| 23 | + workflowId: `sync-github/${integrationId}/delete-connection/${repo.repo.owner}/${repo.repo.repoName}/${repo.connectionId}`, |
| 24 | + parentClosePolicy: ParentClosePolicy.PARENT_CLOSE_POLICY_ABANDON, |
| 25 | + args: [ |
| 26 | + { |
| 27 | + integrationId, |
| 28 | + providerConfigKey: result.providerConfigKey, |
| 29 | + connectionId: repo.connectionId, |
| 30 | + repo: repo.repo, |
| 31 | + }, |
| 32 | + ], |
| 33 | + }) |
26 | 34 | } |
27 | 35 |
|
28 | | - // delete duplicate connections |
| 36 | + // Delete duplicate connections - fire and forget (parallel) |
29 | 37 | for (const repo of result.duplicatesToDelete) { |
30 | | - // delete nango connection |
31 | | - await activity.deleteConnection(integrationId, result.providerConfigKey, repo.connectionId) |
32 | | - |
33 | | - // delete connection from integrations.settings.nangoMapping object |
34 | | - await activity.removeGithubConnection(integrationId, repo.connectionId) |
35 | | - |
36 | | - // we don't unmap because this one was duplicated |
| 38 | + await startChild(deleteDuplicateGithubConnection, { |
| 39 | + workflowId: `sync-github/${integrationId}/delete-duplicate/${repo.repo.owner}/${repo.repo.repoName}/${repo.connectionId}`, |
| 40 | + parentClosePolicy: ParentClosePolicy.PARENT_CLOSE_POLICY_ABANDON, |
| 41 | + args: [ |
| 42 | + { |
| 43 | + integrationId, |
| 44 | + providerConfigKey: result.providerConfigKey, |
| 45 | + connectionId: repo.connectionId, |
| 46 | + repo: repo.repo, |
| 47 | + }, |
| 48 | + ], |
| 49 | + }) |
37 | 50 | } |
38 | 51 |
|
39 | | - // create connections for repos that are not already connected |
| 52 | + // Create connections for repos that are not already connected - sequential (rate limiting) |
40 | 53 | for (const repo of result.reposToSync) { |
41 | | - const canCreate = await activity.canCreateGithubConnection() |
42 | | - |
43 | | - if (!canCreate) { |
| 54 | + const { skipped } = await executeChild(syncGithubRepo, { |
| 55 | + workflowId: `sync-github/${integrationId}/create-connection/${repo.owner}/${repo.repoName}`, |
| 56 | + parentClosePolicy: ParentClosePolicy.PARENT_CLOSE_POLICY_REQUEST_CANCEL, |
| 57 | + args: [ |
| 58 | + { |
| 59 | + integrationId, |
| 60 | + providerConfigKey: result.providerConfigKey, |
| 61 | + repo, |
| 62 | + }, |
| 63 | + ], |
| 64 | + }) |
| 65 | + |
| 66 | + if (skipped) { |
44 | 67 | await activity.logInfo( |
45 | 68 | `Not enough time has passed since last connection! Skipping repo ${repo.owner}/${repo.repoName} from integration ${integrationId}!`, |
46 | 69 | ) |
47 | | - continue |
48 | 70 | } |
49 | | - |
50 | | - // create nango connection |
51 | | - const connectionId = await activity.createGithubConnection(integrationId, repo) |
52 | | - |
53 | | - // add connection to integrations.settings.nangoMapping object |
54 | | - await activity.setGithubConnection(integrationId, repo, connectionId) |
55 | | - |
56 | | - // add repo to git integration |
57 | | - await activity.updateGitIntegrationWithRepo(integrationId, repo) |
58 | | - |
59 | | - // add repo to public.repositories (+ git.repositoryProcessing if first time) |
60 | | - await activity.mapGithubRepoToRepositories(integrationId, repo) |
61 | | - |
62 | | - // start nango sync |
63 | | - await activity.startNangoSync(integrationId, result.providerConfigKey, connectionId) |
64 | 71 | } |
65 | 72 | } |
0 commit comments