Skip to content

Commit

Permalink
Android: Add comment with manifest diff script
Browse files Browse the repository at this point in the history
  • Loading branch information
ParaskP7 committed Feb 20, 2025
1 parent 8cf8d18 commit 9d702c6
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions bin/comment_with_manifest_diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!/bin/bash

# This script is used in a Buildkite CI pipeline to analyze and comment on manifest changes in a
# GitHub pull request (PR). It compares the project manifest between the
# trunk version and the PR version to identify changes caused by updates to the project.
#
# Usage:
# ./comment_with_manifest_diff <MODULE>
#
# Parameters:
# <MODULE> : The project module whose manifest will be checked.
#
# Steps:
# 1. It copies the manifest reports for both the trunk and PR versions.
# 2. The script compares these reports to detect any changes in the project's manifest.
# 3. If changes are found, the script creates a comment on the GitHub PR with the results.
# 4. If no changes are detected, the script exits without posting any comment.
#
# Notes:
# - The comment on the PR will include a detailed list of changes.
#
# Example:
# ./comment_with_manifest_diff app
#
# This will analyze the `app` module and post the result to the PR.

set -euo pipefail

MODULE="${1:-}"

ANDROID_MERGED_MANIFEST="${HOME}/.android/merged_manifest/${MODULE}"
TRUNK_MANIFEST_PATH="${ANDROID_MERGED_MANIFEST}/jalapenoDebug/processJalapenoDebugMainManifest/AndroidManifest.xml"
PR_MANIFEST_PATH="${MODULE}/build/intermediates/merged_manifest/jalapenoDebug/processJalapenoDebugMainManifest/AndroidManifest.xml"

if [ -z "$MODULE" ]; then
echo "Not enough arguments provided. Usage: ./comment_with_manifest_diff <MODULE>"
exit 1
fi

if [ "${BUILDKITE_PULL_REQUEST:-false}" == "false" ]; then
echo "This script should only be called on pull requests. Be sure to set \`if: build.pull_request.id != null\` on the step invoking it in your Buildkite pipeline"
exit 2
fi

DIFF_MANIFEST_FOLDER="./build/reports/diff_manifest"
TRUNK_MANIFEST_FILE="$DIFF_MANIFEST_FOLDER/trunk_AndroidManifest.txt"
PR_MANIFEST_FILE="$DIFF_MANIFEST_FOLDER/pr_AndroidManifest.txt"
DIFF_MANIFEST_FILE="$DIFF_MANIFEST_FOLDER/diff_manifest.txt"

mkdir -p "$DIFF_MANIFEST_FOLDER"

# Get trunk and PR manifest
cp "$TRUNK_MANIFEST_PATH" "$TRUNK_MANIFEST_FILE"
cp "$PR_MANIFEST_PATH" "$PR_MANIFEST_FILE"

# Generate diff
echo "--> Generating manifest diff"
diff -u "$TRUNK_MANIFEST_FILE" "$PR_MANIFEST_FILE" > "$MANIFEST_DIFF_FILE" || true

generate_manifest_comment_body() {
cat <<EOF
The following changes in \`$MODULE\` AndroidManifest.xml were detected (configuration ):
\`\`\`diff
$(cat "$DIFF_MANIFEST_FILE")
\`\`\`
EOF
}

if [ -n "$DIFF_MANIFEST_FILE" ]; then
echo "--> Commenting manifest diff to GitHub"

# Create or clear the comment body file
COMMENT_FILE="$DIFF_MANIFEST_FOLDER/comment_body.txt"
true > $COMMENT_FILE # 'true' used to clear the file as a no-op

append_content() {
local header="$1"
local content="$2"
local github_comment_max_size=65400 # GitHub constraint is 65536, but we decrease this value slightly to reserve for hidden comment_on_pr content and line breaks
local warning_message="Content exceeds $github_comment_max_size characters. [Navigate to Buildkite build artifacts](${BUILDKITE_BUILD_URL}#${BUILDKITE_JOB_ID}) to see details."

local current_size
current_size=$(wc -c < "$COMMENT_FILE")

# Calculate if there's enough space for the content and header
if (( current_size + ${#header} + ${#content} > github_comment_max_size - ${#header} - ${#warning_message})); then
printf "\n%s\n%s\n" "$header" "$warning_message" > $COMMENT_FILE
return
fi

printf "\n%s\n" "$header" >> $COMMENT_FILE
printf "%s\n" "$content" >> $COMMENT_FILE
}

if [ -s "$DIFF_MANIFEST_FILE" ]; then
append_content "## Project manifest changes" "$(generate_manifest_comment_body)"
fi

comment_on_pr --id "manifest-diff" --if-exist update "$COMMENT_FILE"
else
comment_on_pr --id "manifest-diff" --if-exist delete
fi

0 comments on commit 9d702c6

Please sign in to comment.