Commit 271c7c8f authored by Adrian DC's avatar Adrian DC
Browse files

feat(cli): implement '.python-gitlab.cfg' GitLab configurations files

Signed-off-by: default avatarAdrian DC <radian.dc@gmail.com>
parent 43621013
Loading
Loading
Loading
Loading
Loading
+1 −0
Changes for .gitlab-ci.yml: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -283,6 +283,7 @@ readme:
          | sed '1d; $d' \
          | sed "s/^\(  ${parameter} \[${word}\]${alignments}\)/\1# /g" \
          | sed "s/^\(  ${parameter}\( ${word}\)\{1,\}${alignments}\)/\1# /g" \
          | sed "s/^\(  ${parameter}\( ${word}\)\{1,\}, ${parameter}\( ${word}\)\{1,\}${alignments}\)/\1# /g" \
          | sed "s/^\(  ${parameter}${alignments}\)/\1# /g" \
          | sed "s/^\(  ${parameter}, ${parameter}${alignments}\)/\1# /g" \
          | sed "s/^\(  ${separator}${alignments}\)/\1# /g" \
+6 −3
Changes for README.md: 6 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -50,9 +50,9 @@ gitlab-projects-settings --reset-features 'https://gitlab.com/group/project'
<!-- readme-help-start -->

```yaml
usage: gitlab-projects-settings [-h] [--version] [--update-check] [--settings] [--set GROUP KEY VAL] [--confirm]
                                [--dry-run] [--dump] [--exclude-group] [--exclude-subgroups] [--exclude-projects]
                                [--available-features] [--reset-features [KEEP_FEATURES]]
usage: gitlab-projects-settings [-h] [--version] [--update-check] [--settings] [--set GROUP KEY VAL] [-c FILES]
                                [--confirm] [--dry-run] [--dump] [--exclude-group] [--exclude-subgroups]
                                [--exclude-projects] [--available-features] [--reset-features [KEEP_FEATURES]]
                                [--disable-features FEATURES] [--enable-features FEATURES] [--reset-members]
                                [--set-avatar FILE] [--set-description TEXT] [--update-description]
                                [--run-housekeeping] [--prune-unreachable-objects]
@@ -73,6 +73,9 @@ internal arguments:
  --set GROUP KEY VAL               # Set settings specific 'VAL' value to [GROUP] > KEY
                                    # or unset by using 'UNSET' as 'VAL'

credentials arguments:
  -c FILES, --config FILES          # Python GitLab configuration files (default: PYTHON_GITLAB_CFG environment)

common settings arguments:
  --confirm                         # Automatically confirm all removal and contents warnings
  --dry-run                         # Enable dry run mode to check without saving
+25 −5
Changes for src/cli/entrypoint.py: 25 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ from typing import Dict, List, Optional, Union
import urllib.parse

# Modules libraries
from gitlab.config import ConfigError, GitlabConfigParser
from gitlab.exceptions import GitlabGetError
from gitlab.v4.objects import (
    Group as GitLabGroup,
@@ -31,7 +32,7 @@ class Entrypoint:
    # Enumerations
    Result = Enum('Result', ['SUCCESS', 'FINALIZE', 'ERROR', 'CRITICAL'])

    # CLI, pylint: disable=too-many-branches,too-many-statements
    # CLI, pylint: disable=too-many-branches,too-many-locals,too-many-statements
    @staticmethod
    def cli(options: Namespace, environments: Environments) -> Result:

@@ -46,15 +47,34 @@ class Entrypoint:

        # Parse URL variables
        gitlab_splits: urllib.parse.SplitResult = urllib.parse.urlsplit(options.url_path)
        gitlab_id: str = f'{gitlab_splits.netloc}'
        gitlab_url: str = f'{gitlab_splits.scheme}://{gitlab_splits.netloc}'
        gitlab_path: str = gitlab_splits.path.lstrip('/')

        # Prepare credentials
        private_token: str = environments.value('gitlab_token')
        job_token: str = environments.value('ci_job_token')
        ssl_verify: Union[bool, str] = True

        # Parse configuration files
        try:
            config: GitlabConfigParser
            if not private_token:
                config = GitlabConfigParser(gitlab_id, options.configs)
                private_token = str(config.private_token)
                if ssl_verify and (not config.ssl_verify
                                   or isinstance(config.ssl_verify, str)):
                    ssl_verify = config.ssl_verify
        except ConfigError as e:
            print(str(e))

        # GitLab client
        gitlab = GitLabFeature(
            gitlab_url,
            environments.value('gitlab_token'),
            environments.value('ci_job_token'),
            options.dry_run,
            url=gitlab_url,
            private_token=private_token,
            job_token=job_token,
            ssl_verify=ssl_verify,
            dry_run=options.dry_run,
        )
        print(f'{Colors.BOLD} - GitLab host: '
              f'{Colors.GREEN}{gitlab.url}'
+12 −0
Changes for src/cli/main.py: 12 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -96,6 +96,18 @@ def main() -> None:
             'or unset by using \'UNSET\' as \'VAL\'',
    )

    # Arguments credentials definitions
    group = parser.add_argument_group('credentials arguments')
    group.add_argument(
        '-c',
        '--config',
        dest='configs',
        action='append',
        metavar='FILES',
        help=f'Python GitLab configuration files'
        f' (default: {Bundle.ENV_PYTHON_GITLAB_CFG} environment)',
    )

    # Arguments common settings definitions
    group = parser.add_argument_group('common settings arguments')
    group.add_argument(
+20 −5
Changes for src/features/gitlab.py: 20 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -35,12 +35,13 @@ class GitLabFeature:
    __dry_run: bool = False
    __gitlab: Gitlab

    # Constructor
    # Constructor, pylint: disable=too-many-arguments
    def __init__(
        self,
        url: str,
        private_token: str,
        job_token: str,
        ssl_verify: Union[bool, str] = True,
        dry_run: bool = False,
    ) -> None:

@@ -49,12 +50,26 @@ class GitLabFeature:

        # Create GitLab client
        if private_token:
            self.__gitlab = Gitlab(url, private_token=private_token)
            self.__gitlab.auth()
            self.__gitlab = Gitlab(
                url=url,
                private_token=private_token,
                ssl_verify=ssl_verify,
            )
        elif job_token:
            self.__gitlab = Gitlab(url, job_token=job_token)
            self.__gitlab = Gitlab(
                url=url,
                job_token=job_token,
                ssl_verify=ssl_verify,
            )
        else:
            self.__gitlab = Gitlab(url)
            self.__gitlab = Gitlab(
                url=url,
                ssl_verify=ssl_verify,
            )

        # Authenticate if available
        if self.__gitlab.private_token or self.__gitlab.oauth_token:
            self.__gitlab.auth()

    # Group
    def group(self, criteria: str) -> Group:
Loading