Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Add support for personal access tokens
* Add support for project integrations endpoints
* Add support for `Projects::updateDeployKey`
* Add support for `Projects::latestPipeline`
* Add support for project push rules
* Add support for additional parameters in `Projects::fork`
* Correct project forks list parameter handling
Expand Down
16 changes: 16 additions & 0 deletions src/Api/Projects.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,22 @@ public function pipeline(int|string $project_id, int $pipeline_id): mixed
return $this->get($this->getProjectPath($project_id, 'pipelines/'.self::encodePath($pipeline_id)));
}

/**
* @param array $parameters {
*
* @var string $ref branch or tag to check for the latest pipeline
* }
*/
public function latestPipeline(int|string $project_id, array $parameters = []): mixed
{
$resolver = new OptionsResolver();
$resolver->setDefined('ref')
->setAllowedTypes('ref', 'string')
;

return $this->get($this->getProjectPath($project_id, 'pipelines/latest'), $resolver->resolve($parameters));
}

public function pipelineJobs(int|string $project_id, int $pipeline_id): mixed
{
return $this->get($this->getProjectPath($project_id, 'pipelines/'.self::encodePath($pipeline_id).'/jobs'));
Expand Down
63 changes: 63 additions & 0 deletions tests/Api/ProjectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,69 @@ public function shouldGetPipeline(): void
$this->assertEquals($expectedArray, $api->pipeline(1, 3));
}

#[Test]
public function shouldGetLatestPipeline(): void
{
$expectedArray = [
'id' => 287,
'iid' => 144,
'project_id' => 21,
'ref' => 'main',
'status' => 'success',
];

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('projects/1/pipelines/latest', [])
->willReturn($expectedArray)
;

$this->assertEquals($expectedArray, $api->latestPipeline(1));
}

#[Test]
public function shouldGetLatestPipelineWithRef(): void
{
$expectedArray = [
'id' => 287,
'iid' => 144,
'project_id' => 21,
'ref' => 'develop',
'status' => 'success',
];

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('projects/1/pipelines/latest', ['ref' => 'develop'])
->willReturn($expectedArray)
;

$this->assertEquals($expectedArray, $api->latestPipeline(1, ['ref' => 'develop']));
}

#[Test]
public function shouldGetLatestPipelineForStringProjectPath(): void
{
$expectedArray = [
'id' => 287,
'iid' => 144,
'project_id' => 21,
'ref' => 'main',
'status' => 'success',
];

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('projects/group%2Fproject/pipelines/latest', [])
->willReturn($expectedArray)
;

$this->assertEquals($expectedArray, $api->latestPipeline('group/project'));
}

#[Test]
public function shouldGetPipelineJobs(): void
{
Expand Down