Skip to content
Merged
Show file tree
Hide file tree
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…
Dec 14, 2022
e5f7fd5
Add github workflow to run pre-commit changelog verification logic on…
Dec 14, 2022
2ccbe9a
Add docs and bin directories to paths to ignore for changelog verific…
Dec 14, 2022
40f4a73
Add pre-commit git hook activation guide to prepare local environment…
Dec 14, 2022
e187abe
Change tabs to spaces everywhere, fix grammar, address PR comments
Dec 15, 2022
f3e5dfb
Remove spaces fixing indentation
Dec 15, 2022
9bc71a5
Removed unneeded cat call, removed previous_date not used, added quot…
Dec 16, 2022
51190ca
Add do not merge label when verification fails
Dec 17, 2022
a949a1e
Update guide wording in development documentation
Dec 17, 2022
9d46990
Fix if statement
Dec 17, 2022
13d541f
Add continue on error to validate job
Dec 17, 2022
85dd301
Fix one to on
Dec 17, 2022
9db1310
Fix leading underscores
Dec 17, 2022
7241bde
Remove adding 'do not merge' label rely only on exit status of script…
Dec 20, 2022
11d0cf4
Merge branch 'main' into changelog-verify
Dec 20, 2022
1a113d2
Store locations of changelog files to account for different storage l…
Dec 20, 2022
8daf8de
Update CHANGELOG.md
Dec 20, 2022
ccf72d0
Remove date command variations and compare dates in string format
Dec 20, 2022
1f06928
Fix comments
Dec 20, 2022
e71df75
Remove root level CHANGELOG.md check, improve comment verbosity
Dec 20, 2022
3cf3d7d
Check for root-level CHANGELOG.md file, remove CHANGELOG updates
Dec 21, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions .githooks/pre-commit
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}"/* ]]
Comment thread
Olshansk marked this conversation as resolved.
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
42 changes: 42 additions & 0 deletions .github/workflows/changelog-verify.yml
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}}
Comment thread
Olshansk marked this conversation as resolved.
9 changes: 9 additions & 0 deletions docs/development/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ $ git clone git@github.com:pokt-network/pocket.git && cd pocket
$ make develop_start
```

Optionally activate changelog pre-commit hook
```bash
cp .githooks/pre-commit .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
```

Comment thread
Olshansk marked this conversation as resolved.
_Please note that the Github workflow will still prevent this from merging
unless the CHANGELOG is updated._

### View Available Commands

```bash
Expand Down