Skip to content

Commit

Permalink
Simplify new entry checking
Browse files Browse the repository at this point in the history
  • Loading branch information
chad1008 committed Apr 18, 2023
1 parent 71d3635 commit 6059461
Showing 1 changed file with 17 additions and 44 deletions.
61 changes: 17 additions & 44 deletions .github/workflows/check-components-changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,51 +41,24 @@ jobs:
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
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}))"
# 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,")
unreleased_section=$(sed -n '/^## Unreleased$/,/^## /p' "${changelog_path}")
# 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"
# Confirm that the CHANGELOG has an entry for the current PR
if ! grep -nq -e "${pr_link_grep_pattern}" "${changelog_path}"; then
echo "Please make sure your CHANGELOG entry has a link to the current PR."
echo
echo "${optional_check_notice}"
exit 1
fi
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
# Confirm that there is an 'Unreleased' section and that the relevant entry is in that section
if ! grep -nq -e '^## Unreleased' "${changelog_path}" || \
! [[ $unreleased_section = *${pr_link_pattern}* ]]; then
echo "Please make sure your CHANGELOG entry is in the \`## Unreleased\` section"
echo
echo "${optional_check_notice}"
exit 1
fi

0 comments on commit 6059461

Please sign in to comment.