-
Notifications
You must be signed in to change notification settings - Fork 732
Expand file tree
/
Copy pathrefreshGithubRepoSettings.ts
More file actions
68 lines (53 loc) · 2.25 KB
/
refreshGithubRepoSettings.ts
File metadata and controls
68 lines (53 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* eslint-disable no-continue */
import cronGenerator from 'cron-time-generator'
import { IS_DEV_ENV, timeout } from '@crowd/common'
import { getServiceChildLogger } from '@crowd/logging'
import SequelizeRepository from '../../database/repositories/sequelizeRepository'
import IntegrationService from '../../services/integrationService'
import { CrowdJob } from '../../types/jobTypes'
const log = getServiceChildLogger('refreshGithubRepoSettings')
const refreshForGitHub = async () => {
log.info('Updating Github repo settings.')
const dbOptions = await SequelizeRepository.getDefaultIRepositoryOptions()
interface Integration {
id: string
tenantId: string
integrationIdentifier: string
}
const githubIntegrations = await dbOptions.database.sequelize.query(
`SELECT id, "tenantId", "integrationIdentifier" FROM integrations
WHERE platform = 'github' AND "deletedAt" IS NULL
AND "createdAt" < NOW() - INTERVAL '1 minute' AND "integrationIdentifier" IS NOT NULL`,
)
for (const integration of githubIntegrations[0] as Integration[]) {
log.info(`Updating repo settings for Github integration: ${integration.id}`)
try {
const options = await SequelizeRepository.getDefaultIRepositoryOptions()
options.currentTenant = { id: integration.tenantId }
const integrationService = new IntegrationService(options)
// newly discovered repos will be mapped to default segment of the integration
await integrationService.updateGithubIntegrationSettings(integration.integrationIdentifier)
log.info(`Successfully updated repo settings for Github integration: ${integration.id}`)
} catch (err) {
log.error(
`Error updating repo settings for Github integration ${integration.id}: ${err.message}`,
)
} finally {
await timeout(1000)
}
}
log.info('Finished updating Github repo settings.')
}
export const refreshGithubRepoSettings = async () => {
log.info('Updating Github repo settings.')
await refreshForGitHub()
}
const job: CrowdJob = {
name: 'Refresh Github repo settings',
// every day
cronTime: IS_DEV_ENV ? cronGenerator.every(5).minutes() : cronGenerator.every(1).days(),
onTrigger: async () => {
await refreshGithubRepoSettings()
},
}
export default job