Skip to content

Commit 14f294c

Browse files
committed
use OptionsResolver in Jobs api
1 parent ee2b0e6 commit 14f294c

File tree

2 files changed

+58
-13
lines changed

2 files changed

+58
-13
lines changed

lib/Gitlab/Api/Jobs.php

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace Gitlab\Api;
22

33
use Psr\Http\Message\StreamInterface;
4+
use Symfony\Component\OptionsResolver\OptionsResolver;
45

56
class Jobs extends AbstractApi
67
{
@@ -15,27 +16,40 @@ class Jobs extends AbstractApi
1516

1617
/**
1718
* @param int|string $project_id
18-
* @param array $scope
19+
* @param array $parameters (
20+
*
21+
* @var string|string[] $scope The scope of jobs to show, one or array of: created, pending, running, failed,
22+
* success, canceled, skipped, manual; showing all jobs if none provided.
23+
* )
24+
*
1925
* @return mixed
2026
*/
21-
public function all($project_id, array $scope = [])
27+
public function all($project_id, array $parameters = [])
2228
{
23-
return $this->get("projects/".$this->encodePath($project_id)."/jobs", array(
24-
'scope' => $scope
25-
));
29+
$resolver = $this->createOptionsResolver();
30+
31+
return $this->get("projects/".$this->encodePath($project_id)."/jobs", $resolver->resolve($parameters));
2632
}
2733

2834
/**
2935
* @param int|string $project_id
3036
* @param int $pipeline_id
31-
* @param array $scope
37+
* @param array $parameters (
38+
*
39+
* @var string|string[] $scope The scope of jobs to show, one or array of: created, pending, running, failed,
40+
* success, canceled, skipped, manual; showing all jobs if none provided.
41+
* )
42+
*
3243
* @return mixed
3344
*/
34-
public function pipelineJobs($project_id, $pipeline_id, array $scope = [])
45+
public function pipelineJobs($project_id, $pipeline_id, array $parameters = [])
3546
{
36-
return $this->get("projects/".$this->encodePath($project_id)."/pipelines/".$this->encodePath($pipeline_id)."/jobs", array(
37-
'scope' => $scope
38-
));
47+
$resolver = $this->createOptionsResolver();
48+
49+
return $this->get(
50+
$this->getProjectPath($project_id, 'pipelines/').$this->encodePath($pipeline_id)."/jobs",
51+
$resolver->resolve($parameters)
52+
);
3953
}
4054

4155
/**
@@ -130,4 +144,35 @@ public function play($project_id, $job_id)
130144
{
131145
return $this->post("projects/".$this->encodePath($project_id)."/jobs/".$this->encodePath($job_id)."/play");
132146
}
147+
148+
/**
149+
* {@inheritdoc}
150+
*/
151+
protected function createOptionsResolver()
152+
{
153+
$allowedScopeValues = [
154+
self::SCOPE_CANCELED,
155+
self::SCOPE_CREATED,
156+
self::SCOPE_FAILED,
157+
self::SCOPE_MANUAL,
158+
self::SCOPE_PENDING,
159+
self::SCOPE_RUNNING,
160+
self::SCOPE_SKIPPED,
161+
self::SCOPE_SUCCESS,
162+
];
163+
164+
$resolver = parent::createOptionsResolver();
165+
$resolver->setDefined('scope')
166+
->setAllowedTypes('scope', ['string', 'array'])
167+
->setAllowedValues('scope', $allowedScopeValues)
168+
->addAllowedValues('scope', function ($value) use ($allowedScopeValues) {
169+
return is_array($value) && empty(array_diff($value, $allowedScopeValues));
170+
})
171+
->setNormalizer('scope', function (OptionsResolver $resolver, $value) {
172+
return (array) $value;
173+
})
174+
;
175+
176+
return $resolver;
177+
}
133178
}

test/Gitlab/Tests/Api/JobsTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function shouldGetAllJobs()
2424
->will($this->returnValue($expectedArray))
2525
;
2626

27-
$this->assertEquals($expectedArray, $api->all(1, [Jobs::SCOPE_PENDING]));
27+
$this->assertEquals($expectedArray, $api->all(1, ['scope' => Jobs::SCOPE_PENDING]));
2828
}
2929

3030
/**
@@ -41,12 +41,12 @@ public function shouldGetPipelineJobs()
4141
$api->expects($this->once())
4242
->method('get')
4343
->with('projects/1/pipelines/2/jobs', array(
44-
'scope' => ['pending']
44+
'scope' => ['pending', 'running']
4545
))
4646
->will($this->returnValue($expectedArray))
4747
;
4848

49-
$this->assertEquals($expectedArray, $api->pipelineJobs(1, 2, [Jobs::SCOPE_PENDING]));
49+
$this->assertEquals($expectedArray, $api->pipelineJobs(1, 2, ['scope' => [Jobs::SCOPE_PENDING, Jobs::SCOPE_RUNNING]]));
5050
}
5151

5252
/**

0 commit comments

Comments
 (0)