This repository has been archived by the owner on Oct 16, 2024. It is now read-only.
forked from lananrg/JIRAConnectorExtension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJIRAConnector.body.php
300 lines (235 loc) · 8.42 KB
/
JIRAConnector.body.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
/**
* The JIRAConnector Class connects MediaWiki to Jira.
* @author Swetlana Stickhof
*
*/
class JIRAConnector {
//Parser parameters names.
const parserParameterJIRAKey = "jiraissuekey";
//Possible field names of JIRA issue.
const JIRAIssueKey = "key";
const JIRAIssueStatus = "status";
const JIRAIssueType = "issuetype";
const JIRAIssueSummary = "summary";
//JIRA REST API Wrapper.
protected static $jiraWrapper = null;
// Register parser functions at MediaWiki.
public static function RegisterParserFunctions( &$parser ) {
global $jiraURL, $jiraUsername, $jiraPassword;
//Create JIRA wrapper.
JIRAConnector::$jiraWrapper = new JIRARestApiWrapper($jiraURL,$jiraUsername,$jiraPassword);
//Map parser function ReadJIRAIssue to the magic word readjiraissue.
$parser->setFunctionHook( 'readjiraissue', 'JIRAConnector::RenderJIRAIssueStatus' );
$parser->setHook( 'jira', 'JIRAConnector::RenderJIRAIssues');
// Return true so that MediaWiki continues to load extensions.
return true;
}
/**
* Render the output of the parser function ReadJIRAIssue.
* @param unknown $parser
* @return multitype:boolean string
*/
public static function RenderJIRAIssueStatus( $parser ) {
//Disable caching for this extension.
$parser->disableCache();
//Get function parameters.
$functionParameters = array();
for ( $i = 1; $i < func_num_args(); $i++ ) {
$functionParameters[] = func_get_arg( $i );
}
$functionParameters = JIRAConnector::convertFunctionParameters( $functionParameters );
//Get issue data from JIRA.
$returnedIssueData =
JIRAConnector::$jiraWrapper->getIssues(
array(
JIRAConnector::JIRAIssueKey => array($functionParameters[JIRAConnector::parserParameterJIRAKey])
),
"",
array(
JIRAConnector::JIRAIssueKey,
JIRAConnector::JIRAIssueType,
JIRAConnector::JIRAIssueStatus,
JIRAConnector::JIRAIssueSummary
)
);
//Extract status name from the issue data.
$output = $returnedIssueData[0]["fields"]["status"]["name"];
//Return output and let MediaWiki parse the output.
return array( $output, 'noparse' => false );
}
/**
* Render the output of the tag extension <jira>.
* @param unknown $parser
* @return multitype:boolean string
*/
public static function RenderJIRAIssues( $text, array $args, Parser $parser, PPFrame $frame ) {
global $jiraURL;
//Disable caching for this extension.
$parser->disableCache();
//Get issue data from JIRA.
$returnedIssueData =
JIRAConnector::$jiraWrapper->getIssuesForJQL($text, array(
JIRAConnector::JIRAIssueKey,
JIRAConnector::JIRAIssueType,
JIRAConnector::JIRAIssueStatus,
JIRAConnector::JIRAIssueSummary
));
//Render output.
$output = "<table><tbody>";
foreach ($returnedIssueData as $jiraIssue)
{
$issueKey = $jiraIssue[JIRAConnector::JIRAIssueKey];
$issueURL = $jiraIssue["self"];
$issueStatus = $jiraIssue["fields"][JIRAConnector::JIRAIssueStatus]["name"];
$issueTypeIcon = $jiraIssue["fields"][JIRAConnector::JIRAIssueType]["iconUrl"];
$summary = $jiraIssue["fields"][JIRAConnector::JIRAIssueSummary];
$output .= "<tr>";
$output .= "<td><img src=\"$issueTypeIcon\"/></td>";
$output .= "<td><a href=\"$jiraURL/browse/$issueKey\">";
if ($issueStatus == "Resolved") {
$output .= "<strike>" ;
}
$output .= $issueKey;
if ($issueStatus == "Resolved") {
$output .= "</strike>";
}
$output .= "</a></td>";
$output .= "<td>$summary</td>";
$output .= "</tr>";
}
$output .= "</tbody></table>";
//Return output and let MediaWiki parse the output.
return array( $output, 'noparse' => true, 'isHTML' => true);
}
/**
* Converts an array of values in form [0] => "name=value" into a real
* associative array in form [name] => value
*
* @param array string $options
* @return array $results
*/
public static function convertFunctionParameters( array $options ) {
$results = array();
foreach ( $options as $option ) {
$pair = explode( '=', $option );
if ( count( $pair ) == 2 ) {
$name = trim( $pair[0] );
$value = trim( $pair[1] );
$results[$name] = $value;
}
}
return $results;
}
}
/**
* This class is a wrapper for the JIRA REST API.
*
* @author Swetlana Stickhof
*
*/
class JIRARestApiWrapper{
//This user name is used to establish a connection to a JIRA system.
protected $userName = "";
//The password for the user.
protected $userPassword = "";
//The REST API endpoint URL of the JIRA system.
protected $JIRAEndpointURL = "";
/**
* This is the primary class constructor.
* @param $JIRAEndpointURL The URL of the JIRA end point
* @param $userName The name of the user which should be used for the connection to the JIRA
* @param $userPassword The user password
*/
public function __construct( $JIRAEndpointURL, $userName, $userPassword ){
//Set members.
$this->JIRAEndpointURL = $JIRAEndpointURL;
$this->userName = $userName;
$this->userPassword = $userPassword;
}
/**
* This is a generalized function for retrieving REST data from a JIRA system.
* @param $dataURL This is the additional part to the JIRA endpoint URL which identifies concrete data to be retrieved.
* @return Return requested data und the decoded JSON format.
*/
protected function getRESTData($dataURL){
$headers = array();
$headers[] = "Accept: application/json";
$headers[] = "Content-Type: application/json";
if (!is_null($this->userName)) {
//Encode credentials as base 64.
$credentialsEncoded = base64_encode( $this->userName . ":" . $this->userPassword );
$headers[] = $credentialsEncoded;
}
//Define context options.
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=> $headers
)
);
//Create context for the connection.
$context = stream_context_create($opts);
//Request data from the JIRA system.
$requestedData = file_get_contents($this->JIRAEndpointURL . $dataURL, false, $context);
//Parse JSON.
$decodedRequestedData = json_decode($requestedData, TRUE);
//return $decodedRequestedData;
return $decodedRequestedData;
}
/**
* This function returns issues which fullfill defined filter criteria from a JIRA system.
* @param $filterCriteria filter criteria for returned issues. Only issues which fullfill this criteria will be returned.
* The format of the filter criteria is following ["field name"][]="fieldvalue"
* @param $orderIssuesBy field name by which the returned issues should be ordered.
* @param $fieldNames a list of field names of the fields which should be returned for each issue.
*/
public function getIssues( $filterCriteria, $orderIssuesBy, $fieldNames ){
//Concatenate filter criteria.
$jql = "";
$filterCriteriaKeys = array_keys($filterCriteria);
foreach ( $filterCriteriaKeys as $filterCriteriaKey ){
$jql = $jql . $filterCriteriaKey . "=" . $filterCriteria[$filterCriteriaKey][0];
}
//Set order by.
if ($orderIssuesBy != "" && $jql != "") {
$jql = $jql . "+" . "order+by+" . $orderIssuesBy;
}
//Get data from JIRA system.
$restQueryResult = $this->getIssuesForJQL($jql, $fieldNames);
return $restQueryResult;
}
/**
* This function returns issues which fullfill the JQL from a JIRA system.
* @param $jql the JIRA Query Language request to execute
* @param $fieldNames a list of field names of the fields which should be returned for each issue.
*/
public function getIssuesForJQL( $jql, $fieldNames ) {
//Base url for query data.
$dataURL = "/rest/api/2/search";
//Set which fields of each issue have to be returned.
$fieldsQueryParamValue = "";
foreach ( $fieldNames as $fieldName ){
if ($fieldsQueryParamValue != "") {
$fieldsQueryParamValue = $fieldsQueryParamValue . ",";
}
$fieldsQueryParamValue = $fieldsQueryParamValue . $fieldName;
}
$queryString = "";
if ($jql != "") {
$queryString = "jql=" . urlencode($jql);
}
if ($fieldsQueryParamValue != "") {
$queryString = $queryString . "&fields=" . $fieldsQueryParamValue;
}
$dataURL = $dataURL . "?" . $queryString;
//Get data from JIRA system.
$restQueryResult = $this->getRESTData($dataURL);
//Return only issues.
return $restQueryResult["issues"];
}
}
// JIRAConnector::$jiraWrapper = new JIRARestApiWrapper(JIRAConnector::JIRAEndpoint,JIRAConnector::JIRAUserName,JIRAConnector::JIRAUserPassword);
// $output = JIRAConnector::ReadJIRAIssue(null,"jiraissuekey=DEMO-7");
// var_dump($output);
?>