| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2023, Gabriele Pongelli ([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_label |
| 10 |
short_description: Creates/updates/deletes GitLab Labels belonging to project or group |
| 11 |
version_added: 8.3.0 |
| 12 |
description: |
| 13 |
- When a label does not exist, it is created. |
| 14 |
- When a label does exist, its value is updated when the values are different. |
| 15 |
- Labels can be purged. |
| 16 |
author: |
| 17 |
- "Gabriele Pongelli (@gpongelli)" |
| 18 |
requirements: |
| 19 |
- python-gitlab python module |
| 20 |
extends_documentation_fragment: |
| 21 |
- community.general._auth_basic |
| 22 |
- community.general._gitlab |
| 23 |
- community.general._attributes |
| 24 |
|
| 25 |
attributes: |
| 26 |
check_mode: |
| 27 |
support: full |
| 28 |
diff_mode: |
| 29 |
support: none |
| 30 |
|
| 31 |
options: |
| 32 |
state: |
| 33 |
description: |
| 34 |
- Create or delete project or group label. |
| 35 |
default: present |
| 36 |
type: str |
| 37 |
choices: ["present", "absent"] |
| 38 |
purge: |
| 39 |
description: |
| 40 |
- When set to V(true), delete all labels which are not mentioned in the task. |
| 41 |
default: false |
| 42 |
type: bool |
| 43 |
project: |
| 44 |
description: |
| 45 |
- The path and name of the project. Either this or O(group) is required. |
| 46 |
type: str |
| 47 |
group: |
| 48 |
description: |
| 49 |
- The path of the group. Either this or O(project) is required. |
| 50 |
type: str |
| 51 |
labels: |
| 52 |
description: |
| 53 |
- A list of dictionaries that represents gitlab project's or group's labels. |
| 54 |
type: list |
| 55 |
elements: dict |
| 56 |
default: [] |
| 57 |
suboptions: |
| 58 |
name: |
| 59 |
description: |
| 60 |
- The name of the label. |
| 61 |
type: str |
| 62 |
required: true |
| 63 |
color: |
| 64 |
description: |
| 65 |
- The color of the label. |
| 66 |
- Required when O(state=present). |
| 67 |
type: str |
| 68 |
priority: |
| 69 |
description: |
| 70 |
- Integer value to give priority to the label. |
| 71 |
type: int |
| 72 |
default: |
| 73 |
description: |
| 74 |
description: |
| 75 |
- Label's description. |
| 76 |
type: str |
| 77 |
default: |
| 78 |
new_name: |
| 79 |
description: |
| 80 |
- Optional field to change label's name. |
| 81 |
type: str |
| 82 |
default: |
| 83 |
""" |
| 84 |
|
| 85 |
|
| 86 |
EXAMPLES = r""" |
| 87 |
# same project's task can be executed for group |
| 88 |
- name: Create one Label |
| 89 |
community.general.gitlab_label: |
| 90 |
api_url: https://gitlab.com |
| 91 |
api_token: secret_access_token |
| 92 |
project: "group1/project1" |
| 93 |
labels: |
| 94 |
- name: label_one |
| 95 |
color: "#123456" |
| 96 |
state: present |
| 97 |
|
| 98 |
- name: Create many group labels |
| 99 |
community.general.gitlab_label: |
| 100 |
api_url: https://gitlab.com |
| 101 |
api_token: secret_access_token |
| 102 |
group: "group1" |
| 103 |
labels: |
| 104 |
- name: label_one |
| 105 |
color: "#123456" |
| 106 |
description: this is a label |
| 107 |
priority: 20 |
| 108 |
- name: label_two |
| 109 |
color: "#554422" |
| 110 |
state: present |
| 111 |
|
| 112 |
- name: Create many project labels |
| 113 |
community.general.gitlab_label: |
| 114 |
api_url: https://gitlab.com |
| 115 |
api_token: secret_access_token |
| 116 |
project: "group1/project1" |
| 117 |
labels: |
| 118 |
- name: label_one |
| 119 |
color: "#123456" |
| 120 |
description: this is a label |
| 121 |
priority: 20 |
| 122 |
- name: label_two |
| 123 |
color: "#554422" |
| 124 |
state: present |
| 125 |
|
| 126 |
- name: Set or update some labels |
| 127 |
community.general.gitlab_label: |
| 128 |
api_url: https://gitlab.com |
| 129 |
api_token: secret_access_token |
| 130 |
project: "group1/project1" |
| 131 |
labels: |
| 132 |
- name: label_one |
| 133 |
color: "#224488" |
| 134 |
state: present |
| 135 |
|
| 136 |
- name: Add label in check mode |
| 137 |
community.general.gitlab_label: |
| 138 |
api_url: https://gitlab.com |
| 139 |
api_token: secret_access_token |
| 140 |
project: "group1/project1" |
| 141 |
labels: |
| 142 |
- name: label_one |
| 143 |
color: "#224488" |
| 144 |
check_mode: true |
| 145 |
|
| 146 |
- name: Delete Label |
| 147 |
community.general.gitlab_label: |
| 148 |
api_url: https://gitlab.com |
| 149 |
api_token: secret_access_token |
| 150 |
project: "group1/project1" |
| 151 |
labels: |
| 152 |
- name: label_one |
| 153 |
state: absent |
| 154 |
|
| 155 |
- name: Change Label name |
| 156 |
community.general.gitlab_label: |
| 157 |
api_url: https://gitlab.com |
| 158 |
api_token: secret_access_token |
| 159 |
project: "group1/project1" |
| 160 |
labels: |
| 161 |
- name: label_one |
| 162 |
new_name: label_two |
| 163 |
state: absent |
| 164 |
|
| 165 |
- name: Purge all labels |
| 166 |
community.general.gitlab_label: |
| 167 |
api_url: https://gitlab.com |
| 168 |
api_token: secret_access_token |
| 169 |
project: "group1/project1" |
| 170 |
purge: true |
| 171 |
|
| 172 |
- name: Delete many labels |
| 173 |
community.general.gitlab_label: |
| 174 |
api_url: https://gitlab.com |
| 175 |
api_token: secret_access_token |
| 176 |
project: "group1/project1" |
| 177 |
state: absent |
| 178 |
labels: |
| 179 |
- name: label-abc123 |
| 180 |
- name: label-two |
| 181 |
""" |
| 182 |
|
| 183 |
RETURN = r""" |
| 184 |
labels: |
| 185 |
description: Four lists of the labels which were added, updated, removed or exist. |
| 186 |
returned: success |
| 187 |
type: dict |
| 188 |
contains: |
| 189 |
added: |
| 190 |
description: A list of labels which were created. |
| 191 |
returned: always |
| 192 |
type: list |
| 193 |
sample: ["abcd", "label-one"] |
| 194 |
untouched: |
| 195 |
description: A list of labels which exist. |
| 196 |
returned: always |
| 197 |
type: list |
| 198 |
sample: ["defg", "new-label"] |
| 199 |
removed: |
| 200 |
description: A list of labels which were deleted. |
| 201 |
returned: always |
| 202 |
type: list |
| 203 |
sample: ["defg", "new-label"] |
| 204 |
updated: |
| 205 |
description: A list pre-existing labels whose values have been set. |
| 206 |
returned: always |
| 207 |
type: list |
| 208 |
sample: ["defg", "new-label"] |
| 209 |
labels_obj: |
| 210 |
description: API object. |
| 211 |
returned: success |
| 212 |
type: dict |
| 213 |
""" |
| 214 |
|
| 215 |
from ansible.module_utils.api import basic_auth_argument_spec |
| 216 |
from ansible.module_utils.basic import AnsibleModule |
| 217 |
|
| 218 |
from ansible_collections.community.general.plugins.module_utils._gitlab import ( |
| 219 |
auth_argument_spec, |
| 220 |
ensure_gitlab_package, |
| 221 |
find_group, |
| 222 |
find_project, |
| 223 |
gitlab_authentication, |
| 224 |
) |
| 225 |
|
| 226 |
|
| 227 |
class GitlabLabels: |
| 228 |
def __init__(self, module, gitlab_instance, group_id, project_id): |
| 229 |
self._gitlab = gitlab_instance |
| 230 |
self.gitlab_object = group_id if group_id else project_id |
| 231 |
self.is_group_label = True if group_id else False |
| 232 |
self._module = module |
| 233 |
|
| 234 |
def list_all_labels(self): |
| 235 |
page_nb = 1 |
| 236 |
labels = [] |
| 237 |
vars_page = self.gitlab_object.labels.list(page=page_nb) |
| 238 |
while len(vars_page) > 0: |
| 239 |
labels += vars_page |
| 240 |
page_nb += 1 |
| 241 |
vars_page = self.gitlab_object.labels.list(page=page_nb) |
| 242 |
return labels |
| 243 |
|
| 244 |
def create_label(self, var_obj): |
| 245 |
if self._module.check_mode: |
| 246 |
return True, True |
| 247 |
|
| 248 |
var = { |
| 249 |
"name": var_obj.get("name"), |
| 250 |
"color": var_obj.get("color"), |
| 251 |
} |
| 252 |
|
| 253 |
if var_obj.get("description") is not None: |
| 254 |
var["description"] = var_obj.get("description") |
| 255 |
|
| 256 |
if var_obj.get("priority") is not None: |
| 257 |
var["priority"] = var_obj.get("priority") |
| 258 |
|
| 259 |
_obj = self.gitlab_object.labels.create(var) |
| 260 |
return True, _obj.asdict() |
| 261 |
|
| 262 |
def update_label(self, var_obj): |
| 263 |
if self._module.check_mode: |
| 264 |
return True, True |
| 265 |
_label = self.gitlab_object.labels.get(var_obj.get("name")) |
| 266 |
|
| 267 |
if var_obj.get("new_name") is not None: |
| 268 |
_label.new_name = var_obj.get("new_name") |
| 269 |
|
| 270 |
if var_obj.get("description") is not None: |
| 271 |
_label.description = var_obj.get("description") |
| 272 |
if var_obj.get("priority") is not None: |
| 273 |
_label.priority = var_obj.get("priority") |
| 274 |
if var_obj.get("color") is not None: |
| 275 |
_label.color = var_obj.get("color") |
| 276 |
|
| 277 |
# save returns None |
| 278 |
_label.save() |
| 279 |
return True, _label.asdict() |
| 280 |
|
| 281 |
def delete_label(self, var_obj): |
| 282 |
if self._module.check_mode: |
| 283 |
return True, True |
| 284 |
_label = self.gitlab_object.labels.get(var_obj.get("name")) |
| 285 |
# delete returns None |
| 286 |
_label.delete() |
| 287 |
return True, _label.asdict() |
| 288 |
|
| 289 |
|
| 290 |
def compare(requested_labels, existing_labels, state): |
| 291 |
# we need to do this, because it was determined in a previous version - more or less buggy |
| 292 |
# basically it is not necessary and might result in more/other bugs! |
| 293 |
# but it is required and only relevant for check mode!! |
| 294 |
# logic represents state 'present' when not purge. all other can be derived from that |
| 295 |
# untouched => equal in both |
| 296 |
# updated => name and scope are equal |
| 297 |
# added => name and scope does not exist |
| 298 |
untouched = list() |
| 299 |
updated = list() |
| 300 |
added = list() |
| 301 |
|
| 302 |
if state == "present": |
| 303 |
_existing_labels = list() |
| 304 |
for item in existing_labels: |
| 305 |
_existing_labels.append({"name": item.get("name")}) |
| 306 |
|
| 307 |
for var in requested_labels: |
| 308 |
if var in existing_labels: |
| 309 |
untouched.append(var) |
| 310 |
else: |
| 311 |
compare_item = {"name": var.get("name")} |
| 312 |
if compare_item in _existing_labels: |
| 313 |
updated.append(var) |
| 314 |
else: |
| 315 |
added.append(var) |
| 316 |
|
| 317 |
return untouched, updated, added |
| 318 |
|
| 319 |
|
| 320 |
def native_python_main(this_gitlab, purge, requested_labels, state, module): |
| 321 |
change = False |
| 322 |
return_value = dict(added=[], updated=[], removed=[], untouched=[]) |
| 323 |
return_obj = dict(added=[], updated=[], removed=[]) |
| 324 |
|
| 325 |
labels_before = [x.asdict() for x in this_gitlab.list_all_labels()] |
| 326 |
|
| 327 |
# filter out and enrich before compare |
| 328 |
for item in requested_labels: |
| 329 |
# add defaults when not present |
| 330 |
if item.get("description") is None: |
| 331 |
item["description"] = "" |
| 332 |
if item.get("new_name") is None: |
| 333 |
item["new_name"] = None |
| 334 |
if item.get("priority") is None: |
| 335 |
item["priority"] = None |
| 336 |
|
| 337 |
# group label does not have priority, removing for comparison |
| 338 |
if this_gitlab.is_group_label: |
| 339 |
item.pop("priority") |
| 340 |
|
| 341 |
for item in labels_before: |
| 342 |
# remove field only from server |
| 343 |
item.pop("id") |
| 344 |
item.pop("description_html") |
| 345 |
item.pop("text_color") |
| 346 |
item.pop("subscribed") |
| 347 |
# field present only when it is a project's label |
| 348 |
if "is_project_label" in item: |
| 349 |
item.pop("is_project_label") |
| 350 |
item["new_name"] = None |
| 351 |
|
| 352 |
if state == "present": |
| 353 |
add_or_update = [x for x in requested_labels if x not in labels_before] |
| 354 |
for item in add_or_update: |
| 355 |
try: |
| 356 |
_rv, _obj = this_gitlab.create_label(item) |
| 357 |
if _rv: |
| 358 |
return_value["added"].append(item) |
| 359 |
return_obj["added"].append(_obj) |
| 360 |
except Exception: |
| 361 |
# create raises exception with following error message when label already exists |
| 362 |
_rv, _obj = this_gitlab.update_label(item) |
| 363 |
if _rv: |
| 364 |
return_value["updated"].append(item) |
| 365 |
return_obj["updated"].append(_obj) |
| 366 |
|
| 367 |
if purge: |
| 368 |
# re-fetch |
| 369 |
_labels = this_gitlab.list_all_labels() |
| 370 |
|
| 371 |
for item in labels_before: |
| 372 |
_rv, _obj = this_gitlab.delete_label(item) |
| 373 |
if _rv: |
| 374 |
return_value["removed"].append(item) |
| 375 |
return_obj["removed"].append(_obj) |
| 376 |
|
| 377 |
elif state == "absent": |
| 378 |
if not purge: |
| 379 |
_label_names_requested = [x["name"] for x in requested_labels] |
| 380 |
remove_requested = [x for x in labels_before if x["name"] in _label_names_requested] |
| 381 |
for item in remove_requested: |
| 382 |
_rv, _obj = this_gitlab.delete_label(item) |
| 383 |
if _rv: |
| 384 |
return_value["removed"].append(item) |
| 385 |
return_obj["removed"].append(_obj) |
| 386 |
else: |
| 387 |
for item in labels_before: |
| 388 |
_rv, _obj = this_gitlab.delete_label(item) |
| 389 |
if _rv: |
| 390 |
return_value["removed"].append(item) |
| 391 |
return_obj["removed"].append(_obj) |
| 392 |
|
| 393 |
if module.check_mode: |
| 394 |
_untouched, _updated, _added = compare(requested_labels, labels_before, state) |
| 395 |
return_value = dict(added=_added, updated=_updated, removed=return_value["removed"], untouched=_untouched) |
| 396 |
|
| 397 |
if any(return_value[x] for x in ["added", "removed", "updated"]): |
| 398 |
change = True |
| 399 |
|
| 400 |
labels_after = [x.asdict() for x in this_gitlab.list_all_labels()] |
| 401 |
|
| 402 |
return change, return_value, labels_before, labels_after, return_obj |
| 403 |
|
| 404 |
|
| 405 |
def main(): |
| 406 |
argument_spec = basic_auth_argument_spec() |
| 407 |
argument_spec.update(auth_argument_spec()) |
| 408 |
argument_spec.update( |
| 409 |
project=dict(type="str"), |
| 410 |
group=dict(type="str"), |
| 411 |
purge=dict(type="bool", default=False), |
| 412 |
labels=dict( |
| 413 |
type="list", |
| 414 |
elements="dict", |
| 415 |
default=list(), |
| 416 |
options=dict( |
| 417 |
name=dict(type="str", required=True), |
| 418 |
color=dict(type="str"), |
| 419 |
description=dict(type="str"), |
| 420 |
priority=dict(type="int"), |
| 421 |
new_name=dict(type="str"), |
| 422 |
), |
| 423 |
), |
| 424 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 425 |
) |
| 426 |
|
| 427 |
module = AnsibleModule( |
| 428 |
argument_spec=argument_spec, |
| 429 |
mutually_exclusive=[ |
| 430 |
["api_username", "api_token"], |
| 431 |
["api_username", "api_oauth_token"], |
| 432 |
["api_username", "api_job_token"], |
| 433 |
["api_token", "api_oauth_token"], |
| 434 |
["api_token", "api_job_token"], |
| 435 |
["project", "group"], |
| 436 |
], |
| 437 |
required_together=[ |
| 438 |
["api_username", "api_password"], |
| 439 |
], |
| 440 |
required_one_of=[["api_username", "api_token", "api_oauth_token", "api_job_token"], ["project", "group"]], |
| 441 |
supports_check_mode=True, |
| 442 |
) |
| 443 |
ensure_gitlab_package(module) |
| 444 |
|
| 445 |
gitlab_project = module.params["project"] |
| 446 |
gitlab_group = module.params["group"] |
| 447 |
purge = module.params["purge"] |
| 448 |
label_list = module.params["labels"] |
| 449 |
state = module.params["state"] |
| 450 |
|
| 451 |
gitlab_instance = gitlab_authentication(module, min_version="3.2.0") |
| 452 |
|
| 453 |
# find_project can return None, but the other must exist |
| 454 |
gitlab_project_id = find_project(gitlab_instance, gitlab_project) |
| 455 |
|
| 456 |
# find_group can return None, but the other must exist |
| 457 |
gitlab_group_id = find_group(gitlab_instance, gitlab_group) |
| 458 |
|
| 459 |
# if both not found, module must exist |
| 460 |
if not gitlab_project_id and not gitlab_group_id: |
| 461 |
if gitlab_project and not gitlab_project_id: |
| 462 |
module.fail_json(msg=f"project '{gitlab_project}' not found.") |
| 463 |
if gitlab_group and not gitlab_group_id: |
| 464 |
module.fail_json(msg=f"group '{gitlab_group}' not found.") |
| 465 |
|
| 466 |
this_gitlab = GitlabLabels( |
| 467 |
module=module, gitlab_instance=gitlab_instance, group_id=gitlab_group_id, project_id=gitlab_project_id |
| 468 |
) |
| 469 |
|
| 470 |
if state == "present": |
| 471 |
_existing_labels = [x.asdict()["name"] for x in this_gitlab.list_all_labels()] |
| 472 |
|
| 473 |
# color is mandatory when creating label, but it is optional when changing name or updating other fields |
| 474 |
if any(x["color"] is None and x["new_name"] is None and x["name"] not in _existing_labels for x in label_list): |
| 475 |
module.fail_json(msg="color parameter is required for new labels") |
| 476 |
|
| 477 |
change, raw_return_value, before, after, _obj = native_python_main(this_gitlab, purge, label_list, state, module) |
| 478 |
|
| 479 |
if not module.check_mode: |
| 480 |
raw_return_value["untouched"] = [x for x in before if x in after] |
| 481 |
|
| 482 |
added = [x.get("name") for x in raw_return_value["added"]] |
| 483 |
updated = [x.get("name") for x in raw_return_value["updated"]] |
| 484 |
removed = [x.get("name") for x in raw_return_value["removed"]] |
| 485 |
untouched = [x.get("name") for x in raw_return_value["untouched"]] |
| 486 |
return_value = dict(added=added, updated=updated, removed=removed, untouched=untouched) |
| 487 |
|
| 488 |
module.exit_json(changed=change, labels=return_value, labels_obj=_obj) |
| 489 |
|
| 490 |
|
| 491 |
if __name__ == "__main__": |
| 492 |
main() |