- Notifications
You must be signed in to change notification settings - Fork673
feat(cli): add support for global CI lint#2128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
4 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionsdocs/api-objects.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletionsdocs/cli-examples.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletionsdocs/cli-usage.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletionsdocs/gl_objects/ci_lint.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
####### | ||
CI Lint | ||
####### | ||
Reference | ||
--------- | ||
* v4 API: | ||
+ :class:`gitlab.v4.objects.CiLint` | ||
+ :class:`gitlab.v4.objects.CiLintManager` | ||
+ :attr:`gitlab.Gitlab.ci_lint` | ||
+ :class:`gitlab.v4.objects.ProjectCiLint` | ||
+ :class:`gitlab.v4.objects.ProjectCiLintManager` | ||
+ :attr:`gitlab.v4.objects.Project.ci_lint` | ||
* GitLab API: https://docs.gitlab.com/ee/api/lint.html | ||
Examples | ||
--------- | ||
Validate a CI YAML configuration:: | ||
gitlab_ci_yml = """.api_test: | ||
rules: | ||
- if: $CI_PIPELINE_SOURCE=="merge_request_event" | ||
changes: | ||
- src/api/* | ||
deploy: | ||
extends: | ||
- .api_test | ||
rules: | ||
- when: manual | ||
allow_failure: true | ||
script: | ||
- echo "hello world" | ||
""" | ||
lint_result = gl.ci_lint.create({"content": gitlab_ci_yml}) | ||
print(lint_result.status) # Print the status of the CI YAML | ||
print(lint_result.merged_yaml) # Print the merged YAML file | ||
Validate a project's CI configuration:: | ||
lint_result = project.ci_lint.get() | ||
assert lint_result.valid is True # Test that the .gitlab-ci.yml is valid | ||
print(lint_result.merged_yaml) # Print the merged YAML file | ||
Validate a CI YAML configuration with a namespace:: | ||
lint_result = project.ci_lint.create({"content": gitlab_ci_yml}) | ||
assert lint_result.valid is True # Test that the .gitlab-ci.yml is valid | ||
print(lint_result.merged_yaml) # Print the merged YAML file |
43 changes: 0 additions & 43 deletionsdocs/gl_objects/projects.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletionsgitlab/client.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletionsgitlab/v4/objects/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletionsgitlab/v4/objects/ci_lint.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
""" | ||
GitLab API: | ||
https://docs.gitlab.com/ee/api/lint.html | ||
""" | ||
from typing import Any, cast | ||
from gitlab.base import RESTManager, RESTObject | ||
from gitlab.mixins import CreateMixin, GetWithoutIdMixin | ||
from gitlab.types import RequiredOptional | ||
__all__ = [ | ||
"CiLint", | ||
"CiLintManager", | ||
"ProjectCiLint", | ||
"ProjectCiLintManager", | ||
] | ||
class CiLint(RESTObject): | ||
_id_attr = None | ||
class CiLintManager(CreateMixin, RESTManager): | ||
_path = "/ci/lint" | ||
_obj_cls = CiLint | ||
_create_attrs = RequiredOptional( | ||
required=("content",), optional=("include_merged_yaml", "include_jobs") | ||
) | ||
class ProjectCiLint(RESTObject): | ||
pass | ||
class ProjectCiLintManager(GetWithoutIdMixin, CreateMixin, RESTManager): | ||
_path = "/projects/{project_id}/ci/lint" | ||
_obj_cls = ProjectCiLint | ||
_from_parent_attrs = {"project_id": "id"} | ||
_create_attrs = RequiredOptional( | ||
required=("content",), optional=("dry_run", "include_jobs", "ref") | ||
) | ||
def get(self, **kwargs: Any) -> ProjectCiLint: | ||
return cast(ProjectCiLint, super().get(**kwargs)) |
21 changes: 2 additions & 19 deletionsgitlab/v4/objects/projects.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletionstests/unit/objects/test_ci_lint.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import pytest | ||
import responses | ||
gitlab_ci_yml = """--- | ||
:test_job: | ||
:script: echo 1 | ||
""" | ||
ci_lint_create_content = {"status": "valid", "errors": [], "warnings": []} | ||
project_ci_lint_content = { | ||
"valid": True, | ||
"merged_yaml": "---\n:test_job:\n :script: echo 1\n", | ||
"errors": [], | ||
"warnings": [], | ||
} | ||
@pytest.fixture | ||
def resp_create_ci_lint(): | ||
with responses.RequestsMock() as rsps: | ||
rsps.add( | ||
method=responses.POST, | ||
url="http://localhost/api/v4/ci/lint", | ||
json=ci_lint_create_content, | ||
content_type="application/json", | ||
status=200, | ||
) | ||
yield rsps | ||
@pytest.fixture | ||
def resp_get_project_ci_lint(): | ||
with responses.RequestsMock() as rsps: | ||
rsps.add( | ||
method=responses.GET, | ||
url="http://localhost/api/v4/projects/1/ci/lint", | ||
json=project_ci_lint_content, | ||
content_type="application/json", | ||
status=200, | ||
) | ||
yield rsps | ||
@pytest.fixture | ||
def resp_create_project_ci_lint(): | ||
with responses.RequestsMock() as rsps: | ||
rsps.add( | ||
method=responses.POST, | ||
url="http://localhost/api/v4/projects/1/ci/lint", | ||
json=project_ci_lint_content, | ||
content_type="application/json", | ||
status=200, | ||
) | ||
yield rsps | ||
def test_ci_lint_create(gl, resp_create_ci_lint): | ||
lint_result = gl.ci_lint.create({"content": gitlab_ci_yml}) | ||
assert lint_result.status == "valid" | ||
def test_project_ci_lint_get(project, resp_get_project_ci_lint): | ||
lint_result = project.ci_lint.get() | ||
assert lint_result.valid is True | ||
def test_project_ci_lint_create(project, resp_create_project_ci_lint): | ||
lint_result = project.ci_lint.create({"content": gitlab_ci_yml}) | ||
assert lint_result.valid is True |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.