Skip to content

Commit 5a6159d

Browse files
committed
Adding the Jobs API
1 parent eea2a84 commit 5a6159d

File tree

6 files changed

+523
-0
lines changed

6 files changed

+523
-0
lines changed

lib/Gitlab/Api/Jobs.php

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

lib/Gitlab/Client.php

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*
1818
* @property-read \Gitlab\Api\Groups $groups
1919
* @property-read \Gitlab\Api\Issues $issues
20+
* @property-read \Gitlab\Api\Jobs $jobs
2021
* @property-read \Gitlab\Api\MergeRequests $merge_requests
2122
* @property-read \Gitlab\Api\MergeRequests $mr
2223
* @property-read \Gitlab\Api\Milestones $milestones
@@ -120,6 +121,10 @@ public function api($name)
120121
$api = new Api\Issues($this);
121122
break;
122123

124+
case 'jobs':
125+
$api = new Api\Jobs($this);
126+
break;
127+
123128
case 'mr':
124129
case 'merge_requests':
125130
$api = new Api\MergeRequests($this);

lib/Gitlab/Model/Job.php

+83
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

+49
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 $job->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

+45
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)