| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2021, Werner Dijkerman ([email protected]) |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
module: gitlab_protected_branch |
| 10 |
short_description: Manage protection of existing branches |
| 11 |
version_added: 3.4.0 |
| 12 |
description: |
| 13 |
- (un)Marking existing branches for protection. |
| 14 |
author: |
| 15 |
- "Werner Dijkerman (@dj-wasabi)" |
| 16 |
requirements: |
| 17 |
- python-gitlab >= 2.3.0 |
| 18 |
extends_documentation_fragment: |
| 19 |
- community.general._auth_basic |
| 20 |
- community.general._gitlab |
| 21 |
- community.general._attributes |
| 22 |
|
| 23 |
attributes: |
| 24 |
check_mode: |
| 25 |
support: full |
| 26 |
diff_mode: |
| 27 |
support: none |
| 28 |
|
| 29 |
options: |
| 30 |
state: |
| 31 |
description: |
| 32 |
- Create or delete protected branch. |
| 33 |
default: present |
| 34 |
type: str |
| 35 |
choices: ["present", "absent"] |
| 36 |
project: |
| 37 |
description: |
| 38 |
- The path and name of the project. |
| 39 |
required: true |
| 40 |
type: str |
| 41 |
name: |
| 42 |
description: |
| 43 |
- The name of the branch that needs to be protected. |
| 44 |
- Can make use a wildcard character for like V(production/*) or just have V(main) or V(develop) as value. |
| 45 |
required: true |
| 46 |
type: str |
| 47 |
merge_access_levels: |
| 48 |
description: |
| 49 |
- Access levels allowed to merge. |
| 50 |
default: maintainer |
| 51 |
type: str |
| 52 |
choices: ["maintainer", "developer", "nobody"] |
| 53 |
push_access_level: |
| 54 |
description: |
| 55 |
- Access levels allowed to push. |
| 56 |
default: maintainer |
| 57 |
type: str |
| 58 |
choices: ["maintainer", "developer", "nobody"] |
| 59 |
allow_force_push: |
| 60 |
description: |
| 61 |
- Whether or not to allow force pushes to the protected branch. |
| 62 |
type: bool |
| 63 |
version_added: '11.3.0' |
| 64 |
code_owner_approval_required: |
| 65 |
description: |
| 66 |
- Whether or not to require code owner approval to push. |
| 67 |
type: bool |
| 68 |
version_added: '11.3.0' |
| 69 |
""" |
| 70 |
|
| 71 |
|
| 72 |
EXAMPLES = r""" |
| 73 |
- name: Create protected branch on main |
| 74 |
community.general.gitlab_protected_branch: |
| 75 |
api_url: https://gitlab.com |
| 76 |
api_token: secret_access_token |
| 77 |
project: "dj-wasabi/collection.general" |
| 78 |
name: main |
| 79 |
merge_access_levels: maintainer |
| 80 |
push_access_level: nobody |
| 81 |
""" |
| 82 |
|
| 83 |
RETURN = r""" |
| 84 |
""" |
| 85 |
|
| 86 |
from ansible.module_utils.api import basic_auth_argument_spec |
| 87 |
from ansible.module_utils.basic import AnsibleModule |
| 88 |
|
| 89 |
from ansible_collections.community.general.plugins.module_utils._gitlab import ( |
| 90 |
auth_argument_spec, |
| 91 |
gitlab, |
| 92 |
gitlab_authentication, |
| 93 |
) |
| 94 |
from ansible_collections.community.general.plugins.module_utils._version import LooseVersion |
| 95 |
|
| 96 |
|
| 97 |
class GitlabProtectedBranch: |
| 98 |
def __init__(self, module, project, gitlab_instance): |
| 99 |
self.repo = gitlab_instance |
| 100 |
self._module = module |
| 101 |
self.project = self.get_project(project) |
| 102 |
self.ACCESS_LEVEL = { |
| 103 |
"nobody": gitlab.const.NO_ACCESS, |
| 104 |
"developer": gitlab.const.DEVELOPER_ACCESS, |
| 105 |
"maintainer": gitlab.const.MAINTAINER_ACCESS, |
| 106 |
} |
| 107 |
|
| 108 |
def get_project(self, project_name): |
| 109 |
return self.repo.projects.get(project_name) |
| 110 |
|
| 111 |
def protected_branch_exist(self, name): |
| 112 |
try: |
| 113 |
return self.project.protectedbranches.get(name) |
| 114 |
except Exception: |
| 115 |
return False |
| 116 |
|
| 117 |
def create_or_update_protected_branch(self, name, options): |
| 118 |
protected_branch_options = { |
| 119 |
"name": name, |
| 120 |
"allow_force_push": options["allow_force_push"], |
| 121 |
"code_owner_approval_required": options["code_owner_approval_required"], |
| 122 |
} |
| 123 |
protected_branch = self.protected_branch_exist(name=name) |
| 124 |
changed = False |
| 125 |
if protected_branch and self.can_update(protected_branch, options): |
| 126 |
for arg_key, arg_value in protected_branch_options.items(): |
| 127 |
if arg_value is not None: |
| 128 |
if getattr(protected_branch, arg_key) != arg_value: |
| 129 |
setattr(protected_branch, arg_key, arg_value) |
| 130 |
changed = True |
| 131 |
if changed and not self._module.check_mode: |
| 132 |
protected_branch.save() |
| 133 |
else: |
| 134 |
# Set immutable options only on (re)creation |
| 135 |
protected_branch_options["merge_access_level"] = options["merge_access_levels"] |
| 136 |
protected_branch_options["push_access_level"] = options["push_access_level"] |
| 137 |
if protected_branch: |
| 138 |
# Exists, but couldn't update. So, delete first |
| 139 |
self.delete_protected_branch(name) |
| 140 |
if not self._module.check_mode: |
| 141 |
self.project.protectedbranches.create(protected_branch_options) |
| 142 |
changed = True |
| 143 |
|
| 144 |
return changed |
| 145 |
|
| 146 |
def can_update(self, protected_branch, options): |
| 147 |
# these keys are not set on update the same way they are on creation |
| 148 |
configured_merge = options["merge_access_levels"] |
| 149 |
configured_push = options["push_access_level"] |
| 150 |
current_merge = protected_branch.merge_access_levels[0]["access_level"] |
| 151 |
current_push = protected_branch.push_access_levels[0]["access_level"] |
| 152 |
return (configured_merge is None or current_merge == configured_merge) and ( |
| 153 |
configured_push is None or current_push == configured_push |
| 154 |
) |
| 155 |
|
| 156 |
def delete_protected_branch(self, name): |
| 157 |
if self._module.check_mode: |
| 158 |
return True |
| 159 |
return self.project.protectedbranches.delete(name) |
| 160 |
|
| 161 |
|
| 162 |
def main(): |
| 163 |
argument_spec = basic_auth_argument_spec() |
| 164 |
argument_spec.update(auth_argument_spec()) |
| 165 |
argument_spec.update( |
| 166 |
project=dict(type="str", required=True), |
| 167 |
name=dict(type="str", required=True), |
| 168 |
merge_access_levels=dict(type="str", default="maintainer", choices=["maintainer", "developer", "nobody"]), |
| 169 |
push_access_level=dict(type="str", default="maintainer", choices=["maintainer", "developer", "nobody"]), |
| 170 |
allow_force_push=dict(type="bool"), |
| 171 |
code_owner_approval_required=dict(type="bool"), |
| 172 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 173 |
) |
| 174 |
|
| 175 |
module = AnsibleModule( |
| 176 |
argument_spec=argument_spec, |
| 177 |
mutually_exclusive=[ |
| 178 |
["api_username", "api_token"], |
| 179 |
["api_username", "api_oauth_token"], |
| 180 |
["api_username", "api_job_token"], |
| 181 |
["api_token", "api_oauth_token"], |
| 182 |
["api_token", "api_job_token"], |
| 183 |
], |
| 184 |
required_together=[ |
| 185 |
["api_username", "api_password"], |
| 186 |
], |
| 187 |
required_one_of=[["api_username", "api_token", "api_oauth_token", "api_job_token"]], |
| 188 |
supports_check_mode=True, |
| 189 |
) |
| 190 |
|
| 191 |
# check prerequisites and connect to gitlab server |
| 192 |
gitlab_instance = gitlab_authentication(module) |
| 193 |
|
| 194 |
project = module.params["project"] |
| 195 |
name = module.params["name"] |
| 196 |
merge_access_levels = module.params["merge_access_levels"] |
| 197 |
push_access_level = module.params["push_access_level"] |
| 198 |
state = module.params["state"] |
| 199 |
|
| 200 |
gitlab_version = gitlab.__version__ |
| 201 |
if LooseVersion(gitlab_version) < LooseVersion("2.3.0"): |
| 202 |
module.fail_json( |
| 203 |
msg=f"community.general.gitlab_protected_branch requires python-gitlab Python module >= 2.3.0 (installed version: [{gitlab_version}])." |
| 204 |
" Please upgrade python-gitlab to version 2.3.0 or above." |
| 205 |
) |
| 206 |
|
| 207 |
this_gitlab = GitlabProtectedBranch(module=module, project=project, gitlab_instance=gitlab_instance) |
| 208 |
|
| 209 |
p_branch = this_gitlab.protected_branch_exist(name=name) |
| 210 |
options = { |
| 211 |
"merge_access_levels": this_gitlab.ACCESS_LEVEL[merge_access_levels], |
| 212 |
"push_access_level": this_gitlab.ACCESS_LEVEL[push_access_level], |
| 213 |
"allow_force_push": module.params["allow_force_push"], |
| 214 |
"code_owner_approval_required": module.params["code_owner_approval_required"], |
| 215 |
} |
| 216 |
if state == "present": |
| 217 |
changed = this_gitlab.create_or_update_protected_branch(name, options) |
| 218 |
module.exit_json(changed=changed, msg="Created or updated the protected branch.") |
| 219 |
elif p_branch and state == "absent": |
| 220 |
this_gitlab.delete_protected_branch(name=name) |
| 221 |
module.exit_json(changed=True, msg="Deleted the protected branch.") |
| 222 |
module.exit_json(changed=False, msg="No changes are needed.") |
| 223 |
|
| 224 |
|
| 225 |
if __name__ == "__main__": |
| 226 |
main() |