<commit_before>const github = require('./github');

function codebuildUrl(event) {
  const region = event.region;
  const account = event.account;
  const project = event.detail['project-name'];
  const uuid = event.detail['build-id'].split('/')[1].split(':')[1];

  return `https://console.aws.amazon.com/codesuite/codebuild/${account}/projects/${project}/build/${project}%3A${uuid}?region=${region}`;
}

module.exports = {
  async blocks(event) {
    const blox = [];

    const info = event.detail['additional-information'];

    const envVars = info.environment['environment-variables'];
    const exportedEnvVars = info['exported-environment-variables'] || [];

    // Combine all envionment variables into a single key-value object (rather
    // than multiple arrays of objects)
    const allEnvVars = [...envVars, ...exportedEnvVars].reduce(
      (acc, cur) => ({ ...acc, [cur.name]: cur.value }),
      {},
    );

    const ownerAndRepo = allEnvVars.PRX_REPO;
    const sha = allEnvVars.PRX_COMMIT;
    const sha7 = sha.substring(0, 7);
    const branch = allEnvVars.PRX_BRANCH;
    const pr = allEnvVars.PRX_GITHUB_PR;
    const codeBucket = allEnvVars.PRX_APPLICATION_CODE_BUCKET;

    const verb = {
      IN_PROGRESS: 'Building',
      SUCCEEDED: 'Built',
      FAILED: 'Failed to build',
      STOPPED: 'Stopped building',
    };

    // e.g.,
    // Building PRX/Infrastructure main branch with commit be8e83b
    // Built PRX/Infrastructure main branch with commit 674811e
    // Building PRX/Infrastructure #582 with commit 26fad6b
    const line1 = [
      `<${codebuildUrl(event)}|${verb[event.detail['build-status']]}>`,
      ownerAndRepo,
      pr
        ? `<https://github.com/${ownerAndRepo}/pull/${pr}|#${pr}>`
        : `\`${branch}\` branch`,
      `with commit <https://github.com/${ownerAndRepo}/commit/${sha}|${sha7}>`,
    ];

    // TODO
    // For IN PROGRESS pushes, include the pushed commits
    // For PRs include the title and action (ready_for_review, etc)

    let moreLines = [];

    // For builds with ECR artifacts, link to the image in ECR
    if (allEnvVars.PRX_ECR_IMAGE) {
      const imageName = allEnvVars.PRX_ECR_IMAGE;

      const region = imageName.match(/dkr\.ecr\.(.*)\.amazonaws\.com/)[1];
      const accountId = imageName.match(/^([0-9]+)\./)[1];
      const repoName = imageName.match(/\/([^:]+):/)[1];
      const tag = imageName.match(/:([a-f0-9]+)$/)[1].substring(0, 7);

      const ecrUrl = `https://console.aws.amazon.com/ecr/repositories/private/${accountId}/${repoName}?region=${region}`;
      moreLines.push(
        `» Docker image pushed to <${ecrUrl}|ECR> with tag \`${tag}…\``,
      );
    }

    // For builds with Lambda artifacts, link to the S3 object
    if (allEnvVars.PRX_LAMBDA_CODE_CONFIG_VALUE) {
      const objectKey = allEnvVars.PRX_LAMBDA_CODE_CONFIG_VALUE;
<commit_msg>Handle new ECR build variables<commit_after>const github = require('./github');

function codebuildUrl(event) {
  const region = event.region;
  const account = event.account;
  const project = event.detail['project-name'];
  const uuid = event.detail['build-id'].split('/')[1].split(':')[1];

  return `https://console.aws.amazon.com/codesuite/codebuild/${account}/projects/${project}/build/${project}%3A${uuid}?region=${region}`;
}

module.exports = {
  async blocks(event) {
    const blox = [];

    const info = event.detail['additional-information'];

    const envVars = info.environment['environment-variables'];
    const exportedEnvVars = info['exported-environment-variables'] || [];

    // Combine all envionment variables into a single key-value object (rather
    // than multiple arrays of objects)
    const allEnvVars = [...envVars, ...exportedEnvVars].reduce(
      (acc, cur) => ({ ...acc, [cur.name]: cur.value }),
      {},
    );

    const ownerAndRepo = allEnvVars.PRX_REPO;
    const sha = allEnvVars.PRX_COMMIT;
    const sha7 = sha.substring(0, 7);
    const branch = allEnvVars.PRX_BRANCH;
    const pr = allEnvVars.PRX_GITHUB_PR;
    const codeBucket = allEnvVars.PRX_APPLICATION_CODE_BUCKET;

    const verb = {
      IN_PROGRESS: 'Building',
      SUCCEEDED: 'Built',
      FAILED: 'Failed to build',
      STOPPED: 'Stopped building',
    };

    // e.g.,
    // Building PRX/Infrastructure main branch with commit be8e83b
    // Built PRX/Infrastructure main branch with commit 674811e
    // Building PRX/Infrastructure #582 with commit 26fad6b
    const line1 = [
      `<${codebuildUrl(event)}|${verb[event.detail['build-status']]}>`,
      ownerAndRepo,
      pr
        ? `<https://github.com/${ownerAndRepo}/pull/${pr}|#${pr}>`
        : `\`${branch}\` branch`,
      `with commit <https://github.com/${ownerAndRepo}/commit/${sha}|${sha7}>`,
    ];

    // TODO
    // For IN PROGRESS pushes, include the pushed commits
    // For PRs include the title and action (ready_for_review, etc)

    let moreLines = [];

    // For builds with ECR artifacts, link to the images in ECR
    if (allEnvVars.PRX_SPIRE_ECR_PKG_PARAMETERS) {
      // raw will be like:
      // "MY_APP=/ssm/parameter/path", or
      // "MY_APP=/ssm/parameter/path;APP_TWO=/some/path"
      const raw = allEnvVars.PRX_SPIRE_ECR_PKG_PARAMETERS;
      const images = raw.split(';');

      // For each label=parameter(s) entry, get the envar that matches the
      // label, which will be a Docker image+tag
      for (const image of images) {
        // image will be like:
        // "MY_APP=/ssm/parameter/path", or
        // "MY_APP=/ssm/parameter/path,/another/ssm/path"
        const parts = image.split('=');

        if (parts.length === 2) {
          // e.g., "MY_APP"
          const imageEnvarName = parts[0];
          const imageName = allEnvVars[imageEnvarName];

          const region = imageName.match(/dkr\.ecr\.(.*)\.amazonaws\.com/)[1];
          const accountId = imageName.match(/^([0-9]+)\./)[1];
          const repoName = imageName.match(/\/([^:]+):/)[1];
          const tag = imageName.match(/:([a-f0-9]+)$/)[1].substring(0, 7);

          const ecrUrl = `https://console.aws.amazon.com/ecr/repositories/private/${accountId}/${repoName}?region=${region}`;
          moreLines.push(
            `» Docker image pushed to <${ecrUrl}|ECR> with tag \`${tag}…\``,
          );
        }
      }
    }

    // For builds with ECR artifacts, link to the image in ECR
    // TODO This is the legacy way of doing it
    if (allEnvVars.PRX_ECR_IMAGE) {
      const imageName = allEnvVars.PRX_ECR_IMAGE;

      const region = imageName.match(/dkr\.ecr\.(.*)\.amazonaws\.com/)[1];
      const accountId = imageName.match(/^([0-9]+)\./)[1];
      const repoName = imageName.match(/\/([^:]+):/)[1];
      const tag = imageName.match(/:([a-f0-9]+)$/)[1].substring(0, 7);

      const ecrUrl = `https://console.aws.amazon.com/ecr/repositories/private/${accountId}/${repoName}?region=${region}`;
      moreLines.push(
        `» Docker image pushed to <${ecrUrl}|ECR> with tag \`${tag}…\``,
      );
    }

    // For builds with Lambda artifacts, link to the S3 object
    if (allEnvVars.PRX_LAMBDA_CODE_CONFIG_VALUE) {
      const objectKey = allEnvVars.PRX_LAMBDA_CODE_CONFIG_VALUE;

{'fraction_non_alphanumeric': 0.16489955357142858, 'fraction_numerical': 0.011160714285714286, 'mean_word_length': 2.911074740861975, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 14, 'https://': 9, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2543002', 'n_tokens_mistral': 2438, 'n_tokens_neox': 2399, 'n_words': 504}