Enforce Drupal Conventional commits using Grumphp

Marouene
2 min readFeb 28, 2023

--

What’s conventional commits ?

The Conventional Commits is a lightweight convention on top of commit messages. It provides an easy set of rules for creating an explicit commit history; which makes it easier to write automated tools on top of.

The structure of commit message should be :

<type>[optional scope]: <description>

[optional body]

[optional footer(s)]

Control commits message using Grumphp

GrumPHP is a tool that allows you to automate code validation tasks like code standards, code formatting, linting, and more. To validate Conventional Commits with GrumPHP, you need to add a custom task to your GrumPHP configuration file.

you need to install Grumphp with composer :

composer require --dev phpro/grumphp

Create a Configuration file grumphp.yml

grumphp:
process_timeout: 480
git_hook_variables:
EXEC_GRUMPHP_COMMAND: exec
ascii:
failed: ~
succeeded: ~
tasks:
git_commit_message:
allow_empty_message: false
enforce_capitalized_subject: false
enforce_no_subject_punctuations: false
enforce_no_subject_trailing_period: true
enforce_single_lined_subject: true
type_scope_conventions: []
max_body_width: 72
max_subject_width: 60
matchers:
Must contain: /^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\([a-z]+\))?:[\w ]+$/
case_insensitive: true
multiline: true
additional_modifiers: ''

When you commit changes to your codebase, GrumPHP will run the git_commit_message task and validate the commit message format using the Conventional Commits rules. For example :

git commit -m "docs(init): add contributing guidelines and readme"

You can change the default command that will execute the grumphp script by the docker service with the following line :

git_hook_variables:
EXEC_GRUMPHP_COMMAND: 'docker-compose run --rm --no-deps web'

That’s it! You can now use GrumPHP to automatically validate Conventional Commits in your Drupal project.

--

--