Вы находитесь на странице: 1из 10

Connect Jira Tempo Rest Api with PHP

Jira is well known team planning and management application. Thousands of companies using
Jira to assign work and tean activities. Jira provides facilities to add plugin as per your
requirement. Jira Tempo is one of famous Plugin to get the User, Task and Time-line Reports.
Tempo Restful Api build in Java. So we have build our Jira Class in PHP to use Tempo restful
apis..
Steps for connecting jira tempo rest api with php
1. Make sure CURL is enable in your server or php.ini file (php has phpinfo function to check
server setting). Check curl is working with below php code.
function isCurlInstalled() {
if (in_array ('curl', get_loaded_extensions())) {
return true;
}
else {
return false;
}
}
if (isCurlInstalled()) {
echo "installed";
} else {
echo "NOT installed";
}
2. Create Jira Class (You can create as per your convenient), 3 Private method and CURL
functions to connect Jira
class Jira {
private static $url = "http://192.168.0.59/"; // Tempo URL setup in API TOKEN
private static $credential = 'ezeelivetechnologies:test123'; // Jira Username and Password
private static $tempoApiToken = '15ezeelive-7f3f-40f2-a95f-4521918ba183'; // Tempo Token Key

// Static function to Get Data
private static function getCURL($rest_url, $https = FALSE, $return_format = FALSE, $full_url = FALSE) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
if (empty($full_url))
curl_setopt($ch, CURLOPT_URL, Jira::$url . $rest_url);
else
curl_setopt($ch, CURLOPT_URL, $rest_url);

curl_setopt($ch, CURLOPT_USERPWD, Jira::$credential);

if (!empty($https)) {
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
}
if (!empty($return_format) && $return_format == 'json')
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}

// Static function to POST Data

private static function postCURL($rest_url, $data = FALSE, $https = FALSE, $return_format = FALSE) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_URL, Jira::$url . $rest_url);
curl_setopt($ch, CURLOPT_USERPWD, Jira::$credential);

if (!empty($https)) {
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
}
if (!empty($return_format) && $return_format == 'json')
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

curl_setopt($ch, CURLOPT_POST, 1);
if (!empty($data))
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}

public static function getAllJiraProjectsKey() {
$return_data = array();
$rest_url = "rest/api/2/project";
$data = Jira::getCURL($rest_url, FALSE, $return_format = 'json');
if (!empty($data)) {
$data = json_decode($data, TRUE);
foreach ($data as $k => $dt) {
$return_data[] = $dt['key'];
}
}
return $return_data;
}

//......
//......
}
3. Now its time to get all the User list associated with Your Jira Project
public static function getJiraProjectUsers($proj_key) {

$return_data = array();
$rest_url = "rest/api/2/project/{$proj_key}/role";
$data = Jira::getCURL($rest_url, FALSE, $return_format = 'json');

if (!empty($data)) {
$data = json_decode($data, TRUE);
foreach ($data as $k => $dt) {
if (!is_array($dt)) {
$role_data = json_decode(Jira::getCURL($dt, FALSE, $return_format = 'json', TRUE), true);
if (!empty($role_data['actors'])) {
foreach ($role_data['actors'] as $v) {
if(!empty($v['displayName'])){
$return_data[] = array(
'disp_name' => $v['displayName'],
'uid' => !empty($v['id']) ?$v['id']:'',
'jira_auth' => $jauth,
'usr_name' => $v['name'],
'rates' => $rates,
'bill_ty' => $bill_type
);
}
}
}
}
}
}
return $return_data;
}
4. Once you get all the User Associated with Your Jira Project. You can get all the Worklog by
Jira User or Project Key
public static function getJiraUserWorklogs($jiraUserName,$fromDate,$toDate) { // By Jira UserName
$return_data = array();
$rest_url = "plugins/servlet/tempo-
getWorklog/?addUserDetails=true&dateFrom={$data['sdate']}&dateTo={$data['edate']}&format=xml&diffOnly=fal
se&tempoApiToken=" . Jira::$tempoApiToken . "&addIssueDetails=true&userName={$jiraUserName}";

$return_data = Jira::getCURL($rest_url);
return $return_data;
}

