Skip to content

Commit

Permalink
chore: autocollapse release notes
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobmoellerdev committed Nov 26, 2024
1 parent 1b31de5 commit b8df617
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/config/markdownignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
examples/lib/tour/0*
docs/reference
docs/pluginreference
hack/collapse/*
3 changes: 2 additions & 1 deletion .github/config/wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ bool
boolean
buildx
cas
changelog
chocolateyinstall
cli
cliconfig
Expand Down Expand Up @@ -307,4 +308,4 @@ xml
yaml
yitsushi
yml
yyyy
yyyy
11 changes: 11 additions & 0 deletions .github/workflows/release-drafter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ jobs:
fetch-tags: true
- name: Setup Release with gh
env:
COLLAPSE_THRESHOLD: 5
REF: ${{ github.ref }}
REPO: ${{ github.repository }}
GH_TOKEN: ${{ steps.generate_token.outputs.token }}
Expand Down Expand Up @@ -81,6 +82,16 @@ jobs:
-q .body \
)
if [[ -z $notes ]]; then
echo "No release notes generated from API, failed"
exit 1
fi
echo "Auto-Collapsing release notes to reduce size"
echo $notes > $RUNNER_TEMP/release_notes.md
bash hack/collapse/auto_collapse.sh $RUNNER_TEMP/release_notes.md $RUNNER_TEMP/release_notes_processed.md ${{ env.COLLAPSE_THRESHOLD }}
notes=$(cat $RUNNER_TEMP/release_notes_processed.md)
echo "Release Notes generated for ${{env.RELEASE_VERSION}}:"
echo "${notes}"
Expand Down
1 change: 1 addition & 0 deletions hack/collapse/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test_example_collapsible.md
99 changes: 99 additions & 0 deletions hack/collapse/auto_collapse.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/bin/bash

# Auto Collapse
#This is a script that takes in
# - a markdown file
# - a designated output file
# - the number of lines as a threshold on when to collapse a section.
#
# Sample
# ./auto_collapse.sh test_example.md test_example_collapsible.md 10

# Input and output files
INPUT_FILE=${1:-"README.md"}
OUTPUT_FILE=${2:-"README_collapsible.md"}
THRESHOLD=${3:-10}

# Ensure output file is empty initially
rm -f "$OUTPUT_FILE"

# Variables to track sections
inside_section=false
section_lines=()
section_header=""

INPUT="$(cat "$INPUT_FILE")"
FULL_CHANGELOG=$(grep -E "Full Changelog" < "$INPUT_FILE")

if [[ -z $FULL_CHANGELOG ]]; then
echo "Full Changelog not found in the input file."
else
echo "Full Changelog found in the input file."
INPUT=$(echo "$INPUT" | sed '/Full Changelog/d')
fi

# Function to process and write a section
write_section() {
local header="$1"
local lines=("${@:2}")
local num_lines=$((${#lines[@]}-1))

# Write the section header as is
echo "$header" >> "$OUTPUT_FILE"

if [[ $num_lines -gt $THRESHOLD ]]; then
# Collapse only the content with a dynamic summary
{
echo "<details>"
echo "<summary>${num_lines} changes</summary>"
echo ""
printf "%s\n" "${lines[@]}"
echo "</details>"
} >> "$OUTPUT_FILE"
else
# Write the content as is if it's below the threshold
printf "%s\n" "${lines[@]}" >> "$OUTPUT_FILE"
fi
}

# Read the Markdown file line by line
echo "${INPUT}" | while IFS= read -r line || [[ -n $line ]]; do
# Preserve comment blocks
if [[ $line =~ ^\<!-- ]] || [[ $line =~ ^--\> ]]; then
# Finalize the current section if inside one
if [[ $inside_section == true ]]; then
write_section "$section_header" "${section_lines[@]:1}" # Exclude the header
inside_section=false
fi
# Write the comment directly
echo "$line" >> "$OUTPUT_FILE"
continue
fi

if [[ $line =~ ^#+\ ]]; then # New section starts
if [[ $inside_section == true ]]; then
# Write the previous section
write_section "$section_header" "${section_lines[@]:1}" # Exclude the header
fi
# Start a new section
section_header="$line"
section_lines=("$line") # Initialize section with the header
inside_section=true
else
# Collect lines of the current section
section_lines+=("$line")
fi
done

# Process the last section
if [[ $inside_section == true ]]; then
write_section "$section_header" "${section_lines[@]:1}" # Exclude the header
fi

if [[ ! -z $FULL_CHANGELOG ]]; then
echo "Appending Full Changelog to the end of the file."
printf "\n%s" "$FULL_CHANGELOG" >> "$OUTPUT_FILE"
fi

echo "Collapsible Markdown written to $OUTPUT_FILE"

26 changes: 26 additions & 0 deletions hack/collapse/test_auto_collapse.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

# Paths to files
export INPUT_FILE="$SCRIPT_DIR/test_example.md"
export OUTPUT_FILE="$SCRIPT_DIR/test_example_collapsible.md"
export EXPECTED_FILE="$SCRIPT_DIR/test_example_expected_collapsible.md"
export SCRIPT="$SCRIPT_DIR/auto_collapse.sh"

# Run the script
bash "$SCRIPT" "$INPUT_FILE" "$OUTPUT_FILE" 5

if [ ! -f "$OUTPUT_FILE" ]; then
echo "Test failed: Output file not found."
exit 1
fi

# Compare output with expected file
if diff -q "$OUTPUT_FILE" "$EXPECTED_FILE" > /dev/null; then
echo "Test passed: Output matches expected result."
else
echo "Test failed: Output does not match expected result."
echo "Differences:"
diff "$OUTPUT_FILE" "$EXPECTED_FILE"
fi
16 changes: 16 additions & 0 deletions hack/collapse/test_example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!-- Release notes generated using configuration in .github/config/release.yml at refs/heads/releases/v0.19 -->
<!-- markdown-link-check-disable -->
## What's Changed
### 🚀 Features
* feat(log): log http requests for OCI and docker based on trace level by injecting a logger by @author in https://github.com/open-component-model/ocm/pull/1118
### 🧰 Maintenance
* chore: change guide for 0.18.0 by @author in https://github.com/open-component-model/ocm/pull/1066
* chore: allow publishing to Brew via custom script by @author in https://github.com/open-component-model/ocm/pull/1059
* chore: remove ocm inception during build CTF aggregation by @author in https://github.com/open-component-model/ocm/pull/1065
* chore: release branches as `releases/vX.Y` instead of `releases/vX.Y.Z` by @author in https://github.com/open-component-model/ocm/pull/1071
* chore: cleanup release action by @author in https://github.com/open-component-model/ocm/pull/1076
* chore: bump version to 0.19.0-dev by @author in https://github.com/open-component-model/ocm/pull/1084
* chore: disable mandatory period comments by @author in https://github.com/open-component-model/ocm/pull/1079

**Full Changelog**: https://github.com/open-component-model/ocm/compare/v0.18...v0.19.0
<!-- markdown-link-check-enable -->
22 changes: 22 additions & 0 deletions hack/collapse/test_example_expected_collapsible.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!-- Release notes generated using configuration in .github/config/release.yml at refs/heads/releases/v0.19 -->
<!-- markdown-link-check-disable -->
## What's Changed

### 🚀 Features
* feat(log): log http requests for OCI and docker based on trace level by injecting a logger by @author in https://github.com/open-component-model/ocm/pull/1118
### 🧰 Maintenance
<details>
<summary>7 changes</summary>

* chore: change guide for 0.18.0 by @author in https://github.com/open-component-model/ocm/pull/1066
* chore: allow publishing to Brew via custom script by @author in https://github.com/open-component-model/ocm/pull/1059
* chore: remove ocm inception during build CTF aggregation by @author in https://github.com/open-component-model/ocm/pull/1065
* chore: release branches as `releases/vX.Y` instead of `releases/vX.Y.Z` by @author in https://github.com/open-component-model/ocm/pull/1071
* chore: cleanup release action by @author in https://github.com/open-component-model/ocm/pull/1076
* chore: bump version to 0.19.0-dev by @author in https://github.com/open-component-model/ocm/pull/1084
* chore: disable mandatory period comments by @author in https://github.com/open-component-model/ocm/pull/1079

</details>
<!-- markdown-link-check-enable -->

**Full Changelog**: https://github.com/open-component-model/ocm/compare/v0.18...v0.19.0

0 comments on commit b8df617

Please sign in to comment.