Skip to content

Adding the Jobs API #196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions lib/Gitlab/Api/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Http\Discovery\StreamFactoryDiscovery;
use Http\Message\MultipartStream\MultipartStreamBuilder;
use Http\Message\StreamFactory;
use Psr\Http\Message\ResponseInterface;

/**
* Abstract class for Api classes
Expand Down Expand Up @@ -52,18 +53,29 @@ public function configure()
}

/**
* Performs a GET query and returns the response as a PSR-7 response object.
*
* @param string $path
* @param array $parameters
* @param array $requestHeaders
* @return mixed
* @return ResponseInterface
*/
protected function get($path, array $parameters = array(), $requestHeaders = array())
protected function getAsResponse($path, array $parameters = array(), $requestHeaders = array())
{
$path = $this->preparePath($path, $parameters);

$response = $this->client->getHttpClient()->get($path, $requestHeaders);
return $this->client->getHttpClient()->get($path, $requestHeaders);
}

return ResponseMediator::getContent($response);
/**
* @param string $path
* @param array $parameters
* @param array $requestHeaders
* @return mixed
*/
protected function get($path, array $parameters = array(), $requestHeaders = array())
{
return ResponseMediator::getContent($this->getAsResponse($path, $parameters, $requestHeaders));
}

/**
Expand Down
133 changes: 133 additions & 0 deletions lib/Gitlab/Api/Jobs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php namespace Gitlab\Api;

use Psr\Http\Message\StreamInterface;

class Jobs extends AbstractApi
{
const SCOPE_CREATED = 'created';
const SCOPE_PENDING = 'pending';
const SCOPE_RUNNING = 'running';
const SCOPE_FAILED = 'failed';
const SCOPE_SUCCESS = 'success';
const SCOPE_CANCELED = 'canceled';
const SCOPE_SKIPPED = 'skipped';
const SCOPE_MANUAL = 'manual';

/**
* @param int|string $project_id
* @param array $scope
* @return mixed
*/
public function all($project_id, array $scope = [])
{
return $this->get("projects/".$this->encodePath($project_id)."/jobs", array(
'scope' => $scope
));
}

/**
* @param int|string $project_id
* @param int $pipeline_id
* @param array $scope
* @return mixed
*/
public function pipelineJobs($project_id, $pipeline_id, array $scope = [])
{
return $this->get("projects/".$this->encodePath($project_id)."/pipelines/".$this->encodePath($pipeline_id)."/jobs", array(
'scope' => $scope
));
}

/**
* @param int|string $project_id
* @param int $job_id
* @return mixed
*/
public function show($project_id, $job_id)
{
return $this->get("projects/".$this->encodePath($project_id)."/jobs/".$this->encodePath($job_id));
}

/**
* @param int|string $project_id
* @param int $job_id
* @return StreamInterface
*/
public function artifacts($project_id, $job_id)
{
return $this->getAsResponse("projects/".$this->encodePath($project_id)."/jobs/".$this->encodePath($job_id)."/artifacts")->getBody();
}

/**
* @param int|string $project_id
* @param string $ref_name
* @param string $job_name
* @return StreamInterface
*/
public function artifactsByRefName($project_id, $ref_name, $job_name)
{
return $this->getAsResponse("projects/".$this->encodePath($project_id)."/jobs/artifacts/".$this->encodePath($ref_name)."/download", array(
'job' => $job_name
))->getBody();
}

/**
* @param int|string $project_id
* @param int $job_id
* @return string
*/
public function trace($project_id, $job_id)
{
return $this->get("projects/".$this->encodePath($project_id)."/jobs/".$this->encodePath($job_id)."/trace");
}

/**
* @param int|string $project_id
* @param int $job_id
* @return mixed
*/
public function cancel($project_id, $job_id)
{
return $this->post("projects/".$this->encodePath($project_id)."/jobs/".$this->encodePath($job_id)."/cancel");
}

/**
* @param int|string $project_id
* @param int $job_id
* @return mixed
*/
public function retry($project_id, $job_id)
{
return $this->post("projects/".$this->encodePath($project_id)."/jobs/".$this->encodePath($job_id)."/retry");
}

/**
* @param int|string $project_id
* @param int $job_id
* @return mixed
*/
public function erase($project_id, $job_id)
{
return $this->post("projects/".$this->encodePath($project_id)."/jobs/".$this->encodePath($job_id)."/erase");
}

/**
* @param int|string $project_id
* @param int $job_id
* @return mixed
*/
public function keepArtifacts($project_id, $job_id)
{
return $this->post("projects/".$this->encodePath($project_id)."/jobs/".$this->encodePath($job_id)."/artifacts/keep");
}