public static function getJiraProjectWorklogs($prjkey,$fromDate,$toDate) {
$return_data = array();
$rest_url = "plugins/servlet/tempo-
getWorklog/?addUserDetails=true&dateFrom={$data['sdate']}&dateTo={$data['edate']}&format=xml&diffOnly=fal
se&tempoApiToken=" . Jira::$tempoApiToken . "&addIssueDetails=true&projectKey={$prjkey}";

$return_data = Jira::getCURL($rest_url);
return $return_data;
}
5. Final the complete class is look like
class Jira {
private static $url = "http://192.168.0.59/"; // Tempo URL setup in API TOKEN
private static $credential = 'ezeelivetechnologies:test123'; // Jira Username and Password
private static $tempoApiToken = '15ezeelive-7f3f-40f2-a95f-4521918ba183'; // Tempo Token Key

// Static function to Get Data
private static function getCURL($rest_url, $https = FALSE, $return_format = FALSE, $full_url = FALSE) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
if (empty($full_url))
curl_setopt($ch, CURLOPT_URL, Jira::$url . $rest_url);
else
curl_setopt($ch, CURLOPT_URL, $rest_url);

curl_setopt($ch, CURLOPT_USERPWD, Jira::$credential);

if (!empty($https)) {
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
}
if (!empty($return_format) && $return_format == 'json')
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}

// Static function to POST Data

private static function postCURL($rest_url, $data = FALSE, $https = FALSE, $return_format = FALSE) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_URL, Jira::$url . $rest_url);
curl_setopt($ch, CURLOPT_USERPWD, Jira::$credential);

if (!empty($https)) {
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
}
if (!empty($return_format) && $return_format == 'json')
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

curl_setopt($ch, CURLOPT_POST, 1);
if (!empty($data))
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}

public static function getAllJiraProjectsKey() {
$return_data = array();
$rest_url = "rest/api/2/project";
$data = Jira::getCURL($rest_url, FALSE, $return_format = 'json');
if (!empty($data)) {
$data = json_decode($data, TRUE);
foreach ($data as $k => $dt) {
$return_data[] = $dt['key'];
}
}
return $return_data;
}

public static function getJiraProjectUsers($proj_key) {

$return_data = array();
$rest_url = "rest/api/2/project/{$proj_key}/role";
$data = Jira::getCURL($rest_url, FALSE, $return_format = 'json');

if (!empty($data)) {
$data = json_decode($data, TRUE);
foreach ($data as $k => $dt) {
if (!is_array($dt)) {
$role_data = json_decode(Jira::getCURL($dt, FALSE, $return_format = 'json', TRUE), true);
if (!empty($role_data['actors'])) {
foreach ($role_data['actors'] as $v) {
if(!empty($v['displayName'])){
$return_data[] = array(
'disp_name' => $v['displayName'],
'uid' => !empty($v['id']) ?$v['id']:'',
'jira_auth' => $jauth,
'usr_name' => $v['name'],
'rates' => $rates,
'bill_ty' => $bill_type
);
}
}
}
}
}
}
return $return_data;
}

public static function getJiraUserWorklogs($jiraUserName,$fromDate,$toDate) { // By Jira UserName
$return_data = array();
$rest_url = "plugins/servlet/tempo-
getWorklog/?addUserDetails=true&dateFrom={$data['sdate']}&dateTo={$data['edate']}&format=xml&diffOnly=fal
se&tempoApiToken=" . Jira::$tempoApiToken . "&addIssueDetails=true&userName={$jiraUserName}";

$return_data = Jira::getCURL($rest_url);
return $return_data;
}

public static function getJiraProjectWorklogs($prjkey,$fromDate,$toDate) {
$return_data = array();
$rest_url = "plugins/servlet/tempo-
getWorklog/?addUserDetails=true&dateFrom={$data['sdate']}&dateTo={$data['edate']}&format=xml&diffOnly=fal
se&tempoApiToken=" . Jira::$tempoApiToken . "&addIssueDetails=true&projectKey={$prjkey}";

$return_data = Jira::getCURL($rest_url);
return $return_data;
}
}
If you have any query, suggestion or help on this jira tempo rest api, please visit
http://www.ezeelive.com/


Posted By : Ezeelive Technologies

Вам также может понравиться