|
1 | | -import { proxyActivities, sleep } from '@temporalio/workflow' |
| 1 | +import { proxyActivities } from '@temporalio/workflow' |
2 | 2 |
|
3 | 3 | import * as activities from '../activities/nangoActivities' |
4 | 4 | import { ISyncGithubIntegrationArguments } from '../types' |
5 | 5 |
|
6 | | -const REPO_ONBOARDING_INTERVAL_MINUTES = 6 |
7 | | - |
8 | 6 | const activity = proxyActivities<typeof activities>({ |
9 | 7 | startToCloseTimeout: '2 hour', |
10 | 8 | retry: { maximumAttempts: 20, backoffCoefficient: 2 }, |
11 | 9 | }) |
12 | 10 |
|
13 | 11 | export async function syncGithubIntegration(args: ISyncGithubIntegrationArguments): Promise<void> { |
14 | | - const limit = await activity.numberOfGithubConnectionsToCreate() |
15 | | - let created = 0 |
16 | | - |
17 | | - for (const integrationId of args.integrationIds) { |
18 | | - const result = await activity.analyzeGithubIntegration(integrationId) |
| 12 | + const integrationId = args.integrationId |
19 | 13 |
|
20 | | - // delete connections that are no longer needed |
21 | | - for (const repo of result.reposToDelete) { |
22 | | - // delete nango connection |
23 | | - await activity.deleteConnection(integrationId, result.providerConfigKey, repo.connectionId) |
| 14 | + const result = await activity.analyzeGithubIntegration(integrationId) |
24 | 15 |
|
25 | | - // delete connection from integrations.settings.nangoMapping object |
26 | | - await activity.removeGithubConnection(integrationId, repo.connectionId) |
| 16 | + // delete connections that are no longer needed |
| 17 | + for (const repo of result.reposToDelete) { |
| 18 | + // delete nango connection |
| 19 | + await activity.deleteConnection(integrationId, result.providerConfigKey, repo.connectionId) |
27 | 20 |
|
28 | | - // delete githubRepos mapping |
29 | | - await activity.unmapGithubRepo(integrationId, repo.repo) |
30 | | - } |
| 21 | + // delete connection from integrations.settings.nangoMapping object |
| 22 | + await activity.removeGithubConnection(integrationId, repo.connectionId) |
31 | 23 |
|
32 | | - // delete duplicate connections |
33 | | - for (const repo of result.duplicatesToDelete) { |
34 | | - // delete nango connection |
35 | | - await activity.deleteConnection(integrationId, result.providerConfigKey, repo.connectionId) |
| 24 | + // delete githubRepos mapping |
| 25 | + await activity.unmapGithubRepo(integrationId, repo.repo) |
| 26 | + } |
36 | 27 |
|
37 | | - // delete connection from integrations.settings.nangoMapping object |
38 | | - await activity.removeGithubConnection(integrationId, repo.connectionId) |
| 28 | + // delete duplicate connections |
| 29 | + for (const repo of result.duplicatesToDelete) { |
| 30 | + // delete nango connection |
| 31 | + await activity.deleteConnection(integrationId, result.providerConfigKey, repo.connectionId) |
39 | 32 |
|
40 | | - // we don't unmap because this one was duplicated |
41 | | - } |
| 33 | + // delete connection from integrations.settings.nangoMapping object |
| 34 | + await activity.removeGithubConnection(integrationId, repo.connectionId) |
42 | 35 |
|
43 | | - // create connections for repos that are not already connected |
44 | | - for (const repo of result.reposToSync) { |
45 | | - if (created >= limit) { |
46 | | - await activity.logInfo( |
47 | | - `Max number of github connections reached! Skipping repo ${repo.owner}/${repo.repoName} from integration ${integrationId}!`, |
48 | | - ) |
49 | | - continue |
50 | | - } |
| 36 | + // we don't unmap because this one was duplicated |
| 37 | + } |
51 | 38 |
|
52 | | - // create nango connection |
53 | | - const connectionId = await activity.createGithubConnection(integrationId, repo) |
| 39 | + // create connections for repos that are not already connected |
| 40 | + for (const repo of result.reposToSync) { |
| 41 | + const canCreate = await activity.canCreateGithubConnection() |
54 | 42 |
|
55 | | - // add connection to integrations.settings.nangoMapping object |
56 | | - await activity.setGithubConnection(integrationId, repo, connectionId) |
| 43 | + if (!canCreate) { |
| 44 | + await activity.logInfo( |
| 45 | + `Not enough time has passed since last connection! Skipping repo ${repo.owner}/${repo.repoName} from integration ${integrationId}!`, |
| 46 | + ) |
| 47 | + continue |
| 48 | + } |
57 | 49 |
|
58 | | - // add repo to githubRepos mapping if it's not already mapped |
59 | | - await activity.mapGithubRepo(integrationId, repo) |
| 50 | + // create nango connection |
| 51 | + const connectionId = await activity.createGithubConnection(integrationId, repo) |
60 | 52 |
|
61 | | - // add repo to git integration |
62 | | - await activity.updateGitIntegrationWithRepo(integrationId, repo) |
| 53 | + // add connection to integrations.settings.nangoMapping object |
| 54 | + await activity.setGithubConnection(integrationId, repo, connectionId) |
63 | 55 |
|
64 | | - // start nango sync |
65 | | - await activity.startNangoSync(integrationId, result.providerConfigKey, connectionId) |
| 56 | + // add repo to githubRepos mapping if it's not already mapped |
| 57 | + await activity.mapGithubRepo(integrationId, repo) |
66 | 58 |
|
67 | | - // sync repositories to segmentRepositories and insightsProjects after processing all repos |
68 | | - await activity.syncGithubReposToInsights(integrationId) |
| 59 | + // add repo to git integration |
| 60 | + await activity.updateGitIntegrationWithRepo(integrationId, repo) |
69 | 61 |
|
70 | | - created++ |
| 62 | + // start nango sync |
| 63 | + await activity.startNangoSync(integrationId, result.providerConfigKey, connectionId) |
71 | 64 |
|
72 | | - if (created < limit) { |
73 | | - // fixed delay to spread onboarding evenly throughout the day |
74 | | - await sleep(REPO_ONBOARDING_INTERVAL_MINUTES * 60 * 1000) |
75 | | - } |
76 | | - } |
| 65 | + // sync repositories to segmentRepositories and insightsProjects after processing all repos |
| 66 | + await activity.syncGithubReposToInsights(integrationId) |
77 | 67 | } |
78 | 68 | } |
0 commit comments