-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prioritise TLDR part over the tree diff #156
Open
wzieba
wants to merge
16
commits into
trunk
Choose a base branch
from
improve_dependency_diff_plugin
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
669bf8f
Prioritise TLDR part over the tree diff
wzieba f97a4b7
add debug information
wzieba bd65354
Add more debug logs
wzieba c174d5b
invert the would_content_fit condition
wzieba 48220b4
Add empty lines at the end of templates to make 'tree too large' link…
wzieba ec017ad
extract comment crafting to separate script
autobott 4b768a1
Add necessary bash declarations
autobott 7e1023c
Export variables shared in another script
autobott 6ea0801
Export variables shared in another script
wzieba 47e9e93
move COMMENT_FILE to comment_with_dependency_diff
wzieba 18bfbf9
add test for crafting the dependency diff comment
wzieba 83ed3c8
apply lint improvements
wzieba ecd8393
Simplify printfs and formatting of templates. Ignore `SC2059`
wzieba 0d519df
Remove debug logs
wzieba 2175c0f
Add comment to craft_comment_with_dependency_diff
wzieba 3c07005
Refactor templating to use methods. Addresses SC2059
AliSoftware File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#!/bin/bash | ||
# shellcheck disable=SC2059 | ||
|
||
# This script is used to craft a comment with a dependency diff and a build environment diff. | ||
# It's extracted from comment_with_dependency_diff to be tested in isolation. | ||
|
||
set -euo pipefail | ||
|
||
true > "$COMMENT_FILE" # Clear the file | ||
MAX_COMMENT_SIZE=65400 | ||
current_size=0 | ||
|
||
# Templates for content formatting | ||
SECTION_HEADER="## %s changes\n\n" | ||
TLDR_TEMPLATE="<details><summary>list</summary> | ||
|
||
\`\`\` | ||
%s | ||
\`\`\` | ||
|
||
</details> | ||
" | ||
TREE_TEMPLATE="<details><summary>tree</summary> | ||
|
||
\`\`\`diff | ||
%s | ||
\`\`\` | ||
|
||
</details>" | ||
TREE_TOO_LARGE="\n ⚠️ %s tree is too large. [View it in Buildkite artifacts](%s#%s)\n" | ||
|
||
# Calculate total size needed for TLDRs | ||
# This assumes that TLDRs are never too large to fit in the comment | ||
tldr_size=0 | ||
for section in "Project dependencies" "Build environment"; do | ||
tldr_file="$TLDR_DIFF_DEPENDENCIES_FILE" | ||
[[ $section == "Build environment" ]] && tldr_file="$TLDR_DIFF_BUILD_ENV_FILE" | ||
|
||
if [ -s "$tldr_file" ]; then | ||
content=$(printf "$SECTION_HEADER$TLDR_TEMPLATE" "$section" "$(cat "$tldr_file")") | ||
tldr_size=$(( tldr_size + ${#content} )) | ||
fi | ||
done | ||
|
||
# Function to check if content would fit | ||
would_content_fit() { | ||
if (( current_size + $1 < MAX_COMMENT_SIZE )); then | ||
return 0 # Success/true - content will fit | ||
else | ||
return 1 # Failure/false - content won't fit | ||
fi | ||
} | ||
|
||
# Print content in order | ||
for section in "Project dependencies" "Build environment"; do | ||
tldr_file="$TLDR_DIFF_DEPENDENCIES_FILE" | ||
tree_file="$DIFF_DEPENDENCIES_FILE" | ||
[[ $section == "Build environment" ]] && tldr_file="$TLDR_DIFF_BUILD_ENV_FILE" && tree_file="$DIFF_BUILD_ENV_FILE" | ||
|
||
if [ -s "$tldr_file" ]; then | ||
# Print section header and TLDR | ||
printf "$SECTION_HEADER" "$section" >> "$COMMENT_FILE" | ||
printf "$TLDR_TEMPLATE" "$(cat "$tldr_file")" >> "$COMMENT_FILE" | ||
current_size=$(wc -c < "$COMMENT_FILE") | ||
|
||
# Try to add tree if it exists | ||
if [ -s "$tree_file" ]; then | ||
tree_content=$(cat "$tree_file") | ||
content=$(printf "$TREE_TEMPLATE" "$tree_content") | ||
if would_content_fit ${#content}; then | ||
echo -e "$content" >> "$COMMENT_FILE" | ||
current_size=$(wc -c < "$COMMENT_FILE") | ||
else | ||
printf "$TREE_TOO_LARGE" "$section" "$BUILDKITE_BUILD_URL" "$BUILDKITE_JOB_ID" >> "$COMMENT_FILE" | ||
fi | ||
fi | ||
fi | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#!/usr/bin/env bats | ||
|
||
setup() { | ||
wzieba marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Get the directory of the test file | ||
DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )" | ||
|
||
# Create a temporary directory for test artifacts | ||
export TEMP_DIR="$(mktemp -d)" | ||
export DIFF_DEPENDENCIES_FOLDER="$TEMP_DIR" | ||
export COMMENT_FILE="$DIFF_DEPENDENCIES_FOLDER/comment_body.txt" | ||
export TLDR_DIFF_DEPENDENCIES_FILE="$TEMP_DIR/tldr_dependencies.txt" | ||
export TLDR_DIFF_BUILD_ENV_FILE="$TEMP_DIR/tldr_build_env.txt" | ||
export DIFF_DEPENDENCIES_FILE="$TEMP_DIR/dependencies.txt" | ||
export DIFF_BUILD_ENV_FILE="$TEMP_DIR/build_env.txt" | ||
|
||
# Mock Buildkite environment variables | ||
export BUILDKITE_BUILD_URL="https://buildkite.com/test/build" | ||
export BUILDKITE_JOB_ID="job123" | ||
} | ||
|
||
teardown() { | ||
rm -rf "$TEMP_DIR" | ||
} | ||
|
||
@test "should create comment with both TLDRs and trees when content is small" { | ||
# Create test dependency files with small content | ||
echo "org.test:library:2.0.0 (from 1.0.0)" > "$TLDR_DIFF_DEPENDENCIES_FILE" | ||
echo "org.test:build-tool:3.0.0 (from 2.0.0)" > "$TLDR_DIFF_BUILD_ENV_FILE" | ||
|
||
echo "+org.test:library:2.0.0 | ||
-org.test:library:1.0.0" > "$DIFF_DEPENDENCIES_FILE" | ||
|
||
echo "+org.test:build-tool:3.0.0 | ||
-org.test:build-tool:2.0.0" > "$DIFF_BUILD_ENV_FILE" | ||
|
||
# Run the script | ||
run "$DIR/../bin/craft_comment_with_dependency_diff" | ||
|
||
# Assert success | ||
[ "$status" -eq 0 ] | ||
|
||
# Read generated comment | ||
local comment_content=$(<"$COMMENT_FILE") | ||
|
||
# Assert both sections are present | ||
[[ "$comment_content" =~ "## Project dependencies changes" ]] | ||
[[ "$comment_content" =~ "## Build environment changes" ]] | ||
|
||
# Assert both TLDRs are present | ||
[[ "$comment_content" =~ "org.test:library:2.0.0 (from 1.0.0)" ]] | ||
[[ "$comment_content" =~ "org.test:build-tool:3.0.0 (from 2.0.0)" ]] | ||
|
||
# Assert both trees are present | ||
[[ "$comment_content" =~ "+org.test:library:2.0.0" ]] | ||
[[ "$comment_content" =~ "+org.test:build-tool:3.0.0" ]] | ||
|
||
# Assert no "too large" warnings | ||
[[ ! "$comment_content" =~ "tree is too large" ]] | ||
} | ||
|
||
@test "should handle mixed tree sizes with large project dependencies" { | ||
# Create large project dependencies tree | ||
echo "org.test:library:2.0.0 (from 1.0.0)" > "$TLDR_DIFF_DEPENDENCIES_FILE" | ||
|
||
# Generate a large tree for project dependencies | ||
local large_tree="" | ||
for i in {1..1100}; do | ||
large_tree+="+org.test:large-lib-${i}:2.0.0 | ||
-org.test:large-lib-${i}:1.0.0 | ||
" | ||
done | ||
echo "$large_tree" > "$DIFF_DEPENDENCIES_FILE" | ||
|
||
# Create small build environment changes | ||
echo "org.test:build-tool:3.0.0 (from 2.0.0)" > "$TLDR_DIFF_BUILD_ENV_FILE" | ||
echo "+org.test:build-tool:3.0.0 | ||
-org.test:build-tool:2.0.0" > "$DIFF_BUILD_ENV_FILE" | ||
|
||
# Run the script | ||
run "$DIR/../bin/craft_comment_with_dependency_diff" | ||
|
||
# Assert success | ||
[ "$status" -eq 0 ] | ||
|
||
# Read generated comment | ||
local comment_content=$(<"$COMMENT_FILE") | ||
|
||
echo "$comment_content"; | ||
|
||
# Assert both sections are present | ||
[[ "$comment_content" =~ "## Project dependencies changes" ]] | ||
[[ "$comment_content" =~ "## Build environment changes" ]] | ||
|
||
# Assert both TLDRs are present | ||
[[ "$comment_content" =~ "org.test:library:2.0.0 (from 1.0.0)" ]] | ||
[[ "$comment_content" =~ "org.test:build-tool:3.0.0 (from 2.0.0)" ]] | ||
|
||
# Assert small build environment tree is present | ||
[[ "$comment_content" =~ "+org.test:build-tool:3.0.0" ]] | ||
[[ "$comment_content" =~ "-org.test:build-tool:2.0.0" ]] | ||
|
||
# Assert large project dependencies tree is replaced with warning | ||
[[ "$comment_content" =~ "Project dependencies tree is too large" ]] | ||
[[ "$comment_content" =~ "View it in Buildkite artifacts" ]] | ||
|
||
# Assert only one "too large" warning | ||
[[ ! "$comment_content" =~ "Build environment tree is too large" ]] | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know how to address this 😖
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I found a solution for that: create helper functions that would do each of the
printf
calls, that way you can get rid of your template env vars and instead pass the template strings hardcoded directly in those helper functions, thus making shellcheck stop complaining.Here's a diff I tried for that that seems to work, feel free to can apply it to your PR if you like it:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is smart, thank you a lot 🙏