Skip to content

Commit 7f5148a

Browse files
Add latest project pipeline endpoint
Support GitLab's latest pipeline endpoint with its ref filter only, without exposing manual pagination parameters. Co-authored-by: Wesley Sartori <25470180+wbsartori@users.noreply.github.com>
1 parent 7c9d379 commit 7f5148a

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
* Add support for personal access tokens
1414
* Add support for project integrations endpoints
1515
* Add support for `Projects::updateDeployKey`
16+
* Add support for `Projects::latestPipeline`
1617
* Add support for project push rules
1718
* Add support for additional parameters in `Projects::fork`
1819
* Correct project forks list parameter handling

src/Api/Projects.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,22 @@ public function pipeline(int|string $project_id, int $pipeline_id): mixed
341341
return $this->get($this->getProjectPath($project_id, 'pipelines/'.self::encodePath($pipeline_id)));
342342
}
343343

344+
/**
345+
* @param array $parameters {
346+
*
347+
* @var string $ref branch or tag to check for the latest pipeline
348+
* }
349+
*/
350+
public function latestPipeline(int|string $project_id, array $parameters = []): mixed
351+
{
352+
$resolver = new OptionsResolver();
353+
$resolver->setDefined('ref')
354+
->setAllowedTypes('ref', 'string')
355+
;
356+
357+
return $this->get($this->getProjectPath($project_id, 'pipelines/latest'), $resolver->resolve($parameters));
358+
}
359+
344360
public function pipelineJobs(int|string $project_id, int $pipeline_id): mixed
345361
{
346362
return $this->get($this->getProjectPath($project_id, 'pipelines/'.self::encodePath($pipeline_id).'/jobs'));

tests/Api/ProjectsTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,69 @@ public function shouldGetPipeline(): void
819819
$this->assertEquals($expectedArray, $api->pipeline(1, 3));
820820
}
821821

822+
#[Test]
823+
public function shouldGetLatestPipeline(): void
824+
{
825+
$expectedArray = [
826+
'id' => 287,
827+
'iid' => 144,
828+
'project_id' => 21,
829+
'ref' => 'main',
830+
'status' => 'success',
831+
];
832+
833+
$api = $this->getApiMock();
834+
$api->expects($this->once())
835+
->method('get')
836+
->with('projects/1/pipelines/latest', [])
837+
->willReturn($expectedArray)
838+
;
839+
840+
$this->assertEquals($expectedArray, $api->latestPipeline(1));
841+
}
842+
843+
#[Test]
844+
public function shouldGetLatestPipelineWithRef(): void
845+
{
846+
$expectedArray = [
847+
'id' => 287,
848+
'iid' => 144,
849+
'project_id' => 21,
850+
'ref' => 'develop',
851+
'status' => 'success',
852+
];
853+
854+
$api = $this->getApiMock();
855+
$api->expects($this->once())
856+
->method('get')
857+
->with('projects/1/pipelines/latest', ['ref' => 'develop'])
858+
->willReturn($expectedArray)
859+
;
860+
861+
$this->assertEquals($expectedArray, $api->latestPipeline(1, ['ref' => 'develop']));
862+
}
863+
864+
#[Test]
865+
public function shouldGetLatestPipelineForStringProjectPath(): void
866+
{
867+
$expectedArray = [
868+
'id' => 287,
869+
'iid' => 144,
870+
'project_id' => 21,
871+
'ref' => 'main',
872+
'status' => 'success',
873+
];
874+
875+
$api = $this->getApiMock();
876+
$api->expects($this->once())
877+
->method('get')
878+
->with('projects/group%2Fproject/pipelines/latest', [])
879+
->willReturn($expectedArray)
880+
;
881+
882+
$this->assertEquals($expectedArray, $api->latestPipeline('group/project'));
883+
}
884+
822885
#[Test]
823886
public function shouldGetPipelineJobs(): void
824887
{

0 commit comments

Comments
 (0)