Git::Hooks::CheckJira - Git::Hooks plugin which requires citation of JIRA issues in commit messages
version 2.6.2
As a Git::Hooks plugin you don't use this Perl module directly. Instead, you may configure it in a Git configuration file like this:
Git::Hooks
[githooks] plugin = CheckJira admin = joe molly [githooks "checkjira"] jiraurl = https://jira.example.net jirauser = jiradmin jirapass = my-secret matchlog = (?s)^\\[([^]]+)\\] jql = project IN (ABC, UTF, GIT) AND issuetype IN (Bug, Story) AND status IN ("In progress", "In testing") by-assignee = true fixversion = refs/heads/master future fixversion = ^refs/heads/(\\d+\\.\\d+)\\. ^$+
The first section enables the plugin and defines the users joe and molly as administrators, effectivelly exempting them from any restrictions the plugin may impose.
joe
molly
The second instance enables some of the options specific to this plugin.
some
The options jiraurl, jirauser, and jirapass specify the JIRA server and are usually kept in the global Git configuration file and not in the repository's configuration file since they are sensitive and usually the same for all repositories.
jiraurl
jirauser
jirapass
The matchlog option requires that the JIRA keys be cited at the beginning of the commit messages title, enclosed in brackets. By default JIRA keys can be cited anywhere, but it's usually a good idea to restrict where they should be cited to avoid falselly identifying other words as JIRA keys.
matchlog
The jql option imposes more restrictions on the cited JIRA issues (besides the few restrictions that are imposed by default, such as they being resolved and they exist).
jql
The by-assignee option requires that all cited JIRA issues be assigned to the user pushing the commits.
by-assignee
The firt fixversion option requires that commits pushed to master must cite JIRA issues associated with the fixVersion future. The second option requires that commits pushed to branches named after a version name (e.g. 1.0.1) must cite JIRA issues associated with a fixVersion named after the same major.minor version number (e.g. 1.0).
fixversion
master
future
1.0.1
major.minor
1.0
This Git::Hooks plugin hooks itself to the hooks below to guarantee that every commit message cites at least one valid JIRA issue key in its log message, so that you can be certain that every change has a proper change request (a.k.a. ticket) open.
commit-msg, applypatch-msg
This hook is invoked during the commit, to check if the commit message cites valid JIRA issues.
update
This hook is invoked multiple times in the remote repository during git push, once per branch being updated, to check if the commit message cites valid JIRA issues.
git push
pre-receive
This hook is invoked once in the remote repository during git push, to check if the commit message cites valid JIRA issues.
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.
ref-update
This hook is invoked when a push request is received by Gerrit Code Review, to check if the commit message cites valid JIRA issues.
patchset-created
This hook is invoked when a push request is received by Gerrit Code Review for a virtual branch (refs/for/*), to check if the commit message cites valid JIRA issues.
It requires that any Git commits affecting all or some branches must make reference to valid JIRA issues in the commit log message. JIRA issues are cited by their keys which, by default, consist of a sequence of uppercase letters separated by an hyphen from a sequence of digits. E.g., CDS-123, RT-1, and GIT-97.
CDS-123
RT-1
GIT-97
To enable it you should add it to the githooks.plugin configuration option:
git config --add githooks.plugin CheckJira
CheckJira - Git::Hooks plugin to implement JIRA checks
The plugin is configured by the following git options.
By default, the message of every commit is checked. If you want to have them checked only for some refs (usually some branch under refs/heads/), you may specify them with one or more instances of this option.
The refs can be specified as a complete ref name (e.g. "refs/heads/master") or by a regular expression starting with a caret (^), which is kept as part of the regexp (e.g. "^refs/heads/(master|fix)").
^
By default, the message of every commit is checked. If you want to exclude some refs (usually some branch under refs/heads/), you may specify them with one or more instances of this option.
The refs can be specified as in the same way as to the ref option above.
ref
Note that the ref option has precedence over the noref option, i.e., if a reference matches both options it will be checked.
noref
This option specifies the JIRA server HTTP URL, used to construct the JIRA::REST object which is used to interact with your JIRA server. Please, see the JIRA::REST documentation to know about them.
JIRA::REST
This option specifies the JIRA server username, used to construct the JIRA::REST object.
This option specifies the JIRA server password, used to construct the JIRA::REST object.
By default, JIRA keys are matched with the regex /\b[A-Z][A-Z]+-\d+\b/, meaning, a sequence of two or more capital letters, followed by an hyphen, followed by a sequence of digits. If you customized your JIRA project keys, you may need to customize how this hook is going to match them. Set this option to a suitable regex to match a complete JIRA issue key.
/\b[A-Z][A-Z]+-\d+\b/
By default, JIRA keys are looked for in all of the commit message. However, this can lead to some false positives, since the default issue pattern can match other things besides JIRA issue keys. You may use this option to restrict the places inside the commit message where the keys are going to be looked for. You do this by specifying a regular expression with a capture group (a pair of parenthesis) in it. The commit message is matched against the regular expression and the JIRA tickets are looked for only within the part that matched the capture group.
Here are some examples:
\[([^]]+)\]
Looks for JIRA keys inside the first pair of brackets found in the message.
(?s)^\[([^]]+)\]
Looks for JIRA keys inside a pair of brackets that must be at the beginning of the message's title.
(?im)^Bug:(.*)
Looks for JIRA keys in a line beginning with Bug:. This is a common convention around some high caliber projects, such as OpenStack and Wikimedia.
Bug:
This is a multi-valued option. You may specify it more than once. All regexes are tried and JIRA keys are looked for in all of them. This allows you to more easily accommodate more than one way of specifying JIRA keys if you wish.
By default, any cited issue must exist on the server and be unresolved. You can specify other restrictions (and even allow for resolved issues) by specifying a JQL expression which must match all cited issues. For example, you may want to:
Allow for resolved issues
[githooks "checkjira"] jql = resolution IS EMPTY OR resolution IS NOT EMPTY
Require specific projects, issuetypes, and statuses
[githooks "checkjira"] jql = project IN (ABC, UTF, GIT) AND issuetype IN (Bug, Story) AND status IN ("In progress", "In testing")
This is a scalar option. Only the last JQL expression will be used to check the issues.
You may impose restrictions on specific branches (or, more broadly, any reference) by mentioning them before the JQL expression. REF can be specified as a complete ref name (e.g. "refs/heads/master") or by a regular expression starting with a caret (^), which is kept as part of the regexp (e.g. "^refs/heads/(master|fix)"). For instance:
[githooks "checkjira"] ref-jql = refs/heads/master fixVersion = future ref-jql = ^refs/heads/release/ fixVersion IN releasedVersions()
Note, though, that if there is a global JQL specified by the githooks.checkjira.jql option it will be checked separately and both expressions must validate the issues matching REF.
By default, the log must reference at least one JIRA issue. You can make the reference optional by setting this option to false.
By default, every issue referenced must be unresolved, i.e., it must not have a resolution. You can relax this requirement by setting this option to false.
This multi-valued option allows you to specify that commits affecting BRANCH must cite only issues that have their Fix For Version field matching FIXVERSION. This may be useful if you have release branches associated with particular JIRA versions.
Fix For Version
BRANCH can be specified as a complete ref name (e.g. "refs/heads/master") or by a regular expression starting with a caret (^), which is kept as part of the regexp (e.g. "^refs/heads/(master|fix)").
FIXVERSION can be specified as a complete JIRA version name (e.g. "1.2.3") or by a regular expression starting with a caret (^), which is kept as part of the regexp (e.g. "^1\.2").
As a special feature, if BRANCH is a regular expression containing capture groups, then every occurrence of the substring $+ in FIXVERSION, if any, is replaced by the text matched by the last capture group in BRANCH. (Hint: Perl's $+ variable is defined as "The text matched by the last bracket of the last successful search pattern.") If FIXVERSION is also a regular expression, the $+ are replaced by the text properly escaped so that it matches literally.
$+
Commits that do not affect any BRANCH are accepted by default.
So, suppose you have this configuration:
[githooks "checkjira"] fixversion = refs/heads/master future fixversion = ^refs/heads/(\d+\.\d+)\. ^$+
Then, commits affecting the master branch must cite issues assigned to the future version. Also, commits affecting any branch which name begins with a version number (e.g. 1.0.3) be assigned to the corresponding JIRA version (e.g. 1.0).
1.0.3
By default, the committer can reference any valid JIRA issue. Setting this value to true requires that the user doing the push/commit (as specified by the userenv configuration variable) be the current issue's assignee.
userenv
If the above checks aren't enough you can use this option to define a custom code to check your commits. The code may be specified directly as the option's value or you may specify it indirectly via the filename of a script. If the option's value starts with "file:", the remaining is treated as the script filename, which is executed by a do command. Otherwise, the option's value is executed directly by an eval. Either way, the code must end with the definition of a routine, which will be called once for each commit with the following arguments:
GIT
The Git repository object used to grok information about the commit.
COMMITID
The SHA-1 id of the Git commit. It is undef in the commit-msg hook, because there is no commit yet.
commit-msg
JIRA
The JIRA::REST object used to talk to the JIRA server.
Note that up to version 0.047 of Git::Hooks::CheckJira this used to be a JIRA::Client object, which uses JIRA's SOAP API which was deprecated on JIRA 6.0 and won't be available anymore on JIRA 7.0.
If you have code relying on the JIRA::Client module you're advised to rewrite it using the JIRA::REST module. As a stopgap measure you can disregard the JIRA::REST object and create your own JIRA::Client object.
ISSUES...
The remaining arguments are RemoteIssue objects representing the issues being cited by the commit's message.
The subroutine should return a boolean value indicating success. Any errors should be produced by invoking the Git::Repository::Plugin::GitHooks::error method.
If the subroutine returns undef it's considered to have succeeded.
If it raises an exception (e.g., by invoking die) it's considered to have failed and a proper message is produced to the user.
If this option is set and the post-receive hook is enabled, for every pushed commit, every cited JIRA issue receives a comment showing the result of the git show --stat COMMIT command. This is meant to notify the issue assignee of commits referring to the issue.
git show --stat COMMIT
Note that the user with which Git::Hooks authenticates to JIRA must have permission to add comments to the issues or an error will be logged. However, since this happens after the push, the result of the operation isn't affected.
You must specify the VISIBILITY of the comments in one of these ways.
role:NAME
In this case, NAME must be the name of a JIRA role, such as Administrators, Developers, or Users.
Administrators
Developers
Users
group:NAME
In this case, NAME must be the name of a JIRA group.
all
In this case, the visibility isn't restricted at all.
By default, all commits are checked. You can exempt merge commits from being checked by setting this option to true.
This option is DEPRECATED. Please, use a JQL expression such the following to restrict by project key:
project IN (ABC, GIT)
By default, the committer can reference any JIRA issue in the commit log. You can restrict the allowed keys to a set of JIRA projects by specifying a JIRA project key to this option. You can allow more than one project by specifying this option multiple times, once per project key.
If you set this option, then any cited JIRA issue that doesn't belong to one of the specified projects causes an error.
This option is DEPRECATED. Please, use a JQL expression such the following to restrict by status:
status IN (Open, "In Progress")
By default, it doesn't matter in which status the JIRA issues are. By setting this multi-valued option you can restrict the valid statuses for the issues.
This option is DEPRECATED. Please, use a JQL expression such the following to restrict by issue type:
issuetype IN (Bug, Story)
By default, it doesn't matter what type of JIRA issues are cited. By setting this multi-valued option you can restrict the valid issue types.
Git::Repository
JIRA::Client
git-jira-hook
This script is heavily inspired (and sometimes derived) from Joyjit Nath's hook.
JIRA SOAP API deprecation notice
Yet Another Commit Checker
This Bitbucket plugin implements some nice checks with JIRA, from which we stole some ideas.
Gustavo L. de M. Chaves <gnustavo@cpan.org>
This software is copyright (c) 2018 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.