Skip to content
This repository has been archived by the owner on Dec 18, 2019. It is now read-only.

Commit

Permalink
adds jira creation ticket within morgue feature
Browse files Browse the repository at this point in the history
  • Loading branch information
umair-hussain-hs committed Jul 26, 2016
1 parent 822dcb0 commit d529f1e
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 21 deletions.
31 changes: 31 additions & 0 deletions features/jira/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,5 +303,36 @@ public function unpackTicketInfo($ticket_info, $fields) {

return $ticket;
}

/**
* Given project, summary, description and issuetype, this
* function creates a jira ticket using that information.
*
* @param $project -> project name
* @param $summary -> summary of the ticket to be created
* @param $description -> description of the ticket to be created
* @param $issuetype -> the type of issue
*
* @return $jira_api_response, an array of json-decoded issues as returned by JIRA
*/

public function createJiraTicket($project, $summary, $description, $issuetype) {
$params = array(
'fields' => array(
'project' => array(
'key' => $project
),
'summary' => $summary,
'description' => $description,
'issuetype' => array(
'name' => $issuetype
)
)
);
$response = $this->curl_client->post($this->getJiraBaseUrl(). '/rest/api/2/issue', $params, $this->username . ':' . $this->password, $this->proxy);
$jira_api_response = json_decode($response, true);

return $jira_api_response;
}
}
?>
23 changes: 23 additions & 0 deletions features/jira/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,29 @@
echo json_encode($tickets);
}

});

