Actions
Here's a list of all CaptainHook's built-in Actions.
- Commit message Validation with regex
- Commit message Validation with rules
- Inject issue key from branch name
- Write invalid commit message to a file
- Load commit message from file
- Composer lock file checks
- PHP syntax check
- Enforce PHPUnit test coverage
- Git commit message notifications
- Prompt the user to confirm the operation
- Check file size
- Prevent committing any content to files
- Prevent committing empty files
- Prevent committing specific content to files
- Block secrets of any kind from the repository
- Ensure a files exists
- Enforce branch naming
- Block pushing of squash or fixup commits
- Force update before push
Actions
Commit-Message validation
Enforce Chris Beams commit message rules
Make sure your commit message complies to the following rules.
- Subject line starts with an upper-case character
- Subject line doesn't exceed 50 characters
- Subject line doesn't end with a period
- Subject line and body are separated by a blank line
- Body lines don't exceed 72 characters
- Use imperative mood
{
"action": "\CaptainHook\App\Hook\Message\Action\Beams",
"options": []
}
Commit message regex validation
Use a regex to validate your commit messages.
{
"action": "\CaptainHook\App\Hook\Message\Action\Regex",
"options": {
"regex": "#.*#i",
"error": "No match for: %s",
"success": "Found match: %s"
}
}
Options "error" and "success" are optional.
Define rules for your commit messages
Use CaptainHooks built in rules or write your own ones.
{
"action": "\CaptainHook\App\Hook\Message\Action\Rules",
"options": [
"\CaptainHook\App\Hook\Message\Rule\MsgNotEmpty"
]
}
Write invalid commit message to file
Writes the invalid commit message to a file. This way it can be loaded for the next commit attempt, and you don't have to type the whole message again. Instead, you can just fix the errors and proceed.
{
"action": "\CaptainHook\App\Hook\Message\Action\CacheOnFail",
"options": {
"file": ".git/CH_MSG.temp"
}
}
Commit-Message preparation
-
Commit without providing a message
Then the editor will load with your prepared commit message to edit. -
Commit with providing a message
This way the commit message will be whatever you prepare. So you have to use the provided message and combine it with your "preparation" otherwise you will just overwrite the message.
Inject issue key into your commit message
Extracts an issue key from the current branch name and appends or prepends it to your commit messages subject or body. If the commit message already contains the key nothing is added. If you set the force option to true it will fail if no issue key can be extracted.
{
"action": "\CaptainHook\App\Hook\Message\Action\InjectIssueKeyFromBranch",
"options": {
"regex": "#([A-Z]+\\-[0-9]+)#i",
"into": "body",
"mode": "append",
"prefix": "\\n\\nIssue: ",
"force": false
}
}
Load commit message from file
Load a commit message from a file for example if you wrote an invalid commit message to a file in a previous commit attempt.
{
"action": "\CaptainHook\App\Hook\Message\Action\LoadFromFile",
"options": {
"file": ".git/CH_MSG.temp"
}
}
Pre-Commit / Pre-Push
Composer lock file check
Make sure your composer.lock file is up to date.
{
"action": "\CaptainHook\App\Hook\Composer\Action\CheckLockFile",
"options": []
}
Lint all changed PHP files
Make sure your PHP code doesn't contain any syntax errors.
{
"action": "\CaptainHook\App\Hook\PHP\Action\Linting",
"options": []
}
Monitor your unit test code coverage
Make sure you code coverage doesn't decrease beyond a given percent value.
{
"action": "\CaptainHook\App\Hook\PHP\Action\TestCoverage",
"options": {
"minCoverage": 90,
"cloverXml": "build/logs/clover.xml"
}
}
Check if files are too big
Make sure you do not commit files that are bigger than a configured size limit.
Use 'B', 'K' or 'M' as size indication.
{
"action": "\CaptainHook\App\Hook\File\Action\MaxSize",
"options": {
"maxSize": "5M"
}
}
Check if a file is empty or does not exist
Make sure you do not commit if there is any file of a configured list that is not empty. The following configuration makes sure you will not commit log files with contents to the repository.
{
"action": "\CaptainHook\App\Hook\File\Action\IsEmpty",
"options": {
"files": [
".env",
"logs/event.log",
"logs/app/*.log"
]
}
}
Check if a file is not empty
Make sure you do not commit empty files to the repository. With this configuration you can make sure that README files are not committed without any contents.
{
"action": "\CaptainHook\App\Hook\File\Action\IsNotEmpty",
"options": {
"files": [
"README.md",
"src/Modules/**/README.md"
]
}
}
Check if a file does not contain certain text
Make sure you do not commit specific contents to a list of file types.
{
"action": "\CaptainHook\App\Hook\File\Action\DoesNotContainRegex",
"options": {
"regex": "#print_r|var_dump#i"
"regexName": "debug output"
"fileExtensions": [
"inc",
"php"
]
}
}
Block secrets from being stored in the repository
Make sure you are not committing passwords or api tokens to the repository. You can configure existing regex supplier oder define your own ones. If it detects something wrongfully you can define an allow list. If you configure an `entropyThreshold` greater than zero. The Cap'n will do some entropy calculations to detect passwords. blockDefault to false.
{
"action": "\CaptainHook\App\Hook\Diff\Action\BlockSecrets",
"options": {
"entropyThreshold": 3.7,
"suppliers": [
"\\CaptainHook\\Secrets\\Regex\\Supplier\\Aws",
"\\CaptainHook\\Secrets\\Regex\\Supplier\\Github",
"\\CaptainHook\\Secrets\\Regex\\Supplier\\Stripe",
"\\CaptainHook\\Secrets\\Regex\\Supplier\\Google",
"\\CaptainHook\\Secrets\\Regex\\Supplier\\Password"
],
"blocked": ["#password\": \"[\S]+\"#i"],
"allowed": ["#my-dummy-token#", "#root#"]
}
}
Ensure a file exists
Make sure you are only allowed to push if configured files exist in the repository.
{
"action": "\CaptainHook\App\Hook\File\Action\Exists",
"options": {
"files": [
"tests/**/*Tests.php",
"README.md"
]
}
}
Enforce branch naming rules
Make sure branch names match a configured regex.
{
"action": "\CaptainHook\App\Hook\Branch\Action\EnsureNaming",
"options": {
"regex": "#master|integration|feature/[a-z]+-[0-9]+#"
}
}
Block pushing of !fixup or !squash commits
Preparing !fixup or !squash commits for smooth rebasing is very handy. But you may not want to push those commits to the main branch. If you don't provide any protected branches push fixup and squash commits is generally prohibited.
{
"action": "\CaptainHook\App\Hook\Branch\Action\BlockFixupAndSquashCommits",
"options": {
"protectedBranches": ["main", "master"]
}
}
Force update before push
Checks if a branch contains any trigger commits, if so it forces you to update your local branch before you can push.
{
"action": "\CaptainHook\App\Hook\Notify\Action\IntegrateBeforePush",
"options": {
"trigger": "[merge]",
"branch": "main"
}
}
Prompt the user to confirm the operation
Ask the user to confirm to continue the current git operation.
{
"action": "\CaptainHook\App\Hook\UserInput\AskConfirmation",
"options": {
"message": "Do you really want to continue? [Yes|no]",
"default": true
}
}
Post-Merge, Post-Rewrite, Post-Checkout
Display notifications if you merge or checkout commits with notifications
Trigger notifications with git commit messages. Use git-notify: in your commit message to display
notifications after merge, rebase or checkout.
Inspired by: git-notify from Jani Eväkallio
Update dev tooling git-notify: WARNING Please update your dev tooling
{
"action": "\CaptainHook\App\Hook\Notify\Action\Notify",
"options": {
"prefix": "git-notify:"
}
}