Skip to content

Commit 3f67c4b

Browse files
nejchJohnVillalovos
authored andcommitted
feat(cli): add support for global CI lint
1 parent 6e1342f commit 3f67c4b

File tree

4 files changed

+61
-15
lines changed

4 files changed

+61
-15
lines changed

gitlab/client.py

+7
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ def __init__(
126126

127127
self.broadcastmessages = objects.BroadcastMessageManager(self)
128128
"""See :class:`~gitlab.v4.objects.BroadcastMessageManager`"""
129+
self.ci_lint = objects.CiLintManager(self)
130+
"""See :class:`~gitlab.v4.objects.CiLintManager`"""
129131
self.deploykeys = objects.DeployKeyManager(self)
130132
"""See :class:`~gitlab.v4.objects.DeployKeyManager`"""
131133
self.deploytokens = objects.DeployTokenManager(self)
@@ -397,6 +399,11 @@ def lint(self, content: str, **kwargs: Any) -> Tuple[bool, List[str]]:
397399
Returns:
398400
(True, []) if the file is valid, (False, errors(list)) otherwise
399401
"""
402+
utils.warn(
403+
"`lint()` is deprecated and will be removed in a future version.\n"
404+
"Please use `ci_lint.create()` instead.",
405+
category=DeprecationWarning,
406+
)
400407
post_data = {"content": content}
401408
data = self.http_post("/ci/lint", post_data=post_data, **kwargs)
402409
if TYPE_CHECKING:

gitlab/v4/objects/ci_lint.py

+19
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,25 @@
99
from gitlab.mixins import CreateMixin, GetWithoutIdMixin
1010
from gitlab.types import RequiredOptional
1111

12+
__all__ = [
13+
"CiLint",
14+
"CiLintManager",
15+
"ProjectCiLint",
16+
"ProjectCiLintManager",
17+
]
18+
19+
20+
class CiLint(RESTObject):
21+
_id_attr = None
22+
23+
24+
class CiLintManager(CreateMixin, RESTManager):
25+
_path = "/ci/lint"
26+
_obj_cls = CiLint
27+
_create_attrs = RequiredOptional(
28+
required=("content",), optional=("include_merged_yaml", "include_jobs")
29+
)
30+
1231

1332
class ProjectCiLint(RESTObject):
1433
pass

gitlab/v4/objects/projects.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
from .badges import ProjectBadgeManager # noqa: F401
3939
from .boards import ProjectBoardManager # noqa: F401
4040
from .branches import ProjectBranchManager, ProjectProtectedBranchManager # noqa: F401
41-
from .ci_lint import ProjectCiLint, ProjectCiLintManager # noqa: F401
41+
from .ci_lint import ProjectCiLintManager # noqa: F401
4242
from .clusters import ProjectClusterManager # noqa: F401
4343
from .commits import ProjectCommitManager # noqa: F401
4444
from .container_registry import ProjectRegistryRepositoryManager # noqa: F401
@@ -102,8 +102,6 @@
102102
"ProjectRemoteMirrorManager",
103103
"ProjectStorage",
104104
"ProjectStorageManager",
105-
"ProjectCiLint",
106-
"ProjectCiLintManager",
107105
]
108106

109107

@@ -165,7 +163,7 @@ class Project(RefreshMixin, SaveMixin, ObjectDeleteMixin, RepositoryMixin, RESTO
165163
badges: ProjectBadgeManager
166164
boards: ProjectBoardManager
167165
branches: ProjectBranchManager
168-
ci_lint: "ProjectCiLintManager"
166+
ci_lint: ProjectCiLintManager
169167
clusters: ProjectClusterManager
170168
commits: ProjectCommitManager
171169
customattributes: ProjectCustomAttributeManager

tests/unit/objects/test_ci_lint.py

+33-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import pytest
22
import responses
33

4-
ci_lint_get_content = {
4+
gitlab_ci_yml = """---
5+
:test_job:
6+
:script: echo 1
7+
"""
8+
9+
ci_lint_create_content = {"status": "valid", "errors": [], "warnings": []}
10+
11+
12+
project_ci_lint_content = {
513
"valid": True,
614
"merged_yaml": "---\n:test_job:\n :script: echo 1\n",
715
"errors": [],
@@ -10,40 +18,54 @@
1018

1119

1220
@pytest.fixture
13-
def resp_get_ci_lint():
21+
def resp_create_ci_lint():
22+
with responses.RequestsMock() as rsps:
23+
rsps.add(
24+
method=responses.POST,
25+
url="http://localhost/api/v4/ci/lint",
26+
json=ci_lint_create_content,
27+
content_type="application/json",
28+
status=200,
29+
)
30+
yield rsps
31+
32+
33+
@pytest.fixture
34+
def resp_get_project_ci_lint():
1435
with responses.RequestsMock() as rsps:
1536
rsps.add(
1637
method=responses.GET,
1738
url="http://localhost/api/v4/projects/1/ci/lint",
18-
json=ci_lint_get_content,
39+
json=project_ci_lint_content,
1940
content_type="application/json",
2041
status=200,
2142
)
2243
yield rsps
2344

2445

2546
@pytest.fixture
26-
def resp_create_ci_lint():
47+
def resp_create_project_ci_lint():
2748
with responses.RequestsMock() as rsps:
2849
rsps.add(
2950
method=responses.POST,
3051
url="http://localhost/api/v4/projects/1/ci/lint",
31-
json=ci_lint_get_content,
52+
json=project_ci_lint_content,
3253
content_type="application/json",
3354
status=200,
3455
)
3556
yield rsps
3657

3758

38-
def test_project_ci_lint_get(project, resp_get_ci_lint):
59+
def test_ci_lint_create(gl, resp_create_ci_lint):
60+
lint_result = gl.ci_lint.create({"content": gitlab_ci_yml})
61+
assert lint_result.status == "valid"
62+
63+
64+
def test_project_ci_lint_get(project, resp_get_project_ci_lint):
3965
lint_result = project.ci_lint.get()
4066
assert lint_result.valid is True
4167

4268

43-
def test_project_ci_lint_create(project, resp_create_ci_lint):
44-
gitlab_ci_yml = """---
45-
:test_job:
46-
:script: echo 1
47-
"""
69+
def test_project_ci_lint_create(project, resp_create_project_ci_lint):
4870
lint_result = project.ci_lint.create({"content": gitlab_ci_yml})
4971
assert lint_result.valid is True

0 commit comments

Comments
 (0)