Skip to content

Commit a7bfdc3

Browse files
authored
Merge pull request #196 from moufmouf/feature/jobs_api
Adding the Jobs API
2 parents 8d9b602 + 52f2b20 commit a7bfdc3

File tree

8 files changed

+543
-5
lines changed

8 files changed

+543
-5
lines changed

lib/Gitlab/Api/AbstractApi.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Http\Discovery\StreamFactoryDiscovery;
66
use Http\Message\MultipartStream\MultipartStreamBuilder;
77
use Http\Message\StreamFactory;
8+
use Psr\Http\Message\ResponseInterface;
89

910
/**
1011
* Abstract class for Api classes
@@ -52,18 +53,29 @@ public function configure()
5253
}
5354

5455
/**
56+
* Performs a GET query and returns the response as a PSR-7 response object.
57+
*
5558
* @param string $path
5659
* @param array $parameters
5760
* @param array $requestHeaders
58-
* @return mixed
61+
* @return ResponseInterface
5962
*/
60-
protected function get($path, array $parameters = array(), $requestHeaders = array())
63+
protected function getAsResponse($path, array $parameters = array(), $requestHeaders = array())
6164
{
6265
$path = $this->preparePath($path, $parameters);
6366

64-
$response = $this->client->getHttpClient()->get($path, $requestHeaders);
67+
return $this->client->getHttpClient()->get($path, $requestHeaders);
68+
}
6569

66-
return ResponseMediator::getContent($response);
70+
/**
71+
* @param string $path
72+
* @param array $parameters
73+
* @param array $requestHeaders
74+
* @return mixed
75+
*/
76+
protected function get($path, array $parameters = array(), $requestHeaders = array())
77+
{
78+
return ResponseMediator::getContent($this->getAsResponse($path, $parameters, $requestHeaders));
6779
}
6880

