Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions Tests/Package/Gists/CommentsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/**
* @copyright Copyright (C) 2005 - 2026 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

namespace Joomla\Github\Tests\Package\Gists;

use Joomla\Github\Package\Gists\Comments;
use Joomla\Github\Tests\Stub\GitHubTestCase;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;

/**
* Tests the routes of the gist comment methods.
*/
class CommentsTest extends GitHubTestCase
{
/**
* @var Comments
*/
protected $object;

protected function setUp(): void
{
parent::setUp();

$this->object = new Comments($this->options, $this->client);
}

public function testGetWithGistId()
{
$this->client->expects($this->once())
->method('get')
->with('/gists/aa5a315d61ae9438b18d/comments/523')
->willReturn($this->response);

$this->assertEquals(json_decode($this->sampleString), $this->object->get(523, 'aa5a315d61ae9438b18d'));
}

public function testEditWithGistId()
{
$this->client->expects($this->once())
->method('patch')
->with('/gists/aa5a315d61ae9438b18d/comments/523', '{"body":"Updated"}')
->willReturn($this->response);

$this->assertEquals(json_decode($this->sampleString), $this->object->edit(523, 'Updated', 'aa5a315d61ae9438b18d'));
}

public function testDeleteWithGistId()
{
$this->client->expects($this->once())
->method('delete')
->with('/gists/aa5a315d61ae9438b18d/comments/523')
->willReturn($this->getResponseObject('', 204));

$this->object->delete(523, 'aa5a315d61ae9438b18d');
}

#[IgnoreDeprecations]
public function testWithoutGistIdIsDeprecated()
{
$this->expectUserDeprecationMessageMatches('/Calling .*Comments::get\(\) without the gist ID is deprecated/');

$this->client->expects($this->once())
->method('get')
->with('/gists/comments/523')
->willReturn($this->response);

$this->object->get(523);
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"php": "^8.3.0",
"joomla/http": "^4.0",
"joomla/registry": "^4.0",
"joomla/uri": "^4.0"
"joomla/uri": "^4.0",
"symfony/deprecation-contracts": "^2|^3"
},
"require-dev": {
"phpunit/phpunit": "^12.2.7",
Expand Down
14 changes: 14 additions & 0 deletions docs/overview.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Github API package

TODO

## Gist comments

`$github->gists->comments->get()`, `edit()` and `delete()` take the gist ID as their last argument, for GitHub's
`/gists/{gist_id}/comments/{comment_id}` route:

```php
$comment = $github->gists->comments->get($commentId, $gistId);
$github->gists->comments->edit($commentId, 'New text', $gistId);
$github->gists->comments->delete($commentId, $gistId);
```

Without the gist ID they use the old `/gists/comments/{comment_id}` route, which GitHub has removed; that is
deprecated, and the gist ID will be required in 5.0.
52 changes: 42 additions & 10 deletions src/Package/Gists/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,18 @@ public function create($gistId, $body)
/**
* Delete a comment.
*
* @param integer $commentId The id of the comment to delete.
* @param integer $commentId The id of the comment to delete.
* @param string|null $gistId The ID of the gist. Deprecated to leave out, it will be required in 5.0.
*
* @return void
*
* @since 1.0
* @throws \DomainException
*/
public function delete($commentId)
public function delete($commentId, $gistId = null)
{
// Build the request path.
$path = '/gists/comments/' . (int) $commentId;
$path = $this->commentPath($commentId, $gistId, __METHOD__);

// Send the request.
$this->processResponse($this->client->delete($this->fetchUrl($path)), 204);
Expand All @@ -69,18 +70,19 @@ public function delete($commentId)
/**
* Edit a comment.
*
* @param integer $commentId The id of the comment to update.
* @param string $body The new body text for the comment.
* @param integer $commentId The id of the comment to update.
* @param string $body The new body text for the comment.
* @param string|null $gistId The ID of the gist. Deprecated to leave out, it will be required in 5.0.
*
* @return object
*
* @since 1.0
* @throws \DomainException
*/
public function edit($commentId, $body)
public function edit($commentId, $body, $gistId = null)
{
// Build the request path.
$path = '/gists/comments/' . (int) $commentId;
$path = $this->commentPath($commentId, $gistId, __METHOD__);

// Build the request data.
$data = json_encode(
Expand All @@ -96,17 +98,18 @@ public function edit($commentId, $body)
/**
* Get a single comment.
*
* @param integer $commentId The comment id to get.
* @param integer $commentId The comment id to get.
* @param string|null $gistId The ID of the gist. Deprecated to leave out, it will be required in 5.0.
*
* @return object
*
* @since 1.0
* @throws \DomainException
*/
public function get($commentId)
public function get($commentId, $gistId = null)
{
// Build the request path.
$path = '/gists/comments/' . (int) $commentId;
$path = $this->commentPath($commentId, $gistId, __METHOD__);

// Send the request.
return $this->processResponse($this->client->get($this->fetchUrl($path)));
Expand All @@ -132,4 +135,33 @@ public function getList($gistId, $page = 0, $limit = 0)
// Send the request.
return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit)));
}

/**
* Build the path of a gist comment.
*
* GitHub has replaced /gists/comments/{comment_id} with /gists/{gist_id}/comments/{comment_id}.
*
* @param integer $commentId The id of the comment.
* @param string|null $gistId The ID of the gist.
* @param string $method The calling method, for the deprecation message.
*
* @return string
*
* @since __DEPLOY_VERSION__
*/
private function commentPath($commentId, $gistId, string $method): string
{
if ($gistId === null) {
trigger_deprecation(
'joomla/github',
'__DEPLOY_VERSION__',
'Calling %s() without the gist ID is deprecated, the gist ID will be required in 5.0.',
$method
);

return '/gists/comments/' . (int) $commentId;
}

return '/gists/' . rawurlencode($gistId) . '/comments/' . (int) $commentId;
}
}
Loading