-
Notifications
You must be signed in to change notification settings - Fork 33
[Tooling] CHANGELOG and Version Bump PreCommit Hook #397
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
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
99ee1f3
Add pre-commit logic to verify CHANGELOG presence in commits and that…
e5f7fd5
Add github workflow to run pre-commit changelog verification logic on…
2ccbe9a
Add docs and bin directories to paths to ignore for changelog verific…
40f4a73
Add pre-commit git hook activation guide to prepare local environment…
e187abe
Change tabs to spaces everywhere, fix grammar, address PR comments
f3e5dfb
Remove spaces fixing indentation
9bc71a5
Removed unneeded cat call, removed previous_date not used, added quot…
51190ca
Add do not merge label when verification fails
a949a1e
Update guide wording in development documentation
9d46990
Fix if statement
13d541f
Add continue on error to validate job
85dd301
Fix one to on
9db1310
Fix leading underscores
7241bde
Remove adding 'do not merge' label rely only on exit status of script…
11d0cf4
Merge branch 'main' into changelog-verify
1a113d2
Store locations of changelog files to account for different storage l…
8daf8de
Update CHANGELOG.md
ccf72d0
Remove date command variations and compare dates in string format
1f06928
Fix comments
e71df75
Remove root level CHANGELOG.md check, improve comment verbosity
3cf3d7d
Check for root-level CHANGELOG.md file, remove CHANGELOG updates
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 hidden or 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,135 @@ | ||
| #!/usr/bin/env bash | ||
| # | ||
| # Pre-commit hook to check if CHANGELOG.md has been included | ||
| # when the project root or relevant module has been edited. | ||
| # | ||
| # If it has been included, the latest version is compared | ||
| # against the previous version by stripping all leading zeros | ||
| # and comparing them. If it is greater it goes on to check | ||
| # that the latest change's date is the current date (i.e. today). | ||
| # If any of these tests fail the script exits and cancels the | ||
| # commit. | ||
| # | ||
| # To bypass this validation add the `--no-verify` or `-n` flag: | ||
| # `git commit --no-verify ...` | ||
|
|
||
| # Get names of staged files being committed. If being run as | ||
| # a git hook, take the result of git diff. If not, receive | ||
| # the edited files from the CLI arguments provided. | ||
| if [[ "${#}" -eq 0 ]] | ||
| then | ||
| STAGED_FILES=($(git diff --cached --name-only)) # Local git hook | ||
| else | ||
| STAGED_FILES=("${@}") # Github workflow passes arguments | ||
| fi | ||
|
|
||
| if [[ "${#STAGED_FILES[@]}" == 0 ]] | ||
| then | ||
| echo "No files modified" | ||
| exit 0 | ||
| fi | ||
|
|
||
| # Initialise arrays and script wide variables | ||
| IGNORE_DIRS=(".github" ".githooks" "docs" "bin") | ||
| MODULES_EDITED=() # Modules are considered top-level directories directly under root | ||
| MODULES_MISSING_CHANGELOG=() | ||
| CHANGELOG_FILES=() # Full paths to CHANGELOG.md files | ||
| CHANGELOG_ERRORS_FOUND=0 | ||
|
|
||
| # Loop through staged files | ||
| for file in "${STAGED_FILES[@]}" | ||
| do | ||
| # Check the files contained in modules/top-level subdirs only | ||
| if [[ "${file}" =~ /+ ]] | ||
| then | ||
| module="$(echo "${file}" | sed 's/\/.*//')" | ||
| # Only add to modules list if not already present | ||
| if [[ ! "${MODULES_EDITED[*]}" =~ ${module} ]] | ||
| then | ||
| # Skip ignored modules if present | ||
| [[ ! "${IGNORE_DIRS[*]}" =~ ${module} ]] && MODULES_EDITED+=("${module}") | ||
| fi | ||
| # If not a subdir but root level CHANGELOG.md | ||
| elif [[ "${file}" == 'CHANGELOG.md' ]] | ||
| then | ||
| CHANGELOG_FILES+=("${file}") # Validate root changelog | ||
| fi | ||
| done | ||
|
|
||
| # Check that for each module edited the CHANGELOG.md file | ||
| # was included in the commit | ||
| if [[ "${#MODULES_EDITED[@]}" != 0 ]] | ||
| then | ||
| # Loop through edited modules checking for CHANGELOG.md | ||
| for module in "${MODULES_EDITED[@]}" | ||
| do | ||
| MODULE_CHANGELOG_INCLUDED=0 | ||
| for file in "${STAGED_FILES[@]}" | ||
| do | ||
| # If any files under /root/{module}/* were edited, make | ||
| # sure that a CHANGELOG.md file was found and save the | ||
| # path to verify it has been updated | ||
| if [[ "${file}" =~ "${module}"/* ]] | ||
| then | ||
| # If the file's path string contains CHANGELOG.md add the | ||
| # path to the CHANGELOG_FILES array to be verified. | ||
| # This RegEx check accounts for CHANGELOG location | ||
| # inconsistencies: module root or doc/docs subdir | ||
| if [[ "${file}" =~ CHANGELOG.md ]] | ||
| then | ||
| CHANGELOG_FILES+=("${file}") | ||
| MODULE_CHANGELOG_INCLUDED=1 | ||
| fi | ||
| fi | ||
| done | ||
| # Add modules missing CHANGELOG.md to array | ||
| if [[ "${MODULE_CHANGELOG_INCLUDED}" == 0 ]] | ||
| then | ||
| MODULES_MISSING_CHANGELOG+=("${module}") | ||
| fi | ||
| MODULE_CHANGELOG_INCLUDED=0 | ||
| done | ||
| fi | ||
|
|
||
| # Print error messages and prepare exit for modules missing changelogs | ||
| if [[ "${#MODULES_MISSING_CHANGELOG[@]}" != 0 ]] | ||
| then | ||
| for module in "${MODULES_MISSING_CHANGELOG[@]}" | ||
| do | ||
| printf 'Missing changelog in module: %s/\n\n' "${module}" >&2 | ||
| CHANGELOG_ERRORS_FOUND=1 | ||
| done | ||
| fi | ||
|
|
||
| # Check version numbers and dates in changelogs provided | ||
| for log in "${CHANGELOG_FILES[@]}" | ||
| do | ||
| # sed using BRE expression for portability | ||
| head_versions=($(sed -n 's/^.*\(\[[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\]\) - \([0-9][0-9]*-[0-9][0-9]*-[0-9][0-9]*\).*$/\1 \2/p' "${log}" | head -n2)) | ||
| # Get latest and previous versions and strip any leading 0s | ||
| # and check the latest version is greater than the previous | ||
| latest_version=$(($(echo "${head_versions[0]:1:-1}" | sed 's/\.//g' | sed 's/^0*//')+0)) | ||
| previous_version=$(($(echo "${head_versions[2]:1:-1}" | sed 's/\.//g' | sed 's/^0*//')+0)) | ||
| if [[ "${latest_version}" -le "${previous_version}" ]] | ||
| then | ||
| printf 'Latest version in %s is incorrect.\nLatest: %s, Previous: %s\n\n' "${log}" "${head_versions[0]}" "${head_versions[2]}" >&2 | ||
| CHANGELOG_ERRORS_FOUND=1 | ||
| fi | ||
| # compare date in string format YY-mm-dd | ||
| latest_date="${head_versions[1]}" | ||
| current_date=$(date +'%Y-%m-%d') # Portable date syntax accross Linux/OSX/BSD | ||
| # Check latest date in changelog is current date | ||
| if [[ "${latest_date}" != "${current_date}" ]] | ||
| then | ||
| printf 'Latest date in %s is incorrect.\nLatest: %s, Current: %s\n\n' "${log}" "${head_versions[1]}" "${current_date}" >&2 | ||
| CHANGELOG_ERRORS_FOUND=1 | ||
| fi | ||
| done | ||
|
|
||
| # Exit with error code 1 after all error messages have been | ||
| # printed to stderr. If no errors detected exit with code 0 | ||
| if [[ "${CHANGELOG_ERRORS_FOUND}" == 1 ]] | ||
| then | ||
| printf 'Changelog verification failed. See error messages for more detail.\n' >&2 | ||
| exit 1 | ||
| fi | ||
This file contains hidden or 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,42 @@ | ||
| # This runs the pre-commit hook logic to check whether the relevent | ||
| # CHANGELOG.md files have been updated for both the root directory | ||
| # and/or modules edited if they have been changed. | ||
|
|
||
| name: Validate Changelogs | ||
|
|
||
| on: | ||
| pull_request: | ||
| types: [opened, reopened, synchronize] | ||
| paths-ignore: | ||
| - ".github/**" | ||
| - ".githooks/**" | ||
| - "docs/**" | ||
| - "bin/**" | ||
|
|
||
| jobs: | ||
| changedfiles: | ||
| runs-on: ubuntu-latest | ||
| # Map a step output to a job output | ||
| outputs: | ||
| all: ${{ steps.changes.outputs.all}} | ||
| steps: | ||
| # Make sure we have some code to diff. | ||
| - name: Checkout repository | ||
| uses: actions/checkout@v3 | ||
| with: | ||
| fetch-depth: 0 | ||
| - name: Get changed files | ||
| id: changes | ||
| # Set outputs using the command. | ||
| run: echo "all=$(git diff --name-only --diff-filter=ACMRT ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | xargs)" >> $GITHUB_OUTPUT | ||
|
|
||
| validate: | ||
| runs-on: ubuntu-latest | ||
| # require the first job to have ran | ||
| needs: changedfiles | ||
| # only run if there are changed files | ||
| if: ${{needs.changedfiles.outputs.all}} | ||
| steps: | ||
| - uses: actions/checkout@v3 | ||
| - name: Verify changelogs | ||
| run: bash ./.githooks/pre-commit ${{needs.changedfiles.outputs.all}} | ||
|
Olshansk marked this conversation as resolved.
|
||
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.