Skip to content

Commit ad3749b

Browse files
committed
project fetch
1 parent f3ddbf6 commit ad3749b

File tree

9 files changed

+220
-51
lines changed

9 files changed

+220
-51
lines changed

phpunit.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
<testsuite name="Jira Rest API Test Suite">
66
<!--
77
<directory>tests/</directory>
8-
-->
9-
<file>tests/CurlTest.php</file>
8+
-->
109
<file>tests/ProjectTest.php</file>
1110
</testsuite>
1211
</testsuites>

src/JiraClient.php

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ private function convertLogLevel($log_level) {
6060

6161
public function __construct($config, $options = null)
6262
{
63-
$json_mapper = new \JsonMapper();
64-
$json_mapper->bExceptionOnUndefinedProperty = true;
63+
$this->json_mapper = new \JsonMapper();
64+
$this->json_mapper->bExceptionOnUndefinedProperty = true;
6565

6666
$this->host = $config['host'];
6767
$this->username = $config['username'];
@@ -141,6 +141,57 @@ public function exec($context, $post_data = null) {
141141

142142
return $response;
143143
}
144+
145+
function indent($json) {
146+
147+
$result = '';
148+
$pos = 0;
149+
$strLen = strlen($json);
150+
$indentStr = ' ';
151+
$newLine = "\n";
152+
$prevChar = '';
153+
$outOfQuotes = true;
154+
155+
for ($i=0; $i<=$strLen; $i++) {
156+
157+
// Grab the next character in the string.
158+
$char = substr($json, $i, 1);
159+
160+
// Are we inside a quoted string?
161+
if ($char == '"' && $prevChar != '\\') {
162+
$outOfQuotes = !$outOfQuotes;
163+
164+
// If this character is the end of an element,
165+
// output a new line and indent the next line.
166+
} else if(($char == '}' || $char == ']') && $outOfQuotes) {
167+
$result .= $newLine;
168+
$pos --;
169+
for ($j=0; $j<$pos; $j++) {
170+
$result .= $indentStr;
171+
}
172+
}
173+
174+
// Add the character to the result string.
175+
$result .= $char;
176+
177+
// If the last character was the beginning of an element,
178+
// output a new line and indent the next line.
179+
if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) {
180+
$result .= $newLine;
181+
if ($char == '{' || $char == '[') {
182+
$pos ++;
183+
}
184+
185+
for ($j = 0; $j < $pos; $j++) {
186+
$result .= $indentStr;
187+
}
188+
}
189+
190+
$prevChar = $char;
191+
}
192+
193+
return $result;
194+
}
144195
}
145196

146197

src/config.jira.example.php

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
11
<?php
22

3-
$jira_config = array ('host' => 'https://jira.example.com',
4-
'username' => 'username',
5-
'password' => 'password');
6-
7-
$options = array(
8-
CURLOPT_SSL_VERIFYHOST => 0,
9-
CURLOPT_SSL_VERIFYPEER => 0,
10-
CURLOPT_VERBOSE => true,
11-
'LOG_FILE' => 'log/jira-rest-client.log',
12-
'LOG_LEVEL' => \Monolog\Logger::INFO
13-
);
3+
/**
4+
* Description get Jira Host Configuration
5+
*
6+
* @return array
7+
*/
8+
function getHostConfig() {
9+
$jira_config = array ('host' => 'https://jira.example.com',
10+
'username' => 'username',
11+
'password' => 'password');
12+
13+
return $jira_config;
14+
}
15+
16+
/**
17+
* Description get Client options
18+
*
19+
* @return array
20+
*/
21+
function getOptions() {
22+
$options = array(
23+
CURLOPT_SSL_VERIFYHOST => 0,
24+
CURLOPT_SSL_VERIFYPEER => 0,
25+
CURLOPT_VERBOSE => true,
26+
'LOG_FILE' => 'log/jira-rest-client.log',
27+
'LOG_LEVEL' => \Monolog\Logger::INFO
28+
);
29+
30+
return $options;
31+
}
1432

1533
?>

src/issue/issueType.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace JiraRestApi\Issue;
4+
5+
class IssueType {
6+
/**
7+
* IssueType URI
8+
* @var string
9+
*/
10+
public $self;
11+
12+
/**
13+
* IssueType id
14+
* @var string
15+
*/
16+
public $id;
17+
18+
/**
19+
* IssueType description
20+
* @var string
21+
*/
22+
public $description;
23+
24+
/**
25+
* IssueType name
26+
* @var string
27+
*/
28+
public $name;
29+
30+
/**
31+
* is subtask
32+
* @var boolean
33+
*/
34+
public $subtak;
35+
}
36+
37+
?>

src/project/Component.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace JiraRestApi\Project;
4+
5+
class Component {
6+
/**
7+
* Component URI
8+
* @var string
9+
*/
10+
public $self;
11+
12+
/**
13+
* Component id
14+
* @var string
15+
*/
16+
public $id;
17+
18+
/**
19+
* Component name
20+
* @var string
21+
*/
22+
public $name;
23+
24+
/**
25+
* Component description
26+
* @var description
27+
*/
28+
public $description;
29+
}
30+
31+
?>

src/project/Project.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
namespace JiraRestApi\Project;
44

5-
require 'vendor/autoload.php';
6-
75
class Project {
6+
/**
7+
* return only if Project query by key(not id)
8+
* @var string
9+
*/
10+
public $expand;
811

912
/**
1013
* Project URI
@@ -44,6 +47,24 @@ class Project {
4447

4548
/* @var string */
4649
public $description;
50+
51+
/* Project leader info @var array */
52+
public $lead;
53+
54+
/* @var ComponentList[\JiraRestApi\Project\Component] */
55+
public $components;
56+
57+
/* @var IssueTypeList[\JiraRestApi\Issue\IssueType] */
58+
public $issueTypes;
59+
60+
/* @var string */
61+
public $assigneeType;
62+
63+
/* @var array */
64+
public $versions;
65+
66+
/* @var array */
67+
public $roles;
4768
}
4869

4970
?>

src/project/ProjectService.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,27 @@ public function __construct($config, $opt_array = null) {
2323
parent::__construct($config, $opt_array);
2424
}
2525

26+
/**
27+
* get all project list
28+
*
29+
* @return array of Project class
30+
*/
2631
public function getAllProjects() {
2732
$ret = $this->exec('/project', null);
28-
33+
2934
$prjs = $this->json_mapper->mapArray(
30-
json_decode($ret), new ArrayObject(), '\JiraRestApi\Project\Project'
35+
json_decode($ret, true), new \ArrayObject(), '\JiraRestApi\Project\Project'
3136
);
32-
33-
return $prjs;
37+
38+
return $prjs;
3439
}
3540

3641
public function get($projectIdOrKey) {
3742
$ret = $this->exec("/project/$projectIdOrKey", null);
3843

39-
#var_dump($ret);
40-
$json_mapper = new \JsonMapper();
41-
$prj = $json_mapper->map(
44+
$this->log->addInfo("Result=" . $ret );
45+
46+
$prj = $this->json_mapper->map(
4247
json_decode($ret), new Project()
4348
);
4449

tests/CurlTest.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
<?php
22

3-
//require 'vendor/autoload.php';
4-
//require_once 'config.php';
5-
63
use JiraRestApi\HTTPException;
74

85
class CurlTest extends PHPUnit_Framework_TestCase
96
{
107
public function testCurlPost()
118
{
129
try {
10+
$config = getHostConfig();
11+
12+
$config['host']='http://requestb.in/vqid8qvq';
1313

14-
$j = new \JiraRestApi\JiraClient($jira_config, $options);
14+
$j = new \JiraRestApi\JiraClient($config, getOptions());
1515

1616
$post_data = array("name" => "value");
1717

1818
$http_status = 0;
19-
$ret = $j->exec('/abcd', json_encode($post_data), $http_status);
19+
$ret = $j->exec('/', json_encode($post_data), $http_status);
2020

2121
var_dump($ret);
2222
$this->assertTrue(TRUE);
2323
} catch (HTTPException $e) {
24-
var_dump($e);
25-
$this->assertTrue(FALSE);
24+
$this->assertTrue(FALSE, $e->getMessage());
2625
}
2726
}
2827
}

tests/ProjectTest.php

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,43 @@
11
<?php
22

3-
require 'vendor/autoload.php';
4-
require_once 'config.jira.php';
5-
63
use JiraRestApi\Project\ProjectService;
74

85
class ProjectTest extends PHPUnit_Framework_TestCase
96
{
10-
public function testProjectLists()
7+
public function testGetProject()
118
{
9+
//$this->markTestIncomplete();
1210
try {
13-
$proj = new ProjectService($jira_config, $options);
14-
15-
//$ret = $proj->getAllProjects();
11+
$proj = new ProjectService(getHostConfig(), getOptions());
12+
13+
$p = $proj->get('TEST');
14+
15+
print_r($p->lead);
16+
foreach ($p->components as $c) {
17+
echo ("COM : " . $c->name . "\n");
18+
}
19+
} catch (HTTPException $e) {
20+
$this->assertTrue(FALSE, $e->getMessage());
21+
}
22+
}
1623

17-
/*
18-
$mapper = new JsonMapper();
19-
$mapper->bExceptionOnUndefinedProperty = true;
24+
public function testGetProjectLists()
25+
{
26+
$this->markTestIncomplete();
27+
try {
28+
$proj = new ProjectService(getHostConfig(), getOptions());
2029

21-
$prjs = $mapper->mapArray(
22-
json_decode($ret), new ArrayObject(), '\JiraRestApi\Project\Project'
23-
);
24-
var_dump($prjs);
25-
*/
26-
$p = $proj->get('BITA');
27-
var_dump($p);
30+
$prjs = $proj->getAllProjects();
2831

29-
//$project = $mapper->map($ret, new Project());
30-
//var_dump($project);
32+
$i = 0;
33+
foreach ($prjs as $p) {
34+
echo sprintf("Project Key:%s, Id:%s, Name:%s, projectCategory: %s\n",
35+
$p->key, $p->id, $p->name, $p->projectCategory['name']
36+
);
37+
38+
}
3139
} catch (HTTPException $e) {
32-
var_dump($e);
40+
$this->assertTrue(FALSE, $e->getMessage());
3341
}
3442
}
3543
//

0 commit comments

Comments
 (0)