diff --git a/CHANGELOG.md b/CHANGELOG.md index f66d43aa..b33efed4 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 `job_inputs` and `job_variables_attributes` in `Jobs::play` * Add support for filters in `Projects::projectAccessTokens` +* Add support for listing merge requests associated with a commit ## [12.0.0] - 2025-02-23 diff --git a/src/Api/Repositories.php b/src/Api/Repositories.php index b3c1f890..38e11606 100644 --- a/src/Api/Repositories.php +++ b/src/Api/Repositories.php @@ -185,6 +185,25 @@ public function commitRefs(int|string $project_id, string $sha, array $parameter ); } + /** + * @param array $parameters { + * + * @var string $state Returns merge requests with the specified state: opened, closed, locked, or merged. + * } + */ + public function commitMergeRequests(int|string $project_id, string $sha, array $parameters = []): mixed + { + $resolver = new OptionsResolver(); + $resolver->setDefined('state') + ->setAllowedValues('state', ['opened', 'closed', 'locked', 'merged']) + ; + + return $this->get( + $this->getProjectPath($project_id, 'repository/commits/'.self::encodePath($sha).'/merge_requests'), + $resolver->resolve($parameters) + ); + } + /** * @param array $parameters { * diff --git a/tests/Api/RepositoriesTest.php b/tests/Api/RepositoriesTest.php index 3b5d7334..3cca7e8d 100644 --- a/tests/Api/RepositoriesTest.php +++ b/tests/Api/RepositoriesTest.php @@ -328,6 +328,41 @@ public function shouldGetCommitRefs(): void $this->assertEquals($expectedArray, $api->commitRefs(1, 'abcd1234')); } + #[Test] + public function shouldGetCommitMergeRequests(): void + { + $expectedArray = [ + ['id' => 1, 'title' => 'A merge request'], + ['id' => 2, 'title' => 'Another merge request'], + ]; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('projects/1/repository/commits/abcd1234/merge_requests', []) + ->willReturn($expectedArray) + ; + + $this->assertEquals($expectedArray, $api->commitMergeRequests(1, 'abcd1234')); + } + + #[Test] + public function shouldGetCommitMergeRequestsWithState(): void + { + $expectedArray = [ + ['id' => 1, 'title' => 'A merge request', 'state' => 'opened'], + ]; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('projects/1/repository/commits/abcd1234/merge_requests', ['state' => 'opened']) + ->willReturn($expectedArray) + ; + + $this->assertEquals($expectedArray, $api->commitMergeRequests(1, 'abcd1234', ['state' => 'opened'])); + } + #[Test] #[DataProvider('dataGetCommitRefsWithParams')] public function shouldGetCommitRefsWithParams(string $type, array $expectedArray): void