diff options
author | Nejc Habjan <nejc.habjan@siemens.com> | 2022-07-09 13:56:33 +0200 |
---|---|---|
committer | John Villalovos <john@sodarock.com> | 2022-07-09 08:22:20 -0700 |
commit | 3b1ede4a27cd730982d4c579437c5c689a8799e5 (patch) | |
tree | 62889d73ce7a6523ff8036a6006111695fbc2894 /gitlab | |
parent | 0daec5fa1428a56a6a927b133613e8b296248167 (diff) | |
download | gitlab-3b1ede4a27cd730982d4c579437c5c689a8799e5.tar.gz |
feat: support validating CI lint results
Diffstat (limited to 'gitlab')
-rw-r--r-- | gitlab/exceptions.py | 4 | ||||
-rw-r--r-- | gitlab/v4/cli.py | 11 | ||||
-rw-r--r-- | gitlab/v4/objects/ci_lint.py | 34 |
3 files changed, 48 insertions, 1 deletions
diff --git a/gitlab/exceptions.py b/gitlab/exceptions.py index 8465838..4a2f1dc 100644 --- a/gitlab/exceptions.py +++ b/gitlab/exceptions.py @@ -62,6 +62,10 @@ class GitlabParsingError(GitlabError): pass +class GitlabCiLintError(GitlabError): + pass + + class GitlabConnectionError(GitlabError): pass diff --git a/gitlab/v4/cli.py b/gitlab/v4/cli.py index d903527..b496669 100644 --- a/gitlab/v4/cli.py +++ b/gitlab/v4/cli.py @@ -25,6 +25,7 @@ import gitlab import gitlab.base import gitlab.v4.objects from gitlab import cli +from gitlab.exceptions import GitlabCiLintError class GitlabCLI: @@ -133,6 +134,16 @@ class GitlabCLI: except Exception as e: # pragma: no cover, cli.die is unit-tested cli.die("Impossible to download the export", e) + def do_validate(self) -> None: + if TYPE_CHECKING: + assert isinstance(self.mgr, gitlab.v4.objects.CiLintManager) + try: + self.mgr.validate(self.args) + except GitlabCiLintError as e: # pragma: no cover, cli.die is unit-tested + cli.die("CI YAML Lint failed", e) + except Exception as e: # pragma: no cover, cli.die is unit-tested + cli.die("Cannot validate CI YAML", e) + def do_create(self) -> gitlab.base.RESTObject: if TYPE_CHECKING: assert isinstance(self.mgr, gitlab.mixins.CreateMixin) diff --git a/gitlab/v4/objects/ci_lint.py b/gitlab/v4/objects/ci_lint.py index 73f9d4d..e6b459c 100644 --- a/gitlab/v4/objects/ci_lint.py +++ b/gitlab/v4/objects/ci_lint.py @@ -6,6 +6,8 @@ https://docs.gitlab.com/ee/api/lint.html from typing import Any, cast from gitlab.base import RESTManager, RESTObject +from gitlab.cli import register_custom_action +from gitlab.exceptions import GitlabCiLintError from gitlab.mixins import CreateMixin, GetWithoutIdMixin from gitlab.types import RequiredOptional @@ -28,9 +30,24 @@ class CiLintManager(CreateMixin, RESTManager): required=("content",), optional=("include_merged_yaml", "include_jobs") ) + @register_custom_action( + "CiLintManager", + ("content",), + optional=("include_merged_yaml", "include_jobs"), + ) + def validate(self, *args: Any, **kwargs: Any) -> None: + """Raise an error if the CI Lint results are not valid. + + This is a custom python-gitlab method to wrap lint endpoints.""" + result = self.create(*args, **kwargs) + + if result.status != "valid": + message = ",\n".join(result.errors) + raise GitlabCiLintError(message) + class ProjectCiLint(RESTObject): - pass + _id_attr = None class ProjectCiLintManager(GetWithoutIdMixin, CreateMixin, RESTManager): @@ -43,3 +60,18 @@ class ProjectCiLintManager(GetWithoutIdMixin, CreateMixin, RESTManager): def get(self, **kwargs: Any) -> ProjectCiLint: return cast(ProjectCiLint, super().get(**kwargs)) + + @register_custom_action( + "ProjectCiLintManager", + ("content",), + optional=("dry_run", "include_jobs", "ref"), + ) + def validate(self, *args: Any, **kwargs: Any) -> None: + """Raise an error if the Project CI Lint results are not valid. + + This is a custom python-gitlab method to wrap lint endpoints.""" + result = self.create(*args, **kwargs) + + if not result.valid: + message = ",\n".join(result.errors) + raise GitlabCiLintError(message) |