-
Notifications
You must be signed in to change notification settings - Fork 42
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
feat(pre-commit): add hook to enforce branch name validation rules #293
feat(pre-commit): add hook to enforce branch name validation rules #293
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's good overall but I'd change the way it's run. Pre-commit is generally a wrong hook for checking branches, it's not the point where branches are created.
.github/workflows/pre-commit.yml
Outdated
- name: Install pre-commit | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install pre-commit | ||
|
||
- name: Install pre-commit hooks | ||
run: pre-commit install |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this really necessary? It should be better documented why we decided to hack copy-pasted code and how the desired behavior differs from default.
.pre-commit-config.yaml
Outdated
hooks: | ||
- id: check-branch-name | ||
name: 'Check Branch Name' | ||
entry: ./scripts/check-branch-name.sh |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TBH I'm not convinced that in this particular case placing it in pre-commit is actually smart. It's not just for CI, the main purpose of pre-commit is running locally before commits creation (notably not before branch creation! after pre-commit runs I can still git branch whatever && git push
).
I'd rather not to do anything there (perhaps pre-push hook tho) and only check this naming convention for pull requests instead - and there's also easier way to do that for PRs (any script we wrap into CI workflow yaml will have branch name as environment variable eliminating the need to hack git plumbing to get those values).
scripts/check-branch-name.sh
Outdated
#!/bin/sh | ||
|
||
# Define allowed prefixes including release | ||
allowed_prefixes="feat|feature|bugfix|hotfix|chore|refactor|test|spike|prototype|release|docs" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Double-check, I think this differs from what Don wanted and doesn't include archive/
that we use.
scripts/check-branch-name.sh
Outdated
release_version_pattern="release/v[0-9]+\.[0-9]+\.[0-9]+(-[a-z0-9]+)?" | ||
|
||
# Get the current branch name | ||
branch_name=$(git symbolic-ref --short HEAD 2>/dev/null || git rev-parse --abbrev-ref HEAD 2>/dev/null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The script is pretty good overall but I'd separate the validation logic from integration bits like this. I think it could be beneficial to run in some places git is not accessible or not yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also these two specific ways of retrieving current branch name would actually break in detached head mode (git branch --show-current
is probably safer) but in CI context it's actually safer to just use ${GITHUB_HEAD_REF}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great spot! Thanks for the detailed explanation. Will definitely integrate this to handle detached HEAD correctly.
.pre-commit-config.yaml
Outdated
name: 'Check Branch Name' | ||
entry: ./scripts/check-branch-name.sh | ||
language: system | ||
files: ^.* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this check need or use files?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also stages
should always be included.
Two separate scripts: one detects current value, the other validates it, no dependency on git or files or anything in the validation script. + Configs cleanup.
I find post-checkout and pre-push the most effective places for branch name check - after actual branch creation and before things get pushed.
The business logic in the check script is unchanged, only the branch detection part. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added some changes, it starts looking good to me but I'd recommend some extra testing before we make this new behaviour the default for everyone (it's easy to change after and doesn't affect operations currently, all checks are just for display purposes, so we shouldn't preoptimize too much either).
@IvanAnishchuk Thanks for improving the PR! I have completed my testing, and the changes look good to me. Please confirm your validation so we can proceed further with this PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Good to go, I think.
Description
This pull request introduces a pre-commit hook to enforce specific rules for branch name validation. The goal is to ensure that all branches follow a consistent naming convention, improving the overall development workflow. The pre-commit hook will check the branch name format before allowing commits to proceed.
Related Issue
Issue # (e.g., #CSN-590) - Create Pre-Commit Hook to Enforce Branch Name Rules.
Changes Made
Added pre-commit hook: Implemented a custom script (
check-branch-name.sh
) that validates the branch name format.Updated
.pre-commit-config.yml
:check-branch-name
hook that runs the validation script.Added file permission changes:
check-branch-name.sh
script executable.How to Test
feat/CSN-1234-add-new-feature
), and the commit should pass.Checklist
Make sure that all of the following are complete before submitting the PR:
Screenshots