From 27487989458b9b49b34c7e589eac9e1449a23d9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mariusz=20W=C3=B3jcik?= Date: Wed, 24 Apr 2024 20:49:03 +0200 Subject: [PATCH 1/2] Stop stale environments --- src/Api/Environments.php | 30 ++++++++++++++++++++++++++++++ tests/Api/EnvironmentsTest.php | 17 +++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/Api/Environments.php b/src/Api/Environments.php index 09cd632b..8aa0ab60 100644 --- a/src/Api/Environments.php +++ b/src/Api/Environments.php @@ -14,6 +14,8 @@ namespace Gitlab\Api; +use DateTimeInterface; +use Symfony\Component\OptionsResolver\Options; use Symfony\Component\OptionsResolver\OptionsResolver; class Environments extends AbstractApi @@ -95,4 +97,32 @@ public function show($project_id, int $environment_id) { return $this->get($this->getProjectPath($project_id, 'environments/'.self::encodePath($environment_id))); } + + /** + * @param int|string $project_id + * @param array $parameters { + * + * @var DateTimeInterface $before Stop environments that have been modified or deployed to before the specified date. + * Expected in ISO 8601 format (2019-03-15T08:00:00Z). + * Valid inputs are between 10 years ago and 1 week ago + * } + * + * @return mixed + */ + public function stopStale($project_id, array $parameters = []) + { + $resolver = new OptionsResolver(); + $resolver->setDefined('before') + ->setRequired('before') + ->setAllowedTypes('before', DateTimeInterface::class) + ->setNormalizer('before', fn (Options $resolver, DateTimeInterface $value): string => $value->format('c')); + + return $this->post( + $this->getProjectPath( + $project_id, + 'environments/stop_stale' + ), + $resolver->resolve($parameters), + ); + } } diff --git a/tests/Api/EnvironmentsTest.php b/tests/Api/EnvironmentsTest.php index 03dec459..abb13d5a 100644 --- a/tests/Api/EnvironmentsTest.php +++ b/tests/Api/EnvironmentsTest.php @@ -204,6 +204,23 @@ public function shouldStopEnvironment(): void $this->assertEquals($expectedBool, $api->stop(1, 3)); } + /** + * @test + */ + public function shouldStopStaleEnvironment(): void + { + $expectedValue = [ + 'message' => 'Successfully requested stop for all stale environments', + ]; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('post') + ->with('projects/1/environments/stop_stale') + ->will($this->returnValue($expectedValue)); + $this->assertEquals($expectedValue, $api->stopStale(1, ['before' => new \DateTime('2020-01-01')])); + } + protected function getApiClass() { return Environments::class; From 345577dc2fcadcf3aa833a5eff5d171dc1738012 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mariusz=20W=C3=B3jcik?= Date: Wed, 24 Apr 2024 20:51:27 +0200 Subject: [PATCH 2/2] code style fix --- src/Api/Environments.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Api/Environments.php b/src/Api/Environments.php index 8aa0ab60..12d91d68 100644 --- a/src/Api/Environments.php +++ b/src/Api/Environments.php @@ -115,7 +115,7 @@ public function stopStale($project_id, array $parameters = []) $resolver->setDefined('before') ->setRequired('before') ->setAllowedTypes('before', DateTimeInterface::class) - ->setNormalizer('before', fn (Options $resolver, DateTimeInterface $value): string => $value->format('c')); + ->setNormalizer('before', fn (Options $resolver, DateTimeInterface $value): string => $value->format('c')); return $this->post( $this->getProjectPath(