Added module for creating protected branches (#2781) (#3024)

* Added module for creating protected branches

* Applied some changes due to comments and added a test that currently fails

* Changing no_access to nobody due to comment on PR

* Changing the description to clarify it a bit more

* Added working tests for module 'gitlab_protected_branch'

* Fixing lint issues

* Added doc that minimum of v2.3.0 is needed to work correctly

* Fixed the requirements notation

* Check the version of the module

* Hopefully fixed the tests by skipping it when lower version of 2.3.0 is installed

* Fix lint issues

* Applying changes due to comments in PR

* Remove commented code

* Removing the trailing dot ...

Co-authored-by: jenkins-x-bot <jenkins-x@googlegroups.com>
Co-authored-by: Werner Dijkerman <iam@werner-dijkerman.nl>
(cherry picked from commit 7734430f23)

Co-authored-by: Werner Dijkerman <werner@dj-wasabi.nl>
This commit is contained in:
patchback[bot] 2021-07-17 10:46:42 +02:00 committed by GitHub
parent 9afb84c8f3
commit 7701ea0293
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 319 additions and 2 deletions

View file

@ -13,7 +13,7 @@ from httmock import urlmatch # noqa
from ansible_collections.community.general.tests.unit.compat import unittest
from gitlab import Gitlab
import gitlab
class FakeAnsibleModule(object):
@ -33,7 +33,7 @@ class GitlabModuleTestCase(unittest.TestCase):
self.mock_module = FakeAnsibleModule()
self.gitlab_instance = Gitlab("http://localhost", private_token="private_token", api_version=4)
self.gitlab_instance = gitlab.Gitlab("http://localhost", private_token="private_token", api_version=4)
# Python 2.7+ is needed for python-gitlab
@ -45,6 +45,14 @@ def python_version_match_requirement():
return sys.version_info >= GITLAB_MINIMUM_PYTHON_VERSION
def python_gitlab_module_version():
return gitlab.__version__
def python_gitlab_version_match_requirement():
return "2.3.0"
# Skip unittest test case if python version don't match requirement
def unitest_python_version_check_requirement(unittest_testcase):
if not python_version_match_requirement():
@ -467,6 +475,32 @@ def resp_delete_project(url, request):
return response(204, content, headers, None, 5, request)
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects/1/protected_branches/master", method="get")
def resp_get_protected_branch(url, request):
headers = {'content-type': 'application/json'}
content = ('{"id": 1, "name": "master", "push_access_levels": [{"access_level": 40, "access_level_description": "Maintainers"}],'
'"merge_access_levels": [{"access_level": 40, "access_level_description": "Maintainers"}],'
'"allow_force_push":false, "code_owner_approval_required": false}')
content = content.encode("utf-8")
return response(200, content, headers, None, 5, request)
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects/1/protected_branches/master", method="get")
def resp_get_protected_branch_not_exist(url, request):
headers = {'content-type': 'application/json'}
content = ('')
content = content.encode("utf-8")
return response(404, content, headers, None, 5, request)
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/projects/1/protected_branches/master", method="delete")
def resp_delete_protected_branch(url, request):
headers = {'content-type': 'application/json'}
content = ('')
content = content.encode("utf-8")
return response(204, content, headers, None, 5, request)
'''
HOOK API
'''

View file

@ -0,0 +1,81 @@
# -*- coding: utf-8 -*-
# Copyright: (c) 2019, Guillaume Martinez (lunik@tiwabbit.fr)
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import pytest
from distutils.version import LooseVersion
from ansible_collections.community.general.plugins.modules.source_control.gitlab.gitlab_protected_branch import GitlabProtectedBranch
def _dummy(x):
"""Dummy function. Only used as a placeholder for toplevel definitions when the test is going
to be skipped anyway"""
return x
pytestmark = []
try:
from .gitlab import (GitlabModuleTestCase,
python_version_match_requirement, python_gitlab_module_version,
python_gitlab_version_match_requirement,
resp_get_protected_branch, resp_get_project_by_name,
resp_get_protected_branch_not_exist,
resp_delete_protected_branch, resp_get_user)
# GitLab module requirements
if python_version_match_requirement():
from gitlab.v4.objects import Project
gitlab_req_version = python_gitlab_version_match_requirement()
gitlab_module_version = python_gitlab_module_version()
if LooseVersion(gitlab_module_version) < LooseVersion(gitlab_req_version):
pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing (Wrong version)"))
except ImportError:
pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing"))
# Unit tests requirements
try:
from httmock import with_httmock # noqa
except ImportError:
pytestmark.append(pytest.mark.skip("Could not load httmock module required for testing"))
with_httmock = _dummy
class TestGitlabProtectedBranch(GitlabModuleTestCase):
@with_httmock(resp_get_project_by_name)
@with_httmock(resp_get_user)
def setUp(self):
super(TestGitlabProtectedBranch, self).setUp()
self.gitlab_instance.user = self.gitlab_instance.users.get(1)
self.moduleUtil = GitlabProtectedBranch(module=self.mock_module, project="foo-bar/diaspora-client", gitlab_instance=self.gitlab_instance)
@with_httmock(resp_get_protected_branch)
def test_protected_branch_exist(self):
rvalue = self.moduleUtil.protected_branch_exist(name="master")
self.assertEqual(rvalue.name, "master")
@with_httmock(resp_get_protected_branch_not_exist)
def test_protected_branch_exist_not_exist(self):
rvalue = self.moduleUtil.protected_branch_exist(name="master")
self.assertEqual(rvalue, False)
@with_httmock(resp_get_protected_branch)
def test_compare_protected_branch(self):
rvalue = self.moduleUtil.compare_protected_branch(name="master", merge_access_levels="maintainer", push_access_level="maintainer")
self.assertEqual(rvalue, True)
@with_httmock(resp_get_protected_branch)
def test_compare_protected_branch_different_settings(self):
rvalue = self.moduleUtil.compare_protected_branch(name="master", merge_access_levels="developer", push_access_level="maintainer")
self.assertEqual(rvalue, False)
@with_httmock(resp_get_protected_branch)
@with_httmock(resp_delete_protected_branch)
def test_delete_protected_branch(self):
rvalue = self.moduleUtil.delete_protected_branch(name="master")
self.assertEqual(rvalue, None)