diff --git a/Tests/Package/Copilot/CloudAgentManagementTest.php b/Tests/Package/Copilot/CloudAgentManagementTest.php new file mode 100644 index 00000000..c094026b --- /dev/null +++ b/Tests/Package/Copilot/CloudAgentManagementTest.php @@ -0,0 +1,75 @@ +object = new CloudAgentManagement($this->options, $this->client); + } + + /** + * Tests the getConfiguration method. + * + * @return void + */ + public function testGetConfiguration(): void + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/copilot/cloud-agent/configuration', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getConfiguration('joomla', 'joomla-platform') + ); + } + + /** + * Tests the getConfiguration method. + * + * @return void + */ + public function testGetConfigurationFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/copilot/cloud-agent/configuration', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->getConfiguration('joomla', 'joomla-platform'); + } +} diff --git a/Tests/Package/Copilot/CodeReviewTest.php b/Tests/Package/Copilot/CodeReviewTest.php new file mode 100644 index 00000000..3820970a --- /dev/null +++ b/Tests/Package/Copilot/CodeReviewTest.php @@ -0,0 +1,317 @@ +object = new CodeReview($this->options, $this->client); + } + + /** + * Tests the getRunnerForOrg method. + * + * @return void + */ + public function testGetRunnerForOrg(): void + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/code-review/runner', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getRunnerForOrg('joomla') + ); + } + + /** + * Tests the getRunnerForOrg method. + * + * @return void + */ + public function testGetRunnerForOrgFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/code-review/runner', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->getRunnerForOrg('joomla'); + } + + /** + * Tests the setRunnerForOrg method. + * + * @return void + */ + public function testSetRunnerForOrg(): void + { + $this->client->expects($this->once()) + ->method('put') + ->with('/orgs/joomla/copilot/code-review/runner', json_encode(['runner_labels' => 'value', 'runner_type' => 'github_hosted']), [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->setRunnerForOrg('joomla', 'github_hosted', ['runner_labels' => 'value']) + ); + } + + /** + * Tests the setRunnerForOrg method. + * + * @return void + */ + public function testSetRunnerForOrgFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('put') + ->with('/orgs/joomla/copilot/code-review/runner', json_encode(['runner_labels' => 'value', 'runner_type' => 'github_hosted']), [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->setRunnerForOrg('joomla', 'github_hosted', ['runner_labels' => 'value']); + } + + /** + * Tests the getSettingsForOrg method. + * + * @return void + */ + public function testGetSettingsForOrg(): void + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/code-review/settings', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getSettingsForOrg('joomla') + ); + } + + /** + * Tests the getSettingsForOrg method. + * + * @return void + */ + public function testGetSettingsForOrgFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/code-review/settings', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->getSettingsForOrg('joomla'); + } + + /** + * Tests the editSettingsForOrg method. + * + * @return void + */ + public function testEditSettingsForOrg(): void + { + $this->client->expects($this->once()) + ->method('patch') + ->with('/orgs/joomla/copilot/code-review/settings', json_encode(['default_review_effort' => 'value']), [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->editSettingsForOrg('joomla', ['default_review_effort' => 'value']) + ); + } + + /** + * Tests the editSettingsForOrg method. + * + * @return void + */ + public function testEditSettingsForOrgFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('patch') + ->with('/orgs/joomla/copilot/code-review/settings', json_encode(['default_review_effort' => 'value']), [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->editSettingsForOrg('joomla', ['default_review_effort' => 'value']); + } + + /** + * Tests the getPolicyForOrg method. + * + * @return void + */ + public function testGetPolicyForOrg(): void + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/policies/code-review', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getPolicyForOrg('joomla') + ); + } + + /** + * Tests the getPolicyForOrg method. + * + * @return void + */ + public function testGetPolicyForOrgFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/policies/code-review', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->getPolicyForOrg('joomla'); + } + + /** + * Tests the setPolicyForOrg method. + * + * @return void + */ + public function testSetPolicyForOrg(): void + { + $this->client->expects($this->once()) + ->method('put') + ->with('/orgs/joomla/copilot/policies/code-review', json_encode(['state' => 'enabled']), [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 204)); + + $this->assertNull($this->object->setPolicyForOrg('joomla', 'enabled')); + } + + /** + * Tests the setPolicyForOrg method. + * + * @return void + */ + public function testSetPolicyForOrgFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('put') + ->with('/orgs/joomla/copilot/policies/code-review', json_encode(['state' => 'enabled']), [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->setPolicyForOrg('joomla', 'enabled'); + } + + /** + * Tests the getSettingsForRepo method. + * + * @return void + */ + public function testGetSettingsForRepo(): void + { + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/copilot/code-review/settings', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getSettingsForRepo('joomla', 'joomla-platform') + ); + } + + /** + * Tests the getSettingsForRepo method. + * + * @return void + */ + public function testGetSettingsForRepoFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('get') + ->with('/repos/joomla/joomla-platform/copilot/code-review/settings', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->getSettingsForRepo('joomla', 'joomla-platform'); + } + + /** + * Tests the editSettingsForRepo method. + * + * @return void + */ + public function testEditSettingsForRepo(): void + { + $this->client->expects($this->once()) + ->method('patch') + ->with('/repos/joomla/joomla-platform/copilot/code-review/settings', json_encode(['review_effort' => 'value']), [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->editSettingsForRepo('joomla', 'joomla-platform', ['review_effort' => 'value']) + ); + } + + /** + * Tests the editSettingsForRepo method. + * + * @return void + */ + public function testEditSettingsForRepoFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('patch') + ->with('/repos/joomla/joomla-platform/copilot/code-review/settings', json_encode(['review_effort' => 'value']), [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->editSettingsForRepo('joomla', 'joomla-platform', ['review_effort' => 'value']); + } +} diff --git a/Tests/Package/Copilot/CodingAgentManagementTest.php b/Tests/Package/Copilot/CodingAgentManagementTest.php new file mode 100644 index 00000000..73f34bff --- /dev/null +++ b/Tests/Package/Copilot/CodingAgentManagementTest.php @@ -0,0 +1,238 @@ +object = new CodingAgentManagement($this->options, $this->client); + } + + /** + * Tests the getPermissions method. + * + * @return void + */ + public function testGetPermissions(): void + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/coding-agent/permissions', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getPermissions('joomla') + ); + } + + /** + * Tests the getPermissions method. + * + * @return void + */ + public function testGetPermissionsFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/coding-agent/permissions', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->getPermissions('joomla'); + } + + /** + * Tests the setPermissions method. + * + * @return void + */ + public function testSetPermissions(): void + { + $this->client->expects($this->once()) + ->method('put') + ->with('/orgs/joomla/copilot/coding-agent/permissions', json_encode(['enabled_repositories' => 'selected']), [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 204)); + + $this->assertNull($this->object->setPermissions('joomla', 'selected')); + } + + /** + * Tests the setPermissions method. + * + * @return void + */ + public function testSetPermissionsFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('put') + ->with('/orgs/joomla/copilot/coding-agent/permissions', json_encode(['enabled_repositories' => 'selected']), [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->setPermissions('joomla', 'selected'); + } + + /** + * Tests the getRepositories method. + * + * @return void + */ + public function testGetRepositories(): void + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/coding-agent/permissions/repositories?page=2&per_page=10', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getRepositories('joomla', 2, 10) + ); + } + + /** + * Tests the getRepositories method. + * + * @return void + */ + public function testGetRepositoriesFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/coding-agent/permissions/repositories?page=2&per_page=10', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->getRepositories('joomla', 2, 10); + } + + /** + * Tests the setRepositories method. + * + * @return void + */ + public function testSetRepositories(): void + { + $this->client->expects($this->once()) + ->method('put') + ->with('/orgs/joomla/copilot/coding-agent/permissions/repositories', json_encode(['selected_repository_ids' => [1, 2]]), [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 204)); + + $this->assertNull($this->object->setRepositories('joomla', [1, 2])); + } + + /** + * Tests the setRepositories method. + * + * @return void + */ + public function testSetRepositoriesFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('put') + ->with('/orgs/joomla/copilot/coding-agent/permissions/repositories', json_encode(['selected_repository_ids' => [1, 2]]), [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->setRepositories('joomla', [1, 2]); + } + + /** + * Tests the enableRepository method. + * + * @return void + */ + public function testEnableRepository(): void + { + $this->client->expects($this->once()) + ->method('put') + ->with('/orgs/joomla/copilot/coding-agent/permissions/repositories/42', '', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 204)); + + $this->assertNull($this->object->enableRepository('joomla', 42)); + } + + /** + * Tests the enableRepository method. + * + * @return void + */ + public function testEnableRepositoryFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('put') + ->with('/orgs/joomla/copilot/coding-agent/permissions/repositories/42', '', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->enableRepository('joomla', 42); + } + + /** + * Tests the disableRepository method. + * + * @return void + */ + public function testDisableRepository(): void + { + $this->client->expects($this->once()) + ->method('delete') + ->with('/orgs/joomla/copilot/coding-agent/permissions/repositories/42', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 204)); + + $this->assertNull($this->object->disableRepository('joomla', 42)); + } + + /** + * Tests the disableRepository method. + * + * @return void + */ + public function testDisableRepositoryFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('delete') + ->with('/orgs/joomla/copilot/coding-agent/permissions/repositories/42', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->disableRepository('joomla', 42); + } +} diff --git a/Tests/Package/Copilot/ContentExclusionManagementTest.php b/Tests/Package/Copilot/ContentExclusionManagementTest.php new file mode 100644 index 00000000..7b39789d --- /dev/null +++ b/Tests/Package/Copilot/ContentExclusionManagementTest.php @@ -0,0 +1,110 @@ +object = new ContentExclusionManagement($this->options, $this->client); + } + + /** + * Tests the get method. + * + * @return void + */ + public function testGet(): void + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/content_exclusion', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->get('joomla') + ); + } + + /** + * Tests the get method. + * + * @return void + */ + public function testGetFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/content_exclusion', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->get('joomla'); + } + + /** + * Tests the set method. + * + * @return void + */ + public function testSet(): void + { + $this->client->expects($this->once()) + ->method('put') + ->with('/orgs/joomla/copilot/content_exclusion', json_encode(['octo-repo' => ['/src/some-dir/kernel.rs']]), [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->set('joomla', ['octo-repo' => ['/src/some-dir/kernel.rs']]) + ); + } + + /** + * Tests the set method. + * + * @return void + */ + public function testSetFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('put') + ->with('/orgs/joomla/copilot/content_exclusion', json_encode(['octo-repo' => ['/src/some-dir/kernel.rs']]), [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->set('joomla', ['octo-repo' => ['/src/some-dir/kernel.rs']]); + } +} diff --git a/Tests/Package/Copilot/TeamCustomizationsTest.php b/Tests/Package/Copilot/TeamCustomizationsTest.php new file mode 100644 index 00000000..5d8ba2a3 --- /dev/null +++ b/Tests/Package/Copilot/TeamCustomizationsTest.php @@ -0,0 +1,142 @@ +object = new TeamCustomizations($this->options, $this->client); + } + + /** + * Tests the get method. + * + * @return void + */ + public function testGet(): void + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/teams/core/copilot/customizations', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->get('joomla', 'core') + ); + } + + /** + * Tests the get method. + * + * @return void + */ + public function testGetFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/teams/core/copilot/customizations', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->get('joomla', 'core'); + } + + /** + * Tests the set method. + * + * @return void + */ + public function testSet(): void + { + $this->client->expects($this->once()) + ->method('put') + ->with('/orgs/joomla/teams/core/copilot/customizations', json_encode(['pluginAssignments' => 'value']), [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->set('joomla', 'core', ['pluginAssignments' => 'value']) + ); + } + + /** + * Tests the set method. + * + * @return void + */ + public function testSetFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('put') + ->with('/orgs/joomla/teams/core/copilot/customizations', json_encode(['pluginAssignments' => 'value']), [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->set('joomla', 'core', ['pluginAssignments' => 'value']); + } + + /** + * Tests the delete method. + * + * @return void + */ + public function testDelete(): void + { + $this->client->expects($this->once()) + ->method('delete') + ->with('/orgs/joomla/teams/core/copilot/customizations', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 204)); + + $this->assertNull($this->object->delete('joomla', 'core')); + } + + /** + * Tests the delete method. + * + * @return void + */ + public function testDeleteFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('delete') + ->with('/orgs/joomla/teams/core/copilot/customizations', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->delete('joomla', 'core'); + } +} diff --git a/Tests/Package/Copilot/UsageMetricsTest.php b/Tests/Package/Copilot/UsageMetricsTest.php new file mode 100644 index 00000000..0daeffac --- /dev/null +++ b/Tests/Package/Copilot/UsageMetricsTest.php @@ -0,0 +1,310 @@ +object = new UsageMetrics($this->options, $this->client); + } + + /** + * Tests the getOrganizationReport method. + * + * @return void + */ + public function testGetOrganizationReport(): void + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/metrics/reports/organization-1-day?day=2026-01-01', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getOrganizationReport('joomla', '2026-01-01') + ); + } + + /** + * Tests the getOrganizationReport method. + * + * @return void + */ + public function testGetOrganizationReportWithStatus204(): void + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/metrics/reports/organization-1-day?day=2026-01-01', [], 0) + ->willReturn($this->getResponseObject('', 204)); + + $this->assertNull($this->object->getOrganizationReport('joomla', '2026-01-01')); + } + + /** + * Tests the getOrganizationReport method. + * + * @return void + */ + public function testGetOrganizationReportFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/metrics/reports/organization-1-day?day=2026-01-01', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->getOrganizationReport('joomla', '2026-01-01'); + } + + /** + * Tests the getOrganizationLatestReport method. + * + * @return void + */ + public function testGetOrganizationLatestReport(): void + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/metrics/reports/organization-28-day/latest', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getOrganizationLatestReport('joomla') + ); + } + + /** + * Tests the getOrganizationLatestReport method. + * + * @return void + */ + public function testGetOrganizationLatestReportFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/metrics/reports/organization-28-day/latest', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->getOrganizationLatestReport('joomla'); + } + + /** + * Tests the getRepositoriesReport method. + * + * @return void + */ + public function testGetRepositoriesReport(): void + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/metrics/reports/repos-1-day?day=2026-01-01', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getRepositoriesReport('joomla', '2026-01-01') + ); + } + + /** + * Tests the getRepositoriesReport method. + * + * @return void + */ + public function testGetRepositoriesReportWithStatus204(): void + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/metrics/reports/repos-1-day?day=2026-01-01', [], 0) + ->willReturn($this->getResponseObject('', 204)); + + $this->assertNull($this->object->getRepositoriesReport('joomla', '2026-01-01')); + } + + /** + * Tests the getRepositoriesReport method. + * + * @return void + */ + public function testGetRepositoriesReportFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/metrics/reports/repos-1-day?day=2026-01-01', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->getRepositoriesReport('joomla', '2026-01-01'); + } + + /** + * Tests the getUserTeamsReport method. + * + * @return void + */ + public function testGetUserTeamsReport(): void + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/metrics/reports/user-teams-1-day?day=2026-01-01', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getUserTeamsReport('joomla', '2026-01-01') + ); + } + + /** + * Tests the getUserTeamsReport method. + * + * @return void + */ + public function testGetUserTeamsReportWithStatus204(): void + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/metrics/reports/user-teams-1-day?day=2026-01-01', [], 0) + ->willReturn($this->getResponseObject('', 204)); + + $this->assertNull($this->object->getUserTeamsReport('joomla', '2026-01-01')); + } + + /** + * Tests the getUserTeamsReport method. + * + * @return void + */ + public function testGetUserTeamsReportFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/metrics/reports/user-teams-1-day?day=2026-01-01', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->getUserTeamsReport('joomla', '2026-01-01'); + } + + /** + * Tests the getUsersReport method. + * + * @return void + */ + public function testGetUsersReport(): void + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/metrics/reports/users-1-day?day=2026-01-01', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getUsersReport('joomla', '2026-01-01') + ); + } + + /** + * Tests the getUsersReport method. + * + * @return void + */ + public function testGetUsersReportWithStatus204(): void + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/metrics/reports/users-1-day?day=2026-01-01', [], 0) + ->willReturn($this->getResponseObject('', 204)); + + $this->assertNull($this->object->getUsersReport('joomla', '2026-01-01')); + } + + /** + * Tests the getUsersReport method. + * + * @return void + */ + public function testGetUsersReportFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/metrics/reports/users-1-day?day=2026-01-01', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->getUsersReport('joomla', '2026-01-01'); + } + + /** + * Tests the getUsersLatestReport method. + * + * @return void + */ + public function testGetUsersLatestReport(): void + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/metrics/reports/users-28-day/latest', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getUsersLatestReport('joomla') + ); + } + + /** + * Tests the getUsersLatestReport method. + * + * @return void + */ + public function testGetUsersLatestReportFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/metrics/reports/users-28-day/latest', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->getUsersLatestReport('joomla'); + } +} diff --git a/Tests/Package/Copilot/UserManagementTest.php b/Tests/Package/Copilot/UserManagementTest.php new file mode 100644 index 00000000..08bdeeab --- /dev/null +++ b/Tests/Package/Copilot/UserManagementTest.php @@ -0,0 +1,285 @@ +object = new UserManagement($this->options, $this->client); + } + + /** + * Tests the getForOrg method. + * + * @return void + */ + public function testGetForOrg(): void + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/billing', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getForOrg('joomla') + ); + } + + /** + * Tests the getForOrg method. + * + * @return void + */ + public function testGetForOrgFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/billing', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->getForOrg('joomla'); + } + + /** + * Tests the getList method. + * + * @return void + */ + public function testGetList(): void + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/billing/seats?page=2&per_page=10', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getList('joomla', 2, 10) + ); + } + + /** + * Tests the getList method. + * + * @return void + */ + public function testGetListFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/copilot/billing/seats?page=2&per_page=10', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->getList('joomla', 2, 10); + } + + /** + * Tests the addTeams method. + * + * @return void + */ + public function testAddTeams(): void + { + $this->client->expects($this->once()) + ->method('post') + ->with('/orgs/joomla/copilot/billing/selected_teams', json_encode(['selected_teams' => ['core', 'docs']]), [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 201)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->addTeams('joomla', ['core', 'docs']) + ); + } + + /** + * Tests the addTeams method. + * + * @return void + */ + public function testAddTeamsFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('post') + ->with('/orgs/joomla/copilot/billing/selected_teams', json_encode(['selected_teams' => ['core', 'docs']]), [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->addTeams('joomla', ['core', 'docs']); + } + + /** + * Tests the removeTeams method. + * + * @return void + */ + public function testRemoveTeams(): void + { + $this->client->expects($this->once()) + ->method('delete') + ->with('/orgs/joomla/copilot/billing/selected_teams', [], 0, json_encode(['selected_teams' => ['core', 'docs']])) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->removeTeams('joomla', ['core', 'docs']) + ); + } + + /** + * Tests the removeTeams method. + * + * @return void + */ + public function testRemoveTeamsFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('delete') + ->with('/orgs/joomla/copilot/billing/selected_teams', [], 0, json_encode(['selected_teams' => ['core', 'docs']])) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->removeTeams('joomla', ['core', 'docs']); + } + + /** + * Tests the addUsers method. + * + * @return void + */ + public function testAddUsers(): void + { + $this->client->expects($this->once()) + ->method('post') + ->with('/orgs/joomla/copilot/billing/selected_users', json_encode(['selected_usernames' => ['octocat', 'octokitten']]), [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 201)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->addUsers('joomla', ['octocat', 'octokitten']) + ); + } + + /** + * Tests the addUsers method. + * + * @return void + */ + public function testAddUsersFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('post') + ->with('/orgs/joomla/copilot/billing/selected_users', json_encode(['selected_usernames' => ['octocat', 'octokitten']]), [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->addUsers('joomla', ['octocat', 'octokitten']); + } + + /** + * Tests the removeUsers method. + * + * @return void + */ + public function testRemoveUsers(): void + { + $this->client->expects($this->once()) + ->method('delete') + ->with('/orgs/joomla/copilot/billing/selected_users', [], 0, json_encode(['selected_usernames' => ['octocat', 'octokitten']])) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->removeUsers('joomla', ['octocat', 'octokitten']) + ); + } + + /** + * Tests the removeUsers method. + * + * @return void + */ + public function testRemoveUsersFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('delete') + ->with('/orgs/joomla/copilot/billing/selected_users', [], 0, json_encode(['selected_usernames' => ['octocat', 'octokitten']])) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->removeUsers('joomla', ['octocat', 'octokitten']); + } + + /** + * Tests the getForUser method. + * + * @return void + */ + public function testGetForUser(): void + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/members/octocat/copilot', [], 0) + ->willReturn($this->getResponseObject($this->sampleString, 200)); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getForUser('joomla', 'octocat') + ); + } + + /** + * Tests the getForUser method. + * + * @return void + */ + public function testGetForUserFailure(): void + { + $this->expectException(UnexpectedResponseException::class); + + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/members/octocat/copilot', [], 0) + ->willReturn($this->getResponseObject($this->errorString, 500)); + + $this->object->getForUser('joomla', 'octocat'); + } +} diff --git a/Tests/Package/CopilotTest.php b/Tests/Package/CopilotTest.php new file mode 100644 index 00000000..8e69b93c --- /dev/null +++ b/Tests/Package/CopilotTest.php @@ -0,0 +1,116 @@ +object = new Copilot($this->options, $this->client); + } + + /** + * Tests that the cloudAgentManagement sub-package is resolved. + * + * @return void + */ + public function testGetCloudAgentManagement(): void + { + $this->assertInstanceOf(CloudAgentManagement::class, $this->object->cloudAgentManagement); + } + + /** + * Tests that the codeReview sub-package is resolved. + * + * @return void + */ + public function testGetCodeReview(): void + { + $this->assertInstanceOf(CodeReview::class, $this->object->codeReview); + } + + /** + * Tests that the codingAgentManagement sub-package is resolved. + * + * @return void + */ + public function testGetCodingAgentManagement(): void + { + $this->assertInstanceOf(CodingAgentManagement::class, $this->object->codingAgentManagement); + } + + /** + * Tests that the contentExclusionManagement sub-package is resolved. + * + * @return void + */ + public function testGetContentExclusionManagement(): void + { + $this->assertInstanceOf(ContentExclusionManagement::class, $this->object->contentExclusionManagement); + } + + /** + * Tests that the teamCustomizations sub-package is resolved. + * + * @return void + */ + public function testGetTeamCustomizations(): void + { + $this->assertInstanceOf(TeamCustomizations::class, $this->object->teamCustomizations); + } + + /** + * Tests that the usageMetrics sub-package is resolved. + * + * @return void + */ + public function testGetUsageMetrics(): void + { + $this->assertInstanceOf(UsageMetrics::class, $this->object->usageMetrics); + } + + /** + * Tests that the userManagement sub-package is resolved. + * + * @return void + */ + public function testGetUserManagement(): void + { + $this->assertInstanceOf(UserManagement::class, $this->object->userManagement); + } +} diff --git a/docs/copilot.md b/docs/copilot.md new file mode 100644 index 00000000..b7f09cb6 --- /dev/null +++ b/docs/copilot.md @@ -0,0 +1,95 @@ +# Copilot + +The Copilot package covers the GitHub Copilot REST API: seat management, code review settings and policies, cloud agent permissions and configuration, content exclusion rules, usage metrics reports and team customizations. Each area is a sub-package of `$github->copilot`, for example `$github->copilot->userManagement->getList('joomla')`. + +```php +$github = new Joomla\Github\Github($options); + +// Give two users a Copilot seat in the organisation. +$result = $github->copilot->userManagement->addUsers('joomla', ['octocat', 'octokitten']); + +// Fetch the latest 28-day usage metrics report. +$report = $github->copilot->usageMetrics->getOrganizationLatestReport('joomla'); +``` + +## Cloud agent management + +`$github->copilot->cloudAgentManagement` - https://docs.github.com/rest/copilot/copilot-cloud-agent-management + +| Method | Endpoint | +| --- | --- | +| `getConfiguration($owner, $repo)` | `GET /repos/{owner}/{repo}/copilot/cloud-agent/configuration` | + +## Code review + +`$github->copilot->codeReview` - https://docs.github.com/rest/copilot/copilot-code-review + +| Method | Endpoint | +| --- | --- | +| `getRunnerForOrg($org)` | `GET /orgs/{org}/copilot/code-review/runner` | +| `setRunnerForOrg($org, $runnerType, $options)` | `PUT /orgs/{org}/copilot/code-review/runner` | +| `getSettingsForOrg($org)` | `GET /orgs/{org}/copilot/code-review/settings` | +| `editSettingsForOrg($org, $options)` | `PATCH /orgs/{org}/copilot/code-review/settings` | +| `getPolicyForOrg($org)` | `GET /orgs/{org}/copilot/policies/code-review` | +| `setPolicyForOrg($org, $state)` | `PUT /orgs/{org}/copilot/policies/code-review` | +| `getSettingsForRepo($owner, $repo)` | `GET /repos/{owner}/{repo}/copilot/code-review/settings` | +| `editSettingsForRepo($owner, $repo, $options)` | `PATCH /repos/{owner}/{repo}/copilot/code-review/settings` | + +## Coding agent management + +`$github->copilot->codingAgentManagement` - https://docs.github.com/rest/copilot/copilot-coding-agent-management + +| Method | Endpoint | +| --- | --- | +| `getPermissions($org)` | `GET /orgs/{org}/copilot/coding-agent/permissions` | +| `setPermissions($org, $enabledRepositories)` | `PUT /orgs/{org}/copilot/coding-agent/permissions` | +| `getRepositories($org, $page, $limit)` | `GET /orgs/{org}/copilot/coding-agent/permissions/repositories` | +| `setRepositories($org, $selectedRepositoryIds)` | `PUT /orgs/{org}/copilot/coding-agent/permissions/repositories` | +| `enableRepository($org, $repositoryId)` | `PUT /orgs/{org}/copilot/coding-agent/permissions/repositories/{repository_id}` | +| `disableRepository($org, $repositoryId)` | `DELETE /orgs/{org}/copilot/coding-agent/permissions/repositories/{repository_id}` | + +## Content exclusion management + +`$github->copilot->contentExclusionManagement` - https://docs.github.com/rest/copilot/copilot-content-exclusion-management + +| Method | Endpoint | +| --- | --- | +| `get($org)` | `GET /orgs/{org}/copilot/content_exclusion` | +| `set($org, $rules)` | `PUT /orgs/{org}/copilot/content_exclusion` | + +## Team customizations + +`$github->copilot->teamCustomizations` - https://docs.github.com/rest/copilot/copilot-team-customizations + +| Method | Endpoint | +| --- | --- | +| `get($org, $teamSlug)` | `GET /orgs/{org}/teams/{team_slug}/copilot/customizations` | +| `set($org, $teamSlug, $options)` | `PUT /orgs/{org}/teams/{team_slug}/copilot/customizations` | +| `delete($org, $teamSlug)` | `DELETE /orgs/{org}/teams/{team_slug}/copilot/customizations` | + +## Usage metrics + +`$github->copilot->usageMetrics` - https://docs.github.com/rest/copilot/copilot-usage-metrics + +| Method | Endpoint | +| --- | --- | +| `getOrganizationReport($org, $day)` | `GET /orgs/{org}/copilot/metrics/reports/organization-1-day` | +| `getOrganizationLatestReport($org)` | `GET /orgs/{org}/copilot/metrics/reports/organization-28-day/latest` | +| `getRepositoriesReport($org, $day)` | `GET /orgs/{org}/copilot/metrics/reports/repos-1-day` | +| `getUserTeamsReport($org, $day)` | `GET /orgs/{org}/copilot/metrics/reports/user-teams-1-day` | +| `getUsersReport($org, $day)` | `GET /orgs/{org}/copilot/metrics/reports/users-1-day` | +| `getUsersLatestReport($org)` | `GET /orgs/{org}/copilot/metrics/reports/users-28-day/latest` | + +## User management + +`$github->copilot->userManagement` - https://docs.github.com/rest/copilot/copilot-user-management + +| Method | Endpoint | +| --- | --- | +| `getForOrg($org)` | `GET /orgs/{org}/copilot/billing` | +| `getList($org, $page, $limit)` | `GET /orgs/{org}/copilot/billing/seats` | +| `addTeams($org, $selectedTeams)` | `POST /orgs/{org}/copilot/billing/selected_teams` | +| `removeTeams($org, $selectedTeams)` | `DELETE /orgs/{org}/copilot/billing/selected_teams` | +| `addUsers($org, $selectedUsernames)` | `POST /orgs/{org}/copilot/billing/selected_users` | +| `removeUsers($org, $selectedUsernames)` | `DELETE /orgs/{org}/copilot/billing/selected_users` | +| `getForUser($org, $username)` | `GET /orgs/{org}/members/{username}/copilot` | diff --git a/src/Package/Copilot.php b/src/Package/Copilot.php new file mode 100644 index 00000000..370d6cac --- /dev/null +++ b/src/Package/Copilot.php @@ -0,0 +1,31 @@ +processResponse($this->client->get($this->fetchUrl($path))); + } +} diff --git a/src/Package/Copilot/CodeReview.php b/src/Package/Copilot/CodeReview.php new file mode 100644 index 00000000..f02c6a01 --- /dev/null +++ b/src/Package/Copilot/CodeReview.php @@ -0,0 +1,181 @@ +processResponse($this->client->get($this->fetchUrl($path))); + } + + /** + * Set the Copilot code review runner configuration for an organisation. + * + * @param string $org The organisation name. + * @param string $runnerType The type of runner to use for Copilot code review. + * @param array $options Optional body fields: `runner_labels`, `runner_group_name`, `allow_repository_customization`. + * + * @return object|array + * + * @link https://docs.github.com/rest/copilot/copilot-code-review#set-the-copilot-code-review-runner-configuration-for-an-organization + * @since __DEPLOY_VERSION__ + */ + public function setRunnerForOrg(string $org, string $runnerType, array $options = []) + { + // Build the request path. + $path = '/orgs/' . $org . '/copilot/code-review/runner'; + + $data = array_merge($options, ['runner_type' => $runnerType]); + + return $this->processResponse($this->client->put($this->fetchUrl($path), json_encode($data))); + } + + /** + * Get Copilot code review settings for an organisation. + * + * @param string $org The organisation name. + * + * @return object|array + * + * @link https://docs.github.com/rest/copilot/copilot-code-review#get-copilot-code-review-settings-for-an-organization + * @since __DEPLOY_VERSION__ + */ + public function getSettingsForOrg(string $org) + { + // Build the request path. + $path = '/orgs/' . $org . '/copilot/code-review/settings'; + + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } + + /** + * Update Copilot code review settings for an organisation. + * + * @param string $org The organisation name. + * @param array $options Optional body fields: `default_review_effort`. + * + * @return object|array + * + * @link https://docs.github.com/rest/copilot/copilot-code-review#update-copilot-code-review-settings-for-an-organization + * @since __DEPLOY_VERSION__ + */ + public function editSettingsForOrg(string $org, array $options = []) + { + // Build the request path. + $path = '/orgs/' . $org . '/copilot/code-review/settings'; + + $data = (object) $options; + + return $this->processResponse($this->client->patch($this->fetchUrl($path), json_encode($data))); + } + + /** + * Get the Copilot code review policy for an organisation. + * + * @param string $org The organisation name. + * + * @return object|array + * + * @link https://docs.github.com/rest/copilot/copilot-code-review#get-the-copilot-code-review-policy-for-an-organization + * @since __DEPLOY_VERSION__ + */ + public function getPolicyForOrg(string $org) + { + // Build the request path. + $path = '/orgs/' . $org . '/copilot/policies/code-review'; + + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } + + /** + * Set the Copilot code review policy for an organisation. + * + * @param string $org The organisation name. + * @param string $state The state of the code review policy. + * + * @return void + * + * @link https://docs.github.com/rest/copilot/copilot-code-review#set-the-copilot-code-review-policy-for-an-organization + * @since __DEPLOY_VERSION__ + */ + public function setPolicyForOrg(string $org, string $state) + { + // Build the request path. + $path = '/orgs/' . $org . '/copilot/policies/code-review'; + + $data = ['state' => $state]; + + $this->processResponse($this->client->put($this->fetchUrl($path), json_encode($data)), 204); + } + + /** + * Get Copilot code review settings for a repository. + * + * @param string $owner The account owner of the repository. + * @param string $repo The name of the repository. + * + * @return object|array + * + * @link https://docs.github.com/rest/copilot/copilot-code-review#get-copilot-code-review-settings-for-a-repository + * @since __DEPLOY_VERSION__ + */ + public function getSettingsForRepo(string $owner, string $repo) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/copilot/code-review/settings'; + + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } + + /** + * Update Copilot code review settings for a repository. + * + * @param string $owner The account owner of the repository. + * @param string $repo The name of the repository. + * @param array $options Optional body fields: `review_effort`, `severity_filter`, `custom_instructions_enabled`, `mcp_enabled`. + * + * @return object|array + * + * @link https://docs.github.com/rest/copilot/copilot-code-review#update-copilot-code-review-settings-for-a-repository + * @since __DEPLOY_VERSION__ + */ + public function editSettingsForRepo(string $owner, string $repo, array $options = []) + { + // Build the request path. + $path = '/repos/' . $owner . '/' . $repo . '/copilot/code-review/settings'; + + $data = (object) $options; + + return $this->processResponse($this->client->patch($this->fetchUrl($path), json_encode($data))); + } +} diff --git a/src/Package/Copilot/CodingAgentManagement.php b/src/Package/Copilot/CodingAgentManagement.php new file mode 100644 index 00000000..9da24b5b --- /dev/null +++ b/src/Package/Copilot/CodingAgentManagement.php @@ -0,0 +1,140 @@ +processResponse($this->client->get($this->fetchUrl($path))); + } + + /** + * Set Copilot cloud agent permissions for an organisation. + * + * @param string $org The organisation name. + * @param string $enabledRepositories The repositories that may use the cloud agent: `all`, `selected` or `none`. + * + * @return void + * + * @link https://docs.github.com/rest/copilot/copilot-coding-agent-management#set-copilot-cloud-agent-permissions-for-an-organization + * @since __DEPLOY_VERSION__ + */ + public function setPermissions(string $org, string $enabledRepositories) + { + // Build the request path. + $path = '/orgs/' . $org . '/copilot/coding-agent/permissions'; + + $data = ['enabled_repositories' => $enabledRepositories]; + + $this->processResponse($this->client->put($this->fetchUrl($path), json_encode($data)), 204); + } + + /** + * List repositories enabled for Copilot cloud agent in an organisation. + * + * @param string $org The organisation name. + * @param int $page The page number from which to get items. + * @param int $limit The number of items on a page. + * + * @return object|array + * + * @link https://docs.github.com/rest/copilot/copilot-coding-agent-management#list-repositories-enabled-for-copilot-cloud-agent-in-an-organization + * @since __DEPLOY_VERSION__ + */ + public function getRepositories(string $org, int $page = 0, int $limit = 0) + { + // Build the request path. + $path = '/orgs/' . $org . '/copilot/coding-agent/permissions/repositories'; + + return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); + } + + /** + * Set selected repositories for Copilot cloud agent in an organisation. + * + * @param string $org The organisation name. + * @param array $selectedRepositoryIds List of repository IDs to enable. + * + * @return void + * + * @link https://docs.github.com/rest/copilot/copilot-coding-agent-management#set-selected-repositories-for-copilot-cloud-agent-in-an-organization + * @since __DEPLOY_VERSION__ + */ + public function setRepositories(string $org, array $selectedRepositoryIds) + { + // Build the request path. + $path = '/orgs/' . $org . '/copilot/coding-agent/permissions/repositories'; + + $data = ['selected_repository_ids' => $selectedRepositoryIds]; + + $this->processResponse($this->client->put($this->fetchUrl($path), json_encode($data)), 204); + } + + /** + * Enable a repository for Copilot cloud agent in an organisation. + * + * @param string $org The organisation name. + * @param int $repositoryId The unique identifier of the repository. + * + * @return void + * + * @link https://docs.github.com/rest/copilot/copilot-coding-agent-management#enable-a-repository-for-copilot-cloud-agent-in-an-organization + * @since __DEPLOY_VERSION__ + */ + public function enableRepository(string $org, int $repositoryId) + { + // Build the request path. + $path = '/orgs/' . $org . '/copilot/coding-agent/permissions/repositories/' . $repositoryId; + + $this->processResponse($this->client->put($this->fetchUrl($path), ''), 204); + } + + /** + * Disable a repository for Copilot cloud agent in an organisation. + * + * @param string $org The organisation name. + * @param int $repositoryId The unique identifier of the repository. + * + * @return void + * + * @link https://docs.github.com/rest/copilot/copilot-coding-agent-management#disable-a-repository-for-copilot-cloud-agent-in-an-organization + * @since __DEPLOY_VERSION__ + */ + public function disableRepository(string $org, int $repositoryId) + { + // Build the request path. + $path = '/orgs/' . $org . '/copilot/coding-agent/permissions/repositories/' . $repositoryId; + + $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); + } +} diff --git a/src/Package/Copilot/ContentExclusionManagement.php b/src/Package/Copilot/ContentExclusionManagement.php new file mode 100644 index 00000000..2220817c --- /dev/null +++ b/src/Package/Copilot/ContentExclusionManagement.php @@ -0,0 +1,61 @@ +processResponse($this->client->get($this->fetchUrl($path))); + } + + /** + * Set Copilot content exclusion rules for an organisation. + * + * @param string $org The organisation name. + * @param array $rules The exclusion rules, keyed by repository name, each a list of paths or match objects. + * + * @return object|array + * + * @link https://docs.github.com/rest/copilot/copilot-content-exclusion-management#set-copilot-content-exclusion-rules-for-an-organization + * @since __DEPLOY_VERSION__ + */ + public function set(string $org, array $rules) + { + // Build the request path. + $path = '/orgs/' . $org . '/copilot/content_exclusion'; + + $data = (object) $rules; + + return $this->processResponse($this->client->put($this->fetchUrl($path), json_encode($data))); + } +} diff --git a/src/Package/Copilot/TeamCustomizations.php b/src/Package/Copilot/TeamCustomizations.php new file mode 100644 index 00000000..e85282f2 --- /dev/null +++ b/src/Package/Copilot/TeamCustomizations.php @@ -0,0 +1,82 @@ +processResponse($this->client->get($this->fetchUrl($path))); + } + + /** + * Set Copilot customizations for a team. + * + * @param string $org The organisation name. + * @param string $teamSlug The slug of the team name. + * @param array $options Optional body fields: `pluginAssignments`, `extraKnownMarketplaces`. + * + * @return object|array + * + * @link https://docs.github.com/rest/copilot/copilot-team-customizations#set-copilot-customizations-for-a-team + * @since __DEPLOY_VERSION__ + */ + public function set(string $org, string $teamSlug, array $options = []) + { + // Build the request path. + $path = '/orgs/' . $org . '/teams/' . $teamSlug . '/copilot/customizations'; + + $data = (object) $options; + + return $this->processResponse($this->client->put($this->fetchUrl($path), json_encode($data))); + } + + /** + * Delete Copilot customizations for a team. + * + * @param string $org The organisation name. + * @param string $teamSlug The slug of the team name. + * + * @return void + * + * @link https://docs.github.com/rest/copilot/copilot-team-customizations#delete-copilot-customizations-for-a-team + * @since __DEPLOY_VERSION__ + */ + public function delete(string $org, string $teamSlug) + { + // Build the request path. + $path = '/orgs/' . $org . '/teams/' . $teamSlug . '/copilot/customizations'; + + $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); + } +} diff --git a/src/Package/Copilot/UsageMetrics.php b/src/Package/Copilot/UsageMetrics.php new file mode 100644 index 00000000..931d5ea6 --- /dev/null +++ b/src/Package/Copilot/UsageMetrics.php @@ -0,0 +1,166 @@ +fetchUrl($path); + $uri->setVar('day', $day); + + $response = $this->client->get($uri); + + return $this->processResponse( + $response, + \in_array($response->getStatusCode(), [200, 204], true) ? $response->getStatusCode() : 200 + ); + } + + /** + * Get the latest 28-day Copilot organisation usage metrics. + * + * @param string $org The organisation name. + * + * @return object|array + * + * @link https://docs.github.com/rest/copilot/copilot-usage-metrics#get-copilot-organization-usage-metrics + * @since __DEPLOY_VERSION__ + */ + public function getOrganizationLatestReport(string $org) + { + // Build the request path. + $path = '/orgs/' . $org . '/copilot/metrics/reports/organization-28-day/latest'; + + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } + + /** + * Get the Copilot organisation repository report for a specific day. + * + * @param string $org The organisation name. + * @param string $day The day to request data for, in `YYYY-MM-DD` format. + * + * @return object|array|null Null when GitHub answers 204 No Content. + * + * @link https://docs.github.com/rest/copilot/copilot-usage-metrics#get-copilot-organization-repository-report-for-a-specific-day + * @since __DEPLOY_VERSION__ + */ + public function getRepositoriesReport(string $org, string $day) + { + // Build the request path. + $path = '/orgs/' . $org . '/copilot/metrics/reports/repos-1-day'; + + $uri = $this->fetchUrl($path); + $uri->setVar('day', $day); + + $response = $this->client->get($uri); + + return $this->processResponse( + $response, + \in_array($response->getStatusCode(), [200, 204], true) ? $response->getStatusCode() : 200 + ); + } + + /** + * Get the Copilot organisation user-teams report for a specific day. + * + * @param string $org The organisation name. + * @param string $day The day to request data for, in `YYYY-MM-DD` format. + * + * @return object|array|null Null when GitHub answers 204 No Content. + * + * @link https://docs.github.com/rest/copilot/copilot-usage-metrics#get-copilot-organization-user-teams-report-for-a-specific-day + * @since __DEPLOY_VERSION__ + */ + public function getUserTeamsReport(string $org, string $day) + { + // Build the request path. + $path = '/orgs/' . $org . '/copilot/metrics/reports/user-teams-1-day'; + + $uri = $this->fetchUrl($path); + $uri->setVar('day', $day); + + $response = $this->client->get($uri); + + return $this->processResponse( + $response, + \in_array($response->getStatusCode(), [200, 204], true) ? $response->getStatusCode() : 200 + ); + } + + /** + * Get Copilot organisation users usage metrics for a specific day. + * + * @param string $org The organisation name. + * @param string $day The day to request data for, in `YYYY-MM-DD` format. + * + * @return object|array|null Null when GitHub answers 204 No Content. + * + * @link https://docs.github.com/rest/copilot/copilot-usage-metrics#get-copilot-organization-users-usage-metrics-for-a-specific-day + * @since __DEPLOY_VERSION__ + */ + public function getUsersReport(string $org, string $day) + { + // Build the request path. + $path = '/orgs/' . $org . '/copilot/metrics/reports/users-1-day'; + + $uri = $this->fetchUrl($path); + $uri->setVar('day', $day); + + $response = $this->client->get($uri); + + return $this->processResponse( + $response, + \in_array($response->getStatusCode(), [200, 204], true) ? $response->getStatusCode() : 200 + ); + } + + /** + * Get the latest 28-day Copilot organisation users usage metrics. + * + * @param string $org The organisation name. + * + * @return object|array + * + * @link https://docs.github.com/rest/copilot/copilot-usage-metrics#get-copilot-organization-users-usage-metrics + * @since __DEPLOY_VERSION__ + */ + public function getUsersLatestReport(string $org) + { + // Build the request path. + $path = '/orgs/' . $org . '/copilot/metrics/reports/users-28-day/latest'; + + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } +} diff --git a/src/Package/Copilot/UserManagement.php b/src/Package/Copilot/UserManagement.php new file mode 100644 index 00000000..ac134eff --- /dev/null +++ b/src/Package/Copilot/UserManagement.php @@ -0,0 +1,163 @@ +processResponse($this->client->get($this->fetchUrl($path))); + } + + /** + * List all Copilot seat assignments for an organisation. + * + * @param string $org The organisation name. + * @param int $page The page number from which to get items. + * @param int $limit The number of items on a page. + * + * @return object|array + * + * @link https://docs.github.com/rest/copilot/copilot-user-management#list-all-copilot-seat-assignments-for-an-organization + * @since __DEPLOY_VERSION__ + */ + public function getList(string $org, int $page = 0, int $limit = 0) + { + // Build the request path. + $path = '/orgs/' . $org . '/copilot/billing/seats'; + + return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit))); + } + + /** + * Add teams to the Copilot subscription for an organisation. + * + * @param string $org The organisation name. + * @param array $selectedTeams List of team names. + * + * @return object|array + * + * @link https://docs.github.com/rest/copilot/copilot-user-management#add-teams-to-the-copilot-subscription-for-an-organization + * @since __DEPLOY_VERSION__ + */ + public function addTeams(string $org, array $selectedTeams) + { + // Build the request path. + $path = '/orgs/' . $org . '/copilot/billing/selected_teams'; + + $data = ['selected_teams' => $selectedTeams]; + + return $this->processResponse($this->client->post($this->fetchUrl($path), json_encode($data)), 201); + } + + /** + * Remove teams from the Copilot subscription for an organisation. + * + * @param string $org The organisation name. + * @param array $selectedTeams List of team names. + * + * @return object|array + * + * @link https://docs.github.com/rest/copilot/copilot-user-management#remove-teams-from-the-copilot-subscription-for-an-organization + * @since __DEPLOY_VERSION__ + */ + public function removeTeams(string $org, array $selectedTeams) + { + // Build the request path. + $path = '/orgs/' . $org . '/copilot/billing/selected_teams'; + + $data = ['selected_teams' => $selectedTeams]; + + return $this->processResponse($this->client->delete($this->fetchUrl($path), [], null, json_encode($data))); + } + + /** + * Add users to the Copilot subscription for an organisation. + * + * @param string $org The organisation name. + * @param array $selectedUsernames List of usernames. + * + * @return object|array + * + * @link https://docs.github.com/rest/copilot/copilot-user-management#add-users-to-the-copilot-subscription-for-an-organization + * @since __DEPLOY_VERSION__ + */ + public function addUsers(string $org, array $selectedUsernames) + { + // Build the request path. + $path = '/orgs/' . $org . '/copilot/billing/selected_users'; + + $data = ['selected_usernames' => $selectedUsernames]; + + return $this->processResponse($this->client->post($this->fetchUrl($path), json_encode($data)), 201); + } + + /** + * Remove users from the Copilot subscription for an organisation. + * + * @param string $org The organisation name. + * @param array $selectedUsernames List of usernames. + * + * @return object|array + * + * @link https://docs.github.com/rest/copilot/copilot-user-management#remove-users-from-the-copilot-subscription-for-an-organization + * @since __DEPLOY_VERSION__ + */ + public function removeUsers(string $org, array $selectedUsernames) + { + // Build the request path. + $path = '/orgs/' . $org . '/copilot/billing/selected_users'; + + $data = ['selected_usernames' => $selectedUsernames]; + + return $this->processResponse($this->client->delete($this->fetchUrl($path), [], null, json_encode($data))); + } + + /** + * Get Copilot seat assignment details for a user. + * + * @param string $org The organisation name. + * @param string $username The handle for the GitHub user account. + * + * @return object|array + * + * @link https://docs.github.com/rest/copilot/copilot-user-management#get-copilot-seat-assignment-details-for-a-user + * @since __DEPLOY_VERSION__ + */ + public function getForUser(string $org, string $username) + { + // Build the request path. + $path = '/orgs/' . $org . '/members/' . $username . '/copilot'; + + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } +}