Skip to content

Commit bf07c09

Browse files
Jaimem1guelpf
authored andcommitted
create, update, show and delete schedules
1 parent 26ac5ce commit bf07c09

File tree

5 files changed

+241
-0
lines changed

5 files changed

+241
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
vendor/
22
composer.lock
3+
4+
.idea

lib/Gitlab/Api/Schedules.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Gitlab\Api;
4+
5+
class Schedules extends AbstractApi
6+
{
7+
/**
8+
* @param int $project_id
9+
* @param array $params
10+
* @return mixed
11+
*/
12+
public function create($project_id, array $params)
13+
{
14+
return $this->post($this->getProjectPath($project_id, 'pipeline_schedules'), $params);
15+
}
16+
17+
/**
18+
* @param int $project_id
19+
* @param int $schedule_id
20+
* @return mixed
21+
*/
22+
public function show($project_id, $schedule_id)
23+
{
24+
return $this->get($this->getProjectPath($project_id, 'pipeline_schedules/'.$this->encodePath($schedule_id)));
25+
}
26+
27+
/**
28+
* @param int $project_id
29+
* @param int $schedule_id
30+
* @param array $params
31+
* @return mixed
32+
*/
33+
public function update($project_id, $schedule_id, array $params)
34+
{
35+
return $this->put($this->getProjectPath($project_id, 'pipeline_schedules/'.$this->encodePath($schedule_id)), $params);
36+
}
37+
38+
/**
39+
* @param int $project_id
40+
* @param int $schedule_id
41+
* @return mixed
42+
*/
43+
public function remove($project_id, $schedule_id)
44+
{
45+
return $this->delete($this->getProjectPath($project_id, 'pipeline_schedules/'.$this->encodePath($schedule_id)));
46+
}
47+
}

lib/Gitlab/Client.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,14 @@ public function environments()
295295
return new Api\Environments($this);
296296
}
297297

