| 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_branch |
| 10 |
short_description: Create or delete a branch |
| 11 |
version_added: 4.2.0 |
| 12 |
description: |
| 13 |
- This module allows to create or delete branches. |
| 14 |
author: |
| 15 |
- paytroff (@paytroff) |
| 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: none |
| 26 |
diff_mode: |
| 27 |
support: none |
| 28 |
|
| 29 |
options: |
| 30 |
state: |
| 31 |
description: |
| 32 |
- Create or delete branch. |
| 33 |
default: present |
| 34 |
type: str |
| 35 |
choices: ["present", "absent"] |
| 36 |
project: |
| 37 |
description: |
| 38 |
- The path or name of the project. |
| 39 |
required: true |
| 40 |
type: str |
| 41 |
branch: |
| 42 |
description: |
| 43 |
- The name of the branch that needs to be created. |
| 44 |
required: true |
| 45 |
type: str |
| 46 |
ref_branch: |
| 47 |
description: |
| 48 |
- Reference branch to create from. |
| 49 |
- This must be specified if O(state=present). |
| 50 |
type: str |
| 51 |
""" |
| 52 |
|
| 53 |
|
| 54 |
EXAMPLES = r""" |
| 55 |
- name: Create branch branch2 from main |
| 56 |
community.general.gitlab_branch: |
| 57 |
api_url: https://gitlab.com |
| 58 |
api_token: secret_access_token |
| 59 |
project: "group1/project1" |
| 60 |
branch: branch2 |
| 61 |
ref_branch: main |
| 62 |
state: present |
| 63 |
|
| 64 |
- name: Delete branch branch2 |
| 65 |
community.general.gitlab_branch: |
| 66 |
api_url: https://gitlab.com |
| 67 |
api_token: secret_access_token |
| 68 |
project: "group1/project1" |
| 69 |
branch: branch2 |
| 70 |
state: absent |
| 71 |
""" |
| 72 |
|
| 73 |
RETURN = r""" |
| 74 |
""" |
| 75 |
|
| 76 |
import traceback |
| 77 |
|
| 78 |
from ansible.module_utils.api import basic_auth_argument_spec |
| 79 |
from ansible.module_utils.basic import AnsibleModule |
| 80 |
|
| 81 |
from ansible_collections.community.general.plugins.module_utils._gitlab import ( |
| 82 |
auth_argument_spec, |
| 83 |
gitlab, |
| 84 |
gitlab_authentication, |
| 85 |
) |
| 86 |
from ansible_collections.community.general.plugins.module_utils._version import LooseVersion |
| 87 |
|
| 88 |
|
| 89 |
class GitlabBranch: |
| 90 |
def __init__(self, module, project, gitlab_instance): |
| 91 |
self.repo = gitlab_instance |
| 92 |
self._module = module |
| 93 |
self.project = self.get_project(project) |
| 94 |
|
| 95 |
def get_project(self, project): |
| 96 |
try: |
| 97 |
return self.repo.projects.get(project) |
| 98 |
except Exception: |
| 99 |
return False |
| 100 |
|
| 101 |
def get_branch(self, branch): |
| 102 |
try: |
| 103 |
return self.project.branches.get(branch) |
| 104 |
except Exception: |
| 105 |
return False |
| 106 |
|
| 107 |
def create_branch(self, branch, ref_branch): |
| 108 |
return self.project.branches.create({"branch": branch, "ref": ref_branch}) |
| 109 |
|
| 110 |
def delete_branch(self, branch): |
| 111 |
return branch.delete() |
| 112 |
|
| 113 |
|
| 114 |
def main(): |
| 115 |
argument_spec = basic_auth_argument_spec() |
| 116 |
argument_spec.update(auth_argument_spec()) |
| 117 |
argument_spec.update( |
| 118 |
project=dict(type="str", required=True), |
| 119 |
branch=dict(type="str", required=True), |
| 120 |
ref_branch=dict(type="str"), |
| 121 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 122 |
) |
| 123 |
|
| 124 |
module = AnsibleModule( |
| 125 |
argument_spec=argument_spec, |
| 126 |
mutually_exclusive=[ |
| 127 |
["api_username", "api_token"], |
| 128 |
["api_username", "api_oauth_token"], |
| 129 |
["api_username", "api_job_token"], |
| 130 |
["api_token", "api_oauth_token"], |
| 131 |
["api_token", "api_job_token"], |
| 132 |
], |
| 133 |
required_together=[ |
| 134 |
["api_username", "api_password"], |
| 135 |
], |
| 136 |
required_one_of=[["api_username", "api_token", "api_oauth_token", "api_job_token"]], |
| 137 |
required_if=[ |
| 138 |
["state", "present", ["ref_branch"], True], |
| 139 |
], |
| 140 |
supports_check_mode=False, |
| 141 |
) |
| 142 |
|
| 143 |
# check prerequisites and connect to gitlab server |
| 144 |
gitlab_instance = gitlab_authentication(module) |
| 145 |
|
| 146 |
project = module.params["project"] |
| 147 |
branch = module.params["branch"] |
| 148 |
ref_branch = module.params["ref_branch"] |
| 149 |
state = module.params["state"] |
| 150 |
|
| 151 |
gitlab_version = gitlab.__version__ |
| 152 |
if LooseVersion(gitlab_version) < LooseVersion("2.3.0"): |
| 153 |
module.fail_json( |
| 154 |
msg=f"community.general.gitlab_branch requires python-gitlab Python module >= 2.3.0 (installed version: [{gitlab_version}])." |
| 155 |
" Please upgrade python-gitlab to version 2.3.0 or above." |
| 156 |
) |
| 157 |
|
| 158 |
this_gitlab = GitlabBranch(module=module, project=project, gitlab_instance=gitlab_instance) |
| 159 |
|
| 160 |
this_branch = this_gitlab.get_branch(branch) |
| 161 |
|
| 162 |
if not this_branch and state == "present": |
| 163 |
r_branch = this_gitlab.get_branch(ref_branch) |
| 164 |
if not r_branch: |
| 165 |
module.fail_json(msg=f"Ref branch {ref_branch} not exist.") |
| 166 |
this_gitlab.create_branch(branch, ref_branch) |
| 167 |
module.exit_json(changed=True, msg=f"Created the branch {branch}.") |
| 168 |
elif this_branch and state == "present": |
| 169 |
module.exit_json(changed=False, msg=f"Branch {branch} already exist") |
| 170 |
elif this_branch and state == "absent": |
| 171 |
try: |
| 172 |
this_gitlab.delete_branch(this_branch) |
| 173 |
module.exit_json(changed=True, msg=f"Branch {branch} deleted.") |
| 174 |
except Exception: |
| 175 |
module.fail_json(msg="Error delete branch.", exception=traceback.format_exc()) |
| 176 |
else: |
| 177 |
module.exit_json(changed=False, msg="No changes are needed.") |
| 178 |
|
| 179 |
|
| 180 |
if __name__ == "__main__": |
| 181 |
main() |