<commit_before>#!/bin/bash
set -e

# First parameter is required and is a boolean indicating if the build was a
# success. Second paremeter is an error message if the build was a failure.
send_sns_callback_message() {
    echo "Sending SNS message to CodeBuild callback topic"

    MSG="$2"

    # Construct the message attributes JSON
    MSGATR="{"

    # Required status code (always comes first, for the comma)
    MSGATR+="\"STATUS\": {\"DataType\": \"String\", \"StringValue\": \"$1\"}"

    # Non-optional values
    MSGATR+=",\"CODEBUILD_BUILD_ARN\": {\"DataType\": \"String\", \"StringValue\": \"$CODEBUILD_BUILD_ARN\"}"
    MSGATR+=",\"PRX_AWS_ACCOUNT_ID\": {\"DataType\": \"String\", \"StringValue\": \"$PRX_AWS_ACCOUNT_ID\"}"
    MSGATR+=",\"PRX_REPO\": {\"DataType\": \"String\", \"StringValue\": \"$PRX_REPO\"}"
    MSGATR+=",\"PRX_COMMIT\": {\"DataType\": \"String\", \"StringValue\": \"$PRX_COMMIT\"}"

    # Optional GitHub pull request number parameter (pass-through)
    [ -z "$PRX_GITHUB_PR" ] || MSGATR+=",\"PRX_GITHUB_PR\": {\"DataType\": \"String\", \"StringValue\": \"$PRX_GITHUB_PR\"}"

    # Optional ECR parameters
    [ -z "$PRX_ECR_REGION" ] || MSGATR+=",\"PRX_ECR_REGION\": {\"DataType\": \"String\", \"StringValue\": \"$PRX_ECR_REGION\"}"
    [ -z "$PRX_ECR_REPOSITORY" ] || MSGATR+=",\"PRX_ECR_REPOSITORY\": {\"DataType\": \"String\", \"StringValue\": \"$PRX_ECR_REPOSITORY\"}"
    [ -z "$PRX_ECR_CONFIG_PARAMETERS" ] || MSGATR+=",\"PRX_ECR_CONFIG_PARAMETERS\": {\"DataType\": \"String\", \"StringValue\": \"$PRX_ECR_CONFIG_PARAMETERS\"}"
    [ -z "$PRX_ECR_IMAGE" ] || MSGATR+=",\"PRX_ECR_IMAGE\": {\"DataType\": \"String\", \"StringValue\": \"$PRX_ECR_IMAGE\"}"
    [ -z "$PRX_ECR_TAG" ] || MSGATR+=",\"PRX_ECR_TAG\": {\"DataType\": \"String\", \"StringValue\": \"$PRX_ECR_TAG\"}"

    # Option Lambda code parameters
    [ -z "$PRX_LAMBDA_CODE_S3_KEY" ] || MSGATR+=",\"PRX_LAMBDA_CODE_S3_KEY\": {\"DataType\": \"String\", \"StringValue\": \"$PRX_LAMBDA_CODE_S3_KEY\"}"
    [ -z "$PRX_LAMBDA_CODE_S3_VERSION_ID" ] || MSGATR+=",\"PRX_LAMBDA_CODE_S3_VERSION_ID\": {\"DataType\": \"String\", \"StringValue\": \"$PRX_LAMBDA_CODE_S3_VERSION_ID\"}"
    [ -z "$PRX_LAMBDA_CODE_CONFIG_PARAMETERS" ] || MSGATR+=",\"PRX_LAMBDA_CODE_CONFIG_PARAMETERS\": {\"DataType\": \"String\", \"StringValue\": \"$PRX_LAMBDA_CODE_CONFIG_PARAMETERS\"}"

    MSGATR+="}"

    echo "$MSGATR"

    OUT=$(aws sns publish --topic-arn "$PRX_SNS_CALLBACK" --message "$MSG" --message-attributes "$MSGATR")

    SNS_CLI_CODE=$?
    if [ $SNS_CLI_CODE -eq 0 ]; then
        echo "Sent SNS message: $MSG"
    else
        echo "Failed to send SNS message: $MSG"
        exit $SNS_CLI_CODE
    fi
}