6981
/**

lib/Gitlab/Api/Jobs.php

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php namespace Gitlab\Api;
2+
3+
use Psr\Http\Message\StreamInterface;
4+
5+
class Jobs extends AbstractApi
6+
{
7+
const SCOPE_CREATED = 'created';
8+
const SCOPE_PENDING = 'pending';
9+
const SCOPE_RUNNING = 'running';
10+
const SCOPE_FAILED = 'failed';
11+
const SCOPE_SUCCESS = 'success';
12+
const SCOPE_CANCELED = 'canceled';
13+
const SCOPE_SKIPPED = 'skipped';
14+
const SCOPE_MANUAL = 'manual';
15+
16+
/**
17+
* @param int|string $project_id
18+
* @param array $scope
19+
* @return mixed
20+
*/
21+
public function all($project_id, array $scope = [])
22+
{
23+
return $this->get("projects/".$this->encodePath($project_id)."/jobs", array(
24+
'scope' => $scope
25+
));
26+
}
27+
28+
/**
29+
* @param int|string $project_id
30+
* @param int $pipeline_id
31+
* @param array $scope
32+
* @return mixed
33+
*/
34+
public function pipelineJobs($project_id, $pipeline_id, array $scope = [])
35+
{
36+
return $this->get("projects/".$this->encodePath($project_id)."/pipelines/".$this->encodePath($pipeline_id)."/jobs", array(
37+
'scope' => $scope
38+
));
39+
}
40+
41+
/**
42+
* @param int|string $project_id
43+
* @param int $job_id
44+
* @return mixed
45+
*/
46+
public function show($project_id, $job_id)
47+
{
48+
return $this->get("projects/".$this->encodePath($project_id)."/jobs/".$this->encodePath($job_id));
49+
}
50+
51+
/**
52+
* @param int|string $project_id
53+
* @param int $job_id
54+
* @return StreamInterface
55+
*/
56+
public function artifacts($project_id, $job_id)
57+
{
58+
return $this->getAsResponse("projects/".$this->encodePath($project_id)."/jobs/".$this->encodePath($job_id)."/artifacts")->getBody();
59+
}
60+
61+
/**
62+
* @param int|string $project_id
63+
* @param string $ref_name
64+
* @param string $job_name
65+
* @return StreamInterface
66+
*/
67+
public function artifactsByRefName($project_id, $ref_name, $job_name)
68+
{
69+
return $this->getAsResponse("projects/".$this->encodePath($project_id)."/jobs/artifacts/".$this->encodePath($ref_name)."/download", array(
70+
'job' => $job_name
71+
))->getBody();
72+
}
73+
74+
/**
75+
* @param int|string $project_id
76+
* @param int $job_id
77+
* @return string
78+
*/
79+
public function trace($project_id, $job_id)
80+
{
81+
return $this->get("projects/".$this->encodePath($project_id)."/jobs/".$this->encodePath($job_id)."/trace");
82+
}
83+
84+
/**
85+
* @param int|string $project_id
86+
* @param int $job_id
87+
* @return mixed
88+
*/
89+
public function cancel($project_id, $job_id)
90+
{
91+
return $this->post("projects/".$this->encodePath($project_id)."/jobs/".$this->encodePath($job_id)."/cancel");
92+
}
93+
94+
/**
95+
* @param int|string $project_id
96+
* @param int $job_id
97+
* @return mixed
98+
*/
99+
public function retry($project_id, $job_id)
100+
{
101+
return $this->post("projects/".$this->encodePath($project_id)."/jobs/".$this->encodePath($job_id)."/retry");
102+
}
103+
104+
/**
105+
* @param int|string $project_id
106+
* @param int $job_id
107+
* @return mixed
108+
*/
109+
public function erase($project_id, $job_id)
110+
{
111+
return $this->post("projects/".$this->encodePath($project_id)."/jobs/".$this->encodePath($job_id)."/erase");
112+
}
113+
114+
/**
115+
* @param int|string $project_id
116+
* @param int $job_id
117+
* @return mixed
118+
*/
119+
public function keepArtifacts($project_id, $job_id)
120+
{
121+
return $this->post("projects/".$this->encodePath($project_id)."/jobs/".$this->encodePath($job_id)."/artifacts/keep");
122+
}
123+
124+
/**
125+
* @param int|string $project_id
126+
* @param int $job_id
127+
* @return mixed
128+
*/
129+
public function play($project_id, $job_id)
130+
{
131+
return $this->post("projects/".$this->encodePath($project_id)."/jobs/".$this->encodePath($job_id)."/play");
132+
}
133+
}

lib/Gitlab/Client.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*
2222
* @property-read \Gitlab\Api\Groups $groups
2323
* @property-read \Gitlab\Api\Issues $issues
24+
* @property-read \Gitlab\Api\Jobs $jobs
2425
* @property-read \Gitlab\Api\MergeRequests $merge_requests
2526
* @property-read \Gitlab\Api\MergeRequests $mr
2627
* @property-read \Gitlab\Api\Milestones $milestones
@@ -138,6 +139,10 @@ public function api($name)
138139
$api = new Api\Issues($this);
139140
break;
140141

142+
case 'jobs':
143+
$api = new Api\Jobs($this);
144+
break;
145+
141146
case 'mr':
142147
case 'merge_requests':
143148
$api = new Api\MergeRequests($this);

