|
| 1 | +import { |
| 2 | + CodeCommitClient, |
| 3 | + ListRepositoriesCommand, |
| 4 | + ListRepositoriesInput, |
| 5 | + RepositoryNameIdPair, |
| 6 | +} from '@aws-sdk/client-codecommit' |
| 7 | +import CloudGraph from '@cloudgraph/sdk' |
| 8 | +import { Config } from 'aws-sdk' |
| 9 | +import { groupBy } from 'lodash' |
| 10 | +import isEmpty from 'lodash/isEmpty' |
| 11 | +import awsLoggerText from '../../properties/logger' |
| 12 | +import AwsErrorLog from '../../utils/errorLog' |
| 13 | + |
| 14 | +const lt = { ...awsLoggerText } |
| 15 | +const { logger } = CloudGraph |
| 16 | + |
| 17 | +const serviceName = 'Code Commit Repository' |
| 18 | +const errorLog = new AwsErrorLog(serviceName) |
| 19 | + |
| 20 | +export interface RawAwsRepository extends RepositoryNameIdPair { |
| 21 | + region: string |
| 22 | +} |
| 23 | + |
| 24 | +const listRepositories = async ( |
| 25 | + cc: CodeCommitClient |
| 26 | +): Promise<RepositoryNameIdPair[]> => |
| 27 | + new Promise(async resolve => { |
| 28 | + const ccRepositories: RepositoryNameIdPair[] = [] |
| 29 | + |
| 30 | + const input: ListRepositoriesInput = {} |
| 31 | + |
| 32 | + const listAllRepositories = (token?: string): void => { |
| 33 | + if (token) { |
| 34 | + input.nextToken = token |
| 35 | + } |
| 36 | + const command = new ListRepositoriesCommand(input) |
| 37 | + cc.send(command) |
| 38 | + .then(data => { |
| 39 | + if (isEmpty(data)) { |
| 40 | + return resolve([]) |
| 41 | + } |
| 42 | + |
| 43 | + const { repositories = [], nextToken } = data || {} |
| 44 | + |
| 45 | + ccRepositories.push(...repositories) |
| 46 | + |
| 47 | + if (nextToken) { |
| 48 | + logger.debug(lt.foundAnotherThousand) |
| 49 | + listAllRepositories(nextToken) |
| 50 | + } else { |
| 51 | + resolve(ccRepositories) |
| 52 | + } |
| 53 | + }) |
| 54 | + .catch(err => { |
| 55 | + errorLog.generateAwsErrorLog({ |
| 56 | + functionName: 'codecommit:listRepositories', |
| 57 | + err, |
| 58 | + }) |
| 59 | + resolve([]) |
| 60 | + }) |
| 61 | + } |
| 62 | + listAllRepositories() |
| 63 | + }) |
| 64 | + |
| 65 | +export default async ({ |
| 66 | + regions, |
| 67 | + config, |
| 68 | +}: { |
| 69 | + regions: string |
| 70 | + config: Config |
| 71 | +}): Promise<{ [property: string]: RawAwsRepository[] }> => |
| 72 | + new Promise(async resolve => { |
| 73 | + const { credentials } = config |
| 74 | + const repositoriesData: RawAwsRepository[] = [] |
| 75 | + |
| 76 | + const regionPromises = regions.split(',').map(region => { |
| 77 | + const cc = new CodeCommitClient({ |
| 78 | + credentials, |
| 79 | + region, |
| 80 | + }) |
| 81 | + return new Promise<void>(async resolveRegion => { |
| 82 | + const repos = (await listRepositories(cc)) || [] |
| 83 | + if (!isEmpty(repos)) |
| 84 | + repositoriesData.push(...repos.map(val => ({ ...val, region }))) |
| 85 | + resolveRegion() |
| 86 | + }) |
| 87 | + }) |
| 88 | + |
| 89 | + await Promise.all(regionPromises) |
| 90 | + errorLog.reset() |
| 91 | + |
| 92 | + resolve(groupBy(repositoriesData, 'region')) |
| 93 | + }) |
0 commit comments