Home

Awesome

jitic - JIRA Ticket Checker

Build Status

jitic - the JIRA Ticket Checker - checks the existence of one or more issues in the JIRA issue tracker. If the issue exists we will shutdown with exit code 0. Otherwise with 1.

jitic - the JIRA Ticket Checker

  1. Usage
    1. Examples
  2. Use cases
    1. Subversion "pre-commit" hook
    2. Git "pre-receive" hook
  3. License

Usage

Usage of ./jitic:
  -issues string
        Message to retrieve the issues from.
  -pass string
        JIRA Password.
  -stdin
        If set to true you can stream "-issues" to stdin instead of an argument. If set "-issues" will be ignored.
  -url string
        JIRA instance URL (format: scheme://[username[:password]@]host[:port]/).
  -user string
        JIRA Username.
  -verbose
        If activated more information will be written to stdout .
  -version
        Outputs the version number and exits.

Examples

Check if issue MESOS-3136 exists in https://issues.apache.org/jira/ from parameter:

$ ./jitic -url="https://issues.apache.org/jira/" -issues="MESOS-3136 - Fix command health check"; echo "Exit code: $?"
Exit code: 0

Check if issue MESOS-3136 exists in https://issues.apache.org/jira/ from stdin.

$ echo "MESOS-3136 - Fix command health check" | ./jitic -url="https://issues.apache.org/jira/" -stdin; echo "Exit code: $?"
Exit code: 0

Check if a fake issue MESOS-123456 exists in https://issues.apache.org/jira/ from parameter:

$ ./jitic -url="https://issues.apache.org/jira/" -issues="MESOS-123456 - Not existing issue" -verbose; echo "Exit code: $?"
2017/11/28 15:41:26 JIRA Request for issue MESOS-123456 returned 404 Not Found (404)

Use cases

Subversion "pre-commit" hook

See Implementing Repository Hooks and pre-commit.

How a pre-commit hook can look like:

#!/bin/sh

REPOS="$1"
TXN="$2"

# Get the commit message
SVNLOOK=/usr/bin/svnlook
COMMIT_MSG=$($SVNLOOK log -t "$TXN" "$REPOS")

JITIC=/usr/bin/jitic
JIRA_URL="https://jira.example.org/"
JIRA_USERNAME="JIRA-API"
JIRA_PASSWORD="SECRET-PASSWORD"

# Exit on all errors.
set -e

# Auth against JIRA and check if the issue(s) exists
$JITIC -url="$JIRA_URL" -user="$JIRA_USERNAME" -pass="$JIRA_PASSWORD" -issues="$COMMIT_MSG"

# All checks passed, so allow the commit.
exit 0

Pro-Tip: Set the variables JIRA_USERNAME and JIRA_PASSWORD in a seperate file and import this file via source into the hook. With this you can store the pre-commit hook itself in git + deploy it with configuration management.

Git "pre-receive" hook

See Customizing Git - Git Hooks and A reasonable git pre-receive-hook and Can git pre-receive hooks evaulate the incoming commit?.

How a pre-receive hook can look like:

#!/bin/sh

GIT=/usr/local/bin/git
JITIC=/usr/bin/jitic
JIRA_URL="https://jira.example.org/"
JIRA_USERNAME="JIRA-API"
JIRA_PASSWORD="SECRET-PASSWORD"

FAIL=""

validate_ref()
{
	# Arguments
	oldrev=$($GIT rev-parse $1)
	newrev=$($GIT rev-parse $2)
	refname="$3"

	# $oldrev / $newrev are commit hashes (sha1) of git
	# $refname is the full name of branch (refs/heads/*) or tag (refs/tags/*)
	# $oldrev could be 0s which means creating $refname
	# $newrev could be 0s which means deleting $refname

	case "$refname" in
		refs/heads/*)
			# We currently only care about updating branches.
			# If you want to take care for deleting branched check this:
			#   if [ 0 -ne $(expr "$newrev" : "0*$") ]; then
			#       # Your code here
			#   fi

			# Pushing a new branch
			if [ 0 -ne $(expr "$oldrev" : "0*$") ]; then
				COMMITS_TO_CHECK=$($GIT rev-list $newrev --not --branches=*)

			# Updating an existing branch
			else
				COMMITS_TO_CHECK=$($GIT rev-list $oldrev..$newrev)
			fi

			# If we push an new, but empty branch we can exit early.
			# In this case there are no commits to check.
			if [ -z "$COMMITS_TO_CHECK" ]; then
				return
			fi

			# Get all commits, loop over and check if there are valid JIRA tickets
			while read REVISION ; do
				COMMIT_MESSAGE=$($GIT log --pretty=format:"%B" -n 1 $REVISION)

				$JITIC -url="$JIRA_URL" -user="$JIRA_USERNAME" -pass="$JIRA_PASSWORD" -issues="$COMMIT_MESSAGE"
				if [ $? != 0 ]; then
					FAIL=1
					echo >&2 "... in revision $REVISION"
				fi
			done <<< "$COMMITS_TO_CHECK"

			return
			;;
		refs/tags/*)
			# Support for tags (new / delete) needs to be done.
			# Things we need to check:
			#   * Get all commits from the new tag that are NOT checked yet
			#     (checked means by jitic). Something like commits which are
			#     not pushed yet, but the tag was pushed.
			# I think we don`t need to care about deleted branches yet.
			;;
		*)
			FAIL=1
			echo >&2 ""
			echo >&2 "*** pre-receive hook does not understand ref $refname in this repository. ***"
			echo >&2 "*** Contact the repository administrator. ***"
			echo >&2 ""
			;;
	esac
}

while read OLD_REVISION NEW_REVISION REFNAME
do
	validate_ref $OLD_REVISION $NEW_REVISION $REFNAME
done

if [ -n "$FAIL" ]; then
	exit $FAIL
fi

Pro-Tip: Set the variables JIRA_USERNAME and JIRA_PASSWORD in a seperate file and import this file via source into the hook. With this you can store the pre-commit hook itself in git + deploy it with configuration management.

License

This project is released under the terms of the MIT license.