Skip to content

Instantly share code, notes, and snippets.

@brunoric
Last active November 17, 2015 11:54
Show Gist options
  • Save brunoric/f3a04ec14962957a6654 to your computer and use it in GitHub Desktop.
Save brunoric/f3a04ec14962957a6654 to your computer and use it in GitHub Desktop.
Jira Commit

Jira Commit

Instalation

curl -sS https://gist.githubusercontent.com/brunoric/f3a04ec14962957a6654/raw/0879f8158f4b82a635e27fc61a22958a78503510/installer.sh | bash

If you experience any permission denied error try call bash with sudo:

curl -sS https://gist.githubusercontent.com/brunoric/f3a04ec14962957a6654/raw/0879f8158f4b82a635e27fc61a22958a78503510/installer.sh | sudo bash

Configuration

Export these variables, putting it in your .profile or .bashrc or .zshrc, to properly configure this Git extension:

export JIRACOMMIT_JIRA_DOMAIN=https://yourjira.yourdomain.com
export JIRACOMMIT_JIRA_TASK_PREFIX=PREFIX-

Login information

Your credentials will be promped but only for one time per session, the script uses cURL cookie strategy to manage your credentials.

#!/usr/bin/php
<?php
/**
* @file
* Jira commit message template generator.
*
* @version 0.1.0
*
* @author brunoric <brunoric@gmail.com>
*/
/**
* Hides terminal's text.
*/
function hide_term() {
if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
system('stty -echo');
}
}
/**
* Restore terminal's text.
*/
function restore_term() {
if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
system('stty echo');
}
}
/**
* Gets user input.
*/
function get_input($message, $hidden = FALSE) {
echo $message;
if ($hidden) {
hide_term();
}
$data = rtrim(fgets(STDIN), PHP_EOL);
if ($hidden) {
restore_term();
}
return $data;
}
/**
* Get user credentials.
*/
function get_credentials() {
$username = get_input('Enter your username: ');
$password = get_input('Enter your password: ', TRUE);
return array(
'username' => $username,
'password' => $password,
);
}
/**
* Get JSON data.
*/
function get_json($url, $auth = FALSE) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/jiracommit.cookie');
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/jiracommit.cookie');
if ($auth && !is_file('/tmp/jiracommit.cookie')) {
$credentials = get_credentials();
curl_setopt($ch, CURLOPT_USERPWD, $credentials['username'] . ':' . $credentials['password']);
}
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
/**
* Parse task summary.
*/
function parse_summary($json) {
$jira = json_decode($json, FALSE);
if (!empty($jira->fields->issuelinks[0]->inwardIssue)) {
return $jira->fields->issuelinks[0]->inwardIssue->fields->summary;
}
if (!empty($jira->fields->issuelinks[0]->outwardIssue)) {
return $jira->fields->issuelinks[0]->outwardIssue->fields->summary;
}
return 'Summary not found.';
}
/**
* Create commit file message template.
*/
function create_template($task_id, $task_summary) {
$task_id_length = strlen($task_id) + 3;
$task_summary_length = 72 - $task_id_length;
$task_summary_croppped = substr($task_summary, 0, $task_summary_length);
$content = "[$task_id] $task_summary_croppped\n";
$content .= "\n";
$content .= 'Work description.';
$filename = '/tmp/jiracommit.template';
file_put_contents($filename, $content);
return $filename;
}
/**
* Initiate the script.
*/
function main($argv) {
$domain = getenv('JIRACOMMIT_JIRA_DOMAIN');
if (empty($domain)) {
echo 'You need to properly export the VARIABLE with the JIRA domain.' . PHP_EOL;
echo 'Example: "export JIRACOMMIT_JIRA_DOMAIN=https://yourjira.yourdomain.com".' . PHP_EOL;
exit(1);
}
if (empty($argv[1])) {
echo 'You need to pass a valid task ID as parameter.' . PHP_EOL;
echo 'Example: "git jiracommit PREFIX-1234".' . PHP_EOL;
exit(1);
}
$task_id = $argv[1];
$task_prefix = getenv('JIRACOMMIT_JIRA_TASK_PREFIX');
if (!empty($task_prefix)) {
$task_id = $task_prefix . $task_id;
}
$url = $domain . '/rest/api/2/issue/' . $task_id;
$json = get_json($url, TRUE);
$task_summary = parse_summary($json);
$commit_file_template = create_template($task_id, $task_summary);
$output = system("git commit --template=$commit_file_template > `tty`");
unlink($commit_file_template);
}
main($argv);
#!/bin/bash
curl -sS https://gist.githubusercontent.com/brunoric/f3a04ec14962957a6654/raw/c4baa0e8fcda54cd3531e2256f8c325492360ac9/git-jiracommit > /usr/local/bin/git-jiracommit;
chmod +x /usr/local/bin/git-jiracommit;
php_executable=$(which php);
if [ "$(uname)" == "Darwin" ]; then
sed -i '' 's#\/usr\/bin\/php#'$php_executable'#g' /usr/local/bin/git-jiracommit
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
sed -i 's#\/usr\/bin\/php#'$php_executable'#g' /usr/local/bin/git-jiracommit
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment