diff --git a/CHANGELOG.md b/CHANGELOG.md index c540bee9..7c2a70a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Api/Projects.php b/src/Api/Projects.php index 4511fabf..96fb06e0 100644 --- a/src/Api/Projects.php +++ b/src/Api/Projects.php @@ -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')); diff --git a/tests/Api/ProjectsTest.php b/tests/Api/ProjectsTest.php index 84c1204a..3f77081c 100644 --- a/tests/Api/ProjectsTest.php +++ b/tests/Api/ProjectsTest.php @@ -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 {