Skip to content

Commit 1ddb54a

Browse files
authored
Merge pull request #1444 from python-gitlab/fix/return-retry-cancel-output
fix(objects): return server data in cancel/retry methods
2 parents 562fbbd + 9fed061 commit 1ddb54a

File tree

4 files changed

+196
-4
lines changed

4 files changed

+196
-4
lines changed

gitlab/tests/objects/test_jobs.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
"""
2+
GitLab API: https://docs.gitlab.com/ee/api/jobs.html
3+
"""
4+
import pytest
5+
import responses
6+
7+
from gitlab.v4.objects import ProjectJob
8+
9+
10+
job_content = {
11+
"commit": {
12+
"author_email": "admin@example.com",
13+
"author_name": "Administrator",
14+
},
15+
"coverage": None,
16+
"allow_failure": False,
17+
"created_at": "2015-12-24T15:51:21.880Z",
18+
"started_at": "2015-12-24T17:54:30.733Z",
19+
"finished_at": "2015-12-24T17:54:31.198Z",
20+
"duration": 0.465,
21+
"queued_duration": 0.010,
22+
"artifacts_expire_at": "2016-01-23T17:54:31.198Z",
23+
"tag_list": ["docker runner", "macos-10.15"],
24+
"id": 1,
25+
"name": "rubocop",
26+
"pipeline": {
27+
"id": 1,
28+
"project_id": 1,
29+
},
30+
"ref": "master",
31+
"artifacts": [],
32+
"runner": None,
33+
"stage": "test",
34+
"status": "failed",
35+
"tag": False,
36+
"web_url": "https://example.com/foo/bar/-/jobs/1",
37+
"user": {"id": 1},
38+
}
39+
40+
41+
@pytest.fixture
42+
def resp_get_job():
43+
with responses.RequestsMock() as rsps:
44+
rsps.add(
45+
method=responses.GET,
46+
url="http://localhost/api/v4/projects/1/jobs/1",
47+
json=job_content,
48+
content_type="application/json",
49+
status=200,
50+
)
51+
yield rsps
52+
53+
54+
@pytest.fixture
55+
def resp_cancel_job():
56+
with responses.RequestsMock() as rsps:
57+
rsps.add(
58+
method=responses.POST,
59+
url="http://localhost/api/v4/projects/1/jobs/1/cancel",
60+
json=job_content,
61+
content_type="application/json",
62+
status=201,
63+
)
64+
yield rsps
65+
66+
67+
@pytest.fixture
68+
def resp_retry_job():
69+
with responses.RequestsMock() as rsps:
70+
rsps.add(
71+
method=responses.POST,
72+
url="http://localhost/api/v4/projects/1/jobs/1/retry",
73+
json=job_content,
74+
content_type="application/json",
75+
status=201,
76+
)
77+
yield rsps
78+
79+
80+
def test_get_project_job(project, resp_get_job):
81+
job = project.jobs.get(1)
82+
assert isinstance(job, ProjectJob)
83+
assert job.ref == "master"
84+
85+
86+
def test_cancel_project_job(project, resp_cancel_job):
87+
job = project.jobs.get(1, lazy=True)
88+
89+
output = job.cancel()
90+
assert output["ref"] == "master"
91+
92+
93+
def test_retry_project_job(project, resp_retry_job):
94+
job = project.jobs.get(1, lazy=True)
95+
96+
output = job.retry()
97+
assert output["ref"] == "master"
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
"""
2+
GitLab API: https://docs.gitlab.com/ee/api/pipelines.html
3+
"""
4+
import pytest
5+
import responses
6+
7+
from gitlab.v4.objects import ProjectPipeline
8+
9+
10+
pipeline_content = {
11+
"id": 46,
12+
"project_id": 1,
13+
"status": "pending",
14+
"ref": "master",
15+
"sha": "a91957a858320c0e17f3a0eca7cfacbff50ea29a",
16+
"before_sha": "a91957a858320c0e17f3a0eca7cfacbff50ea29a",
17+
"tag": False,
18+
"yaml_errors": None,
19+
"user": {
20+
"name": "Administrator",
21+
"username": "root",
22+
"id": 1,
23+
"state": "active",
24+
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
25+
"web_url": "http://localhost:3000/root",
26+
},
27+
"created_at": "2016-08-11T11:28:34.085Z",
28+
"updated_at": "2016-08-11T11:32:35.169Z",
29+
"started_at": None,
30+
"finished_at": "2016-08-11T11:32:35.145Z",
31+
"committed_at": None,
32+
"duration": None,
33+
"queued_duration": 0.010,
34+
"coverage": None,
35+
"web_url": "https://example.com/foo/bar/pipelines/46",
36+
}
37+
38+
39+
@pytest.fixture
40+
def resp_get_pipeline():
41+
with responses.RequestsMock() as rsps:
42+
rsps.add(
43+
method=responses.GET,
44+
url="http://localhost/api/v4/projects/1/pipelines/1",
45+
json=pipeline_content,
46+
content_type="application/json",
47+
status=200,
48+
)
49+
yield rsps
50+
51+
52+
@pytest.fixture
53+
def resp_cancel_pipeline():
54+
with responses.RequestsMock() as rsps:
55+
rsps.add(
56+
method=responses.POST,
57+
url="http://localhost/api/v4/projects/1/pipelines/1/cancel",
58+
json=pipeline_content,
59+
content_type="application/json",
60+
status=201,
61+
)
62+
yield rsps
63+
64+
65+
@pytest.fixture
66+
def resp_retry_pipeline():
67+
with responses.RequestsMock() as rsps:
68+
rsps.add(
69+
method=responses.POST,
70+
url="http://localhost/api/v4/projects/1/pipelines/1/retry",
71+
json=pipeline_content,
72+
content_type="application/json",
73+
status=201,
74+
)
75+
yield rsps
76+
77+
78+
def test_get_project_pipeline(project, resp_get_pipeline):
79+
pipeline = project.pipelines.get(1)
80+
assert isinstance(pipeline, ProjectPipeline)
81+
assert pipeline.ref == "master"
82+
83+
84+
def test_cancel_project_pipeline(project, resp_cancel_pipeline):
85+
pipeline = project.pipelines.get(1, lazy=True)
86+
87+
output = pipeline.cancel()
88+
assert output["ref"] == "master"
89+
90+
91+
def test_retry_project_pipeline(project, resp_retry_pipeline):
92+
pipeline = project.pipelines.get(1, lazy=True)
93+
94+
output = pipeline.retry()
95+
assert output["ref"] == "master"