/**
* @param int|string $project_id
* @param int $job_id
* @return mixed
*/
public function play($project_id, $job_id)
{
return $this->post("projects/".$this->encodePath($project_id)."/jobs/".$this->encodePath($job_id)."/play");
}
}
5 changes: 5 additions & 0 deletions lib/Gitlab/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*
* @property-read \Gitlab\Api\Groups $groups
* @property-read \Gitlab\Api\Issues $issues
* @property-read \Gitlab\Api\Jobs $jobs
* @property-read \Gitlab\Api\MergeRequests $merge_requests
* @property-read \Gitlab\Api\MergeRequests $mr
* @property-read \Gitlab\Api\Milestones $milestones
Expand Down Expand Up @@ -138,6 +139,10 @@ public function api($name)
$api = new Api\Issues($this);
break;

case 'jobs':
$api = new Api\Jobs($this);
break;

case 'mr':
case 'merge_requests':
$api = new Api\MergeRequests($this);
Expand Down
83 changes: 83 additions & 0 deletions lib/Gitlab/Model/Job.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php namespace Gitlab\Model;

use Gitlab\Client;

/**
* Class Commit
*
* @property-read Commit $commit
* @property-read int $id
* @property-read string $coverage
* @property-read string $created_at
* @property-read string $artifacts_file
* @property-read string $finished_at
* @property-read string $name
* @property-read Pipeline $pipeline
* @property-read string $ref
* @property-read string $runner
* @property-read string $stage
* @property-read string $started_at
* @property-read string $status
* @property-read string|bool $tag
* @property-read User $user
*/
class Job extends AbstractModel
{
/**
* @var array
*/
protected static $properties = array(
'id',
'commit',
'coverage',
'created_at',
'artifacts_file',
'finished_at',
'name',
'pipeline',
'ref',
'runner',
'stage',
'started_at',
'status',
'tag',
'user'
);

/**
* @param Client $client
* @param Project $project
* @param array $data
* @return Job
*/
public static function fromArray(Client $client, Project $project, array $data)
{
$job = new static($project, $data['id'], $client);

if (isset($data['user'])) {
$data['user'] = User::fromArray($client, $data['user']);
}

if (isset($data['commit'])) {
$data['commit'] = Commit::fromArray($client, $project, $data['commit']);
}

if (isset($data['pipeline'])) {
$data['pipeline'] = Pipeline::fromArray($client, $project, $data['pipeline']);
}

return $job->hydrate($data);
}

/**
* @param Project $project
* @param int $id
* @param Client $client
*/
public function __construct(Project $project, $id = null, Client $client = null)
{
$this->setClient($client);
$this->setData('project', $project);
$this->setData('id', $id);
}
}
49 changes: 49 additions & 0 deletions lib/Gitlab/Model/Pipeline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php namespace Gitlab\Model;

use Gitlab\Client;

/**
* Class Commit
*
* @property-read int $id
* @property-read string $ref
* @property-read string $sha
* @property-read string $status
*/
class Pipeline extends AbstractModel
{
/**
* @var array
*/
protected static $properties = array(
'id',
'ref',
'sha',
'status'
);

/**
* @param Client $client
* @param Project $project
* @param array $data
* @return Commit
*/
public static function fromArray(Client $client, Project $project, array $data)
{
$pipeline = new static($project, $data['id'], $client);

return $pipeline->hydrate($data);
}

/**
* @param Project $project
* @param int $id
* @param Client $client
*/
public function __construct(Project $project, $id = null, Client $client = null)
{
$this->setClient($client);
$this->setData('project', $project);
$this->setData('id', $id);
}
}
45 changes: 45 additions & 0 deletions lib/Gitlab/Model/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -1092,4 +1092,49 @@ public function contributors()

return $contributors;
}

/**
* @param array $scopes
* @return Job[]
*/
public function jobs(array $scopes = [])
{
$data = $this->api('jobs')->jobs($this->id, $scopes);

$jobs = array();
foreach ($data as $job) {
$jobs[] = Job::fromArray($this->getClient(), $this, $job);
}

return $jobs;
}

/**
* @param int $pipeline_id
* @param array $scopes
* @return Job[]
*/
public function pipelineJobs($pipeline_id, array $scopes = [])
{
$data = $this->api('jobs')->pipelineJobs($this->id, $pipeline_id, $scopes);

$jobs = array();
foreach ($data as $job) {
$jobs[] = Job::fromArray($this->getClient(), $this, $job);
}

return $jobs;
}

/**
* @param int $job_id
* @return Job
*/
public function job($job_id)
{
$data = $this->api('jobs')->show($this->id, $job_id);

return Job::fromArray($this->getClient(), $this, $data);
}

}
Loading