How to Auto Create Issues in Jira From PHP

Good morning again everyone, in an effort to always increase productivity and reduce friction I have a new weapon in my workflow arsenal. To gain some context regarding this post here is some background. We use Jira at Gravity for tracking issues and bugs. Since I'm not always on VPN or have access to our network managing my todos has been cumbersome. I've tried every Todo app out there and always fail to use them for more than 2 days.

I finally saw a great article on just using a simple Todo.txt file in your Dropbox folder and working from that. It's been a dream and working out great. I can use all my fav shell commands and have amazing Quicksilver integration that I'll show in a little video soon.

Anyway, the final joy was to be able to have a cron job run every hour and automatically create jira issues from my todo file for any item that has a #jira tag. That helps me sync with Jira when I want others to know what I have on my plate at the moment. My script that calls this PHP file strips out the #jira tag after it creates the issue

Here is a typical sample of my todo list

2 @work remove errors from error log that is causing email messages #jira
1 @work need to write a script that watches something and does something with that info
2 @home take out the trash
2 @work do some work for the #secret project that I have

with the Jira script below I can do fun things like

grep "#secret" ~/Dropbox/Todo.txt | xargs -0 ~/scripts/jiraissue.php

which would log all items marked as secret project into Jira so I can start keeping track of them there. My cron job looks for anything marked with #jira and inserts it.


here is the youtube video



Here is the jira connection script I use (with help from this code from Chris: http://confluence.atlassian.com/display/JIRACOM/Automating+JIRA+operations+via+wget )



#!/opt/local/bin/php
/**
* automatically log jira tasks from the command line using php/curl
* I have a todo.txt file and anything marked with #jira gets created as a jira issue
* run as:
* php jiraissue.php "This is a new task to do"
*/

$task = urlencode(@$argv[1]);
if($task == "") {
die('need a task por favor!');
}
echo $task."\n";

// set these in your bash profile or other environment locations or hardcode em!
$username=getenv("JIRA_USER");
$password=getenv("JIRA_PASS");
$reporter='jplush';
$assignee='jplush';
$project='10001'; // this is the insights project ID

// do the login routine
$curl = curl_init();
$loginUrl = "http://jira/login.jsp?os_username=" . $username . "&os_password=" . $password . "&os_cookie=true";
curl_setopt($curl,CURLOPT_URL, $loginUrl );
curl_setopt($curl,CURLOPT_POST, true );
curl_setopt($curl,CURLOPT_COOKIEFILE, '/tmp/cookie' );
curl_setopt($curl,CURLOPT_COOKIEJAR, '/tmp/cookiejar' );
curl_setopt($curl,CURLOPT_RETURNTRANSFER, true );
curl_setopt($curl,CURLOPT_FOLLOWLOCATION, false );
curl_setopt($curl,CURLOPT_NOBODY, true );
curl_exec( $curl );

// grab the xsrf token from the create issue screen
curl_setopt($curl,CURLOPT_NOBODY, false );
curl_setopt($curl,CURLOPT_URL, "http://jira/secure/CreateIssue.jspa?pid=10001&issuetype=3" );
$createPage = curl_exec( $curl );
$results = preg_match('/atlassian-token" content="([^"]+)"/ism', $createPage, $matches);
$token = $matches[1];

// pass the token and your arguments to create the new issue
$postUrl = "http://jira/secure/CreateIssueDetails.jspa?atl_token={$token}&pid={$project}&issuetype=1&summary={$task}";
$postUrl .= "&assignee={$assignee}&reporter={$reporter}&priority=3&isCreateIssue=true&hasWorkStarted=false&Create=Create";
curl_setopt($curl,CURLOPT_RETURNTRANSFER, false );
curl_setopt($curl,CURLOPT_URL, $postUrl );
curl_exec( $curl );

curl_close( $curl );

Ready for More?

Follow Me @jimplush