diff --git a/src/Api/Projects.php b/src/Api/Projects.php index fa2877dc..9edf6b76 100644 --- a/src/Api/Projects.php +++ b/src/Api/Projects.php @@ -1597,4 +1597,68 @@ public function deleteProtectedTag($project_id, string $tag_name) { return $this->delete($this->getProjectPath($project_id, 'protected_tags/'.self::encodePath($tag_name))); } + + /** + * @param int|string $project_id + * @param string $integration + * @param array $parameters + * + * @return mixed + */ + public function updateIntegration($project_id, string $integration, array $parameters = []) + { + $resolver = new OptionsResolver(); + $booleanNormalizer = function (Options $resolver, $value): string { + return $value ? 'true' : 'false'; + }; + + if ('jira' === $integration) { + $resolver->setDefined('url') + ->setAllowedTypes('url', 'string') + ->setRequired('url') + ; + $resolver->setDefined('password') + ->setAllowedTypes('password', 'string') + ->setRequired('password') + ; + $resolver->setDefined('username') + ->setAllowedTypes('username', 'string') + ; + $resolver->setDefined('jira_auth_type') + ->setAllowedTypes('jira_auth_type', 'int') + ->setAllowedValues('jira_auth_type', [0, 1]) + ; + } + + if ('slack' === $integration) { + $resolver->setDefined('webhook') + ->setAllowedTypes('webhook', 'string') + ->setRequired('webhook') + ; + $resolver->setDefined('username') + ->setAllowedTypes('username', 'string') + ; + $resolver->setDefined('channel') + ->setAllowedTypes('channel', 'string') + ; + $resolver->setDefined('pipeline_channel') + ->setAllowedTypes('pipeline_channel', 'string') + ; + $resolver->setDefined('pipeline_events') + ->setAllowedTypes('pipeline_events', 'bool') + ->setNormalizer('pipeline_events', $booleanNormalizer) + ; + $resolver->setDefined('notify_only_broken_pipelines') + ->setAllowedTypes('notify_only_broken_pipelines', 'bool') + ->setNormalizer('notify_only_broken_pipelines', $booleanNormalizer) + ; + + $resolver->setDefined('branches_to_be_notified') + ->setAllowedTypes('branches_to_be_notified', 'string') + ->setAllowedValues('branches_to_be_notified', ['all', 'default', 'protected', 'default_and_protected']) + ; + } + + return $this->put($this->getProjectPath($project_id, 'integrations/'.$integration), $parameters); + } } diff --git a/tests/Api/ProjectsTest.php b/tests/Api/ProjectsTest.php index 709752e1..f2a532e9 100644 --- a/tests/Api/ProjectsTest.php +++ b/tests/Api/ProjectsTest.php @@ -2890,6 +2890,22 @@ public function shouldRemoveProtectedTag(): void $this->assertEquals($expectedBool, $api->deleteProtectedTag(1, 'release-*')); } + /** + * @test + */ + public function shouldUpdateIntegrationConfig(): void + { + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('put') + ->with( + 'projects/1/integrations/jira' + ) + ->will($this->returnValue([])); + + $this->assertEquals([], $api->updateIntegration(1, 'jira', [])); + } + protected function getApiClass() { return Projects::class;