Skip to content

Commit

Permalink
Merge pull request #47 from dhananjay92/dhananjay-gsoc-dev
Browse files Browse the repository at this point in the history
Solve [Issue#26]
  • Loading branch information
nijel committed Jun 24, 2014
2 parents cb405e2 + 0937723 commit 3841ead
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 25 deletions.
32 changes: 32 additions & 0 deletions app/Controller/Component/SourceForgeApiComponent.php
Expand Up @@ -98,4 +98,36 @@ public function createTicket($project, $data) {
return $client->post($accessToken['key'], $accessToken['secret'],
"https://sourceforge.net/rest/p/$project/bugs/new", $data);
}

/**
* Submits a create comment request to the SourceForge.net api. It uses the access
* token that must be set as a property on this component, through
* SourceForgeApiComponent->accessToken
*
* @param String $project the project name of the SourceForge.net project to submit
* the ticket to.
* @param Array $data the ticket data to submit.
* @param Array $ticket_id the bug ticket id on which to comment.
* @return Array the response returned by SourceForge.net
*/
public function createComment($project, $ticket_id, $data) {
$client = $this->createClient();
$accessToken = $this->accessToken;
$url = "https://sourceforge.net/rest/p/" . $project . "/bugs/" . $ticket_id ."/";

// get the discussion thread URL
$ticketInfo = $client->post($accessToken['key'], $accessToken['secret'],
$url);

if(in_array($ticketInfo->code, array('404', '403'))) {
return $ticketInfo;
}

$ticketInfo->body = json_decode($ticketInfo->body, true);

// add the comment on that thread.
$url = $ticketInfo->body['ticket']['discussion_thread_url'] . 'new';
return $client->post($accessToken['key'], $accessToken['secret'],
$url, $data);
}
}
130 changes: 108 additions & 22 deletions app/Controller/SourceForgeController.php
Expand Up @@ -70,34 +70,60 @@ public function create_ticket($reportId) {
$data = $this->_getTicketData($reportId);
$response = $this->SourceForgeApi->createTicket(
Configure::read('SourceForgeProjectName'), $data);
if ($response->code[0] === "3") {
// success
preg_match("<rest/p/.*/bugs/(\d+)/>",
$response->headers['Location'], $matches);
$this->Report->read(null, $reportId);
$this->Report->save(array('sourceforge_bug_id' => $matches[1]));

$this->Session->setFlash('Source forge ticket has been created for this'
. ' report', "default", array("class" => "alert alert-success"));
if ($this->_handleSFResponse($response, 1, $reportId)) {
$this->redirect(array('controller' => 'reports', 'action' => 'view',
$reportId));
} else if ($response->code === "403") {
$this->Session->setFlash(
"Unauthorised access to SourceForge ticketing system. SourceForge"
. " credentials may be out of date. Please check and try again"
. " later.", "default", array("class" => "alert alert-error"));
} else {
//fail
$response->body = json_decode($response->body, true);
CakeLog::write('sourceforge', 'Submission for sourceforge ticket may have failed.',
'sourceforge');
CakeLog::write('sourceforge', 'Response dump:', 'sourceforge');
CakeLog::write('sourceforge', print_r($response["raw"], true), 'sourceforge');
$this->Session->setFlash($this->_getErrors( $response->body), "default",
array("class" => "alert alert-error"));
}
}

/**
* Links error report to existing bug ticket on SF.net
*
*/
public function link_ticket($reportId) {
if (!$reportId) {
throw new NotFoundException(__('Invalid reportId'));
}

$report = $this->Report->findById($reportId);
if (!$report) {
throw new NotFoundException(__('Invalid Report'));
}

$ticket_id = $this->request->query['ticket_id'];
if(!$ticket_id) {
throw new NotFoundException(__('Invalid Ticket ID!!'));
}

$incident = $this->Report->Incident->findByReportId($reportId);
$exception_type = ($incident['Incident']['exception_type']) ? ('php') : ('js');

// "formatted" text of the comment.
$commentText = "Param | Value "
. "\n -----------|--------------------"
. "\n Error Type | " . $report['Report']['error_name']
. "\n Error Message |" . $report['Report']['error_message']
. "\n Exception Type |" . $exception_type
. "\n Link | [Report#"
. $reportId
."]("
. Router::url('/reports/view/'.$reportId,true)
.")"
. "\n\n*This comment is posted automatically by phpMyAdmin's "
. "[error-reporting-server](http://reports.phpmyadmin.net).*";

$response = $this->SourceForgeApi->createComment(
Configure::read('SourceForgeProjectName'),
$ticket_id,
array('text' => $commentText)
);

$this->_handleSFResponse($response, 2, $reportId, $ticket_id);
$this->redirect(array('controller' => 'reports', 'action' => 'view',
$reportId));
}

protected function _getTicketData($reportId) {
$data = array(
'ticket_form.summary' => $this->request->data['Ticket']['summary'],
Expand Down Expand Up @@ -151,4 +177,64 @@ protected function _augmentDescription($description, $reportId) {
. "[#" . $this->Report->id . "](" . $this->Report->getUrl()
. ") on the phpmyadmin error reporting server.";
}

/**
* Sourceforge Response Handler
* @param Object $response the response returned by sourceforge API
* @param Integer $type type of response. 1 for create_ticket, 2 for link_ticket
* @param Integer $report_id report id.
* @param Integer $ticket_id ticket id, required for link tivket only.
*
* @return Boolean value. True on success. False on any type of failure.
*/
protected function _handleSFResponse($response, $type, $report_id, $ticket_id = 1)
{
if (!in_array($type, array(1,2))) {
throw new InvalidArgumentException('Invalid Argument "$type".');
}

if ($response->code[0] === "3") {
// success
if ($type == 1) {
preg_match("<rest/p/.*/bugs/(\d+)/>",
$response->headers['Location'], $matches);
$ticket_id = $matches[1];
}

$this->Report->read(null, $report_id);
$this->Report->save(array('sourceforge_bug_id' => $ticket_id));

if ($type == 2) {
$msg = 'Source forge ticket has been linked with this report.';
} else {
$msg = 'Source forge ticket has been created for this report.';
}
$this->Session->setFlash($msg, "default", array("class" => "alert alert-success"));
return true;
} else if ($response->code === "403") {
$this->Session->setFlash(
"Unauthorised access to SourceForge ticketing system. SourceForge"
. " credentials may be out of date. Please check and try again"
. " later.", "default", array("class" => "alert alert-error"));
return false;
} else if ($response->code === "404"
&& $type == 2
) {
$this->Session->setFlash(
"Bug Ticket not found on SourceForge."
. " Are you sure the ticket number is correct?!! Please check and try again",
"default", array("class" => "alert alert-error"));
return false;
} else {
//fail
$response->body = json_decode($response->body, true);
CakeLog::write('sourceforge', 'Submission for sourceforge ticket may have failed.',
'sourceforge');
CakeLog::write('sourceforge', 'Response dump:', 'sourceforge');
CakeLog::write('sourceforge', print_r($response["raw"], true), 'sourceforge');
$this->Session->setFlash($this->_getErrors( $response->body), "default",
array("class" => "alert alert-error"));
return false;
}
}
}
40 changes: 37 additions & 3 deletions app/View/Reports/view.ctp
Expand Up @@ -44,14 +44,48 @@
</tr>
<tr>
<td>Sourceforge Report</td>
<td><?php if($report['Report']['sourceforge_bug_id']) {
<td>
<?php
if($report['Report']['sourceforge_bug_id']) {
echo $this->Html->link('#' . $report['Report']['sourceforge_bug_id'],
"https://sourceforge.net/p/$project_name/bugs/".
$report['Report']['sourceforge_bug_id'] . "/");
} else {
echo $this->Html->link('Submit report', '/source_forge/create_ticket/'
echo '<table cellspacing="0" class="table table-bordered error-report"'
. ' style="width:300px; margin-bottom:5px;">'
. '<tr><td style="min-width:130px;">';
echo $this->Html->link('Create New Ticket', '/source_forge/create_ticket/'
. $report['Report']['id']);
} ?>

echo '</td><td style="min-width:130px;">';

echo '<form action="'
. Router::url('/', true)
.'source_forge/link_ticket/'
. $report['Report']['id']
.'" method="GET" class="form-horizontal" style="margin-bottom:5px;">';
echo $this->Form->input('ticket_id', array(
'placeholder' => 'Ticket Number',
'type' => 'text',
'label' => false,
'div' => true,
'class' => 'input-large',
'name' => 'ticket_id'
)
);
echo '<br/>';
echo $this->Form->input('Link with existing Ticket', array(
'placeholder' => 'Ticket Number',
'type' => 'submit',
'label' => false,
'div' => '',
'class'=>'btn btn-primary'
)
);
echo '</form>';
echo '</td></tr></table>';
}
?>
</td>
</tr>
<tr>
Expand Down

0 comments on commit 3841ead

Please sign in to comment.