Skip to content

Commit 7e0c013

Browse files
authored
AAP-35592: Move git hooks to own folder, add pre-push hook (ansible#674)
# [AAP-35592] Add git hook checking existence of Jira number in branch and commit messages ## Description <!-- Mandatory: Provide a clear, concise description of the changes and their purpose --> This PR provides following changes: * creates new folder for git hooks to make the git hooks more visible and easier to maintain. The folder itself contains documentation. * adds a new git hook invoked at `git push` to check whether the branch and commit message at the time of push contain Jira number (AAP-NNNNN) or a special marker `NO_JIRA` to indicate the changes don't have/need a Jira item. These changes are needed in order to keep work items in Jira in sync with their corresponding pull requests and commits. The Jira numbers included in the commit messages can be different from Jira number in branch name to allow for cherry-picking of commits when PRs are being back ported. ## Type of Change <!-- Mandatory: Check one or more boxes that apply --> - [ ] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] Documentation update - [ ] Test update - [x] Refactoring (no functional changes) - [x] Development environment change - [ ] Configuration change ## Self-Review Checklist <!-- These items help ensure quality - they complement our automated CI checks --> - [x] I have performed a self-review of my code - [x] I have added relevant comments to complex code sections - [x] I have updated documentation where needed - [x] I have considered the security impact of these changes - [x] I have considered performance implications - [x] I have thought about error handling and edge cases - [x] I have tested the changes in my local environment ## Testing Instructions <!-- Optional for test-only changes. Mandatory for all other changes --> <!-- Must be detailed enough for reviewers to reproduce --> ### Prerequisites <!-- List any specific setup required --> ### Steps to Test 1. Pull down the PR 2. Run `make git_hooks_config` in the root of the repo 3. Create a new branch, make some change, commit it and try to push it to your fork. Based on branch name and commit message you will see different results (see below). ### Expected Results <!-- Describe what should happen after following the steps --> * If the branch did not include either a Jira number in format AAP-NNNN or magic string NO_JIRA you shouldn't be able to push. * If the branch did include Jira number, but commit message did not, you shouldn't be able to push. * If the branch did include Jira number, and the commit message included the same Jira number, your push will succeed. * If the branch did include Jira number and the commit message included **different** Jira number, you will see a warning but the push will succeed.
1 parent b37fdd2 commit 7e0c013

File tree

4 files changed

+79
-8
lines changed

4 files changed

+79
-8
lines changed

.githooks/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# .githooks
2+
3+
This folder contains executable files that will be invoked by git at certain git operations.
4+
5+
By default git hooks are located i `.git/hooks` folder at the root of the repository. Since the default folder is hidden by most IDEs, this repository reconfigures git's hook location in order to make the hooks visible and easier to maintain.
6+
7+
## Configuration
8+
9+
Normal development flows (see file [../README.md](../README.md) ) will call git to change the location of git hooks.
10+
11+
To make this change manually you can invoke following command from the root of the repository:
12+
13+
```sh
14+
make git_hooks_config
15+
```
16+
17+
## Git hooks implementation
18+
19+
Git hooks are simply executable files that follow the rules below:
20+
21+
* have executable permissions
22+
* file name must correspond to git hook name with no extension(no `.sh` or `.py`) (see documentation section below)
23+
24+
Return code other than zero(0) will cause the git operation that triggerred the hook to fail, while zero(0) return code indicates success and git opertaion will succeed.
25+
26+
## Documentation
27+
28+
Git hooks documentation: <https://git-scm.com/docs/githooks>
File renamed without changes.

.githooks/pre-push

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env bash
2+
3+
# Name of default branch from which feature branches are created and to which PRs will be merged back to
4+
DEFAULT_BRANCH="devel"
5+
# Regexp to match jira number AAP-NNNNN or magic string "NO_JIRA"
6+
NO_JIRA_MARKER="NO_JIRA"
7+
JIRA_REGEXP="(aap-[0-9]+|${NO_JIRA_MARKER})"
8+
9+
# Fetch current branch name and list of commits since diverging from default branch
10+
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
11+
CURRENT_COMMITS=$(git --no-pager log --format=%s --reverse ${DEFAULT_BRANCH}..)
12+
13+
# Extract Jira number or magic marker from branch and commit messages(filtered for unique values)
14+
BRANCH_JIRA=$(grep -i -o -E "${JIRA_REGEXP}" <<< ${CURRENT_BRANCH})
15+
COMMIT_JIRAS=$(grep -i -o -E "${JIRA_REGEXP}" <<< ${CURRENT_COMMITS} | uniq )
16+
# Count all Jira numbers and those matching Jira from branch name
17+
COMMIT_JIRA_COUNT=$(grep -c . <<< ${COMMIT_JIRAS})
18+
MATCHING_JIRAS_COUNT=$(grep -ic -E "${BRANCH_JIRA}" <<< ${COMMIT_JIRAS})
19+
20+
echo "JIRA number from branch name: ${BRANCH_JIRA}"
21+
echo "JIRA numbers from commits:"
22+
echo "${COMMIT_JIRAS}"
23+
echo "Number of JIRA numbers from commits matching JIRA number from branch name: ${MATCHING_JIRAS_COUNT}"
24+
25+
# if no Jira or no magic marker found in branch name, fail
26+
echo "Checking branch name..."
27+
if [ "${BRANCH_JIRA}" = "" ]; then
28+
echo "Fail: Branch name does not contain a JIRA number or a ${NO_JIRA_MARKER} marker."
29+
exit 1
30+
# if branch does not have the magic marker, check the commits as well
31+
elif [ "${BRANCH_JIRA}" != "${NO_JIRA_MARKER}" ]; then
32+
echo "Checking commit messages..."
33+
# if there is no Jira number or magic marker, fail
34+
if [ ${COMMIT_JIRA_COUNT} -eq 0 ]; then
35+
echo "Fail: No commit message contains a JIRA number or a ${NO_JIRA_MARKER} marker."
36+
exit 1
37+
# if no Jira numbers or magic marker match the Jira number from branch name, inform the user
38+
# this case might be happening when code is being back-ported under different Jira number in branch name
39+
elif [ ${MATCHING_JIRAS_COUNT} -eq 0 ]; then
40+
echo "Warning: No Jira numbers or ${NO_JIRA_MARKER} marker in commit messages match Jira number from branch name."
41+
else
42+
echo "OK. Found Jira numbers(or ${NO_JIRA_MARKER} marker) in commit messages that match Jira number in branch name."
43+
fi
44+
else
45+
echo "OK. Skipping checks of commit messages, branch name includes ${NO_JIRA_MARKER}."
46+
fi

Makefile

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,14 @@ COMPOSE_UP_OPTS ?=
1111
DOCKER_COMPOSE ?= docker compose
1212

1313
.PHONY: PYTHON_VERSION clean build\
14-
check lint check_black check_flake8 check_isort
14+
check lint check_black check_flake8 check_isort git_hooks_config
1515

1616
PYTHON_VERSION:
1717
@echo "$(subst python,,$(PYTHON))"
1818

19-
## Install the pre-commit hook in the approprate .git directory structure
20-
.git/hooks/pre-commit:
21-
@echo "if [ -x pre-commit.sh ]; then" > .git/hooks/pre-commit
22-
@echo " ./pre-commit.sh;" >> .git/hooks/pre-commit
23-
@echo "fi" >> .git/hooks/pre-commit
24-
@chmod +x .git/hooks/pre-commit
19+
## Set the local git configuration(specific to this repo) to look for hooks in .githooks folder
20+
git_hooks_config:
21+
git config --local core.hooksPath .githooks
2522

2623
## Zero out all of the temp and build files
2724
clean:
@@ -69,7 +66,7 @@ stop-postgres:
6966
# --------------------------------------
7067

7168
# Build the library into /dist
72-
build: .git/hooks/pre-commit
69+
build: git_hooks_config
7370
python -m build .
7471

7572

0 commit comments

Comments
 (0)