From a652c58cf5e06c3d851c0feda2e884f376f4766b Mon Sep 17 00:00:00 2001 From: Hannes Papenberg Date: Fri, 13 Nov 2026 00:20:39 +0100 Subject: [PATCH] Use the gist comment routes with the gist ID Gists\Comments::get(), edit() and delete() requested /gists/comments/{comment_id}, which GitHub has replaced with /gists/{gist_id}/comments/{comment_id}. Accept the gist ID as an optional last argument and use the current route when it is given; calling the methods without it is deprecated. Co-Authored-By: Claude Opus 5.5 --- Tests/Package/Gists/CommentsTest.php | 73 ++++++++++++++++++++++++++++ composer.json | 3 +- docs/overview.md | 14 ++++++ src/Package/Gists/Comments.php | 52 ++++++++++++++++---- 4 files changed, 131 insertions(+), 11 deletions(-) create mode 100644 Tests/Package/Gists/CommentsTest.php diff --git a/Tests/Package/Gists/CommentsTest.php b/Tests/Package/Gists/CommentsTest.php new file mode 100644 index 00000000..377d74d7 --- /dev/null +++ b/Tests/Package/Gists/CommentsTest.php @@ -0,0 +1,73 @@ +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); + } +} diff --git a/composer.json b/composer.json index 49ad2e0a..d44c9b57 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/docs/overview.md b/docs/overview.md index cfd53634..56a7963e 100644 --- a/docs/overview.md +++ b/docs/overview.md @@ -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. diff --git a/src/Package/Gists/Comments.php b/src/Package/Gists/Comments.php index 004cddd2..45bc4733 100644 --- a/src/Package/Gists/Comments.php +++ b/src/Package/Gists/Comments.php @@ -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); @@ -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( @@ -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))); @@ -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; + } }