build_success() {
    echo "Script completed successfully!"
    send_sns_callback_message true "Success"
    exit 0
}

build_error() {
    echo "Script did not complete successfully: $1"
    send_sns_callback_message false "$1"
    exit 1
}

# If PRX_ECR_REPOSITORY is present, we try to push to ECR
push_to_ecr() {
    if [ -n "$PRX_ECR_REPOSITORY" ]
    then
        echo "Found ECR support..."

        if [-z "$PRX_GITHUB_PR"]
        then
            echo "...Skipping ECR push for pull request"
        else
            if [ -z "$PRX_ECR_REGION" ]; then build_error "PRX_ECR_REGION required for ECR push"; fi
            if [ -z "$PRX_ECR_CONFIG_PARAMETERS" ]; then build_error "PRX_ECR_CONFIG_PARAMETERS required for ECR push"; fi
            echo "Handling ECR push..."

            echo "Logging into ECR..."
            $(aws ecr get-login --region $PRX_ECR_REGION)
            echo "...Logged in to ECR"

            echo "Getting Docker image ID"
            IMAGE_ID=$(docker images --filter "label=org.prx.app" --format "{{.ID}}" | head -n 1)

            if [ -z "$IMAGE_ID" ]; then
                build_error "No Docker image found; ensure at least one Dockerfile has an org.prx.app label"
            else
                # Construct the image name with a tag
                TAG="${PRX_COMMIT:0:7}"
                export PRX_ECR_TAG="$TAG"
                TAGGED_IMAGE_NAME="${PRX_AWS_ACCOUNT_ID}.dkr.ecr.${PRX_ECR_REGION}.amazonaws.com/${PRX_ECR_REPOSITORY}:${TAG}"
                export PRX_ECR_IMAGE="$TAGGED_IMAGE_NAME"

                echo "Pushing image $IMAGE_ID to ECR $TAGGED_IMAGE_NAME..."
                docker tag $IMAGE_ID $TAGGED_IMAGE_NAME
                docker push $TAGGED_IMAGE_NAME
            fi
        fi
    fi
}

