From 15dfe7223b37f1ddc61ce6775c662bc0a3e1c5d6 Mon Sep 17 00:00:00 2001 From: mborne Date: Wed, 30 Apr 2025 13:09:17 +0200 Subject: [PATCH] feat(visibility): add getVisibility method (refs #31) --- src/Github/GithubProject.php | 10 ++++++++-- src/Gitlab/GitlabProject.php | 16 ++++++++++++++++ src/Gogs/GogsProject.php | 12 ++++++++++++ src/Local/LocalProject.php | 6 ++++++ src/ProjectInterface.php | 7 +++++++ src/ProjectVisibility.php | 15 +++++++++++++++ tests/GithubClientTest.php | 4 ++++ tests/GitlabClientTest.php | 10 ++++++++++ tests/GogsClientTest.php | 4 ++++ tests/LocalClientTest.php | 11 +++++++++++ 10 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 src/ProjectVisibility.php diff --git a/src/Github/GithubProject.php b/src/Github/GithubProject.php index 506cc8d..e908267 100644 --- a/src/Github/GithubProject.php +++ b/src/Github/GithubProject.php @@ -3,6 +3,7 @@ namespace MBO\RemoteGit\Github; use MBO\RemoteGit\ProjectInterface; +use MBO\RemoteGit\ProjectVisibility; /** * Project implementation for github. @@ -33,14 +34,19 @@ public function getDefaultBranch(): ?string return $this->rawMetadata['default_branch']; } + public function getHttpUrl(): string + { + return $this->rawMetadata['clone_url']; + } + public function isArchived(): bool { return $this->rawMetadata['archived']; } - public function getHttpUrl(): string + public function getVisibility(): ?ProjectVisibility { - return $this->rawMetadata['clone_url']; + return $this->rawMetadata['private'] ? ProjectVisibility::PRIVATE : ProjectVisibility::PUBLIC; } public function getRawMetadata(): array diff --git a/src/Gitlab/GitlabProject.php b/src/Gitlab/GitlabProject.php index 3996649..66efda1 100644 --- a/src/Gitlab/GitlabProject.php +++ b/src/Gitlab/GitlabProject.php @@ -3,6 +3,8 @@ namespace MBO\RemoteGit\Gitlab; use MBO\RemoteGit\ProjectInterface; +use MBO\RemoteGit\ProjectVisibility; +use RuntimeException; /** * Common project properties between different git project host (gitlab, github, etc.). @@ -47,6 +49,20 @@ public function isArchived(): bool return $this->rawMetadata['archived']; } + public function getVisibility(): ?ProjectVisibility + { + switch ($this->rawMetadata['visibility']) { + case 'public': + return ProjectVisibility::PUBLIC; + case 'private': + return ProjectVisibility::PRIVATE; + case 'internal': + return ProjectVisibility::INTERNAL; + default: + throw new RuntimeException("Unknown visibility: {$this->rawMetadata['visibility']}"); + } + } + public function getRawMetadata(): array { return $this->rawMetadata; diff --git a/src/Gogs/GogsProject.php b/src/Gogs/GogsProject.php index 52e9562..b987816 100644 --- a/src/Gogs/GogsProject.php +++ b/src/Gogs/GogsProject.php @@ -3,6 +3,7 @@ namespace MBO\RemoteGit\Gogs; use MBO\RemoteGit\ProjectInterface; +use MBO\RemoteGit\ProjectVisibility; /** * Project implementation for github. @@ -43,6 +44,17 @@ public function isArchived(): bool return $this->rawMetadata['archived']; } + public function getVisibility(): ?ProjectVisibility + { + if ($this->rawMetadata['private']) { + return ProjectVisibility::PRIVATE; + } elseif ($this->rawMetadata['internal']) { + return ProjectVisibility::INTERNAL; + } else { + return ProjectVisibility::PUBLIC; + } + } + public function getRawMetadata(): array { return $this->rawMetadata; diff --git a/src/Local/LocalProject.php b/src/Local/LocalProject.php index 5f20aad..04d1d6a 100644 --- a/src/Local/LocalProject.php +++ b/src/Local/LocalProject.php @@ -3,6 +3,7 @@ namespace MBO\RemoteGit\Local; use MBO\RemoteGit\ProjectInterface; +use MBO\RemoteGit\ProjectVisibility; /** * Project corresponding to a local git folder. @@ -41,6 +42,11 @@ public function isArchived(): bool return false; // Always returns false for LocalClient } + public function getVisibility(): ?ProjectVisibility + { + return null; // Always returns null for LocalClient + } + public function getRawMetadata(): array { return $this->rawMetadata; diff --git a/src/ProjectInterface.php b/src/ProjectInterface.php index 88e9f15..9ca0a4e 100644 --- a/src/ProjectInterface.php +++ b/src/ProjectInterface.php @@ -36,6 +36,13 @@ public function getHttpUrl(): string; */ public function isArchived(): bool; + /** + * Get project visibility. + * + * @warning This method will always return null for LocalClient. + */ + public function getVisibility(): ?ProjectVisibility; + /** * Get hosting service specific properties. * diff --git a/src/ProjectVisibility.php b/src/ProjectVisibility.php new file mode 100644 index 0000000..35a117b --- /dev/null +++ b/src/ProjectVisibility.php @@ -0,0 +1,15 @@ +assertFalse($project->isArchived()); + + /* test getVisibility */ + $this->assertEquals(ProjectVisibility::PUBLIC, $project->getVisibility()); } /** diff --git a/tests/GitlabClientTest.php b/tests/GitlabClientTest.php index 99c2496..f823803 100644 --- a/tests/GitlabClientTest.php +++ b/tests/GitlabClientTest.php @@ -7,6 +7,7 @@ use MBO\RemoteGit\FindOptions; use MBO\RemoteGit\Gitlab\GitlabClient; use MBO\RemoteGit\Gitlab\GitlabProject; +use MBO\RemoteGit\ProjectVisibility; use Psr\Log\NullLogger; class GitlabClientTest extends TestCase @@ -62,9 +63,11 @@ public function testGitlabDotComByUser(): void ); $project = $projectsByName['mborne/sample-composer']; + /* test getDefaultBranch */ $defaultBranch = $project->getDefaultBranch(); $this->assertNotNull($defaultBranch); + /* test getRawFile */ $composer = $client->getRawFile( $project, @@ -72,8 +75,12 @@ public function testGitlabDotComByUser(): void $defaultBranch ); $this->assertStringContainsString('mborne@users.noreply.github.com', $composer); + /* test isArchived */ $this->assertFalse($project->isArchived()); + + /* test getVisibility */ + $this->assertEquals(ProjectVisibility::PUBLIC, $project->getVisibility()); } public function testGitlabDotComOrgs(): void @@ -140,5 +147,8 @@ public function testGitlabDotComSearch(): void /* test isArchived */ $this->assertFalse($project->isArchived()); + + /* test getVisibility */ + $this->assertEquals(ProjectVisibility::PUBLIC, $project->getVisibility()); } } diff --git a/tests/GogsClientTest.php b/tests/GogsClientTest.php index 014a2a2..137914d 100644 --- a/tests/GogsClientTest.php +++ b/tests/GogsClientTest.php @@ -7,6 +7,7 @@ use MBO\RemoteGit\FindOptions; use MBO\RemoteGit\Gogs\GogsClient; use MBO\RemoteGit\Gogs\GogsProject; +use MBO\RemoteGit\ProjectVisibility; /** * Test GogsClient with https://codes.quadtreeworld.net which is a gitea instance. @@ -125,5 +126,8 @@ public function testFindByUserAndOrgs(): void /* test isArchived */ $this->assertFalse($project->isArchived()); + + /* test getVisibility */ + $this->assertEquals(ProjectVisibility::PUBLIC, $project->getVisibility()); } } diff --git a/tests/LocalClientTest.php b/tests/LocalClientTest.php index fa9dbe0..e63750c 100644 --- a/tests/LocalClientTest.php +++ b/tests/LocalClientTest.php @@ -99,6 +99,17 @@ public function testIsArchived(): void } } + /** + * Ensure that isArchived returns public. + */ + public function testGetVisibility(): void + { + $projects = $this->findAllProjects(); + foreach ($projects as $project) { + $this->assertNull($project->getVisibility()); + } + } + /** * Check that raw file content can be retreived from non bare repository. */