Skip to content

Commit d5f0a67

Browse files
author
KwangSeob Jeong
committed
fixed #110 sends a notification to the recipients.
1 parent c913958 commit d5f0a67

File tree

3 files changed

+78
-12
lines changed

3 files changed

+78
-12
lines changed

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ $iss = new IssueService(new ArrayConfiguration(
119119
- [Edit worklog in Issue](#edit-worklog-in-issue)
120120
- [Get Issue worklog](#get-issue-worklog)
121121
- [Add watcher in Issue](#add-issue-watcher)
122+
- [Send a notification to the recipients](#issue-notify)
122123

123124
### IssueLink
124125

@@ -940,10 +941,42 @@ try {
940941
var_dump($wch);
941942

942943
} catch (JiraException $e) {
943-
$this->assertTrue(false, 'testSearch Failed : '.$e->getMessage());
944+
$this->assertTrue(false, 'add watcher Failed : '.$e->getMessage());
944945
}
945946
```
946947

948+
#### issue notify
949+
950+
```php
951+
<?php
952+
require 'vendor/autoload.php';
953+
954+
use JiraRestApi\Issue\IssueService;
955+
use JiraRestApi\Issue\Notify;
956+
use JiraRestApi\JiraException;
957+
958+
$issueKey = 'TEST-961';
959+
960+
try {
961+
$issueService = new IssueService();
962+
963+
$noti = new Notify();
964+
965+
$noti->setSubject('notify test')
966+
->setTextBody('notify test text body')
967+
->setHtmlBody('<h1>notify</h1>test html body')
968+
->sendToAssignee(true)
969+
->sendToWatchers(true)
970+
->sendToUser('lesstif', true)
971+
->sendToGroup('temp-group')
972+
;
973+
974+
$issueService->notify($issueKey, $noti);
975+
976+
} catch (JiraException $e) {
977+
$this->assertTrue(false, 'Issue notify Failed : '.$e->getMessage());
978+
}
979+
```
947980
#### Create Issue Link
948981

949982
The Link Issue Resource provides functionality to manage issue links.

src/Issue/IssueService.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,8 @@ public function getEditMeta($idOrKey, $overrideEditableFlag = false, $overrideSc
723723
}
724724

725725
/**
726+
* Sends a notification (email) to the list or recipients defined in the request.
727+
*
726728
* @param $issueIdOrKey Issue id Or Key
727729
* @param $notify Notify
728730
*
@@ -733,15 +735,23 @@ public function getEditMeta($idOrKey, $overrideEditableFlag = false, $overrideSc
733735
*/
734736
public function notify($issueIdOrKey, $notify)
735737
{
736-
$uri = $this->uri."/$issueIdOrKey/notify";
738+
$full_uri = $this->uri."/$issueIdOrKey/notify";
739+
740+
// set self value
741+
foreach ($notify->to['groups'] as &$g) {
742+
$g['self'] = $this->getConfiguration()->getJiraHost().'/rest/api/2/group?groupname='.$g['name'];
743+
}
744+
foreach ($notify->restrict['groups'] as &$g) {
745+
$g['self'] = $this->getConfiguration()->getJiraHost().'/rest/api/2/group?groupname='.$g['name'];
746+
}
737747

738-
$data = json_encode($notify);
748+
$data = json_encode($notify, JSON_UNESCAPED_SLASHES);
739749

740-
$this->log->addInfo("notify=$data\n");
750+
$this->log->addDebug("notify=$data\n");
741751

742-
$ret = $this->exec($uri, $data, 'POST');
752+
$ret = $this->exec($full_uri, $data, 'POST');
743753

744-
if ($ret !== 204) {
754+
if ($ret !== true) {
745755
throw new JiraException("notify failed: response code=".$ret);
746756
}
747757
}

src/Issue/Notify.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,21 @@ class Notify implements \JsonSerializable
1818
/** @var array|null */
1919
public $groups;
2020

21+
/** @var array */
22+
public $restrict;
23+
2124
public function __construct()
2225
{
2326
$this->to = [];
2427
$this->to['users'] = [];
25-
$this->groups = [];
28+
$this->to['groups'] = [];
29+
30+
$this->restrict = [];
31+
$this->restrict['groups'] = [];
32+
$this->restrict['permissions'] = [];
2633

27-
$this->to['reporter'] = false;
28-
$this->to['assignee'] = false;
34+
$this->to['reporter'] = true;
35+
$this->to['assignee'] = true;
2936
$this->to['watchers'] = true;
3037
$this->to['voters'] = true;
3138
}
@@ -82,10 +89,26 @@ public function sendToUser($name, $active)
8289
public function sendToGroup($groupName)
8390
{
8491
$group['name'] = $groupName;
85-
// FIXME "self": "http://www.example.com/jira/rest/api/2/group?groupname=notification-group"
86-
//$group['self'] = $active;
8792

88-
array_push($this->groups, $group);
93+
array_push($this->to['groups'], $group);
94+
95+
return $this;
96+
}
97+
98+
public function setRestrictGroup($groupName)
99+
{
100+
$group['name'] = $groupName;
101+
102+
array_push($this->restrict['groups'], $group);
103+
104+
return $this;
105+
}
106+
107+
public function setRestrictPermission($id, $key)
108+
{
109+
$perm['id'] = $id;
110+
$perm['key'] = $key;
111+
array_push($this->restrict['permissions'], $perm);
89112

90113
return $this;
91114
}

0 commit comments

Comments
 (0)