# If the buildspec provides an S3 object key for Lambda code, the built code
# from the CodeBuild needs to be pushed to that key in the standard Application
# Code bucket provided by the Storage stack.
push_to_s3_lambda() {
    if [ -n "$PRX_LAMBDA_CODE_S3_KEY" ]
    then
        echo "Found Lambda S3 support..."

        if [-z "$PRX_GITHUB_PR"]
        then
            echo "...Skipping ECR push for pull request"
        else
            if [ -z "$PRX_APPLICATION_CODE_BUCKET" ]; then build_error "PRX_APPLICATION_CODE_BUCKET required for Lambda code push"; fi
            if [ -z "$PRX_LAMBDA_CODE_CONFIG_PARAMETERS" ]; then build_error "PRX_LAMBDA_CODE_CONFIG_PARAMETERS required for Lambda code push"; fi
            if [ -z "$PRX_LAMBDA_ARCHIVE_BUILD_PATH" ]; then export PRX_LAMBDA_ARCHIVE_BUILD_PATH="/.prxci/build.zip" ; fi
            echo "Handling Lambda code push..."

            echo "Getting Docker image ID"
            image_id=$(docker images --filter "label=org.prx.lambda" --format "{{.ID}}" | head -n 1)

            if [ -z "$image_id" ]; then
                build_error "No Docker image found; ensure at least one Dockerfile has an org.prx.lambda label"
            else
                container_id=$(docker create $image_id)

                echo "Copying zip archive for Lambda source..."
                docker cp $container_id:$PRX_LAMBDA_ARCHIVE_BUILD_PATH build.zip

                cleaned=`docker rm $container_id`

                echo "Sending zip archive to S3..."
                version_id=`aws s3api put-object --bucket $PRX_APPLICATION_CODE_BUCKET --key $PRX_LAMBDA_CODE_S3_KEY --acl private --body build.zip --output text --query 'VersionId'`

                export PRX_LAMBDA_CODE_S3_VERSION_ID="$version_id"
            fi
        fi
    fi<commit_msg>Fix logging<commit_after>#!/bin/bash
set -e

# First parameter is required and is a boolean indicating if the build was a
# success. Second paremeter is an error message if the build was a failure.
send_sns_callback_message() {
    echo "Sending SNS message to CodeBuild callback topic"

    MSG="$2"

    # Construct the message attributes JSON
    MSGATR="{"

    # Required status code (always comes first, for the comma)
    MSGATR+="\"STATUS\": {\"DataType\": \"String\", \"StringValue\": \"$1\"}"

    # Non-optional values
    MSGATR+=",\"CODEBUILD_BUILD_ARN\": {\"DataType\": \"String\", \"StringValue\": \"$CODEBUILD_BUILD_ARN\"}"
    MSGATR+=",\"PRX_AWS_ACCOUNT_ID\": {\"DataType\": \"String\", \"StringValue\": \"$PRX_AWS_ACCOUNT_ID\"}"
    MSGATR+=",\"PRX_REPO\": {\"DataType\": \"String\", \"StringValue\": \"$PRX_REPO\"}"
    MSGATR+=",\"PRX_COMMIT\": {\"DataType\": \"String\", \"StringValue\": \"$PRX_COMMIT\"}"

    # Optional GitHub pull request number parameter (pass-through)
    [ -z "$PRX_GITHUB_PR" ] || MSGATR+=",\"PRX_GITHUB_PR\": {\"DataType\": \"String\", \"StringValue\": \"$PRX_GITHUB_PR\"}"

    # Optional ECR parameters
    [ -z "$PRX_ECR_REGION" ] || MSGATR+=",\"PRX_ECR_REGION\": {\"DataType\": \"String\", \"StringValue\": \"$PRX_ECR_REGION\"}"
    [ -z "$PRX_ECR_REPOSITORY" ] || MSGATR+=",\"PRX_ECR_REPOSITORY\": {\"DataType\": \"String\", \"StringValue\": \"$PRX_ECR_REPOSITORY\"}"
    [ -z "$PRX_ECR_CONFIG_PARAMETERS" ] || MSGATR+=",\"PRX_ECR_CONFIG_PARAMETERS\": {\"DataType\": \"String\", \"StringValue\": \"$PRX_ECR_CONFIG_PARAMETERS\"}"
    [ -z "$PRX_ECR_IMAGE" ] || MSGATR+=",\"PRX_ECR_IMAGE\": {\"DataType\": \"String\", \"StringValue\": \"$PRX_ECR_IMAGE\"}"
    [ -z "$PRX_ECR_TAG" ] || MSGATR+=",\"PRX_ECR_TAG\": {\"DataType\": \"String\", \"StringValue\": \"$PRX_ECR_TAG\"}"

    # Option Lambda code parameters
    [ -z "$PRX_LAMBDA_CODE_S3_KEY" ] || MSGATR+=",\"PRX_LAMBDA_CODE_S3_KEY\": {\"DataType\": \"String\", \"StringValue\": \"$PRX_LAMBDA_CODE_S3_KEY\"}"
    [ -z "$PRX_LAMBDA_CODE_S3_VERSION_ID" ] || MSGATR+=",\"PRX_LAMBDA_CODE_S3_VERSION_ID\": {\"DataType\": \"String\", \"StringValue\": \"$PRX_LAMBDA_CODE_S3_VERSION_ID\"}"
    [ -z "$PRX_LAMBDA_CODE_CONFIG_PARAMETERS" ] || MSGATR+=",\"PRX_LAMBDA_CODE_CONFIG_PARAMETERS\": {\"DataType\": \"String\", \"StringValue\": \"$PRX_LAMBDA_CODE_CONFIG_PARAMETERS\"}"

    MSGATR+="}"

    echo "$MSGATR"

    OUT=$(aws sns publish --topic-arn "$PRX_SNS_CALLBACK" --message "$MSG" --message-attributes "$MSGATR")

    SNS_CLI_CODE=$?
    if [ $SNS_CLI_CODE -eq 0 ]; then
        echo "Sent SNS message: $MSG"
    else
        echo "Failed to send SNS message: $MSG"
        exit $SNS_CLI_CODE
    fi
}

build_success() {
    echo "Script completed successfully!"
    send_sns_callback_message true "Success"
    exit 0
}

build_error() {
    echo "Script did not complete successfully: $1"
    send_sns_callback_message false "$1"
    exit 1
}

# If PRX_ECR_REPOSITORY is present, we try to push to ECR
push_to_ecr() {
    if [ -n "$PRX_ECR_REPOSITORY" ]
    then
        echo "Found ECR support..."

        if [-z "$PRX_GITHUB_PR"]
        then
            echo "...Skipping ECR push for pull request"
        else
            if [ -z "$PRX_ECR_REGION" ]; then build_error "PRX_ECR_REGION required for ECR push"; fi
            if [ -z "$PRX_ECR_CONFIG_PARAMETERS" ]; then build_error "PRX_ECR_CONFIG_PARAMETERS required for ECR push"; fi
            echo "Handling ECR push..."

            echo "Logging into ECR..."
            $(aws ecr get-login --region $PRX_ECR_REGION)
            echo "...Logged in to ECR"

            echo "Getting Docker image ID"
            IMAGE_ID=$(docker images --filter "label=org.prx.app" --format "{{.ID}}" | head -n 1)

            if [ -z "$IMAGE_ID" ]; then
                build_error "No Docker image found; ensure at least one Dockerfile has an org.prx.app label"
            else
                # Construct the image name with a tag
                TAG="${PRX_COMMIT:0:7}"
                export PRX_ECR_TAG="$TAG"
                TAGGED_IMAGE_NAME="${PRX_AWS_ACCOUNT_ID}.dkr.ecr.${PRX_ECR_REGION}.amazonaws.com/${PRX_ECR_REPOSITORY}:${TAG}"
                export PRX_ECR_IMAGE="$TAGGED_IMAGE_NAME"

                echo "Pushing image $IMAGE_ID to ECR $TAGGED_IMAGE_NAME..."
                docker tag $IMAGE_ID $TAGGED_IMAGE_NAME
                docker push $TAGGED_IMAGE_NAME
            fi
        fi
    fi
}

# If the buildspec provides an S3 object key for Lambda code, the built code
# from the CodeBuild needs to be pushed to that key in the standard Application
# Code bucket provided by the Storage stack.
push_to_s3_lambda() {
    if [ -n "$PRX_LAMBDA_CODE_S3_KEY" ]
    then
        echo "Found Lambda S3 support..."

        if [-z "$PRX_GITHUB_PR"]
        then
            echo "...Skipping Lambda push for pull request"
        else
            if [ -z "$PRX_APPLICATION_CODE_BUCKET" ]; then build_error "PRX_APPLICATION_CODE_BUCKET required for Lambda code push"; fi
            if [ -z "$PRX_LAMBDA_CODE_CONFIG_PARAMETERS" ]; then build_error "PRX_LAMBDA_CODE_CONFIG_PARAMETERS required for Lambda code push"; fi
            if [ -z "$PRX_LAMBDA_ARCHIVE_BUILD_PATH" ]; then export PRX_LAMBDA_ARCHIVE_BUILD_PATH="/.prxci/build.zip" ; fi
            echo "Handling Lambda code push..."

            echo "Getting Docker image ID"
            image_id=$(docker images --filter "label=org.prx.lambda" --format "{{.ID}}" | head -n 1)

            if [ -z "$image_id" ]; then
                build_error "No Docker image found; ensure at least one Dockerfile has an org.prx.lambda label"
            else
                container_id=$(docker create $image_id)

                echo "Copying zip archive for Lambda source..."
                docker cp $container_id:$PRX_LAMBDA_ARCHIVE_BUILD_PATH build.zip

                cleaned=`docker rm $container_id`

                echo "Sending zip archive to S3..."
                version_id=`aws s3api put-object --bucket $PRX_APPLICATION_CODE_BUCKET --key $PRX_LAMBDA_CODE_S3_KEY --acl private --body build.zip --output text --query 'VersionId'`

                export PRX_LAMBDA_CODE_S3_VERSION_ID="$version_id"
            fi
        fi
    fi
{'fraction_non_alphanumeric': 0.14905343877306493, 'fraction_numerical': 0.003993929227574087, 'mean_word_length': 2.71513353115727, 'pattern_counts': {'":': 84, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '1039490', 'n_tokens_mistral': 4402, 'n_tokens_neox': 4341, 'n_words': 1038}