git commits integration with Trello using git hooks and Laravel
Hello everyone!
The internet is full of awesome tools that make our lives easier. Today I would like to tell you a story about my experience in integration git commits with Trello by using git hooks.
Introduction
I have a customer, who uses Trello as task manager and I want to use card numbers in my commits to understand what task in which commit I’ve done.
And my commits look like:
Couple of words about I've done in a commit.# Links to card number
issue #123
fixed #234
123 and 234 are short numbers of Trello cards (I use them because they are simple to understand and search)
We can easily find the short number but unfortunately can not search by it.
https://trello.com/c/AQ9ZCaOT/123-my-super-task
https://trello.com/c/AQ9ZCaOF/234-my-super-druper-task
How it should be
After a while, I realized that I want to have links to Trello cards in my commits. When I write commit I use format issue #123, but then after committing it will transform in something like that:
[issue #123](https://trello.com/c/AQ9ZCaOT/123-my-super-task)
and then move Trello cards through lists workflow.
For example:
- fix #123 — Move card to DONE list
- issue #234 — Move card to IN PROGRESS list, if it’s in list NEW
And then add a new comment with a link to commit in related cards.
Git hooks
To solve this problem we can use git hooks. You can read the documentation about hook types. In my case I will use:
- commit-msg — The hook takes one parameter, which again is the path to a temporary file that contains the commit message written by the developer. If this script exits non-zero, Git aborts the commit process, so you can use it to validate your project state or commit message before allowing a commit to go through.
- post-commit — After the entire commit process is completed, the
post-commit
hook runs. It doesn’t take any parameters, but you can easily get the last commit by runninggit log -1 HEAD
. Generally, this script is used for notification or something similar.
Creating a tool
At first, I’ve started looking for a composer package that allows me to solve this problem, but all of them were not exactly what I wanted. And I’ve started reinventing the wheel.
After a couple of days, I’ve developed my new package. I wanted to have a tool for Laravel projects.
What does my package do?
- It uses artisan commands to run in git hooks.
- Every git hook uses separate command.
- An unlimited amount of listeners can be run for every hook.
- Listeners go through the pipes and pass all necessary information about the hook.
If you want to know more about my package, so tell me and I will write an article about it.
Ok, let’s back to my problem.
Trello API client
There are a well-documented API and lots of packages for integrating with it, and for my purposes, I used cdaguerre/php-trello-api
At first, I’ve got API key. Then I’ve created a wrap client class for this package:
Adds configs
// config/services.php...
'trello' => [
'api_key' => env('TRELLO_API_KEY'),
'access_token' => env('TRELLO_ACCESS_TOKEN'),
]
...//.env
TRELLO_BOARD_ID=
TRELLO_API_KEY=
TRELLO_ACCESS_TOKEN=
Then register my wrap class in App\Providers\AppServiceProvider
public function register()
{
...
$this->app->singleton(Contracts\Client::class, function() {
return new Client(
$this->app['config']['services.trello.api_key'],
$this->app['config']['services.trello.access_token']
);
});
}
Developer, remember!
Using env helper function everywhere in your project can cause problems. According to official documentation, when you cache your config (php artisan config:cache) env helper will start to return null value.
Creating the hooks
I have two hook listeners:
- Text to links transformer
- Cards mover
Text to links transformer
And then register listener in config file
// config/git_hooks.phpreturn [
...
'commit-msg' => [
Infrastructure\GitHooks\ReplaceIssueNumberIntoLinkToTrello::class => [
'board' => env('TRELLO_BOARD_ID'),
],
],
...
];
If you like tests, there is one for listener
That’s it!
And after next commit I saw information about run Listener
Cards mover
I will show you short version of my listener. It can only move cards to DONE list.
And then register listener in config file
// config/git_hooks.phpreturn [
...
'post-commit' => [
Infrastructure\GitHooks\MoveTrelloCardWhenItFixed::class => [
'board' => env('TRELLO_BOARD_ID'),
'list' => 'DONE', // List name where card should be
],
],
...
];
Of course test for listener
Done!
That’s it.
This problem was not so difficult to solve, but very interesting and I learned a lot of new things during solving it.
Thanks for reading!