Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.15] Repository branch and file check existence methods #782

Open
wants to merge 2 commits into
base: 11.15
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Api/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,20 @@ protected function delete(string $uri, array $params = [], array $headers = [])
return ResponseMediator::getContent($response);
}

/**
* @param string $uri
* @param array<string,mixed> $params
* @param array<string,string> $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
*
Expand Down
13 changes: 13 additions & 0 deletions src/Api/Repositories.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 200 === $this->head($uri, []);
}

/**
* @param int|string $project_id
* @param string $branch
Expand Down
17 changes: 17 additions & 0 deletions src/Api/RepositoryFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 200 === $this->head($uri, $params);
}
}
15 changes: 15 additions & 0 deletions tests/Api/RepositoriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
17 changes: 17 additions & 0 deletions tests/Api/RepositoryFilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down