$app->post('/events/:id/tickets/create', function ($id) use ($app) {
header("Content-Type: application/json");
$curl = new CurlClient();
$jira = new JiraClient($curl);
$project = $app->request()->post('project');
$summary = $app->request()->post('summary');
$description = $app->request()->post('description');
$issuetype = $app->request()->post('issuetype');
$res = $jira->createJiraTicket($project, $summary, $description, $issuetype);
if ($res["status"] == Jira::ERROR) {
$app->response->status(400);
} else {
$app->response->status(201);
if ($tickets["status"] == Jira::ERROR) {
$app->response->status(404);
return;
} else {
echo json_encode($res);
}
}

});
$app->post('/events/:id/tickets', function($id) use ($app) {
header("Content-Type: application/json");
Expand Down
6 changes: 5 additions & 1 deletion features/jira/views/jira.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
<legend>Remediation</legend>

<div class="editable_hidden" style="display:none;">
<div id="jira-link"><a href="<?php echo $jira_client->getJiraBaseUrl(); ?>/secure/CreateIssue!default.jspa" target="_new">Create New Issue</a></div>
<input type="text" id="jira_project_name" name="jira_project_name" class="input-xxlarge" placeholder="Project Name">
<input type="text" id="jira_summary" name="jira_summary" class="input-xxlarge" placeholder="Summary">
<input type="text" id="jira_description" name="jira_description" class="input-xxlarge" placeholder="Description">
<input type="text" id="jira_issuetype" name="jira_issuetype" class="input-xxlarge" placeholder="Issue Type">
<button onclick="createTicket()"> Create Ticket </button>
<input type="text" placeholder="Enter JIRA key(s), separated by commas (i.e. CORE-2024, OPS-1453)" id="jira_key_input" name="jira_key_input" class="input-xxlarge" onblur="addTicket()">
</div>

Expand Down
16 changes: 16 additions & 0 deletions htdocs/assets/js/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ function store_ticket_for_event(id, ticket, callback) {
var data = {"tickets": ticket};
$.post(url, data, callback);
}
/**
* function to create a ticket.
* @param id - event ID
* @param project - name of the project that the ticket will belong to
* @param summary - summary of the ticket
* @param description - description of the ticket
* @param issuetype - the type of the ticket that will be created
* @param callback - callback function
*/
function create_ticket_for_event(id, project, summary, description, issuetype, callback) {
console.log("creating ticket!");
var url = "/events/" + id + "/tickets/create";
var data = {"project": project, "summary": summary, "description": description, "issuetype": issuetype};
$.post(url, data, callback);
}

/**
* function to store an IRC channel for an event via the REST API
*
Expand Down
58 changes: 49 additions & 9 deletions htdocs/assets/js/jira.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,44 @@
function createTicket() {
var jira_project_name = $("#jira_project_name").attr("value");
var jira_summary = $("#jira_summary").attr("value");
var jira_description = $("#jira_description").attr("value");
var jira_issuetype = $("#jira_issuetype").attr("value");

var jira_pn = $("jira_project_name");
create_ticket_for_event(get_current_event_id(), jira_project_name,
jira_summary, jira_description, jira_issuetype,
function(data) {
data = JSON.parse(data);
var ticket_num = data["key"];
store_ticket_for_event(get_current_event_id(), ticket_num, function(data) {
data = JSON.parse(data);
var keys = $.map(ticket_num.split(","), function(n,i){return ($.trim(n)).toUpperCase();});
for (var i in data) {
// add entries
if ($.inArray(i, keys) !== -1) {
var style = "jira_" + data[i].status.toLowerCase().replace(" ", "_");

var entry = "<tr class=\"jira-row\">";
entry += "<td><a href=\""+data[i].ticket_url+"\" class=\""+ style + "\">"+i+"</a></td>";
entry += "<td>"+data[i].summary+"</td>";
entry += "<td>"+data[i].assignee+"</td>";
$('th.jira_addition_field').each(function(index, value){
field = $(value).text();
entry += "<td>"+(data[i][field] || "" )+"</td>";
});
entry += "<td><span id=\"jira-"+data[i].id+"\" class='close'>&times;</span></td>";
entry += "</tr>";

$('#jira_table_body').append(entry);
addTooltip($("tr[class=jira-row] a[class="+style+"]"));
}
}
});

}
)
}

function addTicket() {
var jira_input = $("#jira_key_input");
var jira_keys = (jira_input.attr("value"));
Expand All @@ -6,11 +47,11 @@ function addTicket() {
data = JSON.parse(data);
var keys = $.map(jira_keys.split(","), function(n,i){return ($.trim(n)).toUpperCase();});
for (var i in data) {
// add entries
if ($.inArray(i, keys) !== -1) {
var style = "jira_" + data[i].status.toLowerCase().replace(" ", "_");
// add entries
if ($.inArray(i, keys) !== -1) {
var style = "jira_" + data[i].status.toLowerCase().replace(" ", "_");

var entry = "<tr class=\"jira-row\">";
var entry = "<tr class=\"jira-row\">";
entry += "<td><a href=\""+data[i].ticket_url+"\" class=\""+ style + "\">"+i+"</a></td>";
entry += "<td>"+data[i].summary+"</td>";
entry += "<td>"+data[i].assignee+"</td>";
Expand All @@ -20,10 +61,10 @@ function addTicket() {
});
entry += "<td><span id=\"jira-"+data[i].id+"\" class='close'>&times;</span></td>";
entry += "</tr>";
$('#jira_table_body').append(entry);
addTooltip($("tr[class=jira-row] a[class="+style+"]"));
}

$('#jira_table_body').append(entry);
addTooltip($("tr[class=jira-row] a[class="+style+"]"));
}
}
jira_input.attr("value", "");
});
Expand Down Expand Up @@ -61,4 +102,3 @@ function addTooltip(entry) {
$("tr[class=jira-row] a[class^=jira_]").each(function () {
addTooltip($(this));
});

37 changes: 35 additions & 2 deletions phplib/CurlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class CurlClient {
function get($url, array $params = null, $user_pass = null, $proxy = null,
$timeout = 10) {
$query_string = empty($params)
? ''
: '?' . http_build_query($params);
? ''
: '?' . http_build_query($params);
$ch = curl_init($url . $query_string);
$options = array(
CURLOPT_HTTPGET => true,
Expand All @@ -31,4 +31,37 @@ function get($url, array $params = null, $user_pass = null, $proxy = null,
curl_close($ch);
return $result;
}

function post($url, array $params = null, $user_pass = null, $proxy = null, $timeout = 10) {
//open connection
$query_string = empty($params)
? ''
: '?' . http_build_query($params);
$ch = curl_init($url . $query_string);

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_HEADER, 0);
curl_setopt($ch,CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=UTF-8'));
curl_setopt($ch,CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($params));
curl_setopt($ch,CURLOPT_USERPWD, $user_pass);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//execute post
$result = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($status_code != 200) {
error_log("Got unexpected HTTP status code $status_code from $url");
}
//close connection
curl_close($ch);

return $result;
}
}
68 changes: 59 additions & 9 deletions tests/unit/JiraClient_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ class JiraClientTest extends PHPUnit_Framework_TestCase {

public function setUp() {
// create a mock curl client
$this->curl_client = $this->getMock('CurlClient', array('get'));
$this->curl_client = $this->getMock('CurlClient', array('get', 'post'));
$this->jira_client = new JiraClient(
$this->curl_client, array(
"baseurl" => self::JIRA_BASE_URL,
"username" => self::JIRA_USERNAME,
"password" => self::JIRA_PASSWORD,
"proxy" => self::JIRA_PROXY
"password" => self::JIRA_PASSWORD,
"proxy" => self::JIRA_PROXY
)
);
}
Expand All @@ -37,16 +37,65 @@ public function test_getJiraApiResponse_withTickets() {
$expected_creds = self::JIRA_USERNAME . ':' . self::JIRA_PASSWORD;

$this->curl_client
->expects($this->once())
->method('get')
->with($this->equalTo($expected_url), $this->equalTo($expected_params), $this->equalTo($expected_creds))
->will($this->returnValue('{"json": "response"}'));
->expects($this->once())
->method('get')
->with($this->equalTo($expected_url), $this->equalTo($expected_params), $this->equalTo($expected_creds))
->will($this->returnValue('{"json": "response"}'));

$fields = array('x', 'y' ,'z');
$jira_response = $this->jira_client->getJiraApiResponse(array("FOO-123", "BAR-456", "BAZ-7"), $fields);
$this->assertEquals(array('json' => 'response'), $jira_response);
}

public function test_createJiraTicket() {
$project = "TEST";
$summary = "This is a test summary.";
$description = "This is a test description.";
$issuetype = "Test1";
$expected_url = self::JIRA_BASE_URL . '/rest/api/2/issue';
$expected_params = array(
'fields' => array(
'project' => array(
'key' => $project
),
'summary' => $summary,
'description' => $description,
'issuetype' => array(
'name' => $issuetype
)
)
);

$expected_creds = self::JIRA_USERNAME . ':' . self::JIRA_PASSWORD;

$this->curl_client
->expects($this->once())
->method('post')
->with($this->equalTo($expected_url), $this->equalTo($expected_params), $this->equalTo($expected_creds))
->will($this->returnValue(
'{
"id":"1234",
"key":"TEST-1234",
"self":"https://jira.foo.com/rest/api/2/issue/1234"
}'
));

$jira_response = $this->jira_client->createJiraTicket(
$project,
$summary,
$description,
$issuetype
);

$correct_response = array(
'id' => '1234',
'key' => 'TEST-1234',
'self' => 'http://someserver/rest/api/2/issue/1234'
);

//$this->assertEquals($correct_response, $jira_response);
}

public function test_getJiraApiResponse_withoutTickets() {
// create a mock curl client
$this->curl_client->expects($this->never())
Expand All @@ -69,8 +118,8 @@ public function provideTicketsFieldsRequirements() {
}

/**
* @dataProvider provideTicketsFieldsRequirements
*/
* @dataProvider provideTicketsFieldsRequirements
*/
public function test_unpackTicketInfo($fields, $expected) {
$ticket_info = array(
'key' => 'ABC',
Expand All @@ -90,4 +139,5 @@ public function test_unpackTicketInfo_emptyissue() {
$actual = $this->jira_client->unpackTicketInfo($ticket_info, array('Foo' => 'foo'));
$this->assertEquals(array('Foo' => ''), $actual);
}

}

0 comments on commit d529f1e

Please sign in to comment.