Skip to content

Commit bea98cb

Browse files
authored
Merge pull request #60 from mkrivickiy/develop
implement addWatcher method
2 parents b40dbd6 + e442576 commit bea98cb

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ $iss = new IssueService(new ArrayConfiguration(
113113
- [Add worklog in Issue](#add-worklog-in-issue)
114114
- [Edit worklog in Issue](#edit-worklog-in-issue)
115115
- [Get Issue worklog](#get-issue-worklog)
116+
- [Add watcher in Issue](#add-issue-watcher)
116117

117118
### User
118119
- [Get User Info](#get-user-info)
@@ -819,6 +820,34 @@ try {
819820

820821
```
821822

823+
#### Add watcher in Issue
824+
825+
```php
826+
<?php
827+
require 'vendor/autoload.php';
828+
829+
use JiraRestApi\Issue\IssueService;
830+
use JiraRestApi\Issue\Watcher;
831+
use JiraRestApi\JiraException;
832+
833+
$issueKey = 'TEST-961';
834+
835+
try {
836+
$issueService = new IssueService();
837+
838+
// get issue's all worklog
839+
$watcher = new Watcher('lesstif');
840+
var_dump($watcher);
841+
842+
$wch = $issueService->addWatcher($issueKey, $watcher);
843+
var_dump($wch);
844+
845+
} catch (JiraException $e) {
846+
$this->assertTrue(false, 'testSearch Failed : '.$e->getMessage());
847+
}
848+
849+
```
850+
822851
#### Get User Info
823852

824853
Returns a user.

src/Issue/IssueService.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,4 +545,27 @@ public function getCustomFields($priorityId)
545545

546546
return $prio;
547547
}
548+
549+
/**
550+
* add watcher to issue.
551+
*
552+
* @param mixed $issueIdOrKey
553+
* @param object $watcher
554+
* @param int $worklogId
555+
*
556+
* @return bool
557+
*/
558+
public function addWatcher($issueIdOrKey, $watcher)
559+
{
560+
$this->log->addInfo("addWatcher=\n");
561+
562+
$data = json_encode($watcher);
563+
$url = $this->uri . "/$issueIdOrKey/watchers";
564+
$type = 'POST';
565+
566+
$this->exec($url, $data, $type);
567+
568+
return $this->http_response == 204 ? true : false;
569+
}
570+
548571
}

src/Issue/Watcher.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace JiraRestApi\Issue;
4+
5+
use JiraRestApi\ClassSerialize;
6+
use JiraRestApi\JiraException;
7+
8+
/**
9+
* Class Watcher.
10+
*/
11+
class Watcher implements \JsonSerializable
12+
{
13+
use ClassSerialize;
14+
15+
/** @var string */
16+
public $name;
17+
18+
public function __construct($name)
19+
{
20+
$this->name = $name;
21+
}
22+
23+
/**
24+
* Function to serialize obj vars.
25+
*
26+
* @return array
27+
*/
28+
public function jsonSerialize()
29+
{
30+
return $this->name;
31+
}
32+
33+
}

tests/WatcherLogTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
use JiraRestApi\Dumper;
4+
use JiraRestApi\Issue\IssueService;
5+
use JiraRestApi\Issue\Watcher;
6+
use JiraRestApi\JiraException;
7+
8+
class WatcherLogTest extends PHPUnit_Framework_TestCase
9+
{
10+
public $issueKey = 'TEST-165';
11+
12+
public function testAddWatcherLog()
13+
{
14+
try {
15+
$issueService = new IssueService();
16+
17+
$watcher = new Watcher('lesstif');
18+
// add issue watcher
19+
$ret = $issueService->addWatcher($this->issueKey, $watcher);
20+
21+
Dumper::dump($ret);
22+
} catch (JiraException $e) {
23+
$this->assertTrue(false, 'testAddWatcherLog Failed : '.$e->getMessage());
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)