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
45 changes: 45 additions & 0 deletions Tests/Package/Repositories/ContentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
6 changes: 6 additions & 0 deletions docs/overview.md
Original file line number Diff line number Diff line change
@@ -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.
27 changes: 18 additions & 9 deletions src/Package/Repositories/Contents.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -155,7 +155,7 @@ public function create(
$path,
$message,
$content,
$branch = 'master',
$branch = null,
$authorName = '',
$authorEmail = '',
$committerName = '',
Expand All @@ -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');
Expand Down Expand Up @@ -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
Expand All @@ -231,7 +234,7 @@ public function update(
$message,
$content,
$sha,
$branch = 'master',
$branch = null,
$authorName = '',
$authorEmail = '',
$committerName = '',
Expand All @@ -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');
Expand Down Expand Up @@ -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
Expand All @@ -299,7 +305,7 @@ public function delete(
$path,
$message,
$sha,
$branch = 'master',
$branch = null,
$authorName = '',
$authorEmail = '',
$committerName = '',
Expand All @@ -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');
Expand Down
Loading