From 4a47580ea8480beb8d4870a0a08bee5261c454c2 Mon Sep 17 00:00:00 2001 From: Micheal Wilkinson Date: Sat, 21 Mar 2026 14:17:07 +0000 Subject: [PATCH] fix: extract decorate-pr comment rendering from action yaml --- decorate-pr/action.yml | 126 +---------------------------------- decorate-pr/build-comment.sh | 71 ++++++++++++++++++++ 2 files changed, 73 insertions(+), 124 deletions(-) create mode 100644 decorate-pr/build-comment.sh diff --git a/decorate-pr/action.yml b/decorate-pr/action.yml index b8253c0..4759f04 100644 --- a/decorate-pr/action.yml +++ b/decorate-pr/action.yml @@ -328,37 +328,6 @@ runs: echo "$failure_reason" >&2 exit 1 fi - - name: Extract changelog unreleased entries - id: extract-changelog - shell: bash - env: - CHANGELOG: ${{ inputs.changelog }} - run: | - set -euo pipefail - - if [[ ! -f "$CHANGELOG" ]]; then - printf 'unreleased_entries=%s\n' "" >> "$GITHUB_OUTPUT" - exit 0 - fi - - # Extract everything between [Unreleased] header and the next [X.Y.Z] header - unreleased="$(awk ' - /^## \[Unreleased\]/ { in_unreleased=1; next } - /^## \[[0-9]+\.[0-9]+\.[0-9]+\]/ { if (in_unreleased) exit } - in_unreleased && NF { print } - ' "$CHANGELOG")" - - # Use a temporary file to handle multiline content - tmp_file=$(mktemp) - printf '%s' "$unreleased" > "$tmp_file" - - # Read it back and set as output - delimiter="EOF_CHANGELOG" - printf '%s<<%s\n' "unreleased_entries<<$delimiter" "$delimiter" >> "$GITHUB_OUTPUT" - cat "$tmp_file" >> "$GITHUB_OUTPUT" - printf '%s\n' "$delimiter" >> "$GITHUB_OUTPUT" - - rm -f "$tmp_file" - name: Build PR comment markdown id: build-comment @@ -373,102 +342,11 @@ runs: GATE_MODE: ${{ inputs.changelog-gate-mode }} DOCS_ONLY: ${{ steps.changelog-gate.outputs.docs_only }} ADDITIONS_COUNT: ${{ steps.changelog-gate.outputs.unreleased_additions_count }} - FAILURE_REASON: ${{ steps.changelog-gate.outputs.gate_failure_reason }} + FAILURE_REASON: ${{ steps.changelog-gate.outputs.failure_reason }} run: | set -euo pipefail - # Start building the comment - tmp_file=$(mktemp) - - # Add title and coverage section - cat > "$tmp_file" << 'EOF' - -EOF - - printf '## %s\n\n' "$COMMENT_TITLE" >> "$tmp_file" - - # Coverage badge section - cat >> "$tmp_file" << EOF -### Coverage -![Coverage Badge]($BADGE_URL) - -**Coverage:** $COVERAGE_PCT% - -EOF - - # Changelog gate section (if enabled) - if [[ "$GATE_ENABLED" == "true" ]]; then - gate_status="✓ Pass" - if [[ "$GATE_PASSED" != "true" ]]; then - if [[ "$GATE_MODE" == "strict" ]]; then - gate_status="✗ Fail" - else - gate_status="⚠ Warning" - fi - fi - - cat >> "$tmp_file" << EOF - -### Changelog Gate -**Status:** $gate_status - -EOF - - if [[ "$DOCS_ONLY" == "true" ]]; then - cat >> "$tmp_file" << 'EOF' -This PR only modifies documentation—changelog entry not required. - -EOF - elif [[ "$GATE_PASSED" == "true" ]]; then - cat >> "$tmp_file" << EOF -Found $ADDITIONS_COUNT line(s) added to Unreleased section ✓ - -EOF - else - cat >> "$tmp_file" << EOF -**Issue:** $FAILURE_REASON - -**How to fix:** Add an entry under the appropriate subsection in the \`## [Unreleased]\` section of \`CHANGELOG.md\`. Use one of: -- \`### Breaking\` for backwards-incompatible changes -- \`### Added\` for new features -- \`### Changed\` for behavior changes -- \`### Removed\` for deprecated removals -- \`### Fixed\` for bug fixes - -Example: -\`\`\`markdown -## [Unreleased] -### Added -- New changelog gate validation for PR decoration. -\`\`\` - -EOF - fi - fi - - # Changelog section - if [[ -n "$UNRELEASED" ]]; then - cat >> "$tmp_file" << 'EOF' -### Unreleased Changes - -EOF - printf '%s\n' "$UNRELEASED" >> "$tmp_file" - printf '\n' >> "$tmp_file" - fi - - # Add footer - cat >> "$tmp_file" << 'EOF' ---- -*This comment was automatically generated by [vociferate/decorate-pr](https://git.hrafn.xyz/aether/vociferate).* -EOF - - # Store as output using delimiter - delimiter="EOF_COMMENT" - printf '%s<<%s\n' "comment_body<<$delimiter" "$delimiter" >> "$GITHUB_OUTPUT" - cat "$tmp_file" >> "$GITHUB_OUTPUT" - printf '%s\n' "$delimiter" >> "$GITHUB_OUTPUT" - - rm -f "$tmp_file" + bash "$GITHUB_ACTION_PATH/build-comment.sh" - name: Find and update/post PR comment id: post-comment diff --git a/decorate-pr/build-comment.sh b/decorate-pr/build-comment.sh new file mode 100644 index 0000000..6253842 --- /dev/null +++ b/decorate-pr/build-comment.sh @@ -0,0 +1,71 @@ +#!/usr/bin/env bash + +set -euo pipefail + +tmp_file=$(mktemp) +trap 'rm -f "$tmp_file"' EXIT + +{ + printf '%s\n' '' + printf '\n## %s\n\n' "$COMMENT_TITLE" + printf '### Coverage\n' + printf '![Coverage Badge](%s)\n\n' "$BADGE_URL" + printf '**Coverage:** %s%%\n' "$COVERAGE_PCT" +} > "$tmp_file" + +if [[ "$GATE_ENABLED" == "true" ]]; then + gate_status="Pass" + if [[ "$GATE_PASSED" != "true" ]]; then + if [[ "$GATE_MODE" == "strict" ]]; then + gate_status="Fail" + else + gate_status="Warning" + fi + fi + + { + printf '\n### Changelog Gate\n' + printf '**Status:** %s\n\n' "$gate_status" + } >> "$tmp_file" + + if [[ "$DOCS_ONLY" == "true" ]]; then + printf '%s\n\n' 'This PR only modifies documentation; changelog entry not required.' >> "$tmp_file" + elif [[ "$GATE_PASSED" == "true" ]]; then + printf 'Found %s line(s) added to Unreleased section.\n\n' "$ADDITIONS_COUNT" >> "$tmp_file" + else + printf '**Issue:** %s\n\n' "$FAILURE_REASON" >> "$tmp_file" + cat >> "$tmp_file" <<'EOF' +**How to fix:** Add an entry under the appropriate subsection in the `## [Unreleased]` section of `CHANGELOG.md`. Use one of: +- `### Breaking` for backwards-incompatible changes +- `### Added` for new features +- `### Changed` for behavior changes +- `### Removed` for deprecated removals +- `### Fixed` for bug fixes + +Example: +```markdown +## [Unreleased] +### Added +- New changelog gate validation for PR decoration. +``` + +EOF + fi +fi + +if [[ -n "$UNRELEASED" ]]; then + { + printf '### Unreleased Changes\n\n' + printf '%s\n\n' "$UNRELEASED" + } >> "$tmp_file" +fi + +cat >> "$tmp_file" <<'EOF' +--- +*This comment was automatically generated by [vociferate/decorate-pr](https://git.hrafn.xyz/aether/vociferate).* +EOF + +delimiter="EOF_COMMENT" +printf 'comment_body<<%s\n' "$delimiter" >> "$GITHUB_OUTPUT" +cat "$tmp_file" >> "$GITHUB_OUTPUT" +printf '%s\n' "$delimiter" >> "$GITHUB_OUTPUT" \ No newline at end of file