diff --git a/Tests/Package/RepositoriesTest.php b/Tests/Package/RepositoriesTest.php index 30649952..9b051efb 100644 --- a/Tests/Package/RepositoriesTest.php +++ b/Tests/Package/RepositoriesTest.php @@ -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 ) @@ -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 ) @@ -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); @@ -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. * diff --git a/Tests/Package/UsersTest.php b/Tests/Package/UsersTest.php index 1d8f965f..cf87a849 100755 --- a/Tests/Package/UsersTest.php +++ b/Tests/Package/UsersTest.php @@ -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 * diff --git a/docs/overview.md b/docs/overview.md index cfd53634..150da99f 100644 --- a/docs/overview.md +++ b/docs/overview.md @@ -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. diff --git a/src/Package/Repositories.php b/src/Package/Repositories.php index c79bd231..ec276139 100644 --- a/src/Package/Repositories.php +++ b/src/Package/Repositories.php @@ -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). @@ -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 @@ -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)), @@ -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. @@ -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; @@ -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)) diff --git a/src/Package/Users.php b/src/Package/Users.php index 153d8bf6..ac8f5045 100644 --- a/src/Package/Users.php +++ b/src/Package/Users.php @@ -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 @@ -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, @@ -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';