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
24 changes: 18 additions & 6 deletions Tests/Package/RepositoriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,7 @@ public function testCreate()
->method('post')
->with(
'/user/repos',
'{"name":"joomla-test","description":"","homepage":"","private":false,"has_issues":false,'
. '"has_wiki":false,"has_downloads":false,"team_id":0,"auto_init":false,"gitignore_template":""}',
'{"name":"joomla-test"}',
[],
0
)
Expand All @@ -229,8 +228,7 @@ public function testCreateWithOrg()
->method('post')
->with(
'/orgs/joomla.org/repos',
'{"name":"joomla-test","description":"","homepage":"","private":false,"has_issues":false,'
. '"has_wiki":false,"has_downloads":false,"team_id":0,"auto_init":false,"gitignore_template":""}',
'{"name":"joomla-test"}',
[],
0
)
Expand Down Expand Up @@ -287,8 +285,7 @@ public function testEdit()
->method('patch')
->with(
'/repos/joomla/joomla-test',
'{"name":"joomla-test-1","description":"","homepage":"","private":'
. 'false,"has_issues":false,"has_wiki":false,"has_downloads":false,"default_branch":""}',
'{"name":"joomla-test-1"}',
[]
)
->willReturn($this->response);
Expand All @@ -301,6 +298,21 @@ public function testEdit()
);
}

/**
* Tests that the Edit method sends only the given values
*
* @return void
*/
public function testEditSendsOnlyGivenValues()
{
$this->client->expects($this->once())
->method('patch')
->with('/repos/joomla/joomla-test', '{"name":"joomla-test","has_wiki":false}')
->willReturn($this->response);

$this->object->edit('joomla', 'joomla-test', 'joomla-test', hasWiki: false);
}

/**
* Tests the GetListContributors method.
*
Expand Down
15 changes: 15 additions & 0 deletions Tests/Package/UsersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,21 @@ public function testEdit()
);
}

/**
* Tests that the edit method sends only the given values
*
* @return void
*/
public function testEditSendsOnlyGivenValues()
{
$this->client->expects($this->once())
->method('patch')
->with('/user', '{"bio":"There once..."}')
->willReturn($this->response);

$this->object->edit(bio: 'There once...');
}

/**
* Tests the getUsers method
*
Expand Down
16 changes: 16 additions & 0 deletions docs/overview.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# Github API package

TODO

## Creating and editing repositories and the user profile

`$github->repositories->create()`, `$github->repositories->edit()` and `$github->users->edit()` send only the values
that are given. Leave a parameter out (or pass `null`) to keep the current setting, or GitHub's default when creating:

```php
// Renames the repository; description, visibility, issues and wiki stay as they are
$github->repositories->edit('joomla', 'old-name', 'new-name');

// Changes only the bio
$github->users->edit(bio: 'Joomla! contributor');
```

Before, every parameter that was not given was sent with its default (`''` or `false`), so a plain rename cleared
the description, disabled issues and the wiki and made a private repository public.
41 changes: 25 additions & 16 deletions src/Package/Repositories.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ public function getList($id = 0)
* Create.
*
* Create a new repository for the authenticated user or an organization. OAuth users must supply repo scope.
* Only the values that are given (not null) are sent; GitHub's defaults apply to the others.
*
* @param string $name The repository name.
* @param string $org The organization name (if needed).
Expand All @@ -198,15 +199,15 @@ public function getList($id = 0)
public function create(
$name,
$org = '',
$description = '',
$homepage = '',
$private = false,
$hasIssues = false,
$hasWiki = false,
$hasDownloads = false,
$teamId = 0,
$autoInit = false,
$gitignoreTemplate = ''
$description = null,
$homepage = null,
$private = null,
$hasIssues = null,
$hasWiki = null,
$hasDownloads = null,
$teamId = null,
$autoInit = null,
$gitignoreTemplate = null
) {
$path = ($org)
// Create a repository for an organization
Expand All @@ -227,6 +228,9 @@ public function create(
'gitignore_template' => $gitignoreTemplate,
];

// Only send the values that are given, so GitHub's defaults apply to the others.
$data = array_filter($data, static fn ($value) => $value !== null);

// Send the request.
return $this->processResponse(
$this->client->post($this->fetchUrl($path), json_encode($data)),
Expand Down Expand Up @@ -258,6 +262,8 @@ public function get($owner, $repo)
/**
* Edit.
*
* Only the values that are given (not null) are sent; the other settings of the repository are left unchanged.
*
* @param string $owner Repository owner.
* @param string $repo Repository name.
* @param string $name The repository name.
Expand All @@ -278,13 +284,13 @@ public function edit(
$owner,
$repo,
$name,
$description = '',
$homepage = '',
$private = false,
$hasIssues = false,
$hasWiki = false,
$hasDownloads = false,
$defaultBranch = ''
$description = null,
$homepage = null,
$private = null,
$hasIssues = null,
$hasWiki = null,
$hasDownloads = null,
$defaultBranch = null
) {
$path = '/repos/' . $owner . '/' . $repo;

Expand All @@ -299,6 +305,9 @@ public function edit(
'default_branch' => $defaultBranch,
];

// Only send the values that are given, so the other settings of the repository are left unchanged.
$data = array_filter($data, static fn ($value) => $value !== null);

// Send the request.
return $this->processResponse(
$this->client->patch($this->fetchUrl($path), json_encode($data))
Expand Down
7 changes: 6 additions & 1 deletion src/Package/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public function getAuthenticatedUser()
/**
* Update the authenticated user.
*
* Only the values that are given (not null) are sent; the other profile fields are left unchanged.
*
* @param string $name The full name
* @param string $email The email
* @param string $blog The blog
Expand All @@ -80,7 +82,7 @@ public function getAuthenticatedUser()
* @since 1.0
* @throws \DomainException
*/
public function edit($name = '', $email = '', $blog = '', $company = '', $location = '', $hireable = '', $bio = '')
public function edit($name = null, $email = null, $blog = null, $company = null, $location = null, $hireable = null, $bio = null)
{
$data = [
'name' => $name,
Expand All @@ -92,6 +94,9 @@ public function edit($name = '', $email = '', $blog = '', $company = '', $locati
'bio' => $bio,
];

// Only send the values that are given, so the other profile fields are left unchanged.
$data = array_filter($data, static fn ($value) => $value !== null);

// Build the request path.
$path = '/user';

Expand Down
Loading