Skip to content

Commit 6914c00

Browse files
committed
fixed #54 to fix the "Link Issue" and "Remote Issue link" functionality work.
1 parent 0b4c489 commit 6914c00

File tree

6 files changed

+132
-67
lines changed

6 files changed

+132
-67
lines changed

README.md

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,7 +1576,7 @@ try {
15761576

15771577
##### get remote issue link
15781578

1579-
* [See Jira API reference](https://docs.atlassian.com/software/jira/docs/api/REST/latest/#api/2/issue-getRemoteIssueLinks)
1579+
* [See Jira API reference](https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-remote-links/#api-rest-api-3-issue-issueidorkey-remotelink-get)
15801580

15811581
```php
15821582
<?php
@@ -1602,7 +1602,7 @@ try {
16021602

16031603
##### create remote issue link
16041604

1605-
* [See Jira API reference](https://docs.atlassian.com/software/jira/docs/api/REST/latest/#api/2/issue-getRemoteIssueLinks)
1605+
* [See Jira API reference](https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-remote-links/#api-rest-api-3-issue-issueidorkey-remotelink-post)
16061606

16071607
```php
16081608
<?php
@@ -1928,7 +1928,7 @@ try {
19281928
```
19291929
#### Create Issue Link
19301930

1931-
[See Jira API reference](https://docs.atlassian.com/software/jira/docs/api/REST/latest/#api/2/issueLink-linkIssues)
1931+
[See Jira API reference](https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-links/#api-rest-api-3-issuelink-post)
19321932

19331933
The Link Issue Resource provides functionality to manage issue links.
19341934

@@ -1941,25 +1941,43 @@ use JiraCloud\IssueLink\IssueLinkService;
19411941
use JiraCloud\JiraException;
19421942

19431943
try {
1944+
$doc = (new Document())
1945+
->heading(1) // header level 1, can have child blocks (needs to be closed with `->end()`)
1946+
->text('h1') // simple unstyled text, cannot have child blocks (no `->end()` needed)
1947+
->end() // closes `heading` node
1948+
->paragraph() // paragraph, can have child blocks (needs to be closed with `->end()`)
1949+
->text('Issue Link ') // simple unstyled text
1950+
->strong('By ') // text node embedding a `strong` mark
1951+
->text(' REST ') // simple unstyled text
1952+
->em('API')
1953+
->end() // closes `paragraph` node
1954+
;
1955+
1956+
$comment = new AtlassianDocumentFormat($doc);
1957+
19441958
$il = new IssueLink();
19451959

1946-
$il->setInwardIssue('TEST-258')
1947-
->setOutwardIssue('TEST-249')
1948-
->setLinkTypeName('Relates' )
1949-
->setComment('Linked related issue via REST API.');
1950-
1960+
$inwardKey = 'TEST-162';
1961+
$outwardKey = 'ST-3';
1962+
1963+
$il->setInwardIssueByKey($inwardKey)
1964+
->setOutwardIssueByKey($outwardKey)
1965+
->setLinkTypeName('Duplicate' )
1966+
->setCommentAsADF($comment)
1967+
;
1968+
19511969
$ils = new IssueLinkService();
19521970

19531971
$ret = $ils->addIssueLink($il);
1954-
1972+
19551973
} catch (JiraCloud\JiraException $e) {
19561974
print('Error Occurred! ' . $e->getMessage());
19571975
}
19581976
```
19591977

19601978
#### Get Issue LinkType
19611979

1962-
[See Jira API reference](https://docs.atlassian.com/software/jira/docs/api/REST/latest/#api/2/issueLinkType-getIssueLinkTypes)
1980+
[See Jira API reference](https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-link-types/#api-group-issue-link-types)
19631981

19641982
Rest resource to retrieve a list of issue link types.
19651983

src/Issue/RemoteIssueLink.php

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,61 +4,51 @@
44

55
class RemoteIssueLink implements \JsonSerializable
66
{
7-
/** @var int */
8-
public $id;
7+
public int $id;
98

10-
/** @var string */
11-
public $self;
9+
public string $self;
1210

13-
/** @var string */
14-
public $globalId;
11+
public string $globalId;
1512

16-
/** @var array|null */
17-
public $application;
13+
public ?array $application;
1814

19-
/** @var string|null */
20-
public $relationship;
15+
public ?string $relationship;
2116

22-
/** @var \JiraCloud\Issue\RemoteIssueLinkObject|null */
23-
public $object;
17+
public ?RemoteObject $object;
2418

2519
#[\ReturnTypeWillChange]
2620
public function jsonSerialize()
2721
{
2822
return array_filter(get_object_vars($this));
2923
}
3024

31-
/**
32-
* @param string $url
33-
*
34-
* @return $this
35-
*/
36-
public function setUrl(string $url)
37-
{
38-
if (is_null($this->object)) {
39-
$this->object = new self();
40-
}
25+
function __construct() {
26+
$this->object = new RemoteObject();
27+
}
4128

29+
public function setUrl(string $url): static
30+
{
31+
$this->globalId = $url;
4232
$this->object->url = $url;
4333

4434
return $this;
4535
}
4636

47-
public function setTitle($title)
37+
public function setTitle($title): static
4838
{
4939
$this->object->title = $title;
5040

5141
return $this;
5242
}
5343

54-
public function setSummary($summary)
44+
public function setSummary($summary): static
5545
{
5646
$this->object->summary = $summary;
5747

5848
return $this;
5949
}
6050

61-
public function setRelationship($relationship)
51+
public function setRelationship($relationship): static
6252
{
6353
$this->relationship = $relationship;
6454

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,15 @@
22

33
namespace JiraCloud\Issue;
44

5-
class RemoteIssueLinkObject
5+
class RemoteObject
66
{
7-
/** @var string */
8-
public $url;
7+
public string $url;
98

10-
/** @var string */
11-
public $title;
9+
public string $title;
1210

13-
/** @var string|null */
14-
public $summary;
11+
public ?string $summary;
1512

16-
/** @var array|null */
17-
public $icon;
13+
public ?array $icon;
1814

1915
/**
2016
* @var array|null
@@ -30,5 +26,5 @@ class RemoteIssueLinkObject
3026
* }
3127
* ```
3228
*/
33-
public $status;
29+
public ?array $status;
3430
}

src/IssueLink/IssueLink.php

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22

33
namespace JiraCloud\IssueLink;
44

5+
use JiraCloud\ADF\AtlassianDocumentFormat;
56
use JiraCloud\ClassSerialize;
6-
use JiraCloud\Issue\Comment;
7-
use JiraCloud\Issue\Issue;
87

98
class IssueLink implements \JsonSerializable
109
{
1110
use ClassSerialize;
1211

12+
public string $id;
13+
public string $self;
14+
1315
public array $type;
1416

15-
public Issue $inwardIssue;
17+
public ?LinkedIssue $inwardIssue = null;
1618

17-
public Issue $outwardIssue;
19+
public ?LinkedIssue $outwardIssue = null;
1820

19-
public Comment $comment;
21+
public array $comment = [];
2022

2123
#[\ReturnTypeWillChange]
2224
public function jsonSerialize(): array
@@ -39,41 +41,45 @@ public function setLinkTypeName($typeName)
3941
}
4042

4143
/**
42-
* @param string|int $issueKey inward issue key or id
44+
* @param string $issueKey inward issue key or id
4345
*
4446
* @return $this
4547
*/
46-
public function setInwardIssue(string $issueKey): static
48+
public function setInwardIssueByKey(string $issueKey): static
4749
{
48-
$this->inwardIssue['key'] = $issueKey;
50+
if ($this->inwardIssue === null) {
51+
$this->inwardIssue = new LinkedIssue();
52+
}
53+
$this->inwardIssue->key= $issueKey;
4954

5055
return $this;
5156
}
5257

5358
/**
54-
* @param string|int $issueKey out ward issue key or id
59+
* @param string $issueKey out ward issue key or id
5560
*
5661
* @return $this
5762
*/
58-
public function setOutwardIssue(string $issueKey): static
63+
public function setOutwardIssueByKey(string $issueKey): static
5964
{
60-
$this->outwardIssue['key'] = $issueKey;
65+
if ($this->outwardIssue === null) {
66+
$this->outwardIssue = new LinkedIssue();
67+
}
68+
69+
$this->outwardIssue->key = $issueKey;
6170

6271
return $this;
6372
}
6473

6574
/**
66-
* @param string|Comment $comment string or \JiraCloud\Issue\Comment instance
75+
* @param AtlassianDocumentFormat $comment string or \JiraCloud\Issue\Comment instance
6776
*
6877
* @return $this
6978
*/
70-
public function setComment(string|Comment $comment): static
79+
public function setCommentAsADF(?AtlassianDocumentFormat $comment): static
7180
{
72-
if (is_string($comment)) {
73-
$this->comment = new Comment();
74-
$this->comment->setBody($comment);
75-
} elseif ($comment instanceof Comment) {
76-
$this->comment = $comment;
81+
if (!empty($comment)) {
82+
$this->comment['body'] = $comment;
7783
}
7884

7985
return $this;

src/IssueLink/IssueLinkService.php

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace JiraCloud\IssueLink;
44

5+
use ArrayObject;
6+
57
class IssueLinkService extends \JiraCloud\JiraClient
68
{
79
private $uri = '';
@@ -13,7 +15,7 @@ class IssueLinkService extends \JiraCloud\JiraClient
1315
*
1416
* @return bool
1517
*/
16-
public function addIssueLink($issueLink)
18+
public function addIssueLink(IssueLink $issueLink): bool
1719
{
1820
$this->log->info("addIssueLink=\n");
1921

@@ -24,15 +26,17 @@ public function addIssueLink($issueLink)
2426
$url = $this->uri.'/issueLink';
2527
$type = 'POST';
2628

27-
return $this->exec($url, $data, $type);
29+
$ret = $this->exec($url, $data, $type);
30+
31+
return $ret;
2832
}
2933

3034
/**
3135
* @throws \JiraCloud\JiraException
3236
*
3337
* @return IssueLinkType[]
3438
*/
35-
public function getIssueLinkTypes()
39+
public function getIssueLinkTypes(): ArrayObject
3640
{
3741
$this->log->info("getIssueLinkTYpes=\n");
3842

@@ -45,9 +49,41 @@ public function getIssueLinkTypes()
4549
$linkTypes = $this->json_mapper->mapArray(
4650
json_decode($data, false),
4751
new \ArrayObject(),
48-
'\JiraCloud\IssueLink\IssueLinkType'
52+
\JiraCloud\IssueLink\IssueLinkType::class
4953
);
5054

5155
return $linkTypes;
5256
}
57+
58+
/**
59+
* @param string $linkId
60+
* @return IssueLink
61+
* @throws \JiraCloud\JiraException
62+
* @throws \JsonMapper_Exception
63+
*/
64+
public function getIssueLink(string $linkId): IssueLink
65+
{
66+
$this->log->info("getIssueLink=\n");
67+
68+
$url = $this->uri.'/issueLink/'.$linkId;
69+
70+
$ret = $this->exec($url);
71+
72+
return $this->json_mapper->map(
73+
json_decode($ret),
74+
new IssueLink()
75+
);
76+
}
77+
78+
public function deleteIssueLink(string $linkId) : bool
79+
{
80+
$this->log->info("deleteIssueLink=\n");
81+
82+
$url = $this->uri.'/issueLink/'.$linkId;
83+
84+
$ret = $this->exec($url, '', 'DELETE');
85+
86+
return $ret;
87+
}
88+
5389
}

src/IssueLink/LinkedIssue.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace JiraCloud\IssueLink;
4+
5+
class LinkedIssue implements \JsonSerializable
6+
{
7+
public string $self;
8+
9+
public string $id;
10+
public string $key;
11+
12+
public array $fields;
13+
14+
#[\ReturnTypeWillChange]
15+
public function jsonSerialize()
16+
{
17+
return array_filter(get_object_vars($this));
18+
}
19+
}

0 commit comments

Comments
 (0)