Skip to content

Commit b1b43f9

Browse files
committed
process custom field
1 parent 75a3645 commit b1b43f9

File tree

2 files changed

+81
-26
lines changed

2 files changed

+81
-26
lines changed

README.md

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,36 @@
1717

1818
1. Download and Install PHP Composer.
1919

20-
``` sh
21-
curl -sS https://getcomposer.org/installer | php
22-
```
20+
``` sh
21+
curl -sS https://getcomposer.org/installer | php
22+
```
2323

2424
2. Next, run the Composer command to install the latest version of php jira rest client.
25-
``` sh
26-
php composer.phar require lesstif/php-jira-rest-client "^1.7.0"
27-
```
25+
``` sh
26+
php composer.phar require lesstif/php-jira-rest-client "^1.7.0"
27+
```
2828
or add the following to your composer.json file.
29-
```json
30-
{
31-
"require": {
32-
"lesstif/php-jira-rest-client": "^1.7.0"
33-
}
34-
}
35-
```
36-
**Note:**
37-
If you are using **laravel 5.0 or 5.1**(this version dependent on phpdotenv 1.x), then use **"1.5.\*"** version instead.
29+
```json
30+
{
31+
"require": {
32+
"lesstif/php-jira-rest-client": "^1.7.0"
33+
}
34+
}
35+
```
36+
**Note:**
37+
If you are using **laravel 5.0 or 5.1**(this version dependent on phpdotenv 1.x), then use **"1.5.\*"** version instead.
3838

3939
3. Then run Composer's install or update commands to complete installation.
4040

41-
```sh
42-
php composer.phar install
43-
```
44-
41+
```sh
42+
php composer.phar install
43+
```
44+
4545
4. After installing, you need to require Composer's autoloader:
4646

47-
```php
48-
require 'vendor/autoload.php';
49-
```
47+
```php
48+
require 'vendor/autoload.php';
49+
```
5050

5151
# Configuration
5252

@@ -56,7 +56,7 @@ you can choose loads environment variables either 'dotenv' or 'array'.
5656

5757

5858
copy .env.example file to .env on your project root.
59-
59+
6060
JIRA_HOST="https://your-jira.host.com"
6161
JIRA_USER="jira-username"
6262
JIRA_PASS="jira-password"
@@ -247,9 +247,10 @@ try {
247247
} catch (JiraException $e) {
248248
print("Error Occured! " . $e->getMessage());
249249
}
250-
251250
```
252251

252+
You can access the custom field associated with issue through *$issue->fields->customFields* array or through direct custom field id variables(Ex: *$issue->fields->customfield_10300*).
253+
253254
#### Create Issue
254255

255256
```php
@@ -281,9 +282,42 @@ try {
281282
} catch (JiraException $e) {
282283
print("Error Occured! " . $e->getMessage());
283284
}
285+
```
286+
287+
If you want to set custom field, you can call the *addCustomField* function with custom field id and value as parameters.
288+
289+
```php
290+
try {
291+
$issueField = new IssueField();
284292

293+
$issueField->setProjectKey("TEST")
294+
->setSummary("something's wrong")
295+
->setAssigneeName("lesstif")
296+
->setPriorityName("Critical")
297+
->setIssueType("Bug")
298+
->setDescription("Full description for issue")
299+
->addVersion("1.0.1")
300+
->addVersion("1.0.3")
301+
->addCustomField('customfield_10200', ['value' => 'Linux']) // Select List (single choice)
302+
->addCustomField('customfield_10408', [
303+
['value' => 'opt2'], ['value' => 'opt4']
304+
]) // Select List (multiple choice)
305+
306+
;
307+
308+
$issueService = new IssueService();
309+
310+
$ret = $issueService->create($issueField);
311+
312+
//If success, Returns a link to the created issue.
313+
print_r($ret);
314+
} catch (JiraException $e) {
315+
print("Error Occured! " . $e->getMessage());
316+
}
285317
```
286318

319+
Currently, not tested for all custom field types.
320+
287321
#### Create Multiple Issue
288322

289323
```php
@@ -333,7 +367,7 @@ Creating a sub-task is similar to creating a regular issue, with two important m
333367
```
334368

335369
for example
336-
370+
337371
```php
338372
<?php
339373
require 'vendor/autoload.php';
@@ -429,6 +463,8 @@ try {
429463

430464
```
431465

466+
If you want to change the custom field type when updating an issue, you can call the *addCustomField* function just as you did for creating issue.
467+
432468
#### Add comment
433469

434470
```php

src/Issue/IssueField.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,33 @@ public function __construct($updateIssue = false)
2323

2424
public function jsonSerialize()
2525
{
26-
return array_filter(get_object_vars($this));
26+
$vars = array_filter(get_object_vars($this));
27+
28+
// clear undefined json property
29+
unset($vars['customFields']);
30+
31+
// repackaging custom field
32+
if (!empty($this->customFields)) {
33+
foreach ($this->customFields as $key => $value) {
34+
$vars[$key] = $value;
35+
}
36+
}
37+
38+
return $vars;
2739
}
2840

2941
public function getCustomFields()
3042
{
3143
return $this->customFields;
3244
}
3345

46+
public function addCustomField($key, $value)
47+
{
48+
$this->customFields[$key] = $value;
49+
50+
return $this;
51+
}
52+
3453
public function getProjectKey()
3554
{
3655
return $this->project->key;

0 commit comments

Comments
 (0)