lib/Gitlab/Model/Job.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php namespace Gitlab\Model;
2+
3+
use Gitlab\Client;
4+
5+
/**
6+
* Class Commit
7+
*
8+
* @property-read Commit $commit
9+
* @property-read int $id
10+
* @property-read string $coverage
11+
* @property-read string $created_at
12+
* @property-read string $artifacts_file
13+
* @property-read string $finished_at
14+
* @property-read string $name
15+
* @property-read Pipeline $pipeline
16+
* @property-read string $ref
17+
* @property-read string $runner
18+
* @property-read string $stage
19+
* @property-read string $started_at
20+
* @property-read string $status
21+
* @property-read string|bool $tag
22+
* @property-read User $user
23+
*/
24+
class Job extends AbstractModel
25+
{
26+
/**
27+
* @var array
28+
*/
29+
protected static $properties = array(
30+
'id',
31+
'commit',
32+
'coverage',
33+
'created_at',
34+
'artifacts_file',
35+
'finished_at',
36+
'name',
37+
'pipeline',
38+
'ref',
39+
'runner',
40+
'stage',
41+
'started_at',
42+
'status',
43+
'tag',
44+
'user'
45+
);
46+
47+
/**
48+
* @param Client $client
49+
* @param Project $project
50+
* @param array $data
51+
* @return Job
52+
*/
53+
public static function fromArray(Client $client, Project $project, array $data)
54+
{
55+
$job = new static($project, $data['id'], $client);
56+
57+
if (isset($data['user'])) {
58+
$data['user'] = User::fromArray($client, $data['user']);
59+
}
60+
61+
if (isset($data['commit'])) {
62+
$data['commit'] = Commit::fromArray($client, $project, $data['commit']);
63+
}
64+
65+
if (isset($data['pipeline'])) {
66+
$data['pipeline'] = Pipeline::fromArray($client, $project, $data['pipeline']);
67+
}
68+
69+
return $job->hydrate($data);
70+
}
71+
72+
/**
73+
* @param Project $project
74+
* @param int $id
75+
* @param Client $client
76+
*/
77+
public function __construct(Project $project, $id = null, Client $client = null)
78+
{
79+
$this->setClient($client);
80+
$this->setData('project', $project);
81+
$this->setData('id', $id);
82+
}
83+
}

lib/Gitlab/Model/Pipeline.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php namespace Gitlab\Model;
2+
3+
use Gitlab\Client;
4+
5+
/**
6+
* Class Commit
7+
*
8+
* @property-read int $id
9+
* @property-read string $ref
10+
* @property-read string $sha
11+
* @property-read string $status
12+
*/
13+
class Pipeline extends AbstractModel
14+
{
15+
/**
16+
* @var array
17+
*/
18+
protected static $properties = array(
19+
'id',
20+
'ref',
21+
'sha',
22+
'status'
23+
);
24+
25+
/**
26+
* @param Client $client
27+
* @param Project $project
28+
* @param array $data
29+
* @return Commit
30+
*/
31+
public static function fromArray(Client $client, Project $project, array $data)
32+
{
33+
$pipeline = new static($project, $data['id'], $client);
34+
35+
return $pipeline->hydrate($data);
36+
}
37+
38+
/**
39+
* @param Project $project
40+
* @param int $id
41+
* @param Client $client
42+
*/
43+
public function __construct(Project $project, $id = null, Client $client = null)
44+
{
45+
$this->setClient($client);
46+
$this->setData('project', $project);
47+
$this->setData('id', $id);
48+
}
49+
}

lib/Gitlab/Model/Project.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,4 +1092,49 @@ public function contributors()
10921092

10931093
return $contributors;
10941094
}
1095+
1096+
/**
1097+
* @param array $scopes
1098+
* @return Job[]
1099+
*/
1100+
public function jobs(array $scopes = [])
1101+
{
1102+
$data = $this->api('jobs')->jobs($this->id, $scopes);
1103+
1104+
$jobs = array();
1105+
foreach ($data as $job) {
1106+
$jobs[] = Job::fromArray($this->getClient(), $this, $job);
1107+
}
1108+
1109+
return $jobs;
1110+
}
1111+
1112+
/**
1113+
* @param int $pipeline_id
1114+
* @param array $scopes
1115+
* @return Job[]
1116+
*/
1117+
public function pipelineJobs($pipeline_id, array $scopes = [])
1118+
{
1119+
$data = $this->api('jobs')->pipelineJobs($this->id, $pipeline_id, $scopes);
1120+
1121+
$jobs = array();
1122+
foreach ($data as $job) {
1123+
$jobs[] = Job::fromArray($this->getClient(), $this, $job);
1124+
}
1125+
1126+
return $jobs;
1127+
}
1128+
1129+
/**
1130+
* @param int $job_id
1131+
* @return Job
1132+
*/
1133+
public function job($job_id)
1134+
{
1135+
$data = $this->api('jobs')->show($this->id, $job_id);
1136+
1137+
return Job::fromArray($this->getClient(), $this, $data);
1138+
}
1139+
10951140
}

0 commit comments

Comments
 (0)