gitlab/v4/objects/jobs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def cancel(self, **kwargs):
2424
GitlabJobCancelError: If the job could not be canceled
2525
"""
2626
path = "%s/%s/cancel" % (self.manager.path, self.get_id())
27-
self.manager.gitlab.http_post(path)
27+
return self.manager.gitlab.http_post(path)
2828

2929
@cli.register_custom_action("ProjectJob")
3030
@exc.on_http_error(exc.GitlabJobRetryError)
@@ -39,7 +39,7 @@ def retry(self, **kwargs):
3939
GitlabJobRetryError: If the job could not be retried
4040
"""
4141
path = "%s/%s/retry" % (self.manager.path, self.get_id())
42-
self.manager.gitlab.http_post(path)
42+
return self.manager.gitlab.http_post(path)
4343

4444
@cli.register_custom_action("ProjectJob")
4545
@exc.on_http_error(exc.GitlabJobPlayError)

gitlab/v4/objects/pipelines.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def cancel(self, **kwargs):
5050
GitlabPipelineCancelError: If the request failed
5151
"""
5252
path = "%s/%s/cancel" % (self.manager.path, self.get_id())
53-
self.manager.gitlab.http_post(path)
53+
return self.manager.gitlab.http_post(path)
5454

5555
@cli.register_custom_action("ProjectPipeline")
5656
@exc.on_http_error(exc.GitlabPipelineRetryError)
@@ -65,7 +65,7 @@ def retry(self, **kwargs):
6565
GitlabPipelineRetryError: If the request failed
6666
"""
6767
path = "%s/%s/retry" % (self.manager.path, self.get_id())
68-
self.manager.gitlab.http_post(path)
68+
return self.manager.gitlab.http_post(path)
6969

7070

7171
class ProjectPipelineManager(RetrieveMixin, CreateMixin, DeleteMixin, RESTManager):

0 commit comments

Comments
 (0)