diff --git a/Tests/Package/Codespaces/MachinesTest.php b/Tests/Package/Codespaces/MachinesTest.php new file mode 100644 index 00000000..5e3767dd --- /dev/null +++ b/Tests/Package/Codespaces/MachinesTest.php @@ -0,0 +1,112 @@ +object = new Machines($this->options, $this->client); + } + + /** + * Test the getListForRepo method. + * + * @return void + */ + public function testGetListForRepo() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/codespaces/machines?location=WestUs2', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getListForRepo('joomla', 'joomla-platform', ['location' => 'WestUs2']) + ); + } + + /** + * Test the getListForRepo method with an unexpected response. + * + * @return void + */ + public function testGetListForRepoFailure() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/codespaces/machines?location=WestUs2', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->getListForRepo('joomla', 'joomla-platform', ['location' => 'WestUs2']); + } + + /** + * Test the getListForCodespace method. + * + * @return void + */ + public function testGetListForCodespace() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q/machines', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getListForCodespace('monalisa-octocat-hello-world-g4wpq6h95q') + ); + } + + /** + * Test the getListForCodespace method with an unexpected response. + * + * @return void + */ + public function testGetListForCodespaceFailure() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q/machines', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->getListForCodespace('monalisa-octocat-hello-world-g4wpq6h95q'); + } +} diff --git a/Tests/Package/Codespaces/OrganizationSecretsTest.php b/Tests/Package/Codespaces/OrganizationSecretsTest.php new file mode 100644 index 00000000..13d2846e --- /dev/null +++ b/Tests/Package/Codespaces/OrganizationSecretsTest.php @@ -0,0 +1,360 @@ +object = new OrganizationSecrets($this->options, $this->client); + } + + /** + * Test the getList method. + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/codespaces/secrets?page=2&per_page=30', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getList('joomla', 2, 30) + ); + } + + /** + * Test the getList method with an unexpected response. + * + * @return void + */ + public function testGetListFailure() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/codespaces/secrets?page=2&per_page=30', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->getList('joomla', 2, 30); + } + + /** + * Test the getPublicKey method. + * + * @return void + */ + public function testGetPublicKey() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/codespaces/secrets/public-key', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getPublicKey('joomla') + ); + } + + /** + * Test the getPublicKey method with an unexpected response. + * + * @return void + */ + public function testGetPublicKeyFailure() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/codespaces/secrets/public-key', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->getPublicKey('joomla'); + } + + /** + * Test the get method. + * + * @return void + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/codespaces/secrets/GH_TOKEN', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->get('joomla', 'GH_TOKEN') + ); + } + + /** + * Test the get method with an unexpected response. + * + * @return void + */ + public function testGetFailure() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/codespaces/secrets/GH_TOKEN', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->get('joomla', 'GH_TOKEN'); + } + + /** + * Test the createOrUpdate method. + * + * @return void + */ + public function testCreateOrUpdate() + { + $this->client->expects($this->once()) + ->method('put') + ->with('/orgs/joomla/codespaces/secrets/GH_TOKEN', '{"encrypted_value":"c2VjcmV0","visibility":"selected"}', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 201)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->createOrUpdate('joomla', 'GH_TOKEN', 'selected', ['encrypted_value' => 'c2VjcmV0']) + ); + } + + /** + * Test the createOrUpdate method with a 204 response. + * + * @return void + */ + public function testCreateOrUpdateNoContent() + { + $this->client->expects($this->once()) + ->method('put') + ->with('/orgs/joomla/codespaces/secrets/GH_TOKEN', '{"encrypted_value":"c2VjcmV0","visibility":"selected"}', [], 0) + ->willReturn($this->getResponseObject('', 204)); + + $this->assertNull($this->object->createOrUpdate('joomla', 'GH_TOKEN', 'selected', ['encrypted_value' => 'c2VjcmV0'])); + } + + /** + * Test the createOrUpdate method with an unexpected response. + * + * @return void + */ + public function testCreateOrUpdateFailure() + { + $this->client->expects($this->once()) + ->method('put') + ->with('/orgs/joomla/codespaces/secrets/GH_TOKEN', '{"encrypted_value":"c2VjcmV0","visibility":"selected"}', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->createOrUpdate('joomla', 'GH_TOKEN', 'selected', ['encrypted_value' => 'c2VjcmV0']); + } + + /** + * Test the delete method. + * + * @return void + */ + public function testDelete() + { + $this->client->expects($this->once()) + ->method('delete') + ->with('/orgs/joomla/codespaces/secrets/GH_TOKEN', [], 0) + ->willReturn($this->getResponseObject('', 204)); + + $this->assertNull($this->object->delete('joomla', 'GH_TOKEN')); + } + + /** + * Test the delete method with an unexpected response. + * + * @return void + */ + public function testDeleteFailure() + { + $this->client->expects($this->once()) + ->method('delete') + ->with('/orgs/joomla/codespaces/secrets/GH_TOKEN', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->delete('joomla', 'GH_TOKEN'); + } + + /** + * Test the getListSelectedRepositories method. + * + * @return void + */ + public function testGetListSelectedRepositories() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/codespaces/secrets/GH_TOKEN/repositories?page=2&per_page=30', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getListSelectedRepositories('joomla', 'GH_TOKEN', 2, 30) + ); + } + + /** + * Test the getListSelectedRepositories method with an unexpected response. + * + * @return void + */ + public function testGetListSelectedRepositoriesFailure() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/codespaces/secrets/GH_TOKEN/repositories?page=2&per_page=30', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->getListSelectedRepositories('joomla', 'GH_TOKEN', 2, 30); + } + + /** + * Test the setSelectedRepositories method. + * + * @return void + */ + public function testSetSelectedRepositories() + { + $this->client->expects($this->once()) + ->method('put') + ->with('/orgs/joomla/codespaces/secrets/GH_TOKEN/repositories', '{"selected_repository_ids":[1296269,1296280]}', [], 0) + ->willReturn($this->getResponseObject('', 204)); + + $this->assertNull($this->object->setSelectedRepositories('joomla', 'GH_TOKEN', [1296269, 1296280])); + } + + /** + * Test the setSelectedRepositories method with an unexpected response. + * + * @return void + */ + public function testSetSelectedRepositoriesFailure() + { + $this->client->expects($this->once()) + ->method('put') + ->with('/orgs/joomla/codespaces/secrets/GH_TOKEN/repositories', '{"selected_repository_ids":[1296269,1296280]}', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->setSelectedRepositories('joomla', 'GH_TOKEN', [1296269, 1296280]); + } + + /** + * Test the addSelectedRepository method. + * + * @return void + */ + public function testAddSelectedRepository() + { + $this->client->expects($this->once()) + ->method('put') + ->with('/orgs/joomla/codespaces/secrets/GH_TOKEN/repositories/1296269', '', [], 0) + ->willReturn($this->getResponseObject('', 204)); + + $this->assertNull($this->object->addSelectedRepository('joomla', 'GH_TOKEN', 1296269)); + } + + /** + * Test the addSelectedRepository method with an unexpected response. + * + * @return void + */ + public function testAddSelectedRepositoryFailure() + { + $this->client->expects($this->once()) + ->method('put') + ->with('/orgs/joomla/codespaces/secrets/GH_TOKEN/repositories/1296269', '', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->addSelectedRepository('joomla', 'GH_TOKEN', 1296269); + } + + /** + * Test the removeSelectedRepository method. + * + * @return void + */ + public function testRemoveSelectedRepository() + { + $this->client->expects($this->once()) + ->method('delete') + ->with('/orgs/joomla/codespaces/secrets/GH_TOKEN/repositories/1296269', [], 0) + ->willReturn($this->getResponseObject('', 204)); + + $this->assertNull($this->object->removeSelectedRepository('joomla', 'GH_TOKEN', 1296269)); + } + + /** + * Test the removeSelectedRepository method with an unexpected response. + * + * @return void + */ + public function testRemoveSelectedRepositoryFailure() + { + $this->client->expects($this->once()) + ->method('delete') + ->with('/orgs/joomla/codespaces/secrets/GH_TOKEN/repositories/1296269', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->removeSelectedRepository('joomla', 'GH_TOKEN', 1296269); + } +} diff --git a/Tests/Package/Codespaces/OrganizationsTest.php b/Tests/Package/Codespaces/OrganizationsTest.php new file mode 100644 index 00000000..92c943a9 --- /dev/null +++ b/Tests/Package/Codespaces/OrganizationsTest.php @@ -0,0 +1,182 @@ +object = new Organizations($this->options, $this->client); + } + + /** + * Test the getList method. + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/codespaces?page=2&per_page=30', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getList('joomla', 2, 30) + ); + } + + /** + * Test the getList method with an unexpected response. + * + * @return void + */ + public function testGetListFailure() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/codespaces?page=2&per_page=30', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->getList('joomla', 2, 30); + } + + /** + * Test the getListForUser method. + * + * @return void + */ + public function testGetListForUser() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/members/octocat/codespaces?page=2&per_page=30', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getListForUser('joomla', 'octocat', 2, 30) + ); + } + + /** + * Test the getListForUser method with an unexpected response. + * + * @return void + */ + public function testGetListForUserFailure() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/members/octocat/codespaces?page=2&per_page=30', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->getListForUser('joomla', 'octocat', 2, 30); + } + + /** + * Test the delete method. + * + * @return void + */ + public function testDelete() + { + $this->client->expects($this->once()) + ->method('delete') + ->with('/orgs/joomla/members/octocat/codespaces/monalisa-octocat-hello-world-g4wpq6h95q', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 202)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->delete('joomla', 'octocat', 'monalisa-octocat-hello-world-g4wpq6h95q') + ); + } + + /** + * Test the delete method with an unexpected response. + * + * @return void + */ + public function testDeleteFailure() + { + $this->client->expects($this->once()) + ->method('delete') + ->with('/orgs/joomla/members/octocat/codespaces/monalisa-octocat-hello-world-g4wpq6h95q', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->delete('joomla', 'octocat', 'monalisa-octocat-hello-world-g4wpq6h95q'); + } + + /** + * Test the stop method. + * + * @return void + */ + public function testStop() + { + $this->client->expects($this->once()) + ->method('post') + ->with('/orgs/joomla/members/octocat/codespaces/monalisa-octocat-hello-world-g4wpq6h95q/stop', '', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->stop('joomla', 'octocat', 'monalisa-octocat-hello-world-g4wpq6h95q') + ); + } + + /** + * Test the stop method with an unexpected response. + * + * @return void + */ + public function testStopFailure() + { + $this->client->expects($this->once()) + ->method('post') + ->with('/orgs/joomla/members/octocat/codespaces/monalisa-octocat-hello-world-g4wpq6h95q/stop', '', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->stop('joomla', 'octocat', 'monalisa-octocat-hello-world-g4wpq6h95q'); + } +} diff --git a/Tests/Package/Codespaces/RepositorySecretsTest.php b/Tests/Package/Codespaces/RepositorySecretsTest.php new file mode 100644 index 00000000..ff013bd9 --- /dev/null +++ b/Tests/Package/Codespaces/RepositorySecretsTest.php @@ -0,0 +1,229 @@ +object = new RepositorySecrets($this->options, $this->client); + } + + /** + * Test the getList method. + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/codespaces/secrets?page=2&per_page=30', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getList('joomla', 'joomla-platform', 2, 30) + ); + } + + /** + * Test the getList method with an unexpected response. + * + * @return void + */ + public function testGetListFailure() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/codespaces/secrets?page=2&per_page=30', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->getList('joomla', 'joomla-platform', 2, 30); + } + + /** + * Test the getPublicKey method. + * + * @return void + */ + public function testGetPublicKey() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/codespaces/secrets/public-key', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getPublicKey('joomla', 'joomla-platform') + ); + } + + /** + * Test the getPublicKey method with an unexpected response. + * + * @return void + */ + public function testGetPublicKeyFailure() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/codespaces/secrets/public-key', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->getPublicKey('joomla', 'joomla-platform'); + } + + /** + * Test the get method. + * + * @return void + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/codespaces/secrets/GH_TOKEN', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->get('joomla', 'joomla-platform', 'GH_TOKEN') + ); + } + + /** + * Test the get method with an unexpected response. + * + * @return void + */ + public function testGetFailure() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/codespaces/secrets/GH_TOKEN', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->get('joomla', 'joomla-platform', 'GH_TOKEN'); + } + + /** + * Test the createOrUpdate method. + * + * @return void + */ + public function testCreateOrUpdate() + { + $this->client->expects($this->once()) + ->method('put') + ->with('/repos/joomla/joomla-platform/codespaces/secrets/GH_TOKEN', '{"encrypted_value":"c2VjcmV0"}', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 201)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->createOrUpdate('joomla', 'joomla-platform', 'GH_TOKEN', ['encrypted_value' => 'c2VjcmV0']) + ); + } + + /** + * Test the createOrUpdate method with a 204 response. + * + * @return void + */ + public function testCreateOrUpdateNoContent() + { + $this->client->expects($this->once()) + ->method('put') + ->with('/repos/joomla/joomla-platform/codespaces/secrets/GH_TOKEN', '{"encrypted_value":"c2VjcmV0"}', [], 0) + ->willReturn($this->getResponseObject('', 204)); + + $this->assertNull($this->object->createOrUpdate('joomla', 'joomla-platform', 'GH_TOKEN', ['encrypted_value' => 'c2VjcmV0'])); + } + + /** + * Test the createOrUpdate method with an unexpected response. + * + * @return void + */ + public function testCreateOrUpdateFailure() + { + $this->client->expects($this->once()) + ->method('put') + ->with('/repos/joomla/joomla-platform/codespaces/secrets/GH_TOKEN', '{"encrypted_value":"c2VjcmV0"}', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->createOrUpdate('joomla', 'joomla-platform', 'GH_TOKEN', ['encrypted_value' => 'c2VjcmV0']); + } + + /** + * Test the delete method. + * + * @return void + */ + public function testDelete() + { + $this->client->expects($this->once()) + ->method('delete') + ->with('/repos/joomla/joomla-platform/codespaces/secrets/GH_TOKEN', [], 0) + ->willReturn($this->getResponseObject('', 204)); + + $this->assertNull($this->object->delete('joomla', 'joomla-platform', 'GH_TOKEN')); + } + + /** + * Test the delete method with an unexpected response. + * + * @return void + */ + public function testDeleteFailure() + { + $this->client->expects($this->once()) + ->method('delete') + ->with('/repos/joomla/joomla-platform/codespaces/secrets/GH_TOKEN', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->delete('joomla', 'joomla-platform', 'GH_TOKEN'); + } +} diff --git a/Tests/Package/Codespaces/SecretsTest.php b/Tests/Package/Codespaces/SecretsTest.php new file mode 100644 index 00000000..6edf23d0 --- /dev/null +++ b/Tests/Package/Codespaces/SecretsTest.php @@ -0,0 +1,360 @@ +object = new Secrets($this->options, $this->client); + } + + /** + * Test the getList method. + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/user/codespaces/secrets?page=2&per_page=30', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getList(2, 30) + ); + } + + /** + * Test the getList method with an unexpected response. + * + * @return void + */ + public function testGetListFailure() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/user/codespaces/secrets?page=2&per_page=30', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->getList(2, 30); + } + + /** + * Test the getPublicKey method. + * + * @return void + */ + public function testGetPublicKey() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/user/codespaces/secrets/public-key', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getPublicKey() + ); + } + + /** + * Test the getPublicKey method with an unexpected response. + * + * @return void + */ + public function testGetPublicKeyFailure() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/user/codespaces/secrets/public-key', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->getPublicKey(); + } + + /** + * Test the get method. + * + * @return void + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/user/codespaces/secrets/GH_TOKEN', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->get('GH_TOKEN') + ); + } + + /** + * Test the get method with an unexpected response. + * + * @return void + */ + public function testGetFailure() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/user/codespaces/secrets/GH_TOKEN', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->get('GH_TOKEN'); + } + + /** + * Test the createOrUpdate method. + * + * @return void + */ + public function testCreateOrUpdate() + { + $this->client->expects($this->once()) + ->method('put') + ->with('/user/codespaces/secrets/GH_TOKEN', '{"encrypted_value":"c2VjcmV0","key_id":"012345678912345678"}', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 201)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->createOrUpdate('GH_TOKEN', '012345678912345678', ['encrypted_value' => 'c2VjcmV0']) + ); + } + + /** + * Test the createOrUpdate method with a 204 response. + * + * @return void + */ + public function testCreateOrUpdateNoContent() + { + $this->client->expects($this->once()) + ->method('put') + ->with('/user/codespaces/secrets/GH_TOKEN', '{"encrypted_value":"c2VjcmV0","key_id":"012345678912345678"}', [], 0) + ->willReturn($this->getResponseObject('', 204)); + + $this->assertNull($this->object->createOrUpdate('GH_TOKEN', '012345678912345678', ['encrypted_value' => 'c2VjcmV0'])); + } + + /** + * Test the createOrUpdate method with an unexpected response. + * + * @return void + */ + public function testCreateOrUpdateFailure() + { + $this->client->expects($this->once()) + ->method('put') + ->with('/user/codespaces/secrets/GH_TOKEN', '{"encrypted_value":"c2VjcmV0","key_id":"012345678912345678"}', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->createOrUpdate('GH_TOKEN', '012345678912345678', ['encrypted_value' => 'c2VjcmV0']); + } + + /** + * Test the delete method. + * + * @return void + */ + public function testDelete() + { + $this->client->expects($this->once()) + ->method('delete') + ->with('/user/codespaces/secrets/GH_TOKEN', [], 0) + ->willReturn($this->getResponseObject('', 204)); + + $this->assertNull($this->object->delete('GH_TOKEN')); + } + + /** + * Test the delete method with an unexpected response. + * + * @return void + */ + public function testDeleteFailure() + { + $this->client->expects($this->once()) + ->method('delete') + ->with('/user/codespaces/secrets/GH_TOKEN', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->delete('GH_TOKEN'); + } + + /** + * Test the getListSelectedRepositories method. + * + * @return void + */ + public function testGetListSelectedRepositories() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/user/codespaces/secrets/GH_TOKEN/repositories', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getListSelectedRepositories('GH_TOKEN') + ); + } + + /** + * Test the getListSelectedRepositories method with an unexpected response. + * + * @return void + */ + public function testGetListSelectedRepositoriesFailure() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/user/codespaces/secrets/GH_TOKEN/repositories', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->getListSelectedRepositories('GH_TOKEN'); + } + + /** + * Test the setSelectedRepositories method. + * + * @return void + */ + public function testSetSelectedRepositories() + { + $this->client->expects($this->once()) + ->method('put') + ->with('/user/codespaces/secrets/GH_TOKEN/repositories', '{"selected_repository_ids":[1296269,1296280]}', [], 0) + ->willReturn($this->getResponseObject('', 204)); + + $this->assertNull($this->object->setSelectedRepositories('GH_TOKEN', [1296269, 1296280])); + } + + /** + * Test the setSelectedRepositories method with an unexpected response. + * + * @return void + */ + public function testSetSelectedRepositoriesFailure() + { + $this->client->expects($this->once()) + ->method('put') + ->with('/user/codespaces/secrets/GH_TOKEN/repositories', '{"selected_repository_ids":[1296269,1296280]}', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->setSelectedRepositories('GH_TOKEN', [1296269, 1296280]); + } + + /** + * Test the addSelectedRepository method. + * + * @return void + */ + public function testAddSelectedRepository() + { + $this->client->expects($this->once()) + ->method('put') + ->with('/user/codespaces/secrets/GH_TOKEN/repositories/1296269', '', [], 0) + ->willReturn($this->getResponseObject('', 204)); + + $this->assertNull($this->object->addSelectedRepository('GH_TOKEN', 1296269)); + } + + /** + * Test the addSelectedRepository method with an unexpected response. + * + * @return void + */ + public function testAddSelectedRepositoryFailure() + { + $this->client->expects($this->once()) + ->method('put') + ->with('/user/codespaces/secrets/GH_TOKEN/repositories/1296269', '', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->addSelectedRepository('GH_TOKEN', 1296269); + } + + /** + * Test the removeSelectedRepository method. + * + * @return void + */ + public function testRemoveSelectedRepository() + { + $this->client->expects($this->once()) + ->method('delete') + ->with('/user/codespaces/secrets/GH_TOKEN/repositories/1296269', [], 0) + ->willReturn($this->getResponseObject('', 204)); + + $this->assertNull($this->object->removeSelectedRepository('GH_TOKEN', 1296269)); + } + + /** + * Test the removeSelectedRepository method with an unexpected response. + * + * @return void + */ + public function testRemoveSelectedRepositoryFailure() + { + $this->client->expects($this->once()) + ->method('delete') + ->with('/user/codespaces/secrets/GH_TOKEN/repositories/1296269', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->removeSelectedRepository('GH_TOKEN', 1296269); + } +} diff --git a/Tests/Package/CodespacesTest.php b/Tests/Package/CodespacesTest.php new file mode 100644 index 00000000..c12c468d --- /dev/null +++ b/Tests/Package/CodespacesTest.php @@ -0,0 +1,656 @@ +object = new Codespaces($this->options, $this->client); + } + + /** + * Test the getList method. + * + * @return void + */ + public function testGetList() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/user/codespaces?page=2&per_page=30&repository_id=1296269', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getList(['repository_id' => 1296269], 2, 30) + ); + } + + /** + * Test the getList method with an unexpected response. + * + * @return void + */ + public function testGetListFailure() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/user/codespaces?page=2&per_page=30&repository_id=1296269', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->getList(['repository_id' => 1296269], 2, 30); + } + + /** + * Test the create method. + * + * @return void + */ + public function testCreate() + { + $this->client->expects($this->once()) + ->method('post') + ->with('/user/codespaces', '{"ref":"main","repository_id":1296269}', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 201)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->create(1296269, ['ref' => 'main']) + ); + } + + /** + * Test the create method with a 202 response. + * + * @return void + */ + public function testCreateStatus202() + { + $this->client->expects($this->once()) + ->method('post') + ->with('/user/codespaces', '{"ref":"main","repository_id":1296269}', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 202)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->create(1296269, ['ref' => 'main']) + ); + } + + /** + * Test the create method with an unexpected response. + * + * @return void + */ + public function testCreateFailure() + { + $this->client->expects($this->once()) + ->method('post') + ->with('/user/codespaces', '{"ref":"main","repository_id":1296269}', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->create(1296269, ['ref' => 'main']); + } + + /** + * Test the get method. + * + * @return void + */ + public function testGet() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->get('monalisa-octocat-hello-world-g4wpq6h95q') + ); + } + + /** + * Test the get method with an unexpected response. + * + * @return void + */ + public function testGetFailure() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->get('monalisa-octocat-hello-world-g4wpq6h95q'); + } + + /** + * Test the edit method. + * + * @return void + */ + public function testEdit() + { + $this->client->expects($this->once()) + ->method('patch') + ->with('/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q', '{"machine":"standardLinux32gb"}', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->edit('monalisa-octocat-hello-world-g4wpq6h95q', ['machine' => 'standardLinux32gb']) + ); + } + + /** + * Test the edit method with an unexpected response. + * + * @return void + */ + public function testEditFailure() + { + $this->client->expects($this->once()) + ->method('patch') + ->with('/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q', '{"machine":"standardLinux32gb"}', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->edit('monalisa-octocat-hello-world-g4wpq6h95q', ['machine' => 'standardLinux32gb']); + } + + /** + * Test the delete method. + * + * @return void + */ + public function testDelete() + { + $this->client->expects($this->once()) + ->method('delete') + ->with('/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 202)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->delete('monalisa-octocat-hello-world-g4wpq6h95q') + ); + } + + /** + * Test the delete method with an unexpected response. + * + * @return void + */ + public function testDeleteFailure() + { + $this->client->expects($this->once()) + ->method('delete') + ->with('/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->delete('monalisa-octocat-hello-world-g4wpq6h95q'); + } + + /** + * Test the export method. + * + * @return void + */ + public function testExport() + { + $this->client->expects($this->once()) + ->method('post') + ->with('/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q/exports', '', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 202)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->export('monalisa-octocat-hello-world-g4wpq6h95q') + ); + } + + /** + * Test the export method with an unexpected response. + * + * @return void + */ + public function testExportFailure() + { + $this->client->expects($this->once()) + ->method('post') + ->with('/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q/exports', '', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->export('monalisa-octocat-hello-world-g4wpq6h95q'); + } + + /** + * Test the getExport method. + * + * @return void + */ + public function testGetExport() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q/exports/latest', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getExport('monalisa-octocat-hello-world-g4wpq6h95q', 'latest') + ); + } + + /** + * Test the getExport method with an unexpected response. + * + * @return void + */ + public function testGetExportFailure() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q/exports/latest', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->getExport('monalisa-octocat-hello-world-g4wpq6h95q', 'latest'); + } + + /** + * Test the publish method. + * + * @return void + */ + public function testPublish() + { + $this->client->expects($this->once()) + ->method('post') + ->with('/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q/publish', '{"name":"joomla-codespace"}', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 201)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->publish('monalisa-octocat-hello-world-g4wpq6h95q', ['name' => 'joomla-codespace']) + ); + } + + /** + * Test the publish method with an unexpected response. + * + * @return void + */ + public function testPublishFailure() + { + $this->client->expects($this->once()) + ->method('post') + ->with('/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q/publish', '{"name":"joomla-codespace"}', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->publish('monalisa-octocat-hello-world-g4wpq6h95q', ['name' => 'joomla-codespace']); + } + + /** + * Test the start method. + * + * @return void + */ + public function testStart() + { + $this->client->expects($this->once()) + ->method('post') + ->with('/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q/start', '', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->start('monalisa-octocat-hello-world-g4wpq6h95q') + ); + } + + /** + * Test the start method with an unexpected response. + * + * @return void + */ + public function testStartFailure() + { + $this->client->expects($this->once()) + ->method('post') + ->with('/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q/start', '', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->start('monalisa-octocat-hello-world-g4wpq6h95q'); + } + + /** + * Test the stop method. + * + * @return void + */ + public function testStop() + { + $this->client->expects($this->once()) + ->method('post') + ->with('/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q/stop', '', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->stop('monalisa-octocat-hello-world-g4wpq6h95q') + ); + } + + /** + * Test the stop method with an unexpected response. + * + * @return void + */ + public function testStopFailure() + { + $this->client->expects($this->once()) + ->method('post') + ->with('/user/codespaces/monalisa-octocat-hello-world-g4wpq6h95q/stop', '', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->stop('monalisa-octocat-hello-world-g4wpq6h95q'); + } + + /** + * Test the getListForRepo method. + * + * @return void + */ + public function testGetListForRepo() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/codespaces?page=2&per_page=30', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getListForRepo('joomla', 'joomla-platform', 2, 30) + ); + } + + /** + * Test the getListForRepo method with an unexpected response. + * + * @return void + */ + public function testGetListForRepoFailure() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/codespaces?page=2&per_page=30', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->getListForRepo('joomla', 'joomla-platform', 2, 30); + } + + /** + * Test the createForRepo method. + * + * @return void + */ + public function testCreateForRepo() + { + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/codespaces', '{"ref":"main"}', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 201)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->createForRepo('joomla', 'joomla-platform', ['ref' => 'main']) + ); + } + + /** + * Test the createForRepo method with a 202 response. + * + * @return void + */ + public function testCreateForRepoStatus202() + { + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/codespaces', '{"ref":"main"}', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 202)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->createForRepo('joomla', 'joomla-platform', ['ref' => 'main']) + ); + } + + /** + * Test the createForRepo method with an unexpected response. + * + * @return void + */ + public function testCreateForRepoFailure() + { + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/codespaces', '{"ref":"main"}', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->createForRepo('joomla', 'joomla-platform', ['ref' => 'main']); + } + + /** + * Test the getListDevcontainers method. + * + * @return void + */ + public function testGetListDevcontainers() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/codespaces/devcontainers?page=2&per_page=30', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getListDevcontainers('joomla', 'joomla-platform', 2, 30) + ); + } + + /** + * Test the getListDevcontainers method with an unexpected response. + * + * @return void + */ + public function testGetListDevcontainersFailure() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/codespaces/devcontainers?page=2&per_page=30', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->getListDevcontainers('joomla', 'joomla-platform', 2, 30); + } + + /** + * Test the getDefaultAttributes method. + * + * @return void + */ + public function testGetDefaultAttributes() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/codespaces/new?ref=main', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getDefaultAttributes('joomla', 'joomla-platform', ['ref' => 'main']) + ); + } + + /** + * Test the getDefaultAttributes method with an unexpected response. + * + * @return void + */ + public function testGetDefaultAttributesFailure() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/codespaces/new?ref=main', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->getDefaultAttributes('joomla', 'joomla-platform', ['ref' => 'main']); + } + + /** + * Test the checkPermissions method. + * + * @return void + */ + public function testCheckPermissions() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/codespaces/permissions_check?ref=main&devcontainer_path=.devcontainer/example/devcontainer.json', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->checkPermissions('joomla', 'joomla-platform', 'main', '.devcontainer/example/devcontainer.json') + ); + } + + /** + * Test the checkPermissions method with an unexpected response. + * + * @return void + */ + public function testCheckPermissionsFailure() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/codespaces/permissions_check?ref=main&devcontainer_path=.devcontainer/example/devcontainer.json', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->checkPermissions('joomla', 'joomla-platform', 'main', '.devcontainer/example/devcontainer.json'); + } + + /** + * Test the createForPull method. + * + * @return void + */ + public function testCreateForPull() + { + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/pulls/42/codespaces', '{"location":"WestUs2"}', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 201)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->createForPull('joomla', 'joomla-platform', 42, ['location' => 'WestUs2']) + ); + } + + /** + * Test the createForPull method with a 202 response. + * + * @return void + */ + public function testCreateForPullStatus202() + { + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/pulls/42/codespaces', '{"location":"WestUs2"}', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 202)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->createForPull('joomla', 'joomla-platform', 42, ['location' => 'WestUs2']) + ); + } + + /** + * Test the createForPull method with an unexpected response. + * + * @return void + */ + public function testCreateForPullFailure() + { + $this->client->expects($this->once()) + ->method('post') + ->with('/repos/joomla/joomla-platform/pulls/42/codespaces', '{"location":"WestUs2"}', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->expectException(UnexpectedResponseException::class); + + $this->object->createForPull('joomla', 'joomla-platform', 42, ['location' => 'WestUs2']); + } +} diff --git a/docs/codespaces.md b/docs/codespaces.md new file mode 100644 index 00000000..f06f8499 --- /dev/null +++ b/docs/codespaces.md @@ -0,0 +1,122 @@ +# Codespaces + +The `Codespaces` package covers the [GitHub Codespaces REST API](https://docs.github.com/rest/codespaces): the +codespaces of the authenticated user (list, create, start, stop, export, publish, ...), the codespaces of an +organization, the available machine types, and codespaces secrets at user, repository and organization level. +It is reached through `$github->codespaces`; the sub-packages are reached as properties of it, for example +`$github->codespaces->repositorySecrets->getList('joomla', 'joomla-cms')`. + +Codespace and secret names are URL-encoded by the package, so pass them as they are. + +```php +use Joomla\Github\Github; +use Joomla\Registry\Registry; + +$options = new Registry(); +$options->set('gh.token', 'your-token'); + +$github = new Github($options); + +// List the codespaces of the authenticated user for one repository +$codespaces = $github->codespaces->getList(['repository_id' => 1296269]); + +// Create a codespace on the default branch of a repository and stop it again +$codespace = $github->codespaces->createForRepo('joomla', 'joomla-cms', ['machine' => 'standardLinux32gb']); +$github->codespaces->stop($codespace->name); + +// Store a repository secret for codespaces (the value must be encrypted with the repository public key) +$key = $github->codespaces->repositorySecrets->getPublicKey('joomla', 'joomla-cms'); +$github->codespaces->repositorySecrets->createOrUpdate( + 'joomla', + 'joomla-cms', + 'MY_SECRET', + ['encrypted_value' => $encryptedValue, 'key_id' => $key->key_id] +); +``` + +`$options` holds optional request-body fields and `$filters` optional query parameters; the accepted keys are +listed in each method's docblock. Paginated methods take `$page` and `$limit` (items per page) as last arguments. +Methods answered with `204 No Content` return nothing; `createOrUpdate()` returns `null` when an existing secret +was updated (204) and the response object when it was created (201). + +### Codespaces (`$github->codespaces`) + +| Method | Endpoint | +| --- | --- | +| getList($filters, $page, $limit) | GET /user/codespaces | +| create($repositoryId, $options) | POST /user/codespaces | +| get($codespaceName) | GET /user/codespaces/{codespace_name} | +| edit($codespaceName, $options) | PATCH /user/codespaces/{codespace_name} | +| delete($codespaceName) | DELETE /user/codespaces/{codespace_name} | +| export($codespaceName) | POST /user/codespaces/{codespace_name}/exports | +| getExport($codespaceName, $exportId) | GET /user/codespaces/{codespace_name}/exports/{export_id} | +| publish($codespaceName, $options) | POST /user/codespaces/{codespace_name}/publish | +| start($codespaceName) | POST /user/codespaces/{codespace_name}/start | +| stop($codespaceName) | POST /user/codespaces/{codespace_name}/stop | +| getListForRepo($owner, $repo, $page, $limit) | GET /repos/{owner}/{repo}/codespaces | +| createForRepo($owner, $repo, $options) | POST /repos/{owner}/{repo}/codespaces | +| getListDevcontainers($owner, $repo, $page, $limit) | GET /repos/{owner}/{repo}/codespaces/devcontainers | +| getDefaultAttributes($owner, $repo, $filters) | GET /repos/{owner}/{repo}/codespaces/new | +| checkPermissions($owner, $repo, $ref, $devcontainerPath) | GET /repos/{owner}/{repo}/codespaces/permissions_check | +| createForPull($owner, $repo, $pullNumber, $options) | POST /repos/{owner}/{repo}/pulls/{pull_number}/codespaces | + +### Codespaces\Machines (`$github->codespaces->machines`) + +| Method | Endpoint | +| --- | --- | +| getListForRepo($owner, $repo, $filters) | GET /repos/{owner}/{repo}/codespaces/machines | +| getListForCodespace($codespaceName) | GET /user/codespaces/{codespace_name}/machines | + +### Codespaces\Organizations (`$github->codespaces->organizations`) + +| Method | Endpoint | +| --- | --- | +| getList($org, $page, $limit) | GET /orgs/{org}/codespaces | +| getListForUser($org, $username, $page, $limit) | GET /orgs/{org}/members/{username}/codespaces | +| delete($org, $username, $codespaceName) | DELETE /orgs/{org}/members/{username}/codespaces/{codespace_name} | +| stop($org, $username, $codespaceName) | POST /orgs/{org}/members/{username}/codespaces/{codespace_name}/stop | + +### Codespaces\OrganizationSecrets (`$github->codespaces->organizationSecrets`) + +| Method | Endpoint | +| --- | --- | +| getList($org, $page, $limit) | GET /orgs/{org}/codespaces/secrets | +| getPublicKey($org) | GET /orgs/{org}/codespaces/secrets/public-key | +| get($org, $secretName) | GET /orgs/{org}/codespaces/secrets/{secret_name} | +| createOrUpdate($org, $secretName, $visibility, $options) | PUT /orgs/{org}/codespaces/secrets/{secret_name} | +| delete($org, $secretName) | DELETE /orgs/{org}/codespaces/secrets/{secret_name} | +| getListSelectedRepositories($org, $secretName, $page, $limit) | GET /orgs/{org}/codespaces/secrets/{secret_name}/repositories | +| setSelectedRepositories($org, $secretName, $selectedRepositoryIds) | PUT /orgs/{org}/codespaces/secrets/{secret_name}/repositories | +| addSelectedRepository($org, $secretName, $repositoryId) | PUT /orgs/{org}/codespaces/secrets/{secret_name}/repositories/{repository_id} | +| removeSelectedRepository($org, $secretName, $repositoryId) | DELETE /orgs/{org}/codespaces/secrets/{secret_name}/repositories/{repository_id} | + +### Codespaces\RepositorySecrets (`$github->codespaces->repositorySecrets`) + +| Method | Endpoint | +| --- | --- | +| getList($owner, $repo, $page, $limit) | GET /repos/{owner}/{repo}/codespaces/secrets | +| getPublicKey($owner, $repo) | GET /repos/{owner}/{repo}/codespaces/secrets/public-key | +| get($owner, $repo, $secretName) | GET /repos/{owner}/{repo}/codespaces/secrets/{secret_name} | +| createOrUpdate($owner, $repo, $secretName, $options) | PUT /repos/{owner}/{repo}/codespaces/secrets/{secret_name} | +| delete($owner, $repo, $secretName) | DELETE /repos/{owner}/{repo}/codespaces/secrets/{secret_name} | + +### Codespaces\Secrets (`$github->codespaces->secrets`) + +| Method | Endpoint | +| --- | --- | +| getList($page, $limit) | GET /user/codespaces/secrets | +| getPublicKey() | GET /user/codespaces/secrets/public-key | +| get($secretName) | GET /user/codespaces/secrets/{secret_name} | +| createOrUpdate($secretName, $keyId, $options) | PUT /user/codespaces/secrets/{secret_name} | +| delete($secretName) | DELETE /user/codespaces/secrets/{secret_name} | +| getListSelectedRepositories($secretName) | GET /user/codespaces/secrets/{secret_name}/repositories | +| setSelectedRepositories($secretName, $selectedRepositoryIds) | PUT /user/codespaces/secrets/{secret_name}/repositories | +| addSelectedRepository($secretName, $repositoryId) | PUT /user/codespaces/secrets/{secret_name}/repositories/{repository_id} | +| removeSelectedRepository($secretName, $repositoryId) | DELETE /user/codespaces/secrets/{secret_name}/repositories/{repository_id} | + +## Notes + +* `create()` sends the first body variant of `POST /user/codespaces` (`repository_id` plus optional fields). + To create a codespace from a pull request (the second variant) use `createForPull()`. +* The deprecated organization access operations (`PUT /orgs/{org}/codespaces/access` and + `POST|DELETE /orgs/{org}/codespaces/access/selected_users`) are not included. diff --git a/src/Package/Codespaces.php b/src/Package/Codespaces.php new file mode 100644 index 00000000..ac13bfe7 --- /dev/null +++ b/src/Package/Codespaces.php @@ -0,0 +1,400 @@ +fetchUrl($path, $page, $limit); + + foreach ($filters as $key => $value) { + $uri->setVar($key, $value); + } + + return $this->processResponse($this->client->get($uri), 200); + } + + /** + * Create a codespace for the authenticated user. + * + * @param integer $repositoryId The ID of the repository. + * @param array $options Optional body fields: ref, location, geo, client_ip, machine, devcontainer_path, multi_repo_permissions_opt_out, working_directory, idle_timeout_minutes, display_name, retention_period_minutes. + * + * @return object + * + * @link https://docs.github.com/rest/codespaces/codespaces#create-a-codespace-for-the-authenticated-user + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function create(int $repositoryId, array $options = []) + { + // Build the request path. + $path = '/user/codespaces'; + + $data = array_merge($options, ['repository_id' => $repositoryId]); + + $response = $this->client->post($this->fetchUrl($path), json_encode($data)); + + return $this->processResponse( + $response, + \in_array($response->getStatusCode(), [201, 202], true) ? $response->getStatusCode() : 201 + ); + } + + /** + * Get a codespace for the authenticated user. + * + * @param string $codespaceName The name of the codespace. + * + * @return object + * + * @link https://docs.github.com/rest/codespaces/codespaces#get-a-codespace-for-the-authenticated-user + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function get(string $codespaceName) + { + // Build the request path. + $path = '/user/codespaces/' . rawurlencode($codespaceName); + + return $this->processResponse($this->client->get($this->fetchUrl($path)), 200); + } + + /** + * Update a codespace for the authenticated user. + * + * @param string $codespaceName The name of the codespace. + * @param array $options Optional body fields: machine, display_name, recent_folders. + * + * @return object + * + * @link https://docs.github.com/rest/codespaces/codespaces#update-a-codespace-for-the-authenticated-user + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function edit(string $codespaceName, array $options = []) + { + // Build the request path. + $path = '/user/codespaces/' . rawurlencode($codespaceName); + + // Send an object, so that an empty body is encoded as {} rather than []. + $data = (object) $options; + + return $this->processResponse($this->client->patch($this->fetchUrl($path), json_encode($data)), 200); + } + + /** + * Delete a codespace for the authenticated user. + * + * @param string $codespaceName The name of the codespace. + * + * @return object + * + * @link https://docs.github.com/rest/codespaces/codespaces#delete-a-codespace-for-the-authenticated-user + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function delete(string $codespaceName) + { + // Build the request path. + $path = '/user/codespaces/' . rawurlencode($codespaceName); + + return $this->processResponse($this->client->delete($this->fetchUrl($path)), 202); + } + + /** + * Export a codespace for the authenticated user. + * + * @param string $codespaceName The name of the codespace. + * + * @return object + * + * @link https://docs.github.com/rest/codespaces/codespaces#export-a-codespace-for-the-authenticated-user + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function export(string $codespaceName) + { + // Build the request path. + $path = '/user/codespaces/' . rawurlencode($codespaceName) . '/exports'; + + return $this->processResponse($this->client->post($this->fetchUrl($path), ''), 202); + } + + /** + * Get details about a codespace export. + * + * @param string $codespaceName The name of the codespace. + * @param string $exportId The ID of the export operation, or `latest`. + * + * @return object + * + * @link https://docs.github.com/rest/codespaces/codespaces#get-details-about-a-codespace-export + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function getExport(string $codespaceName, string $exportId) + { + // Build the request path. + $path = '/user/codespaces/' . rawurlencode($codespaceName) . '/exports/' . rawurlencode($exportId); + + return $this->processResponse($this->client->get($this->fetchUrl($path)), 200); + } + + /** + * Create a repository from an unpublished codespace. + * + * @param string $codespaceName The name of the codespace. + * @param array $options Optional body fields: name, private. + * + * @return object + * + * @link https://docs.github.com/rest/codespaces/codespaces#create-a-repository-from-an-unpublished-codespace + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function publish(string $codespaceName, array $options = []) + { + // Build the request path. + $path = '/user/codespaces/' . rawurlencode($codespaceName) . '/publish'; + + // Send an object, so that an empty body is encoded as {} rather than []. + $data = (object) $options; + + return $this->processResponse($this->client->post($this->fetchUrl($path), json_encode($data)), 201); + } + + /** + * Start a codespace for the authenticated user. + * + * @param string $codespaceName The name of the codespace. + * + * @return object + * + * @link https://docs.github.com/rest/codespaces/codespaces#start-a-codespace-for-the-authenticated-user + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function start(string $codespaceName) + { + // Build the request path. + $path = '/user/codespaces/' . rawurlencode($codespaceName) . '/start'; + + return $this->processResponse($this->client->post($this->fetchUrl($path), ''), 200); + } + + /** + * Stop a codespace for the authenticated user. + * + * @param string $codespaceName The name of the codespace. + * + * @return object + * + * @link https://docs.github.com/rest/codespaces/codespaces#stop-a-codespace-for-the-authenticated-user + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function stop(string $codespaceName) + { + // Build the request path. + $path = '/user/codespaces/' . rawurlencode($codespaceName) . '/stop'; + + return $this->processResponse($this->client->post($this->fetchUrl($path), ''), 200); + } + + /** + * List codespaces in a repository for the authenticated user. + * + * @param string $owner The name of the owner of the repository. + * @param string $repo The name of the repository. + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * + * @return object + * + * @link https://docs.github.com/rest/codespaces/codespaces#list-codespaces-in-a-repository-for-the-authenticated-user + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function getListForRepo(string $owner, string $repo, int $page = 0, int $limit = 0) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/codespaces'; + + return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit)), 200); + } + + /** + * Create a codespace in a repository. + * + * @param string $owner The name of the owner of the repository. + * @param string $repo The name of the repository. + * @param array $options Optional body fields: ref, location, geo, client_ip, machine, devcontainer_path, multi_repo_permissions_opt_out, working_directory, idle_timeout_minutes, display_name, retention_period_minutes. + * + * @return object + * + * @link https://docs.github.com/rest/codespaces/codespaces#create-a-codespace-in-a-repository + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function createForRepo(string $owner, string $repo, array $options = []) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/codespaces'; + + // Send an object, so that an empty body is encoded as {} rather than []. + $data = (object) $options; + + $response = $this->client->post($this->fetchUrl($path), json_encode($data)); + + return $this->processResponse( + $response, + \in_array($response->getStatusCode(), [201, 202], true) ? $response->getStatusCode() : 201 + ); + } + + /** + * List devcontainer configurations in a repository for the authenticated user. + * + * @param string $owner The name of the owner of the repository. + * @param string $repo The name of the repository. + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * + * @return object + * + * @link https://docs.github.com/rest/codespaces/codespaces#list-devcontainer-configurations-in-a-repository-for-the-authenticated-user + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function getListDevcontainers(string $owner, string $repo, int $page = 0, int $limit = 0) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/codespaces/devcontainers'; + + return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit)), 200); + } + + /** + * Get default attributes for a codespace. + * + * @param string $owner The name of the owner of the repository. + * @param string $repo The name of the repository. + * @param array $filters Optional query parameters: ref, client_ip. + * + * @return object + * + * @link https://docs.github.com/rest/codespaces/codespaces#get-default-attributes-for-a-codespace + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function getDefaultAttributes(string $owner, string $repo, array $filters = []) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/codespaces/new'; + + $uri = $this->fetchUrl($path); + + foreach ($filters as $key => $value) { + $uri->setVar($key, $value); + } + + return $this->processResponse($this->client->get($uri), 200); + } + + /** + * Check if permissions defined by a devcontainer have been accepted by the authenticated user. + * + * @param string $owner The name of the owner of the repository. + * @param string $repo The name of the repository. + * @param string $ref The git reference that points to the location of the devcontainer configuration. + * @param string $devcontainerPath The path to the devcontainer.json configuration. + * + * @return object + * + * @link https://docs.github.com/rest/codespaces/codespaces#check-if-permissions-defined-by-a-devcontainer-have-been-accepted-by-the-authenticated-user + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function checkPermissions(string $owner, string $repo, string $ref, string $devcontainerPath) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/codespaces/permissions_check'; + + $uri = $this->fetchUrl($path); + $uri->setVar('ref', $ref); + $uri->setVar('devcontainer_path', $devcontainerPath); + + return $this->processResponse($this->client->get($uri), 200); + } + + /** + * Create a codespace from a pull request. + * + * @param string $owner The name of the owner of the repository. + * @param string $repo The name of the repository. + * @param integer $pullNumber The number of the pull request. + * @param array $options Optional body fields: location, geo, client_ip, machine, devcontainer_path, multi_repo_permissions_opt_out, working_directory, idle_timeout_minutes, display_name, retention_period_minutes. + * + * @return object + * + * @link https://docs.github.com/rest/codespaces/codespaces#create-a-codespace-from-a-pull-request + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function createForPull(string $owner, string $repo, int $pullNumber, array $options = []) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/pulls/' . $pullNumber . '/codespaces'; + + // Send an object, so that an empty body is encoded as {} rather than []. + $data = (object) $options; + + $response = $this->client->post($this->fetchUrl($path), json_encode($data)); + + return $this->processResponse( + $response, + \in_array($response->getStatusCode(), [201, 202], true) ? $response->getStatusCode() : 201 + ); + } +} diff --git a/src/Package/Codespaces/Machines.php b/src/Package/Codespaces/Machines.php new file mode 100644 index 00000000..b3c88cff --- /dev/null +++ b/src/Package/Codespaces/Machines.php @@ -0,0 +1,69 @@ +fetchUrl($path); + + foreach ($filters as $key => $value) { + $uri->setVar($key, $value); + } + + return $this->processResponse($this->client->get($uri), 200); + } + + /** + * List machine types for a codespace. + * + * @param string $codespaceName The name of the codespace. + * + * @return object + * + * @link https://docs.github.com/rest/codespaces/machines#list-machine-types-for-a-codespace + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function getListForCodespace(string $codespaceName) + { + // Build the request path. + $path = '/user/codespaces/' . rawurlencode($codespaceName) . '/machines'; + + return $this->processResponse($this->client->get($this->fetchUrl($path)), 200); + } +} diff --git a/src/Package/Codespaces/OrganizationSecrets.php b/src/Package/Codespaces/OrganizationSecrets.php new file mode 100644 index 00000000..d1d478a8 --- /dev/null +++ b/src/Package/Codespaces/OrganizationSecrets.php @@ -0,0 +1,219 @@ +processResponse($this->client->get($this->fetchUrl($path, $page, $limit)), 200); + } + + /** + * Get an organization public key. + * + * @param string $org The organization name. + * + * @return object + * + * @link https://docs.github.com/rest/codespaces/organization-secrets#get-an-organization-public-key + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function getPublicKey(string $org) + { + // Build the request path. + $path = '/orgs/' . $org . '/codespaces/secrets/public-key'; + + return $this->processResponse($this->client->get($this->fetchUrl($path)), 200); + } + + /** + * Get an organization secret. + * + * @param string $org The organization name. + * @param string $secretName The name of the secret. + * + * @return object + * + * @link https://docs.github.com/rest/codespaces/organization-secrets#get-an-organization-secret + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function get(string $org, string $secretName) + { + // Build the request path. + $path = '/orgs/' . $org . '/codespaces/secrets/' . rawurlencode($secretName); + + return $this->processResponse($this->client->get($this->fetchUrl($path)), 200); + } + + /** + * Create or update an organization secret. + * + * @param string $org The organization name. + * @param string $secretName The name of the secret. + * @param string $visibility Which repositories can access the secret (all, private or selected). + * @param array $options Optional body fields: encrypted_value, key_id, selected_repository_ids. + * + * @return object|null + * + * @link https://docs.github.com/rest/codespaces/organization-secrets#create-or-update-an-organization-secret + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function createOrUpdate(string $org, string $secretName, string $visibility, array $options = []) + { + // Build the request path. + $path = '/orgs/' . $org . '/codespaces/secrets/' . rawurlencode($secretName); + + $data = array_merge($options, ['visibility' => $visibility]); + + $response = $this->client->put($this->fetchUrl($path), json_encode($data)); + + return $this->processResponse( + $response, + \in_array($response->getStatusCode(), [201, 204], true) ? $response->getStatusCode() : 201 + ); + } + + /** + * Delete an organization secret. + * + * @param string $org The organization name. + * @param string $secretName The name of the secret. + * + * @return void + * + * @link https://docs.github.com/rest/codespaces/organization-secrets#delete-an-organization-secret + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function delete(string $org, string $secretName) + { + // Build the request path. + $path = '/orgs/' . $org . '/codespaces/secrets/' . rawurlencode($secretName); + + $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); + } + + /** + * List selected repositories for an organization secret. + * + * @param string $org The organization name. + * @param string $secretName The name of the secret. + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * + * @return object + * + * @link https://docs.github.com/rest/codespaces/organization-secrets#list-selected-repositories-for-an-organization-secret + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function getListSelectedRepositories(string $org, string $secretName, int $page = 0, int $limit = 0) + { + // Build the request path. + $path = '/orgs/' . $org . '/codespaces/secrets/' . rawurlencode($secretName) . '/repositories'; + + return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit)), 200); + } + + /** + * Set selected repositories for an organization secret. + * + * @param string $org The organization name. + * @param string $secretName The name of the secret. + * @param array $selectedRepositoryIds The IDs of the repositories that can access the secret. + * + * @return void + * + * @link https://docs.github.com/rest/codespaces/organization-secrets#set-selected-repositories-for-an-organization-secret + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function setSelectedRepositories(string $org, string $secretName, array $selectedRepositoryIds) + { + // Build the request path. + $path = '/orgs/' . $org . '/codespaces/secrets/' . rawurlencode($secretName) . '/repositories'; + + $data = ['selected_repository_ids' => $selectedRepositoryIds]; + + $this->processResponse($this->client->put($this->fetchUrl($path), json_encode($data)), 204); + } + + /** + * Add selected repository to an organization secret. + * + * @param string $org The organization name. + * @param string $secretName The name of the secret. + * @param integer $repositoryId The ID of the repository. + * + * @return void + * + * @link https://docs.github.com/rest/codespaces/organization-secrets#add-selected-repository-to-an-organization-secret + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function addSelectedRepository(string $org, string $secretName, int $repositoryId) + { + // Build the request path. + $path = '/orgs/' . $org . '/codespaces/secrets/' . rawurlencode($secretName) . '/repositories/' . $repositoryId; + + $this->processResponse($this->client->put($this->fetchUrl($path), ''), 204); + } + + /** + * Remove selected repository from an organization secret. + * + * @param string $org The organization name. + * @param string $secretName The name of the secret. + * @param integer $repositoryId The ID of the repository. + * + * @return void + * + * @link https://docs.github.com/rest/codespaces/organization-secrets#remove-selected-repository-from-an-organization-secret + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function removeSelectedRepository(string $org, string $secretName, int $repositoryId) + { + // Build the request path. + $path = '/orgs/' . $org . '/codespaces/secrets/' . rawurlencode($secretName) . '/repositories/' . $repositoryId; + + $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); + } +} diff --git a/src/Package/Codespaces/Organizations.php b/src/Package/Codespaces/Organizations.php new file mode 100644 index 00000000..3ba2dedd --- /dev/null +++ b/src/Package/Codespaces/Organizations.php @@ -0,0 +1,108 @@ +processResponse($this->client->get($this->fetchUrl($path, $page, $limit)), 200); + } + + /** + * List codespaces for a user in organization. + * + * @param string $org The organization name. + * @param string $username The handle for the GitHub user account. + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * + * @return object + * + * @link https://docs.github.com/rest/codespaces/organizations#list-codespaces-for-a-user-in-organization + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function getListForUser(string $org, string $username, int $page = 0, int $limit = 0) + { + // Build the request path. + $path = '/orgs/' . $org . '/members/' . $username . '/codespaces'; + + return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit)), 200); + } + + /** + * Delete a codespace from the organization. + * + * @param string $org The organization name. + * @param string $username The handle for the GitHub user account. + * @param string $codespaceName The name of the codespace. + * + * @return object + * + * @link https://docs.github.com/rest/codespaces/organizations#delete-a-codespace-from-the-organization + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function delete(string $org, string $username, string $codespaceName) + { + // Build the request path. + $path = '/orgs/' . $org . '/members/' . $username . '/codespaces/' . rawurlencode($codespaceName); + + return $this->processResponse($this->client->delete($this->fetchUrl($path)), 202); + } + + /** + * Stop a codespace for an organization user. + * + * @param string $org The organization name. + * @param string $username The handle for the GitHub user account. + * @param string $codespaceName The name of the codespace. + * + * @return object + * + * @link https://docs.github.com/rest/codespaces/organizations#stop-a-codespace-for-an-organization-user + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function stop(string $org, string $username, string $codespaceName) + { + // Build the request path. + $path = '/orgs/' . $org . '/members/' . $username . '/codespaces/' . rawurlencode($codespaceName) . '/stop'; + + return $this->processResponse($this->client->post($this->fetchUrl($path), ''), 200); + } +} diff --git a/src/Package/Codespaces/RepositorySecrets.php b/src/Package/Codespaces/RepositorySecrets.php new file mode 100644 index 00000000..428a8986 --- /dev/null +++ b/src/Package/Codespaces/RepositorySecrets.php @@ -0,0 +1,137 @@ +processResponse($this->client->get($this->fetchUrl($path, $page, $limit)), 200); + } + + /** + * Get a repository public key. + * + * @param string $owner The name of the owner of the repository. + * @param string $repo The name of the repository. + * + * @return object + * + * @link https://docs.github.com/rest/codespaces/repository-secrets#get-a-repository-public-key + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function getPublicKey(string $owner, string $repo) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/codespaces/secrets/public-key'; + + return $this->processResponse($this->client->get($this->fetchUrl($path)), 200); + } + + /** + * Get a repository secret. + * + * @param string $owner The name of the owner of the repository. + * @param string $repo The name of the repository. + * @param string $secretName The name of the secret. + * + * @return object + * + * @link https://docs.github.com/rest/codespaces/repository-secrets#get-a-repository-secret + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function get(string $owner, string $repo, string $secretName) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/codespaces/secrets/' . rawurlencode($secretName); + + return $this->processResponse($this->client->get($this->fetchUrl($path)), 200); + } + + /** + * Create or update a repository secret. + * + * @param string $owner The name of the owner of the repository. + * @param string $repo The name of the repository. + * @param string $secretName The name of the secret. + * @param array $options Optional body fields: encrypted_value, key_id. + * + * @return object|null + * + * @link https://docs.github.com/rest/codespaces/repository-secrets#create-or-update-a-repository-secret + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function createOrUpdate(string $owner, string $repo, string $secretName, array $options = []) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/codespaces/secrets/' . rawurlencode($secretName); + + // Send an object, so that an empty body is encoded as {} rather than []. + $data = (object) $options; + + $response = $this->client->put($this->fetchUrl($path), json_encode($data)); + + return $this->processResponse( + $response, + \in_array($response->getStatusCode(), [201, 204], true) ? $response->getStatusCode() : 201 + ); + } + + /** + * Delete a repository secret. + * + * @param string $owner The name of the owner of the repository. + * @param string $repo The name of the repository. + * @param string $secretName The name of the secret. + * + * @return void + * + * @link https://docs.github.com/rest/codespaces/repository-secrets#delete-a-repository-secret + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function delete(string $owner, string $repo, string $secretName) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/codespaces/secrets/' . rawurlencode($secretName); + + $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); + } +} diff --git a/src/Package/Codespaces/Secrets.php b/src/Package/Codespaces/Secrets.php new file mode 100644 index 00000000..63a70024 --- /dev/null +++ b/src/Package/Codespaces/Secrets.php @@ -0,0 +1,207 @@ +processResponse($this->client->get($this->fetchUrl($path, $page, $limit)), 200); + } + + /** + * Get public key for the authenticated user. + * + * @return object + * + * @link https://docs.github.com/rest/codespaces/secrets#get-public-key-for-the-authenticated-user + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function getPublicKey() + { + // Build the request path. + $path = '/user/codespaces/secrets/public-key'; + + return $this->processResponse($this->client->get($this->fetchUrl($path)), 200); + } + + /** + * Get a secret for the authenticated user. + * + * @param string $secretName The name of the secret. + * + * @return object + * + * @link https://docs.github.com/rest/codespaces/secrets#get-a-secret-for-the-authenticated-user + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function get(string $secretName) + { + // Build the request path. + $path = '/user/codespaces/secrets/' . rawurlencode($secretName); + + return $this->processResponse($this->client->get($this->fetchUrl($path)), 200); + } + + /** + * Create or update a secret for the authenticated user. + * + * @param string $secretName The name of the secret. + * @param string $keyId The ID of the key used to encrypt the secret. + * @param array $options Optional body fields: encrypted_value, selected_repository_ids. + * + * @return object|null + * + * @link https://docs.github.com/rest/codespaces/secrets#create-or-update-a-secret-for-the-authenticated-user + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function createOrUpdate(string $secretName, string $keyId, array $options = []) + { + // Build the request path. + $path = '/user/codespaces/secrets/' . rawurlencode($secretName); + + $data = array_merge($options, ['key_id' => $keyId]); + + $response = $this->client->put($this->fetchUrl($path), json_encode($data)); + + return $this->processResponse( + $response, + \in_array($response->getStatusCode(), [201, 204], true) ? $response->getStatusCode() : 201 + ); + } + + /** + * Delete a secret for the authenticated user. + * + * @param string $secretName The name of the secret. + * + * @return void + * + * @link https://docs.github.com/rest/codespaces/secrets#delete-a-secret-for-the-authenticated-user + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function delete(string $secretName) + { + // Build the request path. + $path = '/user/codespaces/secrets/' . rawurlencode($secretName); + + $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); + } + + /** + * List selected repositories for a user secret. + * + * @param string $secretName The name of the secret. + * + * @return object + * + * @link https://docs.github.com/rest/codespaces/secrets#list-selected-repositories-for-a-user-secret + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function getListSelectedRepositories(string $secretName) + { + // Build the request path. + $path = '/user/codespaces/secrets/' . rawurlencode($secretName) . '/repositories'; + + return $this->processResponse($this->client->get($this->fetchUrl($path)), 200); + } + + /** + * Set selected repositories for a user secret. + * + * @param string $secretName The name of the secret. + * @param array $selectedRepositoryIds The IDs of the repositories that can access the secret. + * + * @return void + * + * @link https://docs.github.com/rest/codespaces/secrets#set-selected-repositories-for-a-user-secret + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function setSelectedRepositories(string $secretName, array $selectedRepositoryIds) + { + // Build the request path. + $path = '/user/codespaces/secrets/' . rawurlencode($secretName) . '/repositories'; + + $data = ['selected_repository_ids' => $selectedRepositoryIds]; + + $this->processResponse($this->client->put($this->fetchUrl($path), json_encode($data)), 204); + } + + /** + * Add a selected repository to a user secret. + * + * @param string $secretName The name of the secret. + * @param integer $repositoryId The ID of the repository. + * + * @return void + * + * @link https://docs.github.com/rest/codespaces/secrets#add-a-selected-repository-to-a-user-secret + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function addSelectedRepository(string $secretName, int $repositoryId) + { + // Build the request path. + $path = '/user/codespaces/secrets/' . rawurlencode($secretName) . '/repositories/' . $repositoryId; + + $this->processResponse($this->client->put($this->fetchUrl($path), ''), 204); + } + + /** + * Remove a selected repository from a user secret. + * + * @param string $secretName The name of the secret. + * @param integer $repositoryId The ID of the repository. + * + * @return void + * + * @link https://docs.github.com/rest/codespaces/secrets#remove-a-selected-repository-from-a-user-secret + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function removeSelectedRepository(string $secretName, int $repositoryId) + { + // Build the request path. + $path = '/user/codespaces/secrets/' . rawurlencode($secretName) . '/repositories/' . $repositoryId; + + $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); + } +}