Skip to content

Commit

Permalink
Add more detailed CHANGELOG status logic
Browse files Browse the repository at this point in the history
use dynamic PR number from Github context

reorder conditionals

fix wrong-section logic
  • Loading branch information
chad1008 committed Apr 13, 2023
1 parent ecb650f commit 87929dc
Showing 1 changed file with 56 additions and 2 deletions.
58 changes: 56 additions & 2 deletions .github/workflows/check-components-changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,66 @@ jobs:
fetch-depth: ${{ env.PR_COMMIT_COUNT }}
- name: 'Fetch relevant history from origin'
run: git fetch origin ${{ github.event.pull_request.base.ref }}
- name: Run git diff
- name: Check CHANGELOG status
env:
PR_NUMBER: ${{ github.event.number }}
run: |
changelog_path="packages/components/CHANGELOG.md"
optional_check_notice="This isn't a required check, so if you think your changes are small enough that they don't warrant a CHANGELOG entry, please go ahead and merge without one."
# Fail if the PR doesn't touch the changelog
if git diff --quiet ${{ github.event.pull_request.base.sha }} HEAD -- "$changelog_path"; then
echo "Please add a CHANGELOG entry to $changelog_path"
echo
echo "This isn't a required check, so if you think your changes are small enough that they don't warrant a CHANGELOG entry, please go ahead and merge without one."
echo "${optional_check_notice}"
exit 1
fi
changed_hunks=()
while IFS= read -r range; do
changed_hunks+=("${range}")
done < <( \
# Get CHANGELOG file changes
git diff --unified=0 ${{ github.event.pull_request.base.sha }} HEAD "${changelog_path}" | \
# Remove unwanted headers, meta, and specific change data
grep -v -e '^[+-]' -e '^index' -e '^diff' | \
# sed breakdown (statements are separated by `;`)
# 1. Remove the @@ delimiters from all hunks of changes
# 2. Remove the ` -` from the start of each line
# 3. Remove the number of lines shown in each hunk
# 4. Replace the ` +` with a `-` to show the range of lines changed
sed 's/.*@@\(.*\)@@.*/\1/g; s/^ -//g; s/,[0-9]*//g; s/\(^[0-9]*\) +/\1-/g;' \
)
# Convert the ranges of changed lines to an array of individual line numbers
changed_lines=()
for range in "${changed_hunks[@]}"; do
for (( i=${range%-*}; i<=${range#*-}; i++ )); do changed_lines+=("$i"); done
done
# Find important header line numbers
unreleased_header_line_no=$(grep -n -e '^## Unreleased' "${changelog_path}" | head -1 | sed -r 's/^([0-9]*):## Unreleased/\1/')
release_header_pattern='## [0-9]*\.[0-9]*\.[0-9]* \([0-9]{4}-[0-9]{2}-[0-9]{2}\)'
latest_release_header_line_no=$(grep -nE -e "^${release_header_pattern}$" "${changelog_path}" | head -1 | sed -r "s/^([0-9]*):${release_header_pattern}/\1/")
# Find the line number of the current PR's entry in the changelog
pr_link_pattern="\(\[#${PR_NUMBER}\]\(https://github\.com/WordPress/gutenberg/pull/${PR_NUMBER}\)\)"
pr_link_grep_pattern="(\[#${PR_NUMBER}\](https://github\.com/WordPress/gutenberg/pull/${PR_NUMBER}))"
current_pr_entry_line_no=$(grep -n -e "${pr_link_grep_pattern}" "${changelog_path}" | sed -r "s,^([0-9]*):-.*${pr_link_pattern}.*,\1,")
# Confirm that there is an 'Unreleased' section and that the relevant entry is in that section
if ! grep -nq -e '^## Unreleased' "${changelog_path}" || \
{ [[ -n "${current_pr_entry_line_no}" ]] && ! [[ \
${current_pr_entry_line_no} -gt ${unreleased_header_line_no} && \
${current_pr_entry_line_no} -lt ${latest_release_header_line_no} \
]]; }; then
echo "Please make sure your CHANGELOG entry is in the \`## Unreleased\` section"
exit 1
fi
# Confirm that there is an entry with the right PR link it's part of the current changes
if ! ( grep -nq -e "${pr_link_grep_pattern}" "${changelog_path}" \
&& echo "${changed_lines[@]}" | grep -qw "$current_pr_entry_line_no" ); then
echo "Please make sure your CHANGELOG entry has a link to the current PR."
echo
echo "${optional_check_notice}"
exit 1
fi

0 comments on commit 87929dc

Please sign in to comment.