export async function handler()

in lib/chime-notifier/handler/notifier-handler.ts [31:71]


export async function handler(event: any) {
  // Log the event so we can have a look in CloudWatch logs
  process.stdout.write(`${JSON.stringify(event)}\n`);

  const webhookUrls: string[] = event.webhookUrls || [];
  if (webhookUrls.length === 0) { throw new Error("Expected event field 'webhookUrls'"); }

  const messageTemplate = event.message;
  if (!messageTemplate) { throw new Error("Expected event field 'message'"); }

  const details = event.detail || {};
  const pipelineName = details.pipeline;
  const pipelineExecutionId = details['execution-id'];

  if (!pipelineName || !pipelineExecutionId) {
    process.stderr.write('Malformed event!\n');
    return;
  }

  // Describe the revision that caused the pipeline to fail
  const response = await codePipeline.getPipelineExecution({ pipelineName, pipelineExecutionId }).promise();
  process.stdout.write(`${JSON.stringify(response)}\n`);
  const firstArtifact: AWS.CodePipeline.ArtifactRevision | undefined = (response.pipelineExecution?.artifactRevisions ?? [])[0];
  const revisionSummary = firstArtifact?.revisionSummary ?? firstArtifact?.revisionId ?? `execution ${pipelineExecutionId}`;

  // Find the action that caused the pipeline to fail (no pagination for now)
  const actionResponse = await codePipeline.listActionExecutions({ pipelineName, filter: { pipelineExecutionId } }).promise();
  process.stdout.write(`${JSON.stringify(actionResponse)}\n`);
  const failingActionDetails = actionResponse.actionExecutionDetails?.find(d => d.status === 'Failed');
  const failingAction = failingActionDetails?.actionName || 'UNKNOWN';
  const failureUrl = failingActionDetails?.output?.executionResult?.externalExecutionUrl || '???';

  const message = messageTemplate
    .replace(/\$PIPELINE/g, pipelineName)
    .replace(/\$REVISION/g, revisionSummary)
    .replace(/\$ACTION/g, failingAction)
    .replace(/\$URL/g, failureUrl);

  // Post the failure to all given Chime webhook URLs
  await Promise.all(webhookUrls.map(url => sendChimeNotification(url, message)));
}