diff --git a/.github/BOTMETA.yml b/.github/BOTMETA.yml index 76369a40f2..8b748c06fc 100644 --- a/.github/BOTMETA.yml +++ b/.github/BOTMETA.yml @@ -562,6 +562,8 @@ files: maintainers: paytroff $modules/gitlab_issue.py: maintainers: zvaraondrej + $modules/gitlab_label.py: + maintainers: gpongelli $modules/gitlab_merge_request.py: maintainers: zvaraondrej $modules/gitlab_milestone.py: diff --git a/plugins/module_utils/gitlab.py b/plugins/module_utils/gitlab.py index 803957fc2f..5fe6ce0b70 100644 --- a/plugins/module_utils/gitlab.py +++ b/plugins/module_utils/gitlab.py @@ -59,11 +59,11 @@ def find_project(gitlab_instance, identifier): def find_group(gitlab_instance, identifier): try: - project = gitlab_instance.groups.get(identifier) + group = gitlab_instance.groups.get(identifier) except Exception as e: return None - return project + return group def ensure_gitlab_package(module): diff --git a/plugins/modules/gitlab_label.py b/plugins/modules/gitlab_label.py new file mode 100644 index 0000000000..f2c8393f22 --- /dev/null +++ b/plugins/modules/gitlab_label.py @@ -0,0 +1,500 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# Copyright (c) 2023, Gabriele Pongelli (gabriele.pongelli@gmail.com) +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +DOCUMENTATION = ''' +module: gitlab_label +short_description: Creates/updates/deletes GitLab Labels belonging to project or group. +version_added: 8.3.0 +description: + - When a label does not exist, it will be created. + - When a label does exist, its value will be updated when the values are different. + - Labels can be purged. +author: + - "Gabriele Pongelli (@gpongelli)" +requirements: + - python-gitlab python module +extends_documentation_fragment: + - community.general.auth_basic + - community.general.gitlab + - community.general.attributes + +attributes: + check_mode: + support: full + diff_mode: + support: none + +options: + state: + description: + - Create or delete project or group label. + default: present + type: str + choices: ["present", "absent"] + purge: + description: + - When set to V(true), delete all labels which are not mentioned in the task. + default: false + type: bool + required: false + project: + description: + - The path and name of the project. Either this or O(group) is required. + required: false + type: str + group: + description: + - The path of the group. Either this or O(project) is required. + required: false + type: str + labels: + description: + - A list of dictionaries that represents gitlab project's or group's labels. + type: list + elements: dict + required: false + default: [] + suboptions: + name: + description: + - The name of the label. + type: str + required: true + color: + description: + - The color of the label. + - Required when O(state=present). + type: str + priority: + description: + - Integer value to give priority to the label. + type: int + required: false + default: null + description: + description: + - Label's description. + type: str + default: null + new_name: + description: + - Optional field to change label's name. + type: str + default: null +''' + + +EXAMPLES = ''' +# same project's task can be executed for group +- name: Create one Label + community.general.gitlab_label: + api_url: https://gitlab.com + api_token: secret_access_token + project: "group1/project1" + labels: + - name: label_one + color: "#123456" + state: present + +- name: Create many group labels + community.general.gitlab_label: + api_url: https://gitlab.com + api_token: secret_access_token + group: "group1" + labels: + - name: label_one + color: "#123456" + description: this is a label + priority: 20 + - name: label_two + color: "#554422" + state: present + +- name: Create many project labels + community.general.gitlab_label: + api_url: https://gitlab.com + api_token: secret_access_token + project: "group1/project1" + labels: + - name: label_one + color: "#123456" + description: this is a label + priority: 20 + - name: label_two + color: "#554422" + state: present + +- name: Set or update some labels + community.general.gitlab_label: + api_url: https://gitlab.com + api_token: secret_access_token + project: "group1/project1" + labels: + - name: label_one + color: "#224488" + state: present + +- name: Add label in check mode + community.general.gitlab_label: + api_url: https://gitlab.com + api_token: secret_access_token + project: "group1/project1" + labels: + - name: label_one + color: "#224488" + check_mode: true + +- name: Delete Label + community.general.gitlab_label: + api_url: https://gitlab.com + api_token: secret_access_token + project: "group1/project1" + labels: + - name: label_one + state: absent + +- name: Change Label name + community.general.gitlab_label: + api_url: https://gitlab.com + api_token: secret_access_token + project: "group1/project1" + labels: + - name: label_one + new_name: label_two + state: absent + +- name: Purge all labels + community.general.gitlab_label: + api_url: https://gitlab.com + api_token: secret_access_token + project: "group1/project1" + purge: true + +- name: Delete many labels + community.general.gitlab_label: + api_url: https://gitlab.com + api_token: secret_access_token + project: "group1/project1" + state: absent + labels: + - name: label-abc123 + - name: label-two +''' + +RETURN = ''' +labels: + description: Four lists of the labels which were added, updated, removed or exist. + returned: success + type: dict + contains: + added: + description: A list of labels which were created. + returned: always + type: list + sample: ['abcd', 'label-one'] + untouched: + description: A list of labels which exist. + returned: always + type: list + sample: ['defg', 'new-label'] + removed: + description: A list of labels which were deleted. + returned: always + type: list + sample: ['defg', 'new-label'] + updated: + description: A list pre-existing labels whose values have been set. + returned: always + type: list + sample: ['defg', 'new-label'] +labels_obj: + description: API object. + returned: success + type: dict +''' + +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.version import LooseVersion +from ansible_collections.community.general.plugins.module_utils.gitlab import ( + auth_argument_spec, gitlab_authentication, ensure_gitlab_package, find_group, find_project, gitlab +) + + +class GitlabLabels(object): + + def __init__(self, module, gitlab_instance, group_id, project_id): + self._gitlab = gitlab_instance + self.gitlab_object = group_id if group_id else project_id + self.is_group_label = True if group_id else False + self._module = module + + def list_all_labels(self): + page_nb = 1 + labels = [] + vars_page = self.gitlab_object.labels.list(page=page_nb) + while len(vars_page) > 0: + labels += vars_page + page_nb += 1 + vars_page = self.gitlab_object.labels.list(page=page_nb) + return labels + + def create_label(self, var_obj): + if self._module.check_mode: + return True, True + + var = { + "name": var_obj.get('name'), + "color": var_obj.get('color'), + } + + if var_obj.get('description') is not None: + var["description"] = var_obj.get('description') + + if var_obj.get('priority') is not None: + var["priority"] = var_obj.get('priority') + + _obj = self.gitlab_object.labels.create(var) + return True, _obj.asdict() + + def update_label(self, var_obj): + if self._module.check_mode: + return True, True + _label = self.gitlab_object.labels.get(var_obj.get('name')) + + if var_obj.get('new_name') is not None: + _label.new_name = var_obj.get('new_name') + + if var_obj.get('description') is not None: + _label.description = var_obj.get('description') + if var_obj.get('priority') is not None: + _label.priority = var_obj.get('priority') + + # save returns None + _label.save() + return True, _label.asdict() + + def delete_label(self, var_obj): + if self._module.check_mode: + return True, True + _label = self.gitlab_object.labels.get(var_obj.get('name')) + # delete returns None + _label.delete() + return True, _label.asdict() + + +def compare(requested_labels, existing_labels, state): + # we need to do this, because it was determined in a previous version - more or less buggy + # basically it is not necessary and might result in more/other bugs! + # but it is required and only relevant for check mode!! + # logic represents state 'present' when not purge. all other can be derived from that + # untouched => equal in both + # updated => name and scope are equal + # added => name and scope does not exist + untouched = list() + updated = list() + added = list() + + if state == 'present': + _existing_labels = list() + for item in existing_labels: + _existing_labels.append({'name': item.get('name')}) + + for var in requested_labels: + if var in existing_labels: + untouched.append(var) + else: + compare_item = {'name': var.get('name')} + if compare_item in _existing_labels: + updated.append(var) + else: + added.append(var) + + return untouched, updated, added + + +def native_python_main(this_gitlab, purge, requested_labels, state, module): + change = False + return_value = dict(added=[], updated=[], removed=[], untouched=[]) + return_obj = dict(added=[], updated=[], removed=[]) + + labels_before = [x.asdict() for x in this_gitlab.list_all_labels()] + + # filter out and enrich before compare + for item in requested_labels: + # add defaults when not present + if item.get('description') is None: + item['description'] = "" + if item.get('new_name') is None: + item['new_name'] = None + if item.get('priority') is None: + item['priority'] = None + + # group label does not have priority, removing for comparison + if this_gitlab.is_group_label: + item.pop('priority') + + for item in labels_before: + # remove field only from server + item.pop('id') + item.pop('description_html') + item.pop('text_color') + item.pop('subscribed') + # field present only when it's a project's label + if 'is_project_label' in item: + item.pop('is_project_label') + item['new_name'] = None + + if state == 'present': + add_or_update = [x for x in requested_labels if x not in labels_before] + for item in add_or_update: + try: + _rv, _obj = this_gitlab.create_label(item) + if _rv: + return_value['added'].append(item) + return_obj['added'].append(_obj) + except Exception: + # create raises exception with following error message when label already exists + _rv, _obj = this_gitlab.update_label(item) + if _rv: + return_value['updated'].append(item) + return_obj['updated'].append(_obj) + + if purge: + # re-fetch + _labels = this_gitlab.list_all_labels() + + for item in labels_before: + _rv, _obj = this_gitlab.delete_label(item) + if _rv: + return_value['removed'].append(item) + return_obj['removed'].append(_obj) + + elif state == 'absent': + if not purge: + _label_names_requested = [x['name'] for x in requested_labels] + remove_requested = [x for x in labels_before if x['name'] in _label_names_requested] + for item in remove_requested: + _rv, _obj = this_gitlab.delete_label(item) + if _rv: + return_value['removed'].append(item) + return_obj['removed'].append(_obj) + else: + for item in labels_before: + _rv, _obj = this_gitlab.delete_label(item) + if _rv: + return_value['removed'].append(item) + return_obj['removed'].append(_obj) + + if module.check_mode: + _untouched, _updated, _added = compare(requested_labels, labels_before, state) + return_value = dict(added=_added, updated=_updated, removed=return_value['removed'], untouched=_untouched) + + if any(return_value[x] for x in ['added', 'removed', 'updated']): + change = True + + labels_after = [x.asdict() for x in this_gitlab.list_all_labels()] + + return change, return_value, labels_before, labels_after, return_obj + + +def main(): + argument_spec = basic_auth_argument_spec() + argument_spec.update(auth_argument_spec()) + argument_spec.update( + project=dict(type='str', required=False, default=None), + group=dict(type='str', required=False, default=None), + purge=dict(type='bool', required=False, default=False), + labels=dict(type='list', elements='dict', required=False, default=list(), + options=dict( + name=dict(type='str', required=True), + color=dict(type='str', required=False), + description=dict(type='str', required=False), + priority=dict(type='int', required=False), + new_name=dict(type='str', required=False),) + ), + state=dict(type='str', default="present", choices=["absent", "present"]), + ) + + module = AnsibleModule( + argument_spec=argument_spec, + mutually_exclusive=[ + ['api_username', 'api_token'], + ['api_username', 'api_oauth_token'], + ['api_username', 'api_job_token'], + ['api_token', 'api_oauth_token'], + ['api_token', 'api_job_token'], + ['project', 'group'], + ], + required_together=[ + ['api_username', 'api_password'], + ], + required_one_of=[ + ['api_username', 'api_token', 'api_oauth_token', 'api_job_token'], + ['project', 'group'] + ], + supports_check_mode=True + ) + ensure_gitlab_package(module) + + gitlab_project = module.params['project'] + gitlab_group = module.params['group'] + purge = module.params['purge'] + label_list = module.params['labels'] + state = module.params['state'] + + gitlab_version = gitlab.__version__ + _min_gitlab = '3.2.0' + if LooseVersion(gitlab_version) < LooseVersion(_min_gitlab): + module.fail_json(msg="community.general.gitlab_label requires python-gitlab Python module >= %s " + "(installed version: [%s]). Please upgrade " + "python-gitlab to version %s or above." % (_min_gitlab, gitlab_version, _min_gitlab)) + + gitlab_instance = gitlab_authentication(module) + + # find_project can return None, but the other must exist + gitlab_project_id = find_project(gitlab_instance, gitlab_project) + + # find_group can return None, but the other must exist + gitlab_group_id = find_group(gitlab_instance, gitlab_group) + + # if both not found, module must exist + if not gitlab_project_id and not gitlab_group_id: + if gitlab_project and not gitlab_project_id: + module.fail_json(msg="project '%s' not found." % gitlab_project) + if gitlab_group and not gitlab_group_id: + module.fail_json(msg="group '%s' not found." % gitlab_group) + + this_gitlab = GitlabLabels(module=module, gitlab_instance=gitlab_instance, group_id=gitlab_group_id, + project_id=gitlab_project_id) + + if state == 'present': + _existing_labels = [x.asdict()['name'] for x in this_gitlab.list_all_labels()] + + # color is mandatory when creating label, but it's optional when changing name or updating other fields + if any(x['color'] is None and x['new_name'] is None and x['name'] not in _existing_labels for x in label_list): + module.fail_json(msg='color parameter is required for new labels') + + change, raw_return_value, before, after, _obj = native_python_main(this_gitlab, purge, label_list, state, module) + + if not module.check_mode: + raw_return_value['untouched'] = [x for x in before if x in after] + + added = [x.get('name') for x in raw_return_value['added']] + updated = [x.get('name') for x in raw_return_value['updated']] + removed = [x.get('name') for x in raw_return_value['removed']] + untouched = [x.get('name') for x in raw_return_value['untouched']] + return_value = dict(added=added, updated=updated, removed=removed, untouched=untouched) + + module.exit_json(changed=change, labels=return_value, labels_obj=_obj) + + +if __name__ == '__main__': + main() diff --git a/tests/integration/targets/gitlab_label/README.md b/tests/integration/targets/gitlab_label/README.md new file mode 100644 index 0000000000..e27cb74c8c --- /dev/null +++ b/tests/integration/targets/gitlab_label/README.md @@ -0,0 +1,19 @@ + + +# Gitlab integration tests + +1. to run integration tests locally, I've setup a podman pod with both gitlab-ee image and the testing image +2. gitlab's related information were taken from [here](https://docs.gitlab.com/ee/install/docker.html), about the variable it needs (hostname, ports, volumes); volumes were pre-made before launching the image +3. image that run integration tests is started with `podman run --rm -it --pod --name --network=host --volume /ansible_community/community.general:/workspace/ansible_collections/community/general quay.io/ansible/azure-pipelines-test-container:4.0.1` +4. into the testing image, run +```sh +pip install https://github.com/ansible/ansible/archive/devel.tar.gz --disable-pip-version-check +cd /workspace/ansible_collections/community/general +ansible-test integration gitlab_label -vvv +``` + +While debugging with `q` package, open a second terminal and run `podman exec -it /bin/bash` and inside it do `tail -f /tmp/q` . diff --git a/tests/integration/targets/gitlab_label/aliases b/tests/integration/targets/gitlab_label/aliases new file mode 100644 index 0000000000..4f4e3dcc93 --- /dev/null +++ b/tests/integration/targets/gitlab_label/aliases @@ -0,0 +1,6 @@ +# Copyright (c) Ansible Project +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +gitlab/ci +disabled diff --git a/tests/integration/targets/gitlab_label/defaults/main.yml b/tests/integration/targets/gitlab_label/defaults/main.yml new file mode 100644 index 0000000000..315cbd77f9 --- /dev/null +++ b/tests/integration/targets/gitlab_label/defaults/main.yml @@ -0,0 +1,17 @@ +--- +# Copyright (c) Ansible Project +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +gitlab_project_name: ansible_test_project +gitlab_host: ansible_test_host +gitlab_api_token: ansible_test_api_token +gitlab_project_group: ansible_test_group +gitlab_branch: ansible_test_branch +gitlab_first_label: ansible_test_label +gitlab_first_label_color: "#112233" +gitlab_first_label_description: "label description" +gitlab_first_label_priority: 10 +gitlab_second_label: ansible_test_second_label +gitlab_second_label_color: "#445566" +gitlab_second_label_new_name: ansible_test_second_label_new_name \ No newline at end of file diff --git a/tests/integration/targets/gitlab_label/tasks/main.yml b/tests/integration/targets/gitlab_label/tasks/main.yml new file mode 100644 index 0000000000..880b72cebf --- /dev/null +++ b/tests/integration/targets/gitlab_label/tasks/main.yml @@ -0,0 +1,463 @@ +--- +#################################################################### +# WARNING: These are designed specifically for Ansible tests # +# and should not be used as examples of how to write Ansible roles # +#################################################################### + +# Copyright (c) Ansible Project +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +- name: Install required libs + pip: + name: python-gitlab + state: present + +- block: +### +### Group label +### + + - name: Create {{ gitlab_project_group }} + gitlab_group: + api_url: "{{ gitlab_host }}" + validate_certs: true + api_token: "{{ gitlab_api_token }}" + name: "{{ gitlab_project_group }}" + state: present + + - name: Purge all group labels for check_mode test + gitlab_label: + api_url: "{{ gitlab_host }}" + api_token: "{{ gitlab_api_token }}" + group: "{{ gitlab_project_group }}" + purge: true + + - name: Group label - Add a label in check_mode + gitlab_label: + api_token: "{{ gitlab_api_token }}" + api_url: "{{ gitlab_host }}" + group: "{{ gitlab_project_group }}" + labels: + - name: "{{ gitlab_second_label }}" + color: "{{ gitlab_second_label_color }}" + check_mode: true + register: gitlab_group_label_state + + - name: Group label - Check_mode state must be changed + assert: + that: + - gitlab_group_label_state is changed + + - name: Group label - Create label {{ gitlab_first_label }} and {{ gitlab_second_label }} + gitlab_label: + api_token: "{{ gitlab_api_token }}" + api_url: "{{ gitlab_host }}" + group: "{{ gitlab_project_group }}" + labels: + - name: "{{ gitlab_first_label }}" + color: "{{ gitlab_first_label_color }}" + description: "{{ gitlab_first_label_description }}" + priority: "{{ gitlab_first_label_priority }}" + - name: "{{ gitlab_second_label }}" + color: "{{ gitlab_second_label_color }}" + state: present + register: gitlab_group_label_create + + - name: Group label - Test Label Created + assert: + that: + - gitlab_group_label_create is changed + - gitlab_group_label_create.labels.added|length == 2 + - gitlab_group_label_create.labels.untouched|length == 0 + - gitlab_group_label_create.labels.removed|length == 0 + - gitlab_group_label_create.labels.updated|length == 0 + - gitlab_group_label_create.labels.added[0] == "{{ gitlab_first_label }}" + - gitlab_group_label_create.labels.added[1] == "{{ gitlab_second_label }}" + + - name: Group label - Create Label ( Idempotency test ) + gitlab_label: + api_token: "{{ gitlab_api_token }}" + api_url: "{{ gitlab_host }}" + group: "{{ gitlab_project_group }}" + labels: + - name: "{{ gitlab_first_label }}" + color: "{{ gitlab_first_label_color }}" + description: "{{ gitlab_first_label_description }}" + priority: "{{ gitlab_first_label_priority }}" + state: present + register: gitlab_group_label_create_idempotence + + - name: Group label - Test Create Label is Idempotent + assert: + that: + - gitlab_group_label_create_idempotence is not changed + + - name: Group label - Update Label {{ gitlab_first_label }} changing color + gitlab_label: + api_token: "{{ gitlab_api_token }}" + api_url: "{{ gitlab_host }}" + group: "{{ gitlab_project_group }}" + labels: + - name: "{{ gitlab_first_label }}" + color: "{{ gitlab_second_label_color }}" + state: present + register: gitlab_group_label_update + + - name: Group label - Test Label Updated + assert: + that: + - gitlab_group_label_update.labels.added|length == 0 + - gitlab_group_label_update.labels.untouched|length == 0 + - gitlab_group_label_update.labels.removed|length == 0 + - gitlab_group_label_update.labels.updated|length == 1 + - gitlab_group_label_update.labels.updated[0] == "{{ gitlab_first_label }}" + + - name: Group label - Change label {{ gitlab_second_label }} name to {{ gitlab_second_label_new_name }} + gitlab_label: + api_token: "{{ gitlab_api_token }}" + api_url: "{{ gitlab_host }}" + group: "{{ gitlab_project_group }}" + labels: + - name: "{{ gitlab_second_label }}" + new_name: "{{ gitlab_second_label_new_name }}" + state: present + register: gitlab_group_label_new_name + + - name: Group label - Test Label name changed + assert: + that: + - gitlab_group_label_new_name.labels.added|length == 0 + - gitlab_group_label_new_name.labels.untouched|length == 0 + - gitlab_group_label_new_name.labels.removed|length == 0 + - gitlab_group_label_new_name.labels.updated|length == 1 + - gitlab_group_label_new_name.labels.updated[0] == "{{ gitlab_second_label }}" + + - name: Group label - Change label name back to {{ gitlab_second_label }} + gitlab_label: + api_token: "{{ gitlab_api_token }}" + api_url: "{{ gitlab_host }}" + group: "{{ gitlab_project_group }}" + labels: + - name: "{{ gitlab_second_label_new_name }}" + new_name: "{{ gitlab_second_label }}" + state: present + register: gitlab_group_label_orig_name + + - name: Group label - Test Label name changed back + assert: + that: + - gitlab_group_label_orig_name.labels.added|length == 0 + - gitlab_group_label_orig_name.labels.untouched|length == 0 + - gitlab_group_label_orig_name.labels.removed|length == 0 + - gitlab_group_label_orig_name.labels.updated|length == 1 + - gitlab_group_label_orig_name.labels.updated[0] == "{{ gitlab_second_label_new_name }}" + + - name: Group label - Update Label Test ( Additions ) + gitlab_label: + api_token: "{{ gitlab_api_token }}" + api_url: "{{ gitlab_host }}" + group: "{{ gitlab_project_group }}" + labels: + - name: "{{ gitlab_second_label }}" + description: "{{ gitlab_first_label_description }}" + priority: "{{ gitlab_first_label_priority }}" + state: present + register: gitlab_group_label_update_additions + + - name: Group label - Test Label Updated ( Additions ) + assert: + that: + - gitlab_group_label_update_additions.labels.added|length == 0 + - gitlab_group_label_update_additions.labels.untouched|length == 0 + - gitlab_group_label_update_additions.labels.removed|length == 0 + - gitlab_group_label_update_additions.labels.updated|length == 1 + - gitlab_group_label_update_additions.labels.updated[0] == "{{ gitlab_second_label }}" + + - name: Group label - Delete Label {{ gitlab_second_label }} + gitlab_label: + api_token: "{{ gitlab_api_token }}" + api_url: "{{ gitlab_host }}" + group: "{{ gitlab_project_group }}" + labels: + - name: "{{ gitlab_second_label }}" + state: absent + register: gitlab_group_label_delete + + - name: Group label - Test label is deleted + assert: + that: + - gitlab_group_label_delete is changed + - gitlab_group_label_delete.labels.added|length == 0 + - gitlab_group_label_delete.labels.untouched|length == 0 + - gitlab_group_label_delete.labels.removed|length == 1 + - gitlab_group_label_delete.labels.updated|length == 0 + - gitlab_group_label_delete.labels.removed[0] == "{{ gitlab_second_label }}" + + - name: Group label - Create label {{ gitlab_second_label }} again purging the other + gitlab_label: + api_token: "{{ gitlab_api_token }}" + api_url: "{{ gitlab_host }}" + group: "{{ gitlab_project_group }}" + purge: true + labels: + - name: "{{ gitlab_second_label }}" + color: "{{ gitlab_second_label_color }}" + state: present + register: gitlab_group_label_create_purging + + - name: Group label - Test Label Created again + assert: + that: + - gitlab_group_label_create_purging is changed + - gitlab_group_label_create_purging.labels.added|length == 1 + - gitlab_group_label_create_purging.labels.untouched|length == 0 + - gitlab_group_label_create_purging.labels.removed|length == 1 + - gitlab_group_label_create_purging.labels.updated|length == 0 + - gitlab_group_label_create_purging.labels.added[0] == "{{ gitlab_second_label }}" + - gitlab_group_label_create_purging.labels.removed[0] == "{{ gitlab_first_label }}" + +### +### Project label +### + + - name: Create {{ gitlab_project_name }} + gitlab_project: + api_url: "{{ gitlab_host }}" + validate_certs: true + api_token: "{{ gitlab_api_token }}" + name: "{{ gitlab_project_name }}" + group: "{{ gitlab_project_group }}" + default_branch: "{{ gitlab_branch }}" + initialize_with_readme: true + state: present + + - name: Purge all labels for check_mode test + gitlab_label: + api_url: "{{ gitlab_host }}" + api_token: "{{ gitlab_api_token }}" + project: "{{ gitlab_project_group }}/{{ gitlab_project_name }}" + purge: true + + - name: Add a label in check_mode + gitlab_label: + api_token: "{{ gitlab_api_token }}" + api_url: "{{ gitlab_host }}" + project: "{{ gitlab_project_group }}/{{ gitlab_project_name }}" + labels: + - name: "{{ gitlab_second_label }}" + color: "{{ gitlab_second_label_color }}" + check_mode: true + register: gitlab_first_label_state + + - name: Check_mode state must be changed + assert: + that: + - gitlab_first_label_state is changed + + - name: Create label {{ gitlab_first_label }} and {{ gitlab_second_label }} + gitlab_label: + api_token: "{{ gitlab_api_token }}" + api_url: "{{ gitlab_host }}" + project: "{{ gitlab_project_group }}/{{ gitlab_project_name }}" + labels: + - name: "{{ gitlab_first_label }}" + color: "{{ gitlab_first_label_color }}" + description: "{{ gitlab_first_label_description }}" + priority: "{{ gitlab_first_label_priority }}" + - name: "{{ gitlab_second_label }}" + color: "{{ gitlab_second_label_color }}" + state: present + register: gitlab_first_label_create + + - name: Test Label Created + assert: + that: + - gitlab_first_label_create is changed + - gitlab_first_label_create.labels.added|length == 2 + - gitlab_first_label_create.labels.untouched|length == 0 + - gitlab_first_label_create.labels.removed|length == 0 + - gitlab_first_label_create.labels.updated|length == 0 + - gitlab_first_label_create.labels.added[0] == "{{ gitlab_first_label }}" + - gitlab_first_label_create.labels.added[1] == "{{ gitlab_second_label }}" + + - name: Create Label ( Idempotency test ) + gitlab_label: + api_token: "{{ gitlab_api_token }}" + api_url: "{{ gitlab_host }}" + project: "{{ gitlab_project_group }}/{{ gitlab_project_name }}" + labels: + - name: "{{ gitlab_first_label }}" + color: "{{ gitlab_first_label_color }}" + description: "{{ gitlab_first_label_description }}" + priority: "{{ gitlab_first_label_priority }}" + state: present + register: gitlab_first_label_create_idempotence + + - name: Test Create Label is Idempotent + assert: + that: + - gitlab_first_label_create_idempotence is not changed + + - name: Update Label {{ gitlab_first_label }} changing color + gitlab_label: + api_token: "{{ gitlab_api_token }}" + api_url: "{{ gitlab_host }}" + project: "{{ gitlab_project_group }}/{{ gitlab_project_name }}" + labels: + - name: "{{ gitlab_first_label }}" + color: "{{ gitlab_second_label_color }}" + state: present + register: gitlab_first_label_update + + - name: Test Label Updated + assert: + that: + - gitlab_first_label_update.labels.added|length == 0 + - gitlab_first_label_update.labels.untouched|length == 0 + - gitlab_first_label_update.labels.removed|length == 0 + - gitlab_first_label_update.labels.updated|length == 1 + - gitlab_first_label_update.labels.updated[0] == "{{ gitlab_first_label }}" + + - name: Change label {{ gitlab_second_label }} name to {{ gitlab_second_label_new_name }} + gitlab_label: + api_token: "{{ gitlab_api_token }}" + api_url: "{{ gitlab_host }}" + project: "{{ gitlab_project_group }}/{{ gitlab_project_name }}" + labels: + - name: "{{ gitlab_second_label }}" + new_name: "{{ gitlab_second_label_new_name }}" + state: present + register: gitlab_first_label_new_name + + - name: Test Label name changed + assert: + that: + - gitlab_first_label_new_name.labels.added|length == 0 + - gitlab_first_label_new_name.labels.untouched|length == 0 + - gitlab_first_label_new_name.labels.removed|length == 0 + - gitlab_first_label_new_name.labels.updated|length == 1 + - gitlab_first_label_new_name.labels.updated[0] == "{{ gitlab_second_label }}" + + - name: Change label name back to {{ gitlab_second_label }} + gitlab_label: + api_token: "{{ gitlab_api_token }}" + api_url: "{{ gitlab_host }}" + project: "{{ gitlab_project_group }}/{{ gitlab_project_name }}" + labels: + - name: "{{ gitlab_second_label_new_name }}" + new_name: "{{ gitlab_second_label }}" + state: present + register: gitlab_first_label_orig_name + + - name: Test Label name changed back + assert: + that: + - gitlab_first_label_orig_name.labels.added|length == 0 + - gitlab_first_label_orig_name.labels.untouched|length == 0 + - gitlab_first_label_orig_name.labels.removed|length == 0 + - gitlab_first_label_orig_name.labels.updated|length == 1 + - gitlab_first_label_orig_name.labels.updated[0] == "{{ gitlab_second_label_new_name }}" + + - name: Update Label Test ( Additions ) + gitlab_label: + api_token: "{{ gitlab_api_token }}" + api_url: "{{ gitlab_host }}" + project: "{{ gitlab_project_group }}/{{ gitlab_project_name }}" + labels: + - name: "{{ gitlab_second_label }}" + description: "{{ gitlab_first_label_description }}" + priority: "{{ gitlab_first_label_priority }}" + state: present + register: gitlab_first_label_update_additions + + - name: Test Label Updated ( Additions ) + assert: + that: + - gitlab_first_label_update_additions.labels.added|length == 0 + - gitlab_first_label_update_additions.labels.untouched|length == 0 + - gitlab_first_label_update_additions.labels.removed|length == 0 + - gitlab_first_label_update_additions.labels.updated|length == 1 + - gitlab_first_label_update_additions.labels.updated[0] == "{{ gitlab_second_label }}" + + - name: Delete Label {{ gitlab_second_label }} + gitlab_label: + api_token: "{{ gitlab_api_token }}" + api_url: "{{ gitlab_host }}" + project: "{{ gitlab_project_group }}/{{ gitlab_project_name }}" + labels: + - name: "{{ gitlab_second_label }}" + state: absent + register: gitlab_first_label_delete + + - name: Test label is deleted + assert: + that: + - gitlab_first_label_delete is changed + - gitlab_first_label_delete.labels.added|length == 0 + - gitlab_first_label_delete.labels.untouched|length == 0 + - gitlab_first_label_delete.labels.removed|length == 1 + - gitlab_first_label_delete.labels.updated|length == 0 + - gitlab_first_label_delete.labels.removed[0] == "{{ gitlab_second_label }}" + + - name: Create label {{ gitlab_second_label }} again purging the other + gitlab_label: + api_token: "{{ gitlab_api_token }}" + api_url: "{{ gitlab_host }}" + project: "{{ gitlab_project_group }}/{{ gitlab_project_name }}" + purge: true + labels: + - name: "{{ gitlab_second_label }}" + color: "{{ gitlab_second_label_color }}" + state: present + register: gitlab_first_label_create_purging + + - name: Test Label Created again + assert: + that: + - gitlab_first_label_create_purging is changed + - gitlab_first_label_create_purging.labels.added|length == 1 + - gitlab_first_label_create_purging.labels.untouched|length == 0 + - gitlab_first_label_create_purging.labels.removed|length == 1 + - gitlab_first_label_create_purging.labels.updated|length == 0 + - gitlab_first_label_create_purging.labels.added[0] == "{{ gitlab_second_label }}" + - gitlab_first_label_create_purging.labels.removed[0] == "{{ gitlab_first_label }}" + + always: + - name: Delete Labels + gitlab_label: + api_token: "{{ gitlab_api_token }}" + api_url: "{{ gitlab_host }}" + project: "{{ gitlab_project_group }}/{{ gitlab_project_name }}" + purge: true + labels: + - name: "{{ gitlab_first_label }}" + - name: "{{ gitlab_second_label }}" + state: absent + register: gitlab_first_label_always_delete + + - name: Test label are deleted + assert: + that: + - gitlab_first_label_always_delete is changed + - gitlab_first_label_always_delete.labels.added|length == 0 + - gitlab_first_label_always_delete.labels.untouched|length == 0 + - gitlab_first_label_always_delete.labels.removed|length > 0 + - gitlab_first_label_always_delete.labels.updated|length == 0 + + - name: Clean up {{ gitlab_project_name }} + gitlab_project: + api_url: "{{ gitlab_host }}" + validate_certs: false + api_token: "{{ gitlab_api_token }}" + name: "{{ gitlab_project_name }}" + group: "{{ gitlab_project_group }}" + state: absent + + - name: Clean up {{ gitlab_project_group }} + gitlab_group: + api_url: "{{ gitlab_host }}" + validate_certs: true + api_token: "{{ gitlab_api_token }}" + name: "{{ gitlab_project_group }}" + state: absent \ No newline at end of file