From 66da554b5e90d9024bb3d2da31edba1b3339f04b Mon Sep 17 00:00:00 2001 From: Hannes Papenberg Date: Fri, 13 Nov 2026 00:05:27 +0100 Subject: [PATCH] Use the repository's default branch in Contents create, update and delete The $branch parameter defaulted to 'master' and was always sent, so a call without a branch failed or targeted the wrong branch in repositories whose default branch is 'main', GitHub's default for new repositories since 2020. Default it to null and leave it out of the request, so GitHub uses the default branch. Co-Authored-By: Claude Opus 5.5 --- Tests/Package/Repositories/ContentsTest.php | 45 +++++++++++++++++++++ docs/overview.md | 6 +++ src/Package/Repositories/Contents.php | 27 ++++++++----- 3 files changed, 69 insertions(+), 9 deletions(-) diff --git a/Tests/Package/Repositories/ContentsTest.php b/Tests/Package/Repositories/ContentsTest.php index b0e84e8e..16ca6abe 100644 --- a/Tests/Package/Repositories/ContentsTest.php +++ b/Tests/Package/Repositories/ContentsTest.php @@ -194,6 +194,21 @@ public function testCreate() ); } + /** + * Tests that the Create method leaves out the branch when none is given, so GitHub uses the default branch. + * + * @return void + */ + public function testCreateWithoutBranch() + { + $this->client->expects($this->once()) + ->method('put') + ->with('/repos/joomla/joomla-platform/contents/src/foo', '{"message":"my Message","content":"ABC123def"}') + ->willReturn($this->getResponseObject($this->sampleString, 201)); + + $this->object->create('joomla', 'joomla-platform', 'src/foo', 'my Message', 'ABC123def'); + } + /** * Tests the create method with missing author e-mail. * @@ -270,6 +285,21 @@ public function testUpdate() ); } + /** + * Tests that the Update method leaves out the branch when none is given, so GitHub uses the default branch. + * + * @return void + */ + public function testUpdateWithoutBranch() + { + $this->client->expects($this->once()) + ->method('put') + ->with('/repos/joomla/joomla-platform/contents/src/foo', '{"message":"my Message","content":"ABC123def","sha":"abcd1234"}') + ->willReturn($this->response); + + $this->object->update('joomla', 'joomla-platform', 'src/foo', 'my Message', 'ABC123def', 'abcd1234'); + } + /** * Tests the update method with missing author e-mail. * @@ -347,6 +377,21 @@ public function testDelete() ); } + /** + * Tests that the Delete method leaves out the branch when none is given, so GitHub uses the default branch. + * + * @return void + */ + public function testDeleteWithoutBranch() + { + $this->client->expects($this->once()) + ->method('delete') + ->with('/repos/joomla/joomla-platform/contents/src/foo', [], null, '{"message":"my Message","sha":"abcd1234"}') + ->willReturn($this->response); + + $this->object->delete('joomla', 'joomla-platform', 'src/foo', 'my Message', 'abcd1234'); + } + /** * Tests the delete method with missing author e-mail. * diff --git a/docs/overview.md b/docs/overview.md index cfd53634..64766685 100644 --- a/docs/overview.md +++ b/docs/overview.md @@ -1,3 +1,9 @@ # Github API package TODO + +## Creating, updating and deleting files + +`$github->repositories->contents->create()`, `update()` and `delete()` commit to the repository's default branch +when no `$branch` is given. Before, they always sent `master`, which fails for repositories whose default branch is +`main`, GitHub's default for new repositories since 2020. diff --git a/src/Package/Repositories/Contents.php b/src/Package/Repositories/Contents.php index f863714d..9fc8ff9a 100644 --- a/src/Package/Repositories/Contents.php +++ b/src/Package/Repositories/Contents.php @@ -138,7 +138,7 @@ public function getArchiveLink($owner, $repo, $archiveFormat = 'zipball', $ref = * @param string $path The content path. * @param string $message The commit message. * @param string $content The new file content, Base64 encoded. - * @param string $branch The branch name. If not provided, uses the repository’s default branch (usually master). + * @param string $branch The branch name. If not provided, uses the repository’s default branch. * @param string $authorName The name of the author of the commit * @param string $authorEmail The email of the author of the commit * @param string $committerName The name of the committer of the commit @@ -155,7 +155,7 @@ public function create( $path, $message, $content, - $branch = 'master', + $branch = null, $authorName = '', $authorEmail = '', $committerName = '', @@ -167,9 +167,12 @@ public function create( $data = [ 'message' => $message, 'content' => $content, - 'branch' => $branch, ]; + if ($branch) { + $data['branch'] = $branch; + } + if ($authorName) { if (!$authorEmail) { throw new \UnexpectedValueException('You must provide an author e-mail if you supply an author name'); @@ -213,7 +216,7 @@ public function create( * @param string $message The commit message. * @param string $content The new file content, Base64 encoded. * @param string $sha The blob SHA of the file being replaced. - * @param string $branch The branch name. If not provided, uses the repository’s default branch (usually master). + * @param string $branch The branch name. If not provided, uses the repository’s default branch. * @param string $authorName The name of the author of the commit * @param string $authorEmail The email of the author of the commit * @param string $committerName The name of the committer of the commit @@ -231,7 +234,7 @@ public function update( $message, $content, $sha, - $branch = 'master', + $branch = null, $authorName = '', $authorEmail = '', $committerName = '', @@ -244,9 +247,12 @@ public function update( 'message' => $message, 'content' => $content, 'sha' => $sha, - 'branch' => $branch, ]; + if ($branch) { + $data['branch'] = $branch; + } + if ($authorName) { if (!$authorEmail) { throw new \UnexpectedValueException('You must provide an author e-mail if you supply an author name'); @@ -282,7 +288,7 @@ public function update( * @param string $path The content path. * @param string $message The commit message. * @param string $sha The blob SHA of the file being replaced. - * @param string $branch The branch name. If not provided, uses the repository’s default branch (usually master). + * @param string $branch The branch name. If not provided, uses the repository’s default branch. * @param string $authorName The name of the author of the commit * @param string $authorEmail The email of the author of the commit * @param string $committerName The name of the committer of the commit @@ -299,7 +305,7 @@ public function delete( $path, $message, $sha, - $branch = 'master', + $branch = null, $authorName = '', $authorEmail = '', $committerName = '', @@ -311,9 +317,12 @@ public function delete( $data = [ 'message' => $message, 'sha' => $sha, - 'branch' => $branch, ]; + if ($branch) { + $data['branch'] = $branch; + } + if ($authorName) { if (!$authorEmail) { throw new \UnexpectedValueException('You must provide an author e-mail if you supply an author name');