From c034e8c04f637f9aa0491b73d04e8f2edc6e4496 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Tue, 14 Jul 2020 20:17:46 +0530 Subject: [PATCH] gitlab_project: add support for merge_method on projects (#636) Migrated from ansible/ansible#66813 Signed-off-by: Abhijeet Kasurde --- changelogs/fragments/66813_gitlab_project.yml | 2 ++ .../source_control/gitlab/gitlab_project.py | 28 ++++++++++++++----- .../modules/source_control/gitlab/gitlab.py | 12 ++++---- .../gitlab/test_gitlab_project.py | 20 +++++++++++++ 4 files changed, 49 insertions(+), 13 deletions(-) create mode 100644 changelogs/fragments/66813_gitlab_project.yml diff --git a/changelogs/fragments/66813_gitlab_project.yml b/changelogs/fragments/66813_gitlab_project.yml new file mode 100644 index 0000000000..97c51708c2 --- /dev/null +++ b/changelogs/fragments/66813_gitlab_project.yml @@ -0,0 +1,2 @@ +minor_changes: +- gitlab_project - add support for merge_method on projects (https://github.com/ansible/ansible/pull/66813). diff --git a/plugins/modules/source_control/gitlab/gitlab_project.py b/plugins/modules/source_control/gitlab/gitlab_project.py index 147da77abe..98631c74c2 100644 --- a/plugins/modules/source_control/gitlab/gitlab_project.py +++ b/plugins/modules/source_control/gitlab/gitlab_project.py @@ -8,7 +8,7 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type -DOCUMENTATION = ''' +DOCUMENTATION = r''' --- module: gitlab_project short_description: Creates/updates/deletes GitLab Projects @@ -95,9 +95,17 @@ options: default: present type: str choices: ["present", "absent"] + merge_method: + description: + - What requirements are placed upon merges. + - Possible values are C(merge), C(rebase_merge) merge commit with semi-linear history, C(ff) fast-forward merges only. + type: str + choices: ["ff", "merge", "rebase_merge"] + default: merge + version_added: "1.0.0" ''' -EXAMPLES = ''' +EXAMPLES = r''' - name: Delete GitLab Project community.general.gitlab_project: api_url: https://gitlab.example.com/ @@ -116,6 +124,7 @@ EXAMPLES = ''' name: my_first_project group: ansible issues_enabled: False + merge_method: rebase_merge wiki_enabled: True snippets_enabled: True import_url: http://git.example.com/example/lab.git @@ -123,26 +132,26 @@ EXAMPLES = ''' delegate_to: localhost ''' -RETURN = ''' +RETURN = r''' msg: - description: Success or failure message + description: Success or failure message. returned: always type: str sample: "Success" result: - description: json parsed response from the server + description: json parsed response from the server. returned: always type: dict error: - description: the error message returned by the GitLab API + description: the error message returned by the GitLab API. returned: failed type: str sample: "400: path is already in use" project: - description: API object + description: API object. returned: always type: dict ''' @@ -186,6 +195,7 @@ class GitLabProject(object): 'description': options['description'], 'issues_enabled': options['issues_enabled'], 'merge_requests_enabled': options['merge_requests_enabled'], + 'merge_method': options['merge_method'], 'wiki_enabled': options['wiki_enabled'], 'snippets_enabled': options['snippets_enabled'], 'visibility': options['visibility'], @@ -197,6 +207,7 @@ class GitLabProject(object): 'description': options['description'], 'issues_enabled': options['issues_enabled'], 'merge_requests_enabled': options['merge_requests_enabled'], + 'merge_method': options['merge_method'], 'wiki_enabled': options['wiki_enabled'], 'snippets_enabled': options['snippets_enabled'], 'visibility': options['visibility']}) @@ -276,6 +287,7 @@ def main(): description=dict(type='str'), issues_enabled=dict(type='bool', default=True), merge_requests_enabled=dict(type='bool', default=True), + merge_method=dict(type='str', default='merge', choices=["merge", "rebase_merge", "ff"]), wiki_enabled=dict(type='bool', default=True), snippets_enabled=dict(default=True, type='bool'), visibility=dict(type='str', default="private", choices=["internal", "private", "public"], aliases=["visibility_level"]), @@ -304,6 +316,7 @@ def main(): project_description = module.params['description'] issues_enabled = module.params['issues_enabled'] merge_requests_enabled = module.params['merge_requests_enabled'] + merge_method = module.params['merge_method'] wiki_enabled = module.params['wiki_enabled'] snippets_enabled = module.params['snippets_enabled'] visibility = module.params['visibility'] @@ -346,6 +359,7 @@ def main(): "description": project_description, "issues_enabled": issues_enabled, "merge_requests_enabled": merge_requests_enabled, + "merge_method": merge_method, "wiki_enabled": wiki_enabled, "snippets_enabled": snippets_enabled, "visibility": visibility, diff --git a/tests/unit/plugins/modules/source_control/gitlab/gitlab.py b/tests/unit/plugins/modules/source_control/gitlab/gitlab.py index 100e47431a..77b1075cc8 100644 --- a/tests/unit/plugins/modules/source_control/gitlab/gitlab.py +++ b/tests/unit/plugins/modules/source_control/gitlab/gitlab.py @@ -353,7 +353,7 @@ PROJECT API @urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects", method="get") def resp_find_project(url, request): headers = {'content-type': 'application/json'} - content = ('[{"id": 1,"description": null, "default_branch": "master",' + content = ('[{"id": 1,"description": null, "default_branch": "master", "merge_method": "merge",' '"ssh_url_to_repo": "git@example.com:diaspora/diaspora-client.git",' '"http_url_to_repo": "http://example.com/diaspora/diaspora-client.git",' '"web_url": "http://example.com/diaspora/diaspora-client",' @@ -371,7 +371,7 @@ def resp_find_project(url, request): @urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects/1", method="get") def resp_get_project(url, request): headers = {'content-type': 'application/json'} - content = ('{"id": 1,"description": null, "default_branch": "master",' + content = ('{"id": 1,"description": null, "default_branch": "master", "merge_method": "merge",' '"ssh_url_to_repo": "git@example.com:diaspora/diaspora-client.git",' '"http_url_to_repo": "http://example.com/diaspora/diaspora-client.git",' '"web_url": "http://example.com/diaspora/diaspora-client",' @@ -389,7 +389,7 @@ def resp_get_project(url, request): @urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects/foo-bar%2Fdiaspora-client", method="get") def resp_get_project_by_name(url, request): headers = {'content-type': 'application/json'} - content = ('{"id": 1,"description": null, "default_branch": "master",' + content = ('{"id": 1,"description": null, "default_branch": "master", "merge_method": "merge",' '"ssh_url_to_repo": "git@example.com:diaspora/diaspora-client.git",' '"http_url_to_repo": "http://example.com/diaspora/diaspora-client.git",' '"web_url": "http://example.com/diaspora/diaspora-client",' @@ -407,7 +407,7 @@ def resp_get_project_by_name(url, request): @urlmatch(scheme="http", netloc="localhost", path="/api/v4/groups/1/projects", method="get") def resp_find_group_project(url, request): headers = {'content-type': 'application/json'} - content = ('[{"id": 1,"description": null, "default_branch": "master",' + content = ('[{"id": 1,"description": null, "default_branch": "master", "merge_method": "merge",' '"ssh_url_to_repo": "git@example.com:diaspora/diaspora-client.git",' '"http_url_to_repo": "http://example.com/diaspora/diaspora-client.git",' '"web_url": "http://example.com/diaspora/diaspora-client",' @@ -425,7 +425,7 @@ def resp_find_group_project(url, request): @urlmatch(scheme="http", netloc="localhost", path="/api/v4/groups/1/projects/1", method="get") def resp_get_group_project(url, request): headers = {'content-type': 'application/json'} - content = ('{"id": 1,"description": null, "default_branch": "master",' + content = ('{"id": 1,"description": null, "default_branch": "master", "merge_method": "merge",' '"ssh_url_to_repo": "git@example.com:diaspora/diaspora-client.git",' '"http_url_to_repo": "http://example.com/diaspora/diaspora-client.git",' '"web_url": "http://example.com/diaspora/diaspora-client",' @@ -443,7 +443,7 @@ def resp_get_group_project(url, request): @urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects", method="post") def resp_create_project(url, request): headers = {'content-type': 'application/json'} - content = ('{"id": 1,"description": null, "default_branch": "master",' + content = ('{"id": 1,"description": null, "default_branch": "master", "merge_method": "merge",' '"ssh_url_to_repo": "git@example.com:diaspora/diaspora-client.git",' '"http_url_to_repo": "http://example.com/diaspora/diaspora-client.git",' '"web_url": "http://example.com/diaspora/diaspora-client",' diff --git a/tests/unit/plugins/modules/source_control/gitlab/test_gitlab_project.py b/tests/unit/plugins/modules/source_control/gitlab/test_gitlab_project.py index caa0e92465..dc009c60b8 100644 --- a/tests/unit/plugins/modules/source_control/gitlab/test_gitlab_project.py +++ b/tests/unit/plugins/modules/source_control/gitlab/test_gitlab_project.py @@ -91,6 +91,26 @@ class TestGitlabProject(GitlabModuleTestCase): self.assertEqual(changed, False) self.assertEqual(newProject.name, "New Name") + @with_httmock(resp_get_project) + def test_update_project_merge_method(self): + project = self.gitlab_instance.projects.get(1) + + # merge_method should be 'merge' by default + self.assertEqual(project.merge_method, "merge") + + changed, newProject = self.moduleUtil.updateProject(project, {"name": "New Name", "merge_method": "rebase_merge"}) + + self.assertEqual(changed, True) + self.assertEqual(type(newProject), Project) + self.assertEqual(newProject.name, "New Name") + self.assertEqual(newProject.merge_method, "rebase_merge") + + changed, newProject = self.moduleUtil.updateProject(project, {"name": "New Name", "merge_method": "rebase_merge"}) + + self.assertEqual(changed, False) + self.assertEqual(newProject.name, "New Name") + self.assertEqual(newProject.merge_method, "rebase_merge") + @with_httmock(resp_get_group) @with_httmock(resp_get_project_by_name) @with_httmock(resp_delete_project)