Git::Hooks::Notify - Git::Hooks plugin to notify users via email
version 2.2.1
This Git::Hooks plugin hooks itself to the hooks below to notify users via email about pushed commits affecting specific files in the repository.
post-receive
This hook is invoked once in the remote repository after a successful git push. It's used to notify JIRA of commits citing its issues via comments.
git push
To enable it you should add it to the githooks.plugin configuration option:
git config --add githooks.plugin Notify
By default no notifications are sent. You have to specify rules telling the plugin which email addresses should receive notifications about any change or about changes in specific paths inside the repository. Each rule is checked for each branch affected by the git-push and each combination may produce a specific email notification, with configurable Subject and From headers.
Subject
From
You should avoid configuring too many rules because each one of them will trigger a git-log command and potentially send an email. All this processing will take place while the user is waiting for the command git-push to finish. In order to minimize the delay you should try to configure a single global rule and a single rule for each path specification, grouping all email addresses interested in the same path in the same rule.
git-log
git-push
The body of the message contains information about the changes and the result of a git log command showing the pushed commits and the list of files affected by them. For example:
git log
Subject: [Git::Hooks::Notify] repo:myproject branch:master This is a notification about new commits affecting a repository you're watching. REPOSITORY: myproject BRANCH: master BY: username FROM: 75550b66ab08536787487545904fb062c6e38a7f TO: 6eaa6a84fbd7e2a64e66664f3d58707618e20c72 FILTER: lib/Git/Hooks/ commit 6eaa6a84fbd7e2a64e66664f3d58707618e20c72 Author: Gustavo L. de M. Chaves <gnustavo@cpan.org> Date: Mon Dec 4 21:41:19 2017 -0200 Add plugin Git::Hooks::Notify 305 0 lib/Git/Hooks/Notify.pm 63 0 t/02-notify.t commit c45feb16fe3e6fc105414e60e91ffb031c134cd4 Author: Gustavo L. de M. Chaves <gnustavo@cpan.org> Date: Sat Nov 25 19:13:42 2017 -0200 CheckJira: JQL options are scalar, not multi-valued 40 32 lib/Git/Hooks/CheckJira.pm 12 12 t/02-check-jira.t
The FILTER: line only appears if the rule specifies one or more pathspecs to only show commits affecting matching files.
FILTER:
Each commit shows the files it changes, perhaps filtered by the rule's pathspecs. They're shown in the format produced by git-log's --numstat option. Merge commits don't show changed files, though. Thery're marked with an additional Merge: header.
--numstat
Merge:
You can change the git log format and a few other things in the message using the configuration options explained below.
Notify - Git::Hooks plugin to notify users via email
The plugin is configured by the following git options.
The rule directive adds a notification rule specifying which RECIPIENTS should be notified of pushed commits affecting the specified PATHSPECS.
If no pathspec is specified, the recipients are notified about every push.
RECIPIENTS is a space-separated list of email addresses.
RECIPIENTS
PATHSPECS is a space-separated list of pathspecs, used to restrict notifications to commits affecting particular paths in the repository. Note that the list of paths starts after a double-dash (--).
PATHSPECS
For example:
[githooks "notify"] rule = gnustavo@cpan.org rule = fred@example.net barney@example.net -- lib/Git/Hooks/Notify.pm rule = batman@example.net robin@example.net -- Changes lib/
The first rule above sends notifications to gnustavo@cpan.org about every change pushed to the repository.
The second rule sends notifications to the Bedrock fellows just about changes in the lib/Git/Hooks/Notify.pm file.
The third rule sends notifications to the Dynamic Duo just about modifications in the Changes file in the repository root and about modifications in any file under the lib/ directory.
You can read all about pathspecs in the git help glossary.
git help glossary
By default the messages are sent using Email::Simple's default transport. On Unix systems, it is usually the sendmail command. You can specify another transport using this configuration.
sendmail
TRANSPORT must be the basename of an available transport class, such as SMTP, Maildir, or Mbox. The name is prefixed with Email::Sender::Transport:: and the complete name is required like this:
TRANSPORT
SMTP
Maildir
Mbox
Email::Sender::Transport::
eval "require Email::Sender::Transport::$TRANSPORT";
So, you must make sure such a transport is installed in your server's Perl.
ARGS is a space-separated list of VAR=VALUE pairs. All pairs will be tucked in a hash and passed to the transport's constructor. For example:
ARGS
VAR=VALUE
[githooks "notify"] transport = SMTP host=smtp.example.net ssl=starttls sasl_username=myself sasl_password=myword transport = Mbox filename=/home/user/.mbox transport = Maildir dir=/home/user/maildir
Please, read the transport's class documentation to know which arguments are available.
This allows you to specify a sender address to be used in the notification's To header. If you don't specify it, the sender will probably be the user running your hooks. But you shouldn't count on it. It's better to specify it with a valid email address that your users can reply to. Something like this:
To
[githooks "notify"] from = "Git::Hooks" <git@yourdomain.com>
This allows you to specify the subject of the notification emails. If you don't specify it, the default is like this:
Subject: [Git::Hooks::Notify] repo:%R branch:%B
The %letters symbols are placeholders that are replaced automatically. The three placeholders defined are:
%letters
%R: the repository name.
%R
%B: the branch name.
%B
%A: the username of the user who performed the git-push command.
%A
This allows you to specify a preamble for the notification emails. There is no default preamble.
This allows you to specify the limit of commits that should be shown for each changed branch. Read about the --max-count option in git help log. If not specified, a limit of 10 is used.
git help log
If your Git repository has a web interface it's useful to provide links to the commits shown in the notification message. If configured, each SHA1 contained in the git-log output is substituted by URL_PATTERN, with the %H placeholder replaced by the SHA1.
URL_PATTERN
%H
See below how to configure this for some common Git servers. Replace the angle-bracketed names with values appropriate to your context:
GitHub
https://github.com/<USER>/<REPO>/commit/%H
Bitbucket Cloud
https://bitbucket.org/<USER>/<REPO>/commits/%H
Bitbucket Server
<BITBUCKET_BASE_URL>/projects/<PROJECTID>/repos/<REPOID>/commits/%H
Gerrit with Gitblit
<GERRIT_BASE_URL>/plugins/gitblit/commit/?r=<REPO>&h=%H
By default the email messages are sent in plain text. Enabling this option sends HTML-formatted messages, which look better on some email readers.
Make sure you have the HTML::Entities module installed, because it's needed to format the messages.
These are just a few of the ideas for improving this plugin.
Generalize the commit-url template.
commit-url
It should support other placeholders for the Git server's base URL, repository name, user name, etc. So that we could configure a single template for all repositories in a server. Currently one has to configure a different commit-url for each repository.
Send notifications on Gerrit's change-merged hook.
Email::Sender::Manual::QuickStart
Gustavo L. de M. Chaves <gnustavo@cpan.org>
This software is copyright (c) 2017 by CPqD <www.cpqd.com.br>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Git::Hooks, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Git::Hooks
CPAN shell
perl -MCPAN -e shell install Git::Hooks
For more information on module installation, please visit the detailed CPAN module installation guide.