298+
/**
299+
* @return Api\Schedules
300+
*/
301+
public function schedules()
302+
{
303+
return new Api\Schedules($this);
304+
}
305+
298306
/**
299307
* @param string $name
300308
*
@@ -377,6 +385,9 @@ public function api($name)
377385
case 'deployments':
378386
return $this->deployments();
379387

388+
case 'schedules':
389+
return $this->schedules();
390+
380391
default:
381392
throw new InvalidArgumentException('Invalid endpoint: "'.$name.'"');
382393
}

lib/Gitlab/Model/Schedule.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Gitlab\Model;
4+
5+
use Gitlab\Client;
6+
7+
class Schedule extends AbstractModel
8+
{
9+
/**
10+
* @var array
11+
*/
12+
protected static $properties = array(
13+
"id",
14+
"description",
15+
"ref",
16+
"cron",
17+
"cron_timezone",
18+
"next_run_at",
19+
"active",
20+
"created_at",
21+
"updated_at"
22+
);
23+
24+
/**
25+
* @param Client $client
26+
* @param Project $project
27+
* @param array $data
28+
* @return Schedule
29+
*/
30+
public static function fromArray(Client $client, Project $project, array $data)
31+
{
32+
$schedule = new static($project, $data['id'], $client);
33+
34+
return $schedule->hydrate($data);
35+
}
36+
37+
/**
38+
* @param Project $project
39+
* @param int $id
40+
* @param Client $client
41+
*/
42+
public function __construct(Project $project, $id = null, Client $client = null)
43+
{
44+
$this->setClient($client);
45+
$this->setData('project', $project);
46+
$this->setData('id', $id);
47+
}
48+
49+
/**
50+
* @return Schedule
51+
*/
52+
public function show()
53+
{
54+
$data = $this->client->schedules()->show($this->project->id, $this->id);
55+
56+
return static::fromArray($this->getClient(), $this->project, $data);
57+
}
58+
59+
/**
60+
* @param array $params
61+
* @return Schedule
62+
*/
63+
public function update(array $params)
64+
{
65+
$data = $this->client->schedules()->update($this->project->id, $this->id, $params);
66+
67+
return static::fromArray($this->getClient(), $this->project, $data);
68+
}
69+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
namespace Gitlab\Tests\Api;
4+
5+
class ScheduleTest extends TestCase
6+
{
7+
/**
8+
* @test
9+
*/
10+
public function shouldCreateSchedule()
11+
{
12+
$expectedArray = [
13+
"id" => 13,
14+
"description" => "Test schedule pipeline",
15+
"ref" => "master",
16+
"cron" => "* * * * *",
17+
"cron_timezone" => "Asia/Tokyo",
18+
"next_run_at" => "2017-05-19T13:41:00.000Z",
19+
"active" => true,
20+
"created_at" => "2017-05-19T13:31:08.849Z",
21+
"updated_at" => "2017-05-19T13:40:17.727Z"
22+
];
23+
24+
25+
$api = $this->getApiMock();
26+
$api->expects($this->once())
27+
->method('post')
28+
->with('projects/1/pipeline_schedules', [
29+
"id" => 13,
30+
"description" => "Test schedule pipeline",
31+
"ref" => "master",
32+
"cron" => "* * * * *",
33+
"cron_timezone" => "Asia/Tokyo",
34+
"next_run_at" => "2017-05-19T13:41:00.000Z",
35+
"active" => true,
36+
"created_at" => "2017-05-19T13:31:08.849Z",
37+
"updated_at" => "2017-05-19T13:40:17.727Z"
38+
])
39+
->will($this->returnValue($expectedArray));
40+
41+
$this->assertEquals($expectedArray, $api->create(1,
42+
[
43+
"id" => 13,
44+
"description" => "Test schedule pipeline",
45+
"ref" => "master",
46+
"cron" => "* * * * *",
47+
"cron_timezone" => "Asia/Tokyo",
48+
"next_run_at" => "2017-05-19T13:41:00.000Z",
49+
"active" => true,
50+
"created_at" => "2017-05-19T13:31:08.849Z",
51+
"updated_at" => "2017-05-19T13:40:17.727Z"
52+
]
53+
));
54+
}
55+
56+
/**
57+
* @test
58+
*/
59+
public function shouldShowSchedule()
60+
{
61+
$expectedArray = array('id' => 1, 'name' => 'A schedule');
62+
63+
$api = $this->getApiMock();
64+
$api->expects($this->once())
65+
->method('get')
66+
->with('projects/1/pipeline_schedules/2')
67+
->will($this->returnValue($expectedArray))
68+
;
69+
70+
$this->assertEquals($expectedArray, $api->show(1, 2));
71+
}
72+
73+
/**
74+
* @test
75+
*/
76+
public function shouldUpdateSchedule()
77+
{
78+
$expectedArray = array('id' => 3, 'title' => 'Updated schedule');
79+
80+
$api = $this->getApiMock();
81+
$api->expects($this->once())
82+
->method('put')
83+
->with('projects/1/pipeline_schedules/3', array('title' => 'Updated schedule', 'due_date' => '2015-04-01', 'state_event' => 'close'))
84+
->will($this->returnValue($expectedArray))
85+
;
86+
87+
$this->assertEquals($expectedArray, $api->update(1, 3, array('title' => 'Updated schedule', 'due_date' => '2015-04-01', 'state_event' => 'close')));
88+
}
89+
90+
/**
91+
* @test
92+
*/
93+
public function shouldRemoveSchedule()
94+
{
95+
$expectedBool = true;
96+
97+
$api = $this->getApiMock();
98+
$api->expects($this->once())
99+
->method('delete')
100+
->with('projects/1/pipeline_schedules/2')
101+
->will($this->returnValue($expectedBool))
102+
;
103+
104+
$this->assertEquals($expectedBool, $api->remove(1, 2));
105+
}
106+
107+
108+
protected function getApiClass()
109+
{
110+
return 'Gitlab\Api\Schedules';
111+
}
112+
}

0 commit comments

Comments
 (0)