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');