Commit 24305b5a authored by Adrian DC's avatar Adrian DC
Browse files

feat(cli, gitlab): implement '--{get,set}-group-labels' with JSON

Signed-off-by: default avatarAdrian DC <radian.dc@gmail.com>
parent f1bf888e
Loading
Loading
Loading
Loading
Loading
+3 −0
Changes for README.md: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -59,6 +59,7 @@ usage: gitlab-projects-settings [-h] [--version] [--update-check] [--settings] [
                                [--archive-project | --unarchive-project] [--delete-group] [--delete-project]
                                [--protect-branches] [--protect-tags [LEVEL]]
                                [--erase-jobs-artifacts | --erase-jobs-contents]
                                [--get-group-labels | --set-group-labels JSON]
                                [--get-project-labels | --set-project-labels JSON] [--]
                                [url_path]

@@ -108,6 +109,8 @@ ci/cd settings arguments:
  --erase-jobs-contents             # Erase all CI/CD jobs artifacts and traces

labels arguments:
  --get-group-labels                # Get the GitLab group labels in JSON format
  --set-group-labels JSON           # Set the GitLab group labels from JSON format
  --get-project-labels              # Get the GitLab project labels in JSON format
  --set-project-labels JSON         # Set the GitLab project labels from JSON format

+37 −1
Changes for src/cli/entrypoint.py: 37 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -221,7 +221,7 @@ class Entrypoint:
        # Result
        return answer

    # Group
    # Group, pylint: disable=too-many-locals
    @staticmethod
    def group(
        options: Namespace,
@@ -323,6 +323,42 @@ class Entrypoint:
                  f'{Colors.RESET}')
            Platform.flush()

        # Get group labels
        if options.get_group_labels:
            labels_objects = gitlab.group_get_labels(criteria)
            if labels_objects:
                print(f'{Colors.BOLD}   - Get labels to JSON: '
                      f'{Colors.RESET}', end='')
                labels_list: List[Dict[str, str]] = []
                for label in labels_objects:
                    labels_list += [{
                        'name': label.name,
                        'description': label.description,
                        'text_color': label.text_color,
                        'color': label.color,
                    }]
                print(json.dumps(labels_list))
                Platform.flush()

        # Set group labels
        elif options.set_group_labels:
            print(f'{Colors.BOLD}   - Set labels from JSON: '
                  f'{Colors.RESET}', end='')
            labels_names: List[str] = []
            labels_json = json.loads(options.set_group_labels)
            for label_json in labels_json:
                label_set = gitlab.group_set_label(
                    criteria,
                    label_json['name'],
                    label_json['description'],
                    label_json['text_color'],
                    label_json['color'],
                )
                labels_names += [label_set.name]
            print(f'{Colors.CYAN}{", ".join(labels_names)}'
                  f'{Colors.RESET}')
            Platform.flush()

        # Dump group object
        if options.dump:
            print(' ')
+13 −0
Changes for src/cli/main.py: 13 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -270,6 +270,19 @@ def main() -> None:
    # Arguments labels definitions
    group = parser.add_argument_group('labels arguments')
    subgroup = group.add_mutually_exclusive_group()
    subgroup.add_argument(
        '--get-group-labels',
        dest='get_group_labels',
        action='store_true',
        help='Get the GitLab group labels in JSON format',
    )
    subgroup.add_argument(
        '--set-group-labels',
        dest='set_group_labels',
        metavar='JSON',
        help='Set the GitLab group labels from JSON format',
    )
    subgroup = group.add_mutually_exclusive_group()
    subgroup.add_argument(
        '--get-project-labels',
        dest='get_project_labels',
+41 −0
Changes for src/features/gitlab.py: 41 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ from gitlab.exceptions import (
)
from gitlab.v4.objects import (
    Group,
    GroupLabel,
    Namespace,
    Project,
    ProjectLabel,
@@ -78,6 +79,13 @@ class GitLabFeature:
        # Delay for deletion
        sleep(3)

    # Group get labels
    def group_get_labels(self, criteria: str) -> RESTObjectList | List[RESTObject]:

        # Get group labels
        group = self.group(criteria)
        return group.labels.list(get_all=True)

    # Group reset members
    def group_reset_members(self, criteria: str) -> None:

@@ -114,6 +122,39 @@ class GitLabFeature:
            # Save group
            group.save()

    # Group set label, pylint: disable=too-many-arguments
    def group_set_label(
        self,
        criteria: str,
        name: str,
        description: str,
        text_color: str,
        color: str,
    ) -> GroupLabel:

        # Create group label
        group = self.group(criteria)
        label: GroupLabel
        try:
            label = group.labels.get(name, include_ancestor_groups=False)
        except GitlabGetError:
            group.labels.create({
                'name': name,
                'description': description,
                'text_color': text_color,
                'color': color,
            })
            label = group.labels.get(name, include_ancestor_groups=False)

        # Update group label
        label.description = description
        label.text_color = text_color
        label.color = color
        label.save()

        # Result
        return label

    # Namespace
    def namespace(self, criteria: str) -> Namespace:
        return self.__gitlab.namespaces.get(criteria)