Skip to content

Commit

Permalink
refactor(gitlab modules): remove duplicate gitlab package check
Browse files Browse the repository at this point in the history
  • Loading branch information
Léo GATELLIER committed Nov 6, 2023
1 parent 6d0bcec commit a534d7b
Show file tree
Hide file tree
Showing 17 changed files with 63 additions and 65 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/7486-gitlab-refactor-package-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- gitlab modules - remove duplicate gitlab package check
4 changes: 2 additions & 2 deletions plugins/module_utils/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ def ensure_gitlab_package(module):


def gitlab_authentication(module):
ensure_gitlab_package(module)

gitlab_url = module.params['api_url']
validate_certs = module.params['validate_certs']
gitlab_user = module.params['api_username']
Expand All @@ -82,8 +84,6 @@ def gitlab_authentication(module):
gitlab_oauth_token = module.params['api_oauth_token']
gitlab_job_token = module.params['api_job_token']

ensure_gitlab_package(module)

try:
# python-gitlab library remove support for username/password authentication since 1.13.0
# Changelog : https://github.com/python-gitlab/python-gitlab/releases/tag/v1.13.0
Expand Down
7 changes: 4 additions & 3 deletions plugins/modules/gitlab_branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@

from ansible_collections.community.general.plugins.module_utils.version import LooseVersion
from ansible_collections.community.general.plugins.module_utils.gitlab import (
auth_argument_spec, gitlab_authentication, gitlab, ensure_gitlab_package
auth_argument_spec, gitlab_authentication, gitlab
)


Expand Down Expand Up @@ -144,7 +144,9 @@ def main():
],
supports_check_mode=False
)
ensure_gitlab_package(module)

# check prerequisites and connect to gitlab server
gitlab_instance = gitlab_authentication(module)

project = module.params['project']
branch = module.params['branch']
Expand All @@ -156,7 +158,6 @@ def main():
module.fail_json(msg="community.general.gitlab_proteched_branch requires python-gitlab Python module >= 2.3.0 (installed version: [%s])."
" Please upgrade python-gitlab to version 2.3.0 or above." % gitlab_version)

gitlab_instance = gitlab_authentication(module)
this_gitlab = GitlabBranch(module=module, project=project, gitlab_instance=gitlab_instance)

this_branch = this_gitlab.get_branch(branch)
Expand Down
8 changes: 4 additions & 4 deletions plugins/modules/gitlab_deploy_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
from ansible.module_utils.common.text.converters import to_native

from ansible_collections.community.general.plugins.module_utils.gitlab import (
auth_argument_spec, find_project, gitlab_authentication, gitlab, ensure_gitlab_package
auth_argument_spec, find_project, gitlab_authentication, gitlab
)


Expand Down Expand Up @@ -261,16 +261,16 @@ def main():
],
supports_check_mode=True,
)
ensure_gitlab_package(module)

# check prerequisites and connect to gitlab server
gitlab_instance = gitlab_authentication(module)

state = module.params['state']
project_identifier = module.params['project']
key_title = module.params['title']
key_keyfile = module.params['key']
key_can_push = module.params['can_push']

gitlab_instance = gitlab_authentication(module)

gitlab_deploy_key = GitLabDeployKey(module, gitlab_instance)

project = find_project(gitlab_instance, project_identifier)
Expand Down
8 changes: 4 additions & 4 deletions plugins/modules/gitlab_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@
from ansible.module_utils.common.text.converters import to_native

from ansible_collections.community.general.plugins.module_utils.gitlab import (
auth_argument_spec, find_group, gitlab_authentication, gitlab, ensure_gitlab_package
auth_argument_spec, find_group, gitlab_authentication, gitlab
)


Expand Down Expand Up @@ -355,7 +355,9 @@ def main():
],
supports_check_mode=True,
)
ensure_gitlab_package(module)

# check prerequisites and connect to gitlab server
gitlab_instance = gitlab_authentication(module)

group_name = module.params['name']
group_path = module.params['path']
Expand All @@ -370,8 +372,6 @@ def main():
avatar_path = module.params['avatar_path']
force_delete = module.params['force_delete']

gitlab_instance = gitlab_authentication(module)

# Define default group_path based on group_name
if group_path is None:
group_path = group_name.replace(" ", "_")
Expand Down
9 changes: 4 additions & 5 deletions plugins/modules/gitlab_group_members.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
from ansible.module_utils.basic import AnsibleModule

from ansible_collections.community.general.plugins.module_utils.gitlab import (
auth_argument_spec, gitlab_authentication, gitlab, ensure_gitlab_package
auth_argument_spec, gitlab_authentication, gitlab
)


