/
SourceForgeController.php
154 lines (137 loc) · 5.13 KB
/
SourceForgeController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/* vim: set noexpandtab sw=2 ts=2 sts=2: */
/**
* Sourceforge controller handling source forge ticket submission and creation
*
* phpMyAdmin Error reporting server
* Copyright (c) phpMyAdmin project (http://www.phpmyadmin.net)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) phpMyAdmin project (http://www.phpmyadmin.net)
* @package Server.Controller
* @link http://www.phpmyadmin.net
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
/**
* Sourceforge controller handling source forge ticket submission and creation
*
* @package Server.Controller
*/
class SourceForgeController extends AppController {
public $helpers = array('Html', 'Form');
public $components = array('SourceForgeApi');
public $uses = array('Report');
public function beforeFilter() {
$this->SourceForgeApi->accessToken =
Configure::read('SourceForgeCredentials');
parent::beforeFilter();
}
public function authorize() {
$requestToken =
$this->SourceForgeApi->getRequestToken('/source_forge/callback');
if ($requestToken) {
$this->Session->write('sourceforge_request_token', $requestToken);
$this->redirect($this->SourceForgeApi->getRedirectUrl($requestToken));
}
$this->autoRender = false;
return json_encode($requestToken);
}
public function callback() {
$requestToken = $this->Session->read('sourceforge_request_token');
$accessToken = $this->SourceForgeApi->getAccessToken($requestToken);
$this->autoRender = false;
return json_encode($accessToken);
}
public function create_ticket($reportId) {
if (!$reportId) {
throw new NotFoundException(__('Invalid report'));
}
$report = $this->Report->findById($reportId);
if (!$report) {
throw new NotFoundException(__('Invalid report'));
}
if (empty($this->request->data)) {
return;
}
$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"));
$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"));
}
}
protected function _getTicketData($reportId) {
$data = array(
'ticket_form.summary' => $this->request->data['Ticket']['summary'],
'ticket_form.description' => $this->_augmentDescription(
$this->request->data['Ticket']['description'], $reportId),
'ticket_form.status' => 'open',
'ticket_form.labels' => $this->request->data['Ticket']['labels'],
'ticket_form._milestone' => $this->request->data['Ticket']['milestone'],
);
if (!empty($data['ticket_form.labels'])) {
$data['ticket_form.labels'] .= ',';
}
$data['ticket_form.labels'] .= 'automated-error-report';
return $data;
}
protected function _getErrors($body) {
$errorString = "There were some problems with the ticket submission."
." Returned status is (" . $body["status"] . ")";
$errors = $body["errors"];
if ($body["status"] === "Validation Error") {
$errorString .= '<ul>';
foreach ($errors['ticket_form'] as $field => $message) {
$errorString .= "<li>";
$errorString .= "$field: $message";
$errorString .= "</li>";
}
$errorString .= '</ul>';
} else {
$errorString .= "<br/> Here is the dump for the errors field provided by"
. " sourceforge: <br/>"
. "<pre>"
. print_r($errors, true)
. "</pre>";
}
return $errorString;
}
/**
* Returns the description with the added string to link to the report
* @param String $description the original description submitted by the dev
* @param String $reportId the report id relating to the ticket
*
* @return String augmented description
*/
protected function _augmentDescription($description, $reportId) {
$this->Report->read(null, $reportId);
return "$description\n\n\nThis report is related to user submitted report "
. "[#" . $this->Report->id . "](" . $this->Report->getUrl()
. ") on the phpmyadmin error reporting server.";
}
}