Skip to content

Commit 6b17ff1

Browse files
committed
- fixed #16 - now work well add/update comment function with AtlassianDocumentFormat
- add function type parameter in the IssueService for more source code readability
1 parent 087c5a0 commit 6b17ff1

3 files changed

Lines changed: 152 additions & 76 deletions

File tree

README.md

Lines changed: 93 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,30 +1149,56 @@ Not working at this time.!
11491149
<?php
11501150
require 'vendor/autoload.php';
11511151

1152-
use JiraCloud\Issue\IssueService;
1152+
use DH\Adf\Node\Block\Document;
1153+
use JiraCloud\ADF\AtlassianDocumentFormat;
11531154
use JiraCloud\Issue\Comment;
1154-
use JiraCloud\JiraException;
1155+
use JiraCloud\Issue\IssueService;
11551156

11561157
$issueKey = 'TEST-879';
11571158

11581159
try {
11591160
$comment = new Comment();
11601161

1161-
$body = <<<COMMENT
1162-
Adds a new comment to an issue.
1163-
* Bullet 1
1164-
* Bullet 2
1165-
** sub Bullet 1
1166-
** sub Bullet 2
1167-
* Bullet 3
1168-
COMMENT;
1169-
1170-
$comment->setBody($body)
1171-
->setVisibilityAsString('role', 'Users');
1162+
$code =<<<CODE
1163+
<?php
1164+
\$i = 123;
1165+
\$a = ['hello', 'world', ];
1166+
var_dump([\$i => \$a]);
1167+
CODE;
1168+
1169+
$doc = (new Document())
1170+
->heading(1) // header level 1, can have child blocks (needs to be closed with `->end()`)
1171+
->text('h1') // simple unstyled text, cannot have child blocks (no `->end()` needed)
1172+
->end() // closes `heading` node
1173+
->paragraph() // paragraph, can have child blocks (needs to be closed with `->end()`)
1174+
->text('we’re ') // simple unstyled text
1175+
->strong('support') // text node embedding a `strong` mark
1176+
->text(' ') // simple unstyled text
1177+
->em('markdown') // text node embedding a `em` mark
1178+
->text('. ') // simple unstyled text
1179+
->underline('like') // text node embedding a `underline` mark
1180+
->text(' this.') // simple unstyled text
1181+
->text(' date=' . date("Y-m-d H:i:s"))
1182+
->end() // closes `paragraph` node
1183+
->heading(2) // header level 2
1184+
->text('h2') // simple unstyled text
1185+
->end() // closes `heading` node
1186+
->heading(3)
1187+
->text('heading 3')
1188+
->end()
1189+
->paragraph() // paragraph
1190+
->text('also support heading.') // simple unstyled text
1191+
->end() // closes `paragraph` node
1192+
->codeblock('php')
1193+
->text($code)
1194+
->end()
11721195
;
11731196

1197+
$comment->setBodyByAtlassianDocumentFormat($doc);
1198+
11741199
$issueService = new IssueService();
1175-
$ret = $issueService->addComment($issueKey, $comment);
1200+
$ret = $issueService->addComment($subTaskIssueKey, $comment);
1201+
11761202
print_r($ret);
11771203
} catch (JiraCloud\JiraException $e) {
11781204
$this->assertTrue(FALSE, 'add Comment Failed : ' . $e->getMessage());
@@ -1182,7 +1208,7 @@ COMMENT;
11821208

11831209
#### Get comment
11841210

1185-
[See Jira API reference](https://docs.atlassian.com/software/jira/docs/api/REST/latest/#api/2/issue-getComments)
1211+
[See Jira API reference](https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-comments/#api-rest-api-3-issue-issueidorkey-comment-get)
11861212

11871213
```php
11881214
<?php
@@ -1204,8 +1230,11 @@ try {
12041230

12051231
$comments = $issueService->getComments($issueKey, $param);
12061232

1207-
var_dump($comments);
1208-
1233+
// $comments->comments is a real array of comment
1234+
foreach ($comments->comments as $comment){
1235+
var_dump(["id" => comment->id, "self" => $comment->self]);
1236+
}
1237+
12091238
} catch (JiraCloud\JiraException $e) {
12101239
$this->assertTrue(false, 'get Comment Failed : '.$e->getMessage());
12111240
}
@@ -1243,7 +1272,7 @@ try {
12431272

12441273
#### Delete comment
12451274

1246-
[See Jira API reference](https://docs.atlassian.com/software/jira/docs/api/REST/latest/#api/2/issue-deleteComment)
1275+
[See Jira API reference](https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-comments/#api-rest-api-3-issue-issueidorkey-comment-id-delete)
12471276

12481277
```php
12491278
<?php
@@ -1269,7 +1298,7 @@ try {
12691298

12701299
#### Update comment
12711300

1272-
[See Jira API reference](https://docs.atlassian.com/software/jira/docs/api/REST/latest/#api/2/issue-updateComment)
1301+
[See Jira API reference](https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-comments/#api-rest-api-3-issue-issueidorkey-comment-id-put)
12731302

12741303
```php
12751304
<?php
@@ -1287,9 +1316,51 @@ try {
12871316
$issueService = new IssueService();
12881317

12891318
$comment = new Comment();
1290-
$comment->setBody('Updated comments');
1291-
1292-
$issueService->updateComment($issueKey, $commentId, $comment);
1319+
1320+
$code =<<<CODE
1321+
# This program adds two numbers
1322+
num1 = 1.5
1323+
num2 = 6.3
1324+
1325+
# Add two numbers
1326+
sum = num1 + num2
1327+
1328+
# Display the sum
1329+
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
1330+
CODE;
1331+
1332+
$doc = (new Document())
1333+
->heading(2) // header level 1, can have child blocks (needs to be closed with `->end()`)
1334+
->text('h2') // simple unstyled text, cannot have child blocks (no `->end()` needed)
1335+
->end() // closes `heading` node
1336+
->heading(3) // header level 2
1337+
->text('h3') // simple unstyled text
1338+
->end() // closes `heading` node
1339+
->heading(4)
1340+
->text('heading 4')
1341+
->end()
1342+
->paragraph() // paragraph
1343+
->text('also support heading.') // simple unstyled text
1344+
->end() // closes `paragraph` node
1345+
->codeblock('python')
1346+
->text($code)
1347+
->end()
1348+
->paragraph() // paragraph, can have child blocks (needs to be closed with `->end()`)
1349+
->text('we’re ') // simple unstyled text
1350+
->strong('support') // text node embedding a `strong` mark
1351+
->text(' ') // simple unstyled text
1352+
->em('markdown') // text node embedding a `em` mark
1353+
->text('. ') // simple unstyled text
1354+
->underline('like') // text node embedding a `underline` mark
1355+
->text(' this.') // simple unstyled text
1356+
->text(' date=' . date("Y-m-d H:i:s"))
1357+
->end() // closes `paragraph` node
1358+
;
1359+
1360+
$comment->setBodyByAtlassianDocumentFormat($doc);
1361+
1362+
$issueService = new IssueService();
1363+
$ret = $issueService->updateComment($issueKey, $comment_id, $comment);
12931364

12941365
} catch (JiraCloud\JiraException $e) {
12951366
$this->assertTrue(false, 'Update comment Failed : '.$e->getMessage());

src/Issue/Comment.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
namespace JiraCloud\Issue;
44

55
use DateTimeInterface;
6+
use DH\Adf\Node\Block\Document;
7+
use DH\Adf\Node\Node;
8+
use JiraCloud\ADF\AtlassianDocumentFormat;
9+
use JiraCloud\JiraException;
610

711
class Comment implements \JsonSerializable
812
{
@@ -14,7 +18,7 @@ class Comment implements \JsonSerializable
1418

1519
public Reporter $author;
1620

17-
public string $body;
21+
public array $body;
1822

1923
public Reporter $updateAuthor;
2024

@@ -24,16 +28,32 @@ class Comment implements \JsonSerializable
2428

2529
public ?Visibility $visibility = null;
2630

27-
public function setBody(string $body): static
31+
public bool $jsdPublic;
32+
33+
/**
34+
* mapping function for json_mapper
35+
* @param \stdClass $body
36+
* @return $this
37+
*/
38+
public function setBody(\stdClass $body) : static
39+
{
40+
$this->body = json_decode(json_encode($body), true);
41+
42+
return $this;
43+
}
44+
45+
public function setBodyByAtlassianDocumentFormat(Document|Node $body): static
2846
{
29-
$this->body = $body;
47+
$this->body = $body->jsonSerialize();
3048

3149
return $this;
3250
}
3351

3452
#[\ReturnTypeWillChange]
3553
public function jsonSerialize(): array
3654
{
37-
return array_filter(get_object_vars($this));
55+
return array_filter(get_object_vars($this), function ($var) {
56+
return !is_null($var);
57+
});
3858
}
3959
}

0 commit comments

Comments
 (0)