|
| 1 | +import { |
| 2 | + CodePipelineClient, |
| 3 | + ListPipelinesCommand, |
| 4 | + ListPipelinesInput, |
| 5 | + PipelineSummary, |
| 6 | +} from '@aws-sdk/client-codepipeline' |
| 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 | +const serviceName = 'Code Pipeline' |
| 17 | +const errorLog = new AwsErrorLog(serviceName) |
| 18 | +const MAX_ITEMS = 500 |
| 19 | + |
| 20 | +export interface RawAwsPipelineSummary extends PipelineSummary { |
| 21 | + region: string |
| 22 | +} |
| 23 | + |
| 24 | +const listPipelines = async ( |
| 25 | + cp: CodePipelineClient |
| 26 | +): Promise<PipelineSummary[]> => |
| 27 | + new Promise(async resolve => { |
| 28 | + const codePipelines: PipelineSummary[] = [] |
| 29 | + |
| 30 | + const input: ListPipelinesInput = { |
| 31 | + maxResults: MAX_ITEMS, |
| 32 | + } |
| 33 | + |
| 34 | + const listAllPipelines = (token?: string): void => { |
| 35 | + if (token) { |
| 36 | + input.nextToken = token |
| 37 | + } |
| 38 | + const command = new ListPipelinesCommand(input) |
| 39 | + cp.send(command) |
| 40 | + .then(data => { |
| 41 | + if (isEmpty(data)) { |
| 42 | + return resolve([]) |
| 43 | + } |
| 44 | + |
| 45 | + const { pipelines = [], nextToken } = data || {} |
| 46 | + |
| 47 | + codePipelines.push(...pipelines) |
| 48 | + |
| 49 | + if (nextToken) { |
| 50 | + logger.debug(lt.foundAnotherThousand) |
| 51 | + listAllPipelines(nextToken) |
| 52 | + } else { |
| 53 | + resolve(codePipelines) |
| 54 | + } |
| 55 | + }) |
| 56 | + .catch(err => { |
| 57 | + errorLog.generateAwsErrorLog({ |
| 58 | + functionName: 'codepipeline:listPipelines', |
| 59 | + err, |
| 60 | + }) |
| 61 | + resolve([]) |
| 62 | + }) |
| 63 | + } |
| 64 | + listAllPipelines() |
| 65 | + }) |
| 66 | + |
| 67 | +export default async ({ |
| 68 | + regions, |
| 69 | + config, |
| 70 | +}: { |
| 71 | + regions: string |
| 72 | + config: Config |
| 73 | +}): Promise<{ [property: string]: RawAwsPipelineSummary[] }> => |
| 74 | + new Promise(async resolve => { |
| 75 | + const { credentials } = config |
| 76 | + const pipelinesData: RawAwsPipelineSummary[] = [] |
| 77 | + |
| 78 | + const regionPromises = regions.split(',').map(region => { |
| 79 | + const cpClient = new CodePipelineClient({ |
| 80 | + credentials, |
| 81 | + region, |
| 82 | + }) |
| 83 | + return new Promise<void>(async resolveRegion => { |
| 84 | + const pipelines = (await listPipelines(cpClient)) || [] |
| 85 | + if (!isEmpty(pipelines)) |
| 86 | + pipelinesData.push(...pipelines.map(val => ({ ...val, region }))) |
| 87 | + resolveRegion() |
| 88 | + }) |
| 89 | + }) |
| 90 | + |
| 91 | + await Promise.all(regionPromises) |
| 92 | + errorLog.reset() |
| 93 | + |
| 94 | + resolve(groupBy(pipelinesData, 'region')) |
| 95 | + }) |
0 commit comments