Expand Down Expand Up @@ -273,7 +273,9 @@ def main():
],
supports_check_mode=True,
)
ensure_gitlab_package(module)

# check prerequisites and connect to gitlab server
gl = gitlab_authentication(module)

access_level_int = {
'guest': gitlab.GUEST_ACCESS,
Expand All @@ -291,9 +293,6 @@ def main():
if purge_users:
purge_users = [access_level_int[level] for level in purge_users]

# connect to gitlab server
gl = gitlab_authentication(module)

group = GitLabGroup(module, gl)

gitlab_group_id = group.get_group_id(gitlab_group)
Expand Down
8 changes: 4 additions & 4 deletions plugins/modules/gitlab_group_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.api import basic_auth_argument_spec
from ansible_collections.community.general.plugins.module_utils.gitlab import (
auth_argument_spec, gitlab_authentication, ensure_gitlab_package, filter_returned_variables, vars_to_variables
auth_argument_spec, gitlab_authentication, filter_returned_variables, vars_to_variables
)


Expand Down Expand Up @@ -413,7 +413,9 @@ def main():
],
supports_check_mode=True
)
ensure_gitlab_package(module)

# check prerequisites and connect to gitlab server
gitlab_instance = gitlab_authentication(module)

purge = module.params['purge']
var_list = module.params['vars']
Expand All @@ -428,8 +430,6 @@ def main():
if any(x['value'] is None for x in variables):
module.fail_json(msg='value parameter is required in state present')

gitlab_instance = gitlab_authentication(module)

this_gitlab = GitlabGroupVariables(module=module, gitlab_instance=gitlab_instance)

changed, raw_return_value, before, after = native_python_main(this_gitlab, purge, variables, state, module)
Expand Down
8 changes: 4 additions & 4 deletions plugins/modules/gitlab_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
from ansible.module_utils.basic import AnsibleModule

from ansible_collections.community.general.plugins.module_utils.gitlab import (
auth_argument_spec, find_project, gitlab_authentication, ensure_gitlab_package
auth_argument_spec, find_project, gitlab_authentication
)


Expand Down Expand Up @@ -325,7 +325,9 @@ def main():
],
supports_check_mode=True,
)
ensure_gitlab_package(module)

# check prerequisites and connect to gitlab server
gitlab_instance = gitlab_authentication(module)

state = module.params['state']
project_identifier = module.params['project']
Expand All @@ -342,8 +344,6 @@ def main():
enable_ssl_verification = module.params['hook_validate_certs']
hook_token = module.params['token']

gitlab_instance = gitlab_authentication(module)

gitlab_hook = GitLabHook(module, gitlab_instance)

project = find_project(gitlab_instance, project_identifier)
Expand Down
8 changes: 4 additions & 4 deletions plugins/modules/gitlab_instance_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.api import basic_auth_argument_spec
from ansible_collections.community.general.plugins.module_utils.gitlab import (
auth_argument_spec, gitlab_authentication, ensure_gitlab_package, filter_returned_variables
auth_argument_spec, gitlab_authentication, filter_returned_variables
)


Expand Down Expand Up @@ -326,7 +326,9 @@ def main():
],
supports_check_mode=True
)
ensure_gitlab_package(module)

# check prerequisites and connect to gitlab server
gitlab_instance = gitlab_authentication(module)

purge = module.params['purge']
state = module.params['state']
Expand All @@ -337,8 +339,6 @@ def main():
if any(x['value'] is None for x in variables):
module.fail_json(msg='value parameter is required in state present')

gitlab_instance = gitlab_authentication(module)

this_gitlab = GitlabInstanceVariables(module=module, gitlab_instance=gitlab_instance)

changed, raw_return_value, before, after = native_python_main(this_gitlab, purge, variables, state, module)
Expand Down
8 changes: 4 additions & 4 deletions plugins/modules/gitlab_merge_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@

from ansible_collections.community.general.plugins.module_utils.version import LooseVersion
from ansible_collections.community.general.plugins.module_utils.gitlab import (
auth_argument_spec, gitlab_authentication, gitlab, ensure_gitlab_package, find_project
auth_argument_spec, gitlab_authentication, gitlab, find_project
)


Expand Down Expand Up @@ -321,7 +321,9 @@ def main():
],
supports_check_mode=True
)
ensure_gitlab_package(module)

# check prerequisites and connect to gitlab server
gitlab_instance = gitlab_authentication(module)

project = module.params['project']
source_branch = module.params['source_branch']
Expand All @@ -341,8 +343,6 @@ def main():
module.fail_json(msg="community.general.gitlab_merge_request requires python-gitlab Python module >= 2.3.0 (installed version: [%s])."
" Please upgrade python-gitlab to version 2.3.0 or above." % gitlab_version)

gitlab_instance = gitlab_authentication(module)

this_project = find_project(gitlab_instance, project)
if this_project is None:
module.fail_json(msg="Failed to get the project: %s" % project)
Expand Down
8 changes: 4 additions & 4 deletions plugins/modules/gitlab_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@
from ansible.module_utils.common.text.converters import to_native

from ansible_collections.community.general.plugins.module_utils.gitlab import (
auth_argument_spec, find_group, find_project, gitlab_authentication, gitlab, ensure_gitlab_package
auth_argument_spec, find_group, find_project, gitlab_authentication, gitlab
)

from ansible_collections.community.general.plugins.module_utils.version import LooseVersion
Expand Down Expand Up @@ -558,7 +558,9 @@ def main():
],
supports_check_mode=True,
)
ensure_gitlab_package(module)

# check prerequisites and connect to gitlab server
gitlab_instance = gitlab_authentication(module)

group_identifier = module.params['group']
project_name = module.params['name']
Expand Down Expand Up @@ -596,8 +598,6 @@ def main():
security_and_compliance_access_level = module.params['security_and_compliance_access_level']
topics = module.params['topics']

gitlab_instance = gitlab_authentication(module)

# Set project_path to project_name if it is empty.
if project_path is None:
project_path = project_name.replace(" ", "_")
Expand Down
7 changes: 3 additions & 4 deletions plugins/modules/gitlab_project_badge.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
from ansible.module_utils.basic import AnsibleModule

from ansible_collections.community.general.plugins.module_utils.gitlab import (
auth_argument_spec, gitlab_authentication, find_project, ensure_gitlab_package
auth_argument_spec, gitlab_authentication, find_project
)


Expand Down Expand Up @@ -159,13 +159,12 @@ def absent_strategy(module, gl, project, wished_badge):


def core(module):
ensure_gitlab_package(module)
# check prerequisites and connect to gitlab server
gl = gitlab_authentication(module)

gitlab_project = module.params['project']
state = module.params['state']

gl = gitlab_authentication(module)

project = find_project(gl, gitlab_project)
# project doesn't exist
if not project:
Expand Down
9 changes: 4 additions & 5 deletions plugins/modules/gitlab_project_members.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@
from ansible.module_utils.basic import AnsibleModule

from ansible_collections.community.general.plugins.module_utils.gitlab import (
auth_argument_spec, gitlab_authentication, gitlab, ensure_gitlab_package
auth_argument_spec, gitlab_authentication, gitlab
)


Expand Down Expand Up @@ -279,7 +279,9 @@ def main():
],
supports_check_mode=True,
)
ensure_gitlab_package(module)

# check prerequisites and connect to gitlab server
gl = gitlab_authentication(module)

access_level_int = {
'guest': gitlab.GUEST_ACCESS,
Expand All @@ -296,9 +298,6 @@ def main():
if purge_users:
purge_users = [access_level_int[level] for level in purge_users]

# connect to gitlab server
gl = gitlab_authentication(module)

project = GitLabProjectMembers(module, gl)

gitlab_project_id = project.get_project(gitlab_project)
Expand Down
12 changes: 4 additions & 8 deletions plugins/modules/gitlab_project_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,12 @@
sample: ['ACCESS_KEY_ID', 'SECRET_ACCESS_KEY']
'''

from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.api import basic_auth_argument_spec


from ansible_collections.community.general.plugins.module_utils.gitlab import (
auth_argument_spec, gitlab_authentication, ensure_gitlab_package, filter_returned_variables, vars_to_variables,
HAS_GITLAB_PACKAGE, GITLAB_IMP_ERR
auth_argument_spec, gitlab_authentication, filter_returned_variables, vars_to_variables
)


Expand Down Expand Up @@ -436,10 +435,9 @@ def main():
],
supports_check_mode=True
)
ensure_gitlab_package(module)

if not HAS_GITLAB_PACKAGE:
module.fail_json(msg=missing_required_lib("python-gitlab"), exception=GITLAB_IMP_ERR)
# check prerequisites and connect to gitlab server
gitlab_instance = gitlab_authentication(module)

purge = module.params['purge']
var_list = module.params['vars']
Expand All @@ -454,8 +452,6 @@ def main():
if any(x['value'] is None for x in variables):
module.fail_json(msg='value parameter is required for all variables in state present')

gitlab_instance = gitlab_authentication(module)

this_gitlab = GitlabProjectVariables(module=module, gitlab_instance=gitlab_instance)

change, raw_return_value, before, after = native_python_main(this_gitlab, purge, variables, state, module)
Expand Down
Loading

0 comments on commit a534d7b

Please sign in to comment.