From b8476a6edbbcea718ecf9b275364beade347287e Mon Sep 17 00:00:00 2001 From: Philipp Kolmann Date: Thu, 27 Feb 2025 15:35:16 +0100 Subject: [PATCH 1/3] Add MS Teams and Jira Integrations APIs --- src/Api/Integrations.php | 331 +++++++++++++++++++++++++++++++++ src/Api/Projects.php | 10 + src/Client.php | 6 + tests/Api/IntegrationsTest.php | 188 +++++++++++++++++++ tests/Api/ProjectsTest.php | 15 ++ 5 files changed, 550 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..4bd85789 --- /dev/null +++ b/src/Api/Integrations.php @@ -0,0 +1,331 @@ + + * (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 { + * + * @return mixed + *@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 + * } + * + */ + 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 { + * + * @return mixed + *@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 + * } + * + */ + 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 { + * + * @return mixed + *@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) + * } + * + */ + 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 { + * + * @return mixed + *@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) + * } + * + */ + 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 32aa168a..5deb3c18 100644 --- a/src/Api/Projects.php +++ b/src/Api/Projects.php @@ -1280,4 +1280,14 @@ public function search(int|string $id, array $parameters = []): mixed return $this->get('projects/'.self::encodePath($id).'/search', $resolver->resolve($parameters)); } + + /** + * @param int|string $project_id + * + * @return mixed + */ + public function integrations(int|string $project_id): mixed + { + return $this->get($this->getProjectPath($project_id, 'integrations')); + } } diff --git a/src/Client.php b/src/Client.php index ee4ff8a7..256d5594 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; @@ -295,6 +296,11 @@ public function wiki(): Wiki return new Wiki($this); } + public function integrations(): Integrations + { + return new Integrations($this); + } + /** * Authenticate a user for all next requests. * diff --git a/tests/Api/IntegrationsTest.php b/tests/Api/IntegrationsTest.php new file mode 100644 index 00000000..91651ccf --- /dev/null +++ b/tests/Api/IntegrationsTest.php @@ -0,0 +1,188 @@ + + * (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 Gitlab\Api\Integrations; +use PHPUnit\Framework\MockObject\MockObject; + +class IntegrationsTest extends TestCase +{ + /** + * @test + */ + public function shouldGetAllIntegrations(): void + { + $expectedArray = $this->getMultipleIntegrationsData(); + $api = $this->getMultipleIntegrationsRequestMock('projects/1/integrations', $expectedArray); + + $this->assertEquals($expectedArray, $api->all(1)); + } + + 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' => 'https://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' => 'https://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(): array + { + return [ + ['id' => 1, 'title' => 'Microsoft Teams notifications', 'slug' => 'microsoft-teams'], + ['id' => 2, 'title' => 'Jira', 'slug' => 'jira'], + ]; + } + + protected function getMultipleIntegrationsRequestMock($path, $expectedArray = [], $expectedParameters = []): MockObject + { + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with($path, $expectedParameters) + ->will($this->returnValue($expectedArray)); + + return $api; + } + + protected function getApiClass(): string + { + return Integrations::class; + } +} diff --git a/tests/Api/ProjectsTest.php b/tests/Api/ProjectsTest.php index 1ef556ea..df55b24e 100644 --- a/tests/Api/ProjectsTest.php +++ b/tests/Api/ProjectsTest.php @@ -2765,4 +2765,19 @@ public function shouldSearchGroups(): void 'sort' => 'desc', ])); } + + # [Test] + public function shouldGetIntegrations(): void + { + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('put') + ->with( + 'projects/1/integrations' + ) + ->willReturn([]); + + $this->assertEquals([], $api->integrations(1)); + } + } From 3d60d651e6b3e399df49bc062b50f1bc86e1d70d Mon Sep 17 00:00:00 2001 From: Philipp Kolmann Date: Thu, 27 Feb 2025 15:41:44 +0100 Subject: [PATCH 2/3] Import StyleCI Updates --- src/Api/Integrations.php | 33 --------------------------------- src/Api/Projects.php | 5 ----- tests/Api/ProjectsTest.php | 3 +-- 3 files changed, 1 insertion(+), 40 deletions(-) diff --git a/src/Api/Integrations.php b/src/Api/Integrations.php index 4bd85789..2b2d3b5d 100644 --- a/src/Api/Integrations.php +++ b/src/Api/Integrations.php @@ -19,11 +19,6 @@ 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'); @@ -37,10 +32,8 @@ public function all(int|string $project_id): mixed * Create Microsoft Teams integration * Set Microsoft Teams integration for a project. * - * @param int|string $project_id * @param array $params { * - * @return mixed *@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, @@ -55,7 +48,6 @@ public function all(int|string $project_id): mixed * @var bool $pipeline_events Enable notifications for pipeline events * @var bool $wiki_page_events Enable notifications for wiki page events * } - * */ public function createMicrosoftTeams(int|string $project_id, array $params = []): mixed { @@ -120,10 +112,8 @@ public function createMicrosoftTeams(int|string $project_id, array $params = []) * Update Microsoft Teams integration * Set Microsoft Teams integration for a project. * - * @param int|string $project_id * @param array $params { * - * @return mixed *@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, @@ -138,7 +128,6 @@ public function createMicrosoftTeams(int|string $project_id, array $params = []) * @var bool $pipeline_events Enable notifications for pipeline events * @var bool $wiki_page_events Enable notifications for wiki page events * } - * */ public function updateMicrosoftTeams(int|string $project_id, array $params = []): mixed { @@ -147,10 +136,6 @@ public function updateMicrosoftTeams(int|string $project_id, array $params = []) /** * Get Microsoft Teams integration settings for a project. - * - * @param int|string $project_id - * - * @return mixed */ public function getMicrosoftTeams(int|string $project_id): mixed { @@ -159,10 +144,6 @@ public function getMicrosoftTeams(int|string $project_id): mixed /** * 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 { @@ -175,10 +156,8 @@ public function removeMicrosoftTeams(int|string $project_id): mixed * Create Jira integration * Set Jira integration for a project. * - * @param int|string $project_id * @param array $params { * - * @return mixed *@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, @@ -205,7 +184,6 @@ public function removeMicrosoftTeams(int|string $project_id): mixed * @var string $comment_on_event_enabled Enable comments inside Jira issues on each GitLab event * (commit / merge request) * } - * */ public function createJira(int|string $project_id, array $params = []): mixed { @@ -268,10 +246,8 @@ public function createJira(int|string $project_id, array $params = []): mixed * Update Jira integration * Set Jira integration for a project. * - * @param int|string $project_id * @param array $params { * - * @return mixed *@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, @@ -298,7 +274,6 @@ public function createJira(int|string $project_id, array $params = []): mixed * @var string $comment_on_event_enabled Enable comments inside Jira issues on each GitLab event * (commit / merge request) * } - * */ public function updateJira(int|string $project_id, array $params = []): mixed { @@ -307,10 +282,6 @@ public function updateJira(int|string $project_id, array $params = []): mixed /** * Get Jira integration settings for a project. - * - * @param int|string $project_id - * - * @return mixed */ public function getJira(int|string $project_id): mixed { @@ -319,10 +290,6 @@ public function getJira(int|string $project_id): mixed /** * 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 { diff --git a/src/Api/Projects.php b/src/Api/Projects.php index 5deb3c18..f9e5df34 100644 --- a/src/Api/Projects.php +++ b/src/Api/Projects.php @@ -1281,11 +1281,6 @@ public function search(int|string $id, array $parameters = []): mixed return $this->get('projects/'.self::encodePath($id).'/search', $resolver->resolve($parameters)); } - /** - * @param int|string $project_id - * - * @return mixed - */ public function integrations(int|string $project_id): mixed { return $this->get($this->getProjectPath($project_id, 'integrations')); diff --git a/tests/Api/ProjectsTest.php b/tests/Api/ProjectsTest.php index df55b24e..113c0231 100644 --- a/tests/Api/ProjectsTest.php +++ b/tests/Api/ProjectsTest.php @@ -2766,7 +2766,7 @@ public function shouldSearchGroups(): void ])); } - # [Test] + #[Test] public function shouldGetIntegrations(): void { $api = $this->getApiMock(); @@ -2779,5 +2779,4 @@ public function shouldGetIntegrations(): void $this->assertEquals([], $api->integrations(1)); } - } From 5e466135016e57d6f6a163442fa5f6c189aeb911 Mon Sep 17 00:00:00 2001 From: Philipp Kolmann Date: Thu, 27 Feb 2025 16:30:28 +0100 Subject: [PATCH 3/3] Fix Tests for Integrations API --- tests/Api/IntegrationsTest.php | 63 ++++++++++++++++++++-------------- tests/Api/ProjectsTest.php | 9 +++-- 2 files changed, 43 insertions(+), 29 deletions(-) diff --git a/tests/Api/IntegrationsTest.php b/tests/Api/IntegrationsTest.php index 91651ccf..2a357f77 100644 --- a/tests/Api/IntegrationsTest.php +++ b/tests/Api/IntegrationsTest.php @@ -15,13 +15,12 @@ namespace Gitlab\Tests\Api; use Gitlab\Api\Integrations; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\MockObject\MockObject; class IntegrationsTest extends TestCase { - /** - * @test - */ + #[Test] public function shouldGetAllIntegrations(): void { $expectedArray = $this->getMultipleIntegrationsData(); @@ -30,6 +29,7 @@ public function shouldGetAllIntegrations(): void $this->assertEquals($expectedArray, $api->all(1)); } + #[Test] public function shouldCreateMicrosoftTeams(): void { $expectedArray = [ @@ -40,14 +40,15 @@ public function shouldCreateMicrosoftTeams(): void $api = $this->getApiMock(); $api->expects($this->once()) ->method('put') - ->with('projects/1/integrations') - ->will($this->returnValue($expectedArray)); + ->with('projects/1/integrations/microsoft-teams') + ->willReturn($expectedArray); $this->assertEquals($expectedArray, $api->createMicrosoftTeams(1, [ - 'webroot' => 'https://test.org/', + 'webhook' => 'https://test.org/', ])); } + #[Test] public function shouldUpdateMicrosoftTeams(): void { $expectedArray = [ @@ -58,14 +59,15 @@ public function shouldUpdateMicrosoftTeams(): void $api = $this->getApiMock(); $api->expects($this->once()) ->method('put') - ->with('projects/1/integrations') - ->will($this->returnValue($expectedArray)); + ->with('projects/1/integrations/microsoft-teams') + ->willReturn($expectedArray); $this->assertEquals($expectedArray, $api->updateMicrosoftTeams(1, [ - 'webroot' => 'https://test.org/', + 'webhook' => 'https://test.org/', ])); } + #[Test] public function shouldGetMicrosoftTeams(): void { $expectedArray = [ @@ -76,12 +78,13 @@ public function shouldGetMicrosoftTeams(): void $api = $this->getApiMock(); $api->expects($this->once()) ->method('get') - ->with('projects/1/integrations') - ->will($this->returnValue($expectedArray)); + ->with('projects/1/integrations/microsoft-teams') + ->willReturn($expectedArray); $this->assertEquals($expectedArray, $api->getMicrosoftTeams(1)); } + #[Test] public function shouldRemoveMicrosoftTeams(): void { $expectedBool = true; @@ -89,12 +92,13 @@ public function shouldRemoveMicrosoftTeams(): void $api = $this->getApiMock(); $api->expects($this->once()) ->method('delete') - ->with('projects/1/integrations') - ->will($this->returnValue($expectedBool)); + ->with('projects/1/integrations/microsoft-teams') + ->willReturn($expectedBool); $this->assertEquals($expectedBool, $api->removeMicrosoftTeams(1)); } + #[Test] public function shouldCreateJira(): void { $expectedArray = [ @@ -105,15 +109,16 @@ public function shouldCreateJira(): void $api = $this->getApiMock(); $api->expects($this->once()) ->method('put') - ->with('projects/1/integrations') - ->will($this->returnValue($expectedArray)); + ->with('projects/1/integrations/jira') + ->willReturn($expectedArray); $this->assertEquals($expectedArray, $api->createJira(1, [ - 'url' => 'http://test.org/', + 'url' => 'https://test.org/', 'password' => '123', ])); } + #[Test] public function shouldUpdateJira(): void { $expectedArray = [ @@ -124,15 +129,16 @@ public function shouldUpdateJira(): void $api = $this->getApiMock(); $api->expects($this->once()) ->method('put') - ->with('projects/1/integrations') - ->will($this->returnValue($expectedArray)); + ->with('projects/1/integrations/jira') + ->willReturn($expectedArray); $this->assertEquals($expectedArray, $api->updateJira(1, [ - 'url' => 'http://test.org/', + 'url' => 'https://test.org/', 'password' => '123', ])); } + #[Test] public function shouldGetJira(): void { $expectedArray = [ @@ -143,12 +149,13 @@ public function shouldGetJira(): void $api = $this->getApiMock(); $api->expects($this->once()) ->method('get') - ->with('projects/1/integrations') - ->will($this->returnValue($expectedArray)); + ->with('projects/1/integrations/jira') + ->willReturn($expectedArray); $this->assertEquals($expectedArray, $api->getJira(1)); } + #[Test] public function shouldRemoveJira(): void { $expectedBool = true; @@ -156,13 +163,15 @@ public function shouldRemoveJira(): void $api = $this->getApiMock(); $api->expects($this->once()) ->method('delete') - ->with('projects/1/integrations') - ->will($this->returnValue($expectedBool)); + ->with('projects/1/integrations/jira') + ->willReturn($expectedBool); $this->assertEquals($expectedBool, $api->removeJira(1)); } - protected function getMultipleIntegrationsData(): array + // This method is used to create an array of multiple integrations data. + // NOT A TEST + private function getMultipleIntegrationsData(): array { return [ ['id' => 1, 'title' => 'Microsoft Teams notifications', 'slug' => 'microsoft-teams'], @@ -170,13 +179,15 @@ protected function getMultipleIntegrationsData(): array ]; } - protected function getMultipleIntegrationsRequestMock($path, $expectedArray = [], $expectedParameters = []): MockObject + // This method is used to create a mock for the Integrations class. + // NOT A TEST + private function getMultipleIntegrationsRequestMock($path, $expectedArray = [], $expectedParameters = []): MockObject { $api = $this->getApiMock(); $api->expects($this->once()) ->method('get') ->with($path, $expectedParameters) - ->will($this->returnValue($expectedArray)); + ->willReturn($expectedArray); return $api; } diff --git a/tests/Api/ProjectsTest.php b/tests/Api/ProjectsTest.php index 113c0231..ea962900 100644 --- a/tests/Api/ProjectsTest.php +++ b/tests/Api/ProjectsTest.php @@ -2769,14 +2769,17 @@ public function shouldSearchGroups(): void #[Test] public function shouldGetIntegrations(): void { + $expectedArray = [ + ['id' => 1, 'title' => 'Microsoft Teams notifications', 'slug' => 'microsoft-teams'], + ]; $api = $this->getApiMock(); $api->expects($this->once()) - ->method('put') + ->method('get') ->with( 'projects/1/integrations' ) - ->willReturn([]); + ->willReturn($expectedArray); - $this->assertEquals([], $api->integrations(1)); + $this->assertEquals($expectedArray, $api->integrations(1)); } }