From ea0e3a94eb7e6d92b59b83be677997cece7e8339 Mon Sep 17 00:00:00 2001 From: Philipp Kolmann Date: Tue, 19 Mar 2024 09:36:05 +0100 Subject: [PATCH 01/10] Add MS Teams and Jira Integrations APIs --- src/Api/Integrations.php | 323 +++++++++++++++++++++++++++++++++ src/Api/Projects.php | 12 ++ src/Client.php | 9 + tests/Api/IntegrationsTest.php | 195 ++++++++++++++++++++ tests/Api/ProjectsTest.php | 13 ++ 5 files changed, 552 insertions(+) create mode 100644 src/Api/Integrations.php create mode 100644 tests/Api/IntegrationsTest.php diff --git a/src/Api/Integrations.php b/src/Api/Integrations.php new file mode 100644 index 00000000..5a1eca9c --- /dev/null +++ b/src/Api/Integrations.php @@ -0,0 +1,323 @@ + + * (c) Graham Campbell + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Gitlab\Api; + +use Symfony\Component\OptionsResolver\Options; +use Symfony\Component\OptionsResolver\OptionsResolver; + +class Integrations extends AbstractApi +{ + /** + * @param int|string $project_id + * @return mixed + */ + public function all(int|string $project_id): mixed + { + $path = $this->getProjectPath($project_id, 'integrations'); + return $this->get($path); + } + + // Microsoft Teams + + /** + * Create Microsoft Teams integration + * Set Microsoft Teams integration for a project + * + * @param int|string $project_id + * @param array $params { + * @var string $webhook The Microsoft Teams webhook. + * @var bool $notify_only_broken_pipelines Send notifications for broken pipelines + * @var string $branches_to_be_notified Branches to send notifications for. Valid options are all, default, + * protected, and default_and_protected. The default value is "default" + * @var bool $push_events Enable notifications for push events + * @var bool $issues_events Enable notifications for issue events + * @var bool $confidential_issues_events Enable notifications for confidential issue events + * @var bool $merge_requests_events Enable notifications for merge request events + * @var bool $tag_push_events Enable notifications for tag push events + * @var bool $note_events Enable notifications for note events + * @var bool $confidential_note_events Enable notifications for confidential note events + * @var bool $pipeline_events Enable notifications for pipeline events + * @var bool $wiki_page_events Enable notifications for wiki page events + * } + * + * @return mixed + */ + public function createMicrosoftTeams(int|string $project_id, array $params = []): mixed + { + $resolver = new OptionsResolver(); + $booleanNormalizer = function (Options $resolver, $value): string { + return $value ? 'true' : 'false'; + }; + + $resolver->setDefined('webhook') + ->setAllowedTypes('webhook', 'string') + ->setRequired('webhook') + ; + $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']) + ; + $resolver->setDefined('push_events') + ->setAllowedTypes('push_events', 'bool') + ->setNormalizer('push_events', $booleanNormalizer) + ; + $resolver->setDefined('issues_events') + ->setAllowedTypes('issues_events', 'bool') + ->setNormalizer('issues_events', $booleanNormalizer) + ; + $resolver->setDefined('confidential_issues_events') + ->setAllowedTypes('confidential_issues_events', 'bool') + ->setNormalizer('confidential_issues_events', $booleanNormalizer) + ; + $resolver->setDefined('merge_requests_events') + ->setAllowedTypes('merge_requests_events', 'bool') + ->setNormalizer('merge_requests_events', $booleanNormalizer) + ; + $resolver->setDefined('tag_push_events') + ->setAllowedTypes('tag_push_events', 'bool') + ->setNormalizer('tag_push_events', $booleanNormalizer) + ; + $resolver->setDefined('note_events') + ->setAllowedTypes('note_events', 'bool') + ->setNormalizer('note_events', $booleanNormalizer) + ; + $resolver->setDefined('confidential_note_events') + ->setAllowedTypes('confidential_note_events', 'bool') + ->setNormalizer('confidential_note_events', $booleanNormalizer) + ; + $resolver->setDefined('pipeline_events') + ->setAllowedTypes('pipeline_events', 'bool') + ->setNormalizer('pipeline_events', $booleanNormalizer) + ; + $resolver->setDefined('wiki_page_events') + ->setAllowedTypes('wiki_page_events', 'bool') + ->setNormalizer('wiki_page_events', $booleanNormalizer) + ; + + return $this->put($this->getProjectPath($project_id, 'integrations/microsoft-teams'), $resolver->resolve($params)); + } + + /** + * Update Microsoft Teams integration + * Set Microsoft Teams integration for a project + * + * @param int|string $project_id + * @param array $params { + * @var string $webhook The Microsoft Teams webhook. + * @var bool $notify_only_broken_pipelines Send notifications for broken pipelines + * @var string $branches_to_be_notified Branches to send notifications for. Valid options are all, default, + * protected, and default_and_protected. The default value is "default" + * @var bool $push_events Enable notifications for push events + * @var bool $issues_events Enable notifications for issue events + * @var bool $confidential_issues_events Enable notifications for confidential issue events + * @var bool $merge_requests_events Enable notifications for merge request events + * @var bool $tag_push_events Enable notifications for tag push events + * @var bool $note_events Enable notifications for note events + * @var bool $confidential_note_events Enable notifications for confidential note events + * @var bool $pipeline_events Enable notifications for pipeline events + * @var bool $wiki_page_events Enable notifications for wiki page events + * } + * + * @return mixed + */ + public function updateMicrosoftTeams(int|string $project_id, array $params = []): mixed + { + return $this->createMicrosoftTeams($project_id, $params); + } + + /** + * Get Microsoft Teams integration settings for a project + * + * @param int|string $project_id + * @return mixed + */ + public function getMicrosoftTeams(int|string $project_id): mixed + { + return $this->get($this->getProjectPath($project_id, 'integrations/microsoft-teams')); + } + + /** + * Disable the Microsoft Teams integration for a project. Integration settings are reset + * + * @param int|string $project_id + * @return mixed + */ + public function removeMicrosoftTeams(int|string $project_id): mixed + { + return $this->delete($this->getProjectPath($project_id, 'integrations/microsoft-teams')); + } + + + // JIRA + + /** + * Create Jira integration + * Set Jira integration for a project + * + * @param int|string $project_id + * @param array $params { + * @var string $url The URL to the Jira project which is being linked to this GitLab project + * @var bool $api_url The base URL to the Jira instance API. Web URL value is used if not set + * @var string $username The email or username to be used with Jira. For Jira Cloud use an email, + * for Jira Data Center and Jira Server use a username. Required when using + * Basic authentication (jira_auth_type is 0) + * @var string $password The Jira API token, password, or personal access token to be used with + * Jira. When your authentication method is Basic (jira_auth_type is 0) use + * an API token for Jira Cloud, or a password for Jira Data Center or Jira + * Server. When your authentication method is Jira personal access token + * (jira_auth_type is 1) use a personal access token. + * @var string $active Activates or deactivates the integration. Defaults to false (deactivated). + * @var string $jira_auth_type The authentication method to be used with Jira. 0 means Basic + * Authentication. 1 means Jira personal access token. Defaults to 0. + * @var string $jira_issue_prefix Prefix to match Jira issue keys. + * @var string $jira_issue_regex Regular expression to match Jira issue keys. + * @var string $jira_issue_transition_automatic Enable automatic issue transitions. Takes precedence over + * jira_issue_transition_id if enabled. Defaults to false + * @var string $jira_issue_transition_id The ID of one or more transitions for custom issue + * transitions. Ignored if jira_issue_transition_automatic is + * enabled. Defaults to a blank string, which disables custom + * transitions. + * @var string $commit_events Enable notifications for commit events + * @var string $merge_requests_events Enable notifications for merge request events + * @var string $comment_on_event_enabled Enable comments inside Jira issues on each GitLab event + * (commit / merge request) + * } + * + * @return mixed + */ + public function createJira(int|string $project_id, array $params = []): mixed + { + $resolver = new OptionsResolver(); + $booleanNormalizer = function (Options $resolver, $value): string { + return $value ? 'true' : 'false'; + }; + + $resolver->setDefined('url') + ->setAllowedTypes('url', 'string') + ->setRequired('url') + ; + $resolver->setDefined('api_url') + ->setAllowedTypes('api_url', 'string') + ; + $resolver->setDefined('username') + ->setAllowedTypes('username', 'string') + ; + $resolver->setDefined('password') + ->setAllowedTypes('password', 'string') + ->setRequired('password') + ; + $resolver->setDefined('active') + ->setAllowedTypes('active', 'bool') + ->setNormalizer('active', $booleanNormalizer) + ; + $resolver->setDefined('jira_auth_type') + ->setAllowedTypes('jira_auth_type', 'int') + ; + $resolver->setDefined('jira_issue_prefix') + ->setAllowedTypes('jira_issue_prefix', 'string') + ; + $resolver->setDefined('jira_issue_regex') + ->setAllowedTypes('jira_issue_regex', 'string') + ; + $resolver->setDefined('jira_issue_transition_automatic') + ->setAllowedTypes('jira_issue_transition_automatic', 'bool') + ->setNormalizer('jira_issue_transition_automatic', $booleanNormalizer) + ; + $resolver->setDefined('jira_issue_transition_id') + ->setAllowedTypes('jira_issue_transition_id', 'string') + ; + $resolver->setDefined('commit_events') + ->setAllowedTypes('commit_events', 'bool') + ->setNormalizer('commit_events', $booleanNormalizer) + ; + $resolver->setDefined('merge_requests_events') + ->setAllowedTypes('merge_requests_events', 'bool') + ->setNormalizer('merge_requests_events', $booleanNormalizer) + ; + $resolver->setDefined('comment_on_event_enabled') + ->setAllowedTypes('comment_on_event_enabled', 'bool') + ->setNormalizer('comment_on_event_enabled', $booleanNormalizer) + ; + + return $this->put($this->getProjectPath($project_id, 'integrations/jira'), $resolver->resolve($params)); + } + + /** + * Update Jira integration + * Set Jira integration for a project + * + * @param int|string $project_id + * @param array $params { + * @var string $url The URL to the Jira project which is being linked to this GitLab project + * @var bool $api_url The base URL to the Jira instance API. Web URL value is used if not set + * @var string $username The email or username to be used with Jira. For Jira Cloud use an email, + * for Jira Data Center and Jira Server use a username. Required when using + * Basic authentication (jira_auth_type is 0) + * @var string $password The Jira API token, password, or personal access token to be used with + * Jira. When your authentication method is Basic (jira_auth_type is 0) use + * an API token for Jira Cloud, or a password for Jira Data Center or Jira + * Server. When your authentication method is Jira personal access token + * (jira_auth_type is 1) use a personal access token. + * @var string $active Activates or deactivates the integration. Defaults to false (deactivated). + * @var string $jira_auth_type The authentication method to be used with Jira. 0 means Basic + * Authentication. 1 means Jira personal access token. Defaults to 0. + * @var string $jira_issue_prefix Prefix to match Jira issue keys. + * @var string $jira_issue_regex Regular expression to match Jira issue keys. + * @var string $jira_issue_transition_automatic Enable automatic issue transitions. Takes precedence over + * jira_issue_transition_id if enabled. Defaults to false + * @var string $jira_issue_transition_id The ID of one or more transitions for custom issue + * transitions. Ignored if jira_issue_transition_automatic is + * enabled. Defaults to a blank string, which disables custom + * transitions. + * @var string $commit_events Enable notifications for commit events + * @var string $merge_requests_events Enable notifications for merge request events + * @var string $comment_on_event_enabled Enable comments inside Jira issues on each GitLab event + * (commit / merge request) + * } + * + * @return mixed + */ + public function updateJira(int|string $project_id, array $params = []): mixed + { + return $this->createJira($project_id, $params); + } + + /** + * Get Jira integration settings for a project + * + * @param int|string $project_id + * @return mixed + */ + public function getJira(int|string $project_id): mixed + { + return $this->get($this->getProjectPath($project_id, 'integrations/jira')); + } + + /** + * Disable the Jira integration for a project. Integration settings are reset + * + * @param int|string $project_id + * @return mixed + */ + public function removeJira(int|string $project_id): mixed + { + return $this->delete($this->getProjectPath($project_id, 'integrations/jira')); + } + +} diff --git a/src/Api/Projects.php b/src/Api/Projects.php index 3f5d1962..50374ed4 100644 --- a/src/Api/Projects.php +++ b/src/Api/Projects.php @@ -1832,4 +1832,16 @@ public function search($id, array $parameters = []) return $this->get('projects/'.self::encodePath($id).'/search', $resolver->resolve($parameters)); } + + + /** + * @param int|string $project_id + * + * @return mixed + */ + public function integrations($project_id) + { + return $this->get($this->getProjectPath($project_id, 'integrations')); + } + } diff --git a/src/Client.php b/src/Client.php index 4082f1ac..3b07cb66 100644 --- a/src/Client.php +++ b/src/Client.php @@ -22,6 +22,7 @@ use Gitlab\Api\GroupsBoards; use Gitlab\Api\GroupsEpics; use Gitlab\Api\GroupsMilestones; +use Gitlab\Api\Integrations; use Gitlab\Api\IssueBoards; use Gitlab\Api\IssueLinks; use Gitlab\Api\Issues; @@ -216,6 +217,14 @@ public function groupsMilestones(): GroupsMilestones return new GroupsMilestones($this); } + /** + * @return Integrations + */ + public function integrations(): Integrations + { + return new Integrations($this); + } + /** * @return IssueBoards */ diff --git a/tests/Api/IntegrationsTest.php b/tests/Api/IntegrationsTest.php new file mode 100644 index 00000000..b6e4314b --- /dev/null +++ b/tests/Api/IntegrationsTest.php @@ -0,0 +1,195 @@ + + * (c) Graham Campbell + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Gitlab\Tests\Api; + +use DateTime; +use Gitlab\Api\Integrations; + +class IntegrationsTest extends TestCase +{ + /** + * @test + */ + public function shouldGetAllIntegrations(): void + { + $expectedArray = $this->getMultipleIntegrationsData(); + $api = $this->getMultipleProjectsRequestMock('integrations', $expectedArray); + + $this->assertEquals($expectedArray, $api->all()); + } + + public function shouldCreateMicrosoftTeams(): void + { + $expectedArray = [ + 'title' => 'Microsoft Teams notifications', + 'slug' => 'microsoft-teams' + ]; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('put') + ->with('projects/1/integrations') + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->createMicrosoftTeams(1, [ + 'webroot' => 'http://test.org/', + ])); + + } + + public function shouldUpdateMicrosoftTeams(): void + { + $expectedArray = [ + 'title' => 'Microsoft Teams notifications', + 'slug' => 'microsoft-teams' + ]; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('put') + ->with('projects/1/integrations') + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->updateMicrosoftTeams(1, [ + 'webroot' => 'http://test.org/', + ])); + } + + public function shouldGetMicrosoftTeams(): void + { + $expectedArray = [ + 'title' => 'Microsoft Teams notifications', + 'slug' => 'microsoft-teams' + ]; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('projects/1/integrations') + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->getMicrosoftTeams(1)); + } + + public function shouldRemoveMicrosoftTeams(): void + { + $expectedBool = true; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('delete') + ->with('projects/1/integrations') + ->will($this->returnValue($expectedBool)); + + $this->assertEquals($expectedBool, $api->removeMicrosoftTeams(1)); + } + + + + public function shouldCreateJira(): void + { + $expectedArray = [ + 'title' => 'Jira', + 'slug' => 'jira' + ]; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('put') + ->with('projects/1/integrations') + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->createJira(1, [ + 'url' => 'http://test.org/', + 'password' => '123' + ])); + + } + + public function shouldUpdateJira(): void + { + $expectedArray = [ + 'title' => 'Jira', + 'slug' => 'jira' + ]; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('put') + ->with('projects/1/integrations') + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->updateJira(1, [ + 'url' => 'http://test.org/', + 'password' => '123' + ])); + } + + public function shouldGetJira(): void + { + $expectedArray = [ + 'title' => 'Jira', + 'slug' => 'jira' + ]; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('projects/1/integrations') + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->getJira(1)); + } + + public function shouldRemoveJira(): void + { + $expectedBool = true; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('delete') + ->with('projects/1/integrations') + ->will($this->returnValue($expectedBool)); + + $this->assertEquals($expectedBool, $api->removeJira(1)); + } + + + + protected function getMultipleIntegrationsData() + { + return [ + ['id' => 1, 'title' => 'Microsoft Teams notifications', 'slug' => 'microsoft-teams'], + ['id' => 2, 'title' => 'Jira', 'slug' => 'jira'] + ]; + } + + protected function getMultipleProjectsRequestMock($path, $expectedArray = [], $expectedParameters = []) + { + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with($path, $expectedParameters) + ->will($this->returnValue($expectedArray)); + + return $api; + } + + protected function getApiClass() + { + return Projects::class; + } + +} diff --git a/tests/Api/ProjectsTest.php b/tests/Api/ProjectsTest.php index c3296d62..20c3894c 100644 --- a/tests/Api/ProjectsTest.php +++ b/tests/Api/ProjectsTest.php @@ -3013,6 +3013,19 @@ public function shouldRemoveProtectedTag(): void $this->assertEquals($expectedBool, $api->deleteProtectedTag(1, 'release-*')); } + public function shouldGetIntegrations(): void + { + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('put') + ->with( + 'projects/1/integrations' + ) + ->will($this->returnValue([])); + + $this->assertEquals([], $api->integrations(1)); + } + protected function getApiClass() { return Projects::class; From e746317a296401d2b1ffcfc26b24a8b27f9d5a7c Mon Sep 17 00:00:00 2001 From: Philipp Kolmann Date: Tue, 19 Mar 2024 09:49:12 +0100 Subject: [PATCH 02/10] Fix Api\Projects missing --- src/Api/Integrations.php | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/Api/Integrations.php b/src/Api/Integrations.php index 5a1eca9c..a0488177 100644 --- a/src/Api/Integrations.php +++ b/src/Api/Integrations.php @@ -21,6 +21,7 @@ class Integrations extends AbstractApi { /** * @param int|string $project_id + * * @return mixed */ public function all(int|string $project_id): mixed @@ -33,10 +34,11 @@ public function all(int|string $project_id): mixed /** * Create Microsoft Teams integration - * Set Microsoft Teams integration for a project + * Set Microsoft Teams integration for a project. * * @param int|string $project_id * @param array $params { + * * @var string $webhook The Microsoft Teams webhook. * @var bool $notify_only_broken_pipelines Send notifications for broken pipelines * @var string $branches_to_be_notified Branches to send notifications for. Valid options are all, default, @@ -115,10 +117,11 @@ public function createMicrosoftTeams(int|string $project_id, array $params = []) /** * Update Microsoft Teams integration - * Set Microsoft Teams integration for a project + * Set Microsoft Teams integration for a project. * * @param int|string $project_id * @param array $params { + * * @var string $webhook The Microsoft Teams webhook. * @var bool $notify_only_broken_pipelines Send notifications for broken pipelines * @var string $branches_to_be_notified Branches to send notifications for. Valid options are all, default, @@ -142,9 +145,10 @@ public function updateMicrosoftTeams(int|string $project_id, array $params = []) } /** - * Get Microsoft Teams integration settings for a project + * Get Microsoft Teams integration settings for a project. * * @param int|string $project_id + * * @return mixed */ public function getMicrosoftTeams(int|string $project_id): mixed @@ -153,9 +157,10 @@ public function getMicrosoftTeams(int|string $project_id): mixed } /** - * Disable the Microsoft Teams integration for a project. Integration settings are reset + * Disable the Microsoft Teams integration for a project. Integration settings are reset. * * @param int|string $project_id + * * @return mixed */ public function removeMicrosoftTeams(int|string $project_id): mixed @@ -168,10 +173,11 @@ public function removeMicrosoftTeams(int|string $project_id): mixed /** * Create Jira integration - * Set Jira integration for a project + * Set Jira integration for a project. * * @param int|string $project_id * @param array $params { + * * @var string $url The URL to the Jira project which is being linked to this GitLab project * @var bool $api_url The base URL to the Jira instance API. Web URL value is used if not set * @var string $username The email or username to be used with Jira. For Jira Cloud use an email, @@ -185,8 +191,8 @@ public function removeMicrosoftTeams(int|string $project_id): mixed * @var string $active Activates or deactivates the integration. Defaults to false (deactivated). * @var string $jira_auth_type The authentication method to be used with Jira. 0 means Basic * Authentication. 1 means Jira personal access token. Defaults to 0. - * @var string $jira_issue_prefix Prefix to match Jira issue keys. - * @var string $jira_issue_regex Regular expression to match Jira issue keys. + * @var string $jira_issue_prefix Prefix to match Jira issue keys + * @var string $jira_issue_regex Regular expression to match Jira issue keys * @var string $jira_issue_transition_automatic Enable automatic issue transitions. Takes precedence over * jira_issue_transition_id if enabled. Defaults to false * @var string $jira_issue_transition_id The ID of one or more transitions for custom issue @@ -277,8 +283,8 @@ public function createJira(int|string $project_id, array $params = []): mixed * @var string $active Activates or deactivates the integration. Defaults to false (deactivated). * @var string $jira_auth_type The authentication method to be used with Jira. 0 means Basic * Authentication. 1 means Jira personal access token. Defaults to 0. - * @var string $jira_issue_prefix Prefix to match Jira issue keys. - * @var string $jira_issue_regex Regular expression to match Jira issue keys. + * @var string $jira_issue_prefix Prefix to match Jira issue keys + * @var string $jira_issue_regex Regular expression to match Jira issue keys * @var string $jira_issue_transition_automatic Enable automatic issue transitions. Takes precedence over * jira_issue_transition_id if enabled. Defaults to false * @var string $jira_issue_transition_id The ID of one or more transitions for custom issue @@ -299,9 +305,10 @@ public function updateJira(int|string $project_id, array $params = []): mixed } /** - * Get Jira integration settings for a project + * Get Jira integration settings for a project. * * @param int|string $project_id + * * @return mixed */ public function getJira(int|string $project_id): mixed @@ -310,9 +317,10 @@ public function getJira(int|string $project_id): mixed } /** - * Disable the Jira integration for a project. Integration settings are reset + * Disable the Jira integration for a project. Integration settings are reset. * * @param int|string $project_id + * * @return mixed */ public function removeJira(int|string $project_id): mixed From c75fef12f23920cd65098a7efc79df770ea65983 Mon Sep 17 00:00:00 2001 From: Philipp Kolmann Date: Tue, 19 Mar 2024 09:50:50 +0100 Subject: [PATCH 03/10] Fix Api\Projects missing --- tests/Api/IntegrationsTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Api/IntegrationsTest.php b/tests/Api/IntegrationsTest.php index b6e4314b..8d158034 100644 --- a/tests/Api/IntegrationsTest.php +++ b/tests/Api/IntegrationsTest.php @@ -16,6 +16,7 @@ use DateTime; use Gitlab\Api\Integrations; +use Gitlab\Api\Projects; class IntegrationsTest extends TestCase { From 81eab0c4d831e248ca142d118c3ba9b1aa4836a3 Mon Sep 17 00:00:00 2001 From: Philipp Kolmann Date: Wed, 14 Jun 2023 14:30:53 +0200 Subject: [PATCH 04/10] Improve IntegrationsTest.php --- tests/Api/IntegrationsTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Api/IntegrationsTest.php b/tests/Api/IntegrationsTest.php index 8d158034..775ca370 100644 --- a/tests/Api/IntegrationsTest.php +++ b/tests/Api/IntegrationsTest.php @@ -26,7 +26,7 @@ class IntegrationsTest extends TestCase public function shouldGetAllIntegrations(): void { $expectedArray = $this->getMultipleIntegrationsData(); - $api = $this->getMultipleProjectsRequestMock('integrations', $expectedArray); + $api = $this->getMultipleIntegrationsRequestMock('integrations', $expectedArray); $this->assertEquals($expectedArray, $api->all()); } @@ -177,7 +177,7 @@ protected function getMultipleIntegrationsData() ]; } - protected function getMultipleProjectsRequestMock($path, $expectedArray = [], $expectedParameters = []) + protected function getMultipleIntegrationsRequestMock($path, $expectedArray = [], $expectedParameters = []) { $api = $this->getApiMock(); $api->expects($this->once()) @@ -190,7 +190,7 @@ protected function getMultipleProjectsRequestMock($path, $expectedArray = [], $e protected function getApiClass() { - return Projects::class; + return Integrations::class; } } From 932281a1db0d7bbac5e9efb33755704b30cebbb0 Mon Sep 17 00:00:00 2001 From: Philipp Kolmann Date: Wed, 14 Jun 2023 14:33:56 +0200 Subject: [PATCH 05/10] Improve IntegrationsTest.php (2) --- tests/Api/IntegrationsTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Api/IntegrationsTest.php b/tests/Api/IntegrationsTest.php index 775ca370..b16d31ef 100644 --- a/tests/Api/IntegrationsTest.php +++ b/tests/Api/IntegrationsTest.php @@ -26,9 +26,9 @@ class IntegrationsTest extends TestCase public function shouldGetAllIntegrations(): void { $expectedArray = $this->getMultipleIntegrationsData(); - $api = $this->getMultipleIntegrationsRequestMock('integrations', $expectedArray); + $api = $this->getMultipleIntegrationsRequestMock('projects/1/integrations', $expectedArray); - $this->assertEquals($expectedArray, $api->all()); + $this->assertEquals($expectedArray, $api->all(1)); } public function shouldCreateMicrosoftTeams(): void From a7edd92552b1e54876db0d6632fbb260d7e160d9 Mon Sep 17 00:00:00 2001 From: Philipp Kolmann Date: Wed, 14 Jun 2023 14:39:50 +0200 Subject: [PATCH 06/10] fix style --- src/Api/Integrations.php | 3 ++- src/Api/Projects.php | 1 - tests/Api/IntegrationsTest.php | 18 ++++++------------ 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/src/Api/Integrations.php b/src/Api/Integrations.php index a0488177..bbae15c5 100644 --- a/src/Api/Integrations.php +++ b/src/Api/Integrations.php @@ -266,10 +266,11 @@ public function createJira(int|string $project_id, array $params = []): mixed /** * Update Jira integration - * Set Jira integration for a project + * Set Jira integration for a project. * * @param int|string $project_id * @param array $params { + * * @var string $url The URL to the Jira project which is being linked to this GitLab project * @var bool $api_url The base URL to the Jira instance API. Web URL value is used if not set * @var string $username The email or username to be used with Jira. For Jira Cloud use an email, diff --git a/src/Api/Projects.php b/src/Api/Projects.php index 50374ed4..71574eed 100644 --- a/src/Api/Projects.php +++ b/src/Api/Projects.php @@ -1843,5 +1843,4 @@ public function integrations($project_id) { return $this->get($this->getProjectPath($project_id, 'integrations')); } - } diff --git a/tests/Api/IntegrationsTest.php b/tests/Api/IntegrationsTest.php index b16d31ef..18f816fb 100644 --- a/tests/Api/IntegrationsTest.php +++ b/tests/Api/IntegrationsTest.php @@ -14,7 +14,6 @@ namespace Gitlab\Tests\Api; -use DateTime; use Gitlab\Api\Integrations; use Gitlab\Api\Projects; @@ -35,7 +34,7 @@ public function shouldCreateMicrosoftTeams(): void { $expectedArray = [ 'title' => 'Microsoft Teams notifications', - 'slug' => 'microsoft-teams' + 'slug' => 'microsoft-teams', ]; $api = $this->getApiMock(); @@ -47,14 +46,13 @@ public function shouldCreateMicrosoftTeams(): void $this->assertEquals($expectedArray, $api->createMicrosoftTeams(1, [ 'webroot' => 'http://test.org/', ])); - } public function shouldUpdateMicrosoftTeams(): void { $expectedArray = [ 'title' => 'Microsoft Teams notifications', - 'slug' => 'microsoft-teams' + 'slug' => 'microsoft-teams', ]; $api = $this->getApiMock(); @@ -97,13 +95,11 @@ public function shouldRemoveMicrosoftTeams(): void $this->assertEquals($expectedBool, $api->removeMicrosoftTeams(1)); } - - public function shouldCreateJira(): void { $expectedArray = [ 'title' => 'Jira', - 'slug' => 'jira' + 'slug' => 'jira', ]; $api = $this->getApiMock(); @@ -114,7 +110,7 @@ public function shouldCreateJira(): void $this->assertEquals($expectedArray, $api->createJira(1, [ 'url' => 'http://test.org/', - 'password' => '123' + 'password' => '123', ])); } @@ -123,7 +119,7 @@ public function shouldUpdateJira(): void { $expectedArray = [ 'title' => 'Jira', - 'slug' => 'jira' + 'slug' => 'jira', ]; $api = $this->getApiMock(); @@ -167,13 +163,11 @@ public function shouldRemoveJira(): void $this->assertEquals($expectedBool, $api->removeJira(1)); } - - protected function getMultipleIntegrationsData() { return [ ['id' => 1, 'title' => 'Microsoft Teams notifications', 'slug' => 'microsoft-teams'], - ['id' => 2, 'title' => 'Jira', 'slug' => 'jira'] + ['id' => 2, 'title' => 'Jira', 'slug' => 'jira'], ]; } From fd1333e9fd00070be1100e1aea9bdc072cb782ed Mon Sep 17 00:00:00 2001 From: Philipp Kolmann Date: Wed, 14 Jun 2023 14:49:07 +0200 Subject: [PATCH 07/10] fix PHP 7.4 compatibility --- src/Api/Integrations.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Api/Integrations.php b/src/Api/Integrations.php index bbae15c5..350eccf0 100644 --- a/src/Api/Integrations.php +++ b/src/Api/Integrations.php @@ -24,7 +24,7 @@ class Integrations extends AbstractApi * * @return mixed */ - public function all(int|string $project_id): mixed + public function all($project_id) { $path = $this->getProjectPath($project_id, 'integrations'); return $this->get($path); @@ -56,7 +56,7 @@ public function all(int|string $project_id): mixed * * @return mixed */ - public function createMicrosoftTeams(int|string $project_id, array $params = []): mixed + public function createMicrosoftTeams($project_id, array $params = []) { $resolver = new OptionsResolver(); $booleanNormalizer = function (Options $resolver, $value): string { @@ -139,7 +139,7 @@ public function createMicrosoftTeams(int|string $project_id, array $params = []) * * @return mixed */ - public function updateMicrosoftTeams(int|string $project_id, array $params = []): mixed + public function updateMicrosoftTeams($project_id, array $params = []) { return $this->createMicrosoftTeams($project_id, $params); } @@ -151,7 +151,7 @@ public function updateMicrosoftTeams(int|string $project_id, array $params = []) * * @return mixed */ - public function getMicrosoftTeams(int|string $project_id): mixed + public function getMicrosoftTeams($project_id) { return $this->get($this->getProjectPath($project_id, 'integrations/microsoft-teams')); } @@ -163,7 +163,7 @@ public function getMicrosoftTeams(int|string $project_id): mixed * * @return mixed */ - public function removeMicrosoftTeams(int|string $project_id): mixed + public function removeMicrosoftTeams($project_id) { return $this->delete($this->getProjectPath($project_id, 'integrations/microsoft-teams')); } @@ -207,7 +207,7 @@ public function removeMicrosoftTeams(int|string $project_id): mixed * * @return mixed */ - public function createJira(int|string $project_id, array $params = []): mixed + public function createJira($project_id, array $params = []) { $resolver = new OptionsResolver(); $booleanNormalizer = function (Options $resolver, $value): string { @@ -300,7 +300,7 @@ public function createJira(int|string $project_id, array $params = []): mixed * * @return mixed */ - public function updateJira(int|string $project_id, array $params = []): mixed + public function updateJira($project_id, array $params = []) { return $this->createJira($project_id, $params); } @@ -312,7 +312,7 @@ public function updateJira(int|string $project_id, array $params = []): mixed * * @return mixed */ - public function getJira(int|string $project_id): mixed + public function getJira($project_id) { return $this->get($this->getProjectPath($project_id, 'integrations/jira')); } @@ -324,7 +324,7 @@ public function getJira(int|string $project_id): mixed * * @return mixed */ - public function removeJira(int|string $project_id): mixed + public function removeJira($project_id) { return $this->delete($this->getProjectPath($project_id, 'integrations/jira')); } From c3a84521632e73866fcd8b16ecae40c3ed8e7f4c Mon Sep 17 00:00:00 2001 From: Philipp Kolmann Date: Mon, 19 Jun 2023 10:40:16 +0200 Subject: [PATCH 08/10] Fix StyleCi --- src/Api/Integrations.php | 4 ++-- tests/Api/IntegrationsTest.php | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Api/Integrations.php b/src/Api/Integrations.php index 350eccf0..a1851e7c 100644 --- a/src/Api/Integrations.php +++ b/src/Api/Integrations.php @@ -39,7 +39,7 @@ public function all($project_id) * @param int|string $project_id * @param array $params { * - * @var string $webhook The Microsoft Teams webhook. + * @var string $webhook The Microsoft Teams webhook * @var bool $notify_only_broken_pipelines Send notifications for broken pipelines * @var string $branches_to_be_notified Branches to send notifications for. Valid options are all, default, * protected, and default_and_protected. The default value is "default" @@ -122,7 +122,7 @@ public function createMicrosoftTeams($project_id, array $params = []) * @param int|string $project_id * @param array $params { * - * @var string $webhook The Microsoft Teams webhook. + * @var string $webhook The Microsoft Teams webhook * @var bool $notify_only_broken_pipelines Send notifications for broken pipelines * @var string $branches_to_be_notified Branches to send notifications for. Valid options are all, default, * protected, and default_and_protected. The default value is "default" diff --git a/tests/Api/IntegrationsTest.php b/tests/Api/IntegrationsTest.php index 18f816fb..6999b982 100644 --- a/tests/Api/IntegrationsTest.php +++ b/tests/Api/IntegrationsTest.php @@ -70,7 +70,7 @@ public function shouldGetMicrosoftTeams(): void { $expectedArray = [ 'title' => 'Microsoft Teams notifications', - 'slug' => 'microsoft-teams' + 'slug' => 'microsoft-teams', ]; $api = $this->getApiMock(); @@ -130,7 +130,7 @@ public function shouldUpdateJira(): void $this->assertEquals($expectedArray, $api->updateJira(1, [ 'url' => 'http://test.org/', - 'password' => '123' + 'password' => '123', ])); } @@ -138,7 +138,7 @@ public function shouldGetJira(): void { $expectedArray = [ 'title' => 'Jira', - 'slug' => 'jira' + 'slug' => 'jira', ]; $api = $this->getApiMock(); From 4c85f19601e9c263104ebe07fbef1834f16d000b Mon Sep 17 00:00:00 2001 From: Philipp Kolmann Date: Mon, 19 Jun 2023 10:42:02 +0200 Subject: [PATCH 09/10] Fix StyleCi (2) --- src/Api/Integrations.php | 1 - tests/Api/IntegrationsTest.php | 2 -- 2 files changed, 3 deletions(-) diff --git a/src/Api/Integrations.php b/src/Api/Integrations.php index a1851e7c..118aefc6 100644 --- a/src/Api/Integrations.php +++ b/src/Api/Integrations.php @@ -328,5 +328,4 @@ public function removeJira($project_id) { return $this->delete($this->getProjectPath($project_id, 'integrations/jira')); } - } diff --git a/tests/Api/IntegrationsTest.php b/tests/Api/IntegrationsTest.php index 6999b982..f6a54160 100644 --- a/tests/Api/IntegrationsTest.php +++ b/tests/Api/IntegrationsTest.php @@ -112,7 +112,6 @@ public function shouldCreateJira(): void 'url' => 'http://test.org/', 'password' => '123', ])); - } public function shouldUpdateJira(): void @@ -186,5 +185,4 @@ protected function getApiClass() { return Integrations::class; } - } From 9241664dd933c11c50c18c08601d99d718b65997 Mon Sep 17 00:00:00 2001 From: Philipp Kolmann Date: Tue, 19 Mar 2024 09:58:58 +0100 Subject: [PATCH 10/10] Fix StyleCI errors --- src/Api/Integrations.php | 2 +- src/Api/Projects.php | 1 - tests/Api/IntegrationsTest.php | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Api/Integrations.php b/src/Api/Integrations.php index 118aefc6..2d5d1052 100644 --- a/src/Api/Integrations.php +++ b/src/Api/Integrations.php @@ -27,6 +27,7 @@ class Integrations extends AbstractApi public function all($project_id) { $path = $this->getProjectPath($project_id, 'integrations'); + return $this->get($path); } @@ -168,7 +169,6 @@ public function removeMicrosoftTeams($project_id) return $this->delete($this->getProjectPath($project_id, 'integrations/microsoft-teams')); } - // JIRA /** diff --git a/src/Api/Projects.php b/src/Api/Projects.php index 71574eed..a8c8d29c 100644 --- a/src/Api/Projects.php +++ b/src/Api/Projects.php @@ -1833,7 +1833,6 @@ public function search($id, array $parameters = []) return $this->get('projects/'.self::encodePath($id).'/search', $resolver->resolve($parameters)); } - /** * @param int|string $project_id * diff --git a/tests/Api/IntegrationsTest.php b/tests/Api/IntegrationsTest.php index f6a54160..f62186e7 100644 --- a/tests/Api/IntegrationsTest.php +++ b/tests/Api/IntegrationsTest.php @@ -15,7 +15,6 @@ namespace Gitlab\Tests\Api; use Gitlab\Api\Integrations; -use Gitlab\Api\Projects; class IntegrationsTest extends TestCase {