From 6907d5b3ce4af8e584ce65791cbf5ad4c8928b66 Mon Sep 17 00:00:00 2001 From: "a.o.ivanov" Date: Tue, 31 Oct 2023 15:41:04 +0200 Subject: [PATCH 1/2] Repository branch and file check existence methods --- src/Api/AbstractApi.php | 14 ++++++++++++++ src/Api/Repositories.php | 13 +++++++++++++ src/Api/RepositoryFiles.php | 17 +++++++++++++++++ tests/Api/RepositoriesTest.php | 15 +++++++++++++++ tests/Api/RepositoryFilesTest.php | 17 +++++++++++++++++ 5 files changed, 76 insertions(+) diff --git a/src/Api/AbstractApi.php b/src/Api/AbstractApi.php index c7026135..6d3fd891 100644 --- a/src/Api/AbstractApi.php +++ b/src/Api/AbstractApi.php @@ -232,6 +232,20 @@ protected function delete(string $uri, array $params = [], array $headers = []) return ResponseMediator::getContent($response); } + /** + * @param string $uri + * @param array $params + * @param array $headers + * + * @return int + */ + protected function head(string $uri, array $params, array $headers = []): int + { + $response = $this->client->getHttpClient()->head(self::prepareUri($uri, $params), $headers); + + return $response->getStatusCode(); + } + /** * @param int|string $uri * diff --git a/src/Api/Repositories.php b/src/Api/Repositories.php index ac6f4f0f..471eccc8 100644 --- a/src/Api/Repositories.php +++ b/src/Api/Repositories.php @@ -58,6 +58,19 @@ public function branch($project_id, string $branch) return $this->get($this->getProjectPath($project_id, 'repository/branches/'.self::encodePath($branch))); } + /** + * @param int|string $project_id + * @param string $branch + * + * @return bool + */ + public function branchExists($project_id, string $branch): bool + { + $uri = $this->getProjectPath($project_id, 'repository/branches/'.self::encodePath($branch)); + + return $this->head($uri, []) === 200; + } + /** * @param int|string $project_id * @param string $branch diff --git a/src/Api/RepositoryFiles.php b/src/Api/RepositoryFiles.php index be94b4ee..8bef3fdf 100644 --- a/src/Api/RepositoryFiles.php +++ b/src/Api/RepositoryFiles.php @@ -146,4 +146,21 @@ public function deleteFile($project_id, array $parameters = []) return $this->delete($this->getProjectPath($project_id, 'repository/files/'.self::encodePath($resolved['file_path'])), $resolved); } + + /** + * @param int|string $project_id + * @param string $file_path + * @param string $ref + * + * @return bool + */ + public function fileExists($project_id, string $file_path, string $ref): bool + { + $uri = $this->getProjectPath($project_id, 'repository/files/'.self::encodePath($file_path)); + $params = [ + 'ref' => $ref, + ]; + + return $this->head($uri, $params) === 200; + } } diff --git a/tests/Api/RepositoriesTest.php b/tests/Api/RepositoriesTest.php index 67e9ceb8..30eb7f26 100644 --- a/tests/Api/RepositoriesTest.php +++ b/tests/Api/RepositoriesTest.php @@ -55,6 +55,21 @@ public function shouldGetBranch(): void $this->assertEquals($expectedArray, $api->branch(1, 'master')); } + /** + * @test + */ + public function shouldBranchExists(): void + { + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('head') + ->with('projects/1/repository/branches/master', []) + ->will($this->returnValue(200)) + ; + + $this->assertTrue($api->branchExists(1, 'master')); + } + /** * @test */ diff --git a/tests/Api/RepositoryFilesTest.php b/tests/Api/RepositoryFilesTest.php index 81600a59..cc68df03 100644 --- a/tests/Api/RepositoryFilesTest.php +++ b/tests/Api/RepositoryFilesTest.php @@ -280,6 +280,23 @@ public function shouldDeleteFileWithAuthor(): void ])); } + /** + * @test + */ + public function shouldFileExists(): void + { + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('head') + ->with('projects/1/repository/files/dir%2Ffile1.txt', [ + 'ref' => 'master', + ]) + ->will($this->returnValue(200)) + ; + + $this->assertTrue($api->fileExists(1, 'dir/file1.txt', 'master')); + } + /** * @return string */ From 2915dc733ec283db77568332ea6fa958b9455ee3 Mon Sep 17 00:00:00 2001 From: "a.o.ivanov" Date: Tue, 31 Oct 2023 15:44:43 +0200 Subject: [PATCH 2/2] Using Yoda-style conditions check --- src/Api/Repositories.php | 2 +- src/Api/RepositoryFiles.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Api/Repositories.php b/src/Api/Repositories.php index 471eccc8..0b8fa7e1 100644 --- a/src/Api/Repositories.php +++ b/src/Api/Repositories.php @@ -68,7 +68,7 @@ public function branchExists($project_id, string $branch): bool { $uri = $this->getProjectPath($project_id, 'repository/branches/'.self::encodePath($branch)); - return $this->head($uri, []) === 200; + return 200 === $this->head($uri, []); } /** diff --git a/src/Api/RepositoryFiles.php b/src/Api/RepositoryFiles.php index 8bef3fdf..0667f197 100644 --- a/src/Api/RepositoryFiles.php +++ b/src/Api/RepositoryFiles.php @@ -161,6 +161,6 @@ public function fileExists($project_id, string $file_path, string $ref): bool 'ref' => $ref, ]; - return $this->head($uri, $params) === 200; + return 200 === $this->head($uri, $params); } }