From 9d702c6de28251fca36656d9f3235a550c643135 Mon Sep 17 00:00:00 2001 From: Petros Paraskevopoulos Date: Thu, 20 Feb 2025 16:08:27 +0200 Subject: [PATCH] Android: Add comment with manifest diff script --- bin/comment_with_manifest_diff | 105 +++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100755 bin/comment_with_manifest_diff diff --git a/bin/comment_with_manifest_diff b/bin/comment_with_manifest_diff new file mode 100755 index 00000000..dcb236cb --- /dev/null +++ b/bin/comment_with_manifest_diff @@ -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 +# +# Parameters: +# : 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 " + 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 < 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