diff --git a/gitlab/v4/cli.py b/gitlab/v4/cli.py index 87fcaf261..bdf08e9e3 100644 --- a/gitlab/v4/cli.py +++ b/gitlab/v4/cli.py @@ -97,6 +97,13 @@ def do_custom(self) -> Any: method_name = self.resource_action.replace("-", "_") return getattr(class_instance, method_name)(**self.args) + def do_project_commit_status_create(self) -> gitlab.base.RESTObject: + # Status creation has a different URL from the interpolated list path. + project = self.gl.projects.get(self.parent_args["project_id"], lazy=True) + commit = project.commits.get(self.parent_args["commit_id"], lazy=True) + self.mgr = commit.statuses + return self.do_create() + def do_project_export_download(self) -> None: try: project = self.gl.projects.get(self.parent_args["project_id"], lazy=True) diff --git a/tests/unit/test_cli.py b/tests/unit/test_cli.py index cad27afba..8ccdcc443 100644 --- a/tests/unit/test_cli.py +++ b/tests/unit/test_cli.py @@ -6,6 +6,7 @@ from unittest import mock import pytest +import responses import gitlab.base from gitlab import cli @@ -231,3 +232,32 @@ def test_legacy_display_with_long_repr_truncates(fake_object_long_repr): printer.display(fake_object_long_repr, obj=fake_object_long_repr) assert len(mocked.call_args.args[0]) < 80 + + +@pytest.mark.parametrize( + "project_id,encoded_project_id", + [("42", "42"), ("group/sub project", "group%2Fsub%20project")], +) +def test_cli_create_commit_status(gl, monkeypatch, project_id, encoded_project_id): + manager = gitlab.v4.objects.ProjectCommitStatusManager + # CLI initialization interpolates the class path; restore it after this test. + monkeypatch.setattr(manager, "_path", manager._path) + data = {"state": "pending", "context": "sample"} + url = f"http://localhost/api/v4/projects/{encoded_project_id}/statuses/abcdef" + with responses.RequestsMock() as rsps: + rsps.post( + url, + json={"id": 1, "status": "pending", "name": "sample"}, + status=201, + match=[responses.matchers.json_params_matcher(data)], + ) + command = v4_cli.GitlabCLI( + gl, + "project-commit-status", + "create", + {"project_id": project_id, "commit_id": "abcdef", **data}, + ) + result = command.run() + + assert result.status == "pending" + assert result.name == "sample"