Skip to content

Commit 84ebd2d

Browse files
committed
Projects and users
1 parent 8431c49 commit 84ebd2d

6 files changed

Lines changed: 267 additions & 4 deletions

File tree

‎lib/Gitlab/Api/Projects.php‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace Gitlab\Api;
4+
5+
class Projects extends AbstractApi
6+
{
7+
public function all()
8+
{
9+
return $this->get('projects');
10+
}
11+
12+
public function show($project_id)
13+
{
14+
return $this->get('projects/'.urlencode($project_id));
15+
}
16+
17+
public function create($name, array $params = array())
18+
{
19+
$params['name'] = $name;
20+
21+
return $this->post('projects', $params);
22+
}
23+
24+
public function members($project_id)
25+
{
26+
return $this->get('projects/'.urlencode($project_id).'/members');
27+
}
28+
29+
public function member($project_id, $user_id)
30+
{
31+
return $this->get('projects/'.urlencode($project_id).'/members/'.urlencode($user_id));
32+
}
33+
34+
public function addMember($project_id, $user_id, $access_level)
35+
{
36+
return $this->post('projects/'.urlencode($project_id).'/members', array(
37+
'user_id' => $user_id,
38+
'access_level' => $access_level
39+
));
40+
}
41+
42+
public function saveMember($project_id, $user_id, $access_level)
43+
{
44+
return $this->put('projects/'.urlencode($project_id).'/members/'.urldecode($user_id), array(
45+
'access_level' => $access_level
46+
));
47+
}
48+
49+
public function removeMember($project_id, $user_id)
50+
{
51+
return $this->delete('projects/'.urlencode($project_id).'/members/'.urldecode($user_id));
52+
}
53+
54+
public function hooks($project_id)
55+
{
56+
return $this->get('projects/'.urlencode($project_id).'/hooks');
57+
}
58+
59+
public function hook($project_id, $hook_id)
60+
{
61+
return $this->get('projects/'.urlencode($project_id).'/hooks/'.urlencode($hook_id));
62+
}
63+
64+
public function addHook($project_id, $url)
65+
{
66+
return $this->post('projects/'.urlencode($project_id).'/hooks', array(
67+
'url' => $url
68+
));
69+
}
70+
71+
public function updateHook($project_id, $hook_id, $url)
72+
{
73+
return $this->put('projects/'.urlencode($project_id).'/hooks/'.urlencode($hook_id), array(
74+
'url' => $url
75+
));
76+
}
77+
78+
public function removeHook($project_id, $hook_id)
79+
{
80+
return $this->delete('projects/'.urlencode($project_id).'/hooks', array(
81+
'hook_id' => $hook_id
82+
));
83+
}
84+
85+
86+
}

‎lib/Gitlab/Api/Users.php‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@ public function all()
99
return $this->get('users');
1010
}
1111

12-
public function show($id)
12+
public function show($id = null)
1313
{
14-
return $this->get('users/'.urlencode($id));
14+
$path = 'users';
15+
16+
if (null !== $id) {
17+
$path .= '/'.urlencode($id);
18+
}
19+
20+
return $this->get($path);
1521
}
1622

1723
public function create($email, $password, array $params = array())
@@ -21,4 +27,12 @@ public function create($email, $password, array $params = array())
2127

2228
return $this->post('users', $params);
2329
}
30+
31+
public function session($email, $password)
32+
{
33+
return $this->post('session', array(
34+
'email' => $email,
35+
'password' => $password
36+
));
37+
}
2438
}

‎lib/Gitlab/Client.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ public function api($name)
7878
$api = new Api\Users($this);
7979
break;
8080

81+
case 'projects':
82+
$api = new Api\Projects($this);
83+
break;
84+
8185
default:
8286
throw new InvalidArgumentException();
8387
}

‎lib/Gitlab/Model/Project.php‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
namespace Gitlab\Model;
4+
5+
class Project extends AbstractModel
6+
{
7+
protected static $_properties = array(
8+
'id',
9+
'code',
10+
'name',
11+
'description',
12+
'path',
13+
'default_branch',
14+
'owner',
15+
'private',
16+
'issues_enabled',
17+
'merge_requests_enabled',
18+
'wall_enabled',
19+
'wiki_enabled',
20+
'created_at'
21+
);
22+
23+
public static function fromArray(array $data)
24+
{
25+
$project = new Project($data['id']);
26+
27+
if (isset($data['owner'])) {
28+
$data['owner'] = User::fromArray($data['owner']);
29+
}
30+
31+
return $project->hydrate($data);
32+
}
33+
34+
public function __construct($id = null)
35+
{
36+
$this->id = $id;
37+
}
38+
39+
public function show()
40+
{
41+
$data = $this->api('projects')->show($this->id);
42+
43+
return Project::fromArray($data);
44+
}
45+
46+
public function members()
47+
{
48+
$data = $this->api('projects')->members($this->id);
49+
50+
$members = array();
51+
foreach ($data as $member) {
52+
$members[] = User::fromArray($member);
53+
}
54+
55+
return $members;
56+
}
57+
58+
public function member($user_id)
59+
{
60+
$data = $this->api('projects')->member($this->id, $user_id);
61+
62+
return User::fromArray($data);
63+
}
64+
65+
public function addMember($user_id, $access_level)
66+
{
67+
$data = $this->api('projects')->addMember($this->id, $user_id, $access_level);
68+
69+
return User::fromArray($data);
70+
}
71+
72+
public function saveMember($user_id, $access_level)
73+
{
74+
$data = $this->api('projects')->saveMember($this->id, $user_id, $access_level);
75+
76+
return User::fromArray($data);
77+
}
78+
79+
public function removeMember($user_id)
80+
{
81+
$this->api('projects')->removeMember($this->id, $user_id);
82+
83+
return true;
84+
}
85+
86+
public function hooks()
87+
{
88+
return $this->api('projects')->hooks($this->id);
89+
}
90+
91+
public function hook($hook_id)
92+
{
93+
return $this->api('projects')->hook($this->id, $hook_id);
94+
}
95+
96+
public function addHook($url)
97+
{
98+
return $this->api('projects')->addHook($this->id, $url);
99+
}
100+
101+
public function updateHook($hook_id, $url)
102+
{
103+
return $this->api('projects')->updateHook($this->id, $hook_id, $url);
104+
}
105+
106+
public function removeHook($hook_id)
107+
{
108+
return $this->api('projects')->removeHook($this->id, $hook_id);
109+
}
110+
}

‎lib/Gitlab/Model/Session.php‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Gitlab\Model;
4+
5+
class Session extends AbstractModel
6+
{
7+
protected static $_properties = array(
8+
'id',
9+
'email',
10+
'name',
11+
'private_token',
12+
'created_at',
13+
'blocked'
14+
);
15+
16+
public static function fromArray(array $data)
17+
{
18+
$session = new Session();
19+
20+
return $session->hydrate($data);
21+
}
22+
23+
public function me()
24+
{
25+
$data = $this->api('users')->show();
26+
27+
return User::fromArray($data);
28+
}
29+
30+
public function login($email, $password)
31+
{
32+
$data = $this->api('users')->session($email, $password);
33+
34+
return $this->hydrate($data);
35+
}
36+
}

‎lib/Gitlab/Model/User.php‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class User extends AbstractModel
77
protected static $_properties = array(
88
'id',
99
'email',
10+
'password',
1011
'name',
1112
'bio',
1213
'skype',
@@ -15,6 +16,8 @@ class User extends AbstractModel
1516
'dark_scheme',
1617
'theme_id',
1718
'blocked',
19+
'projects_limit',
20+
'access_level',
1821
'created_at'
1922
);
2023

@@ -25,13 +28,23 @@ public static function fromArray(array $data)
2528
return $user->hydrate($data);
2629
}
2730

28-
public function __construct($id)
31+
public static function create($email, $password, array $params = array())
32+
{
33+
$data = static::client()->api('users')->create($email, $password, $params);
34+
35+
return User::fromArray($data);
36+
}
37+
38+
public function __construct($id = null)
2939
{
3040
$this->id = $id;
3141
}
3242

3343
public function show()
3444
{
35-
return $this->api('users')->show($this->id);
45+
$data = $this->api('users')->show($this->id);
46+
47+
return User::fromArray($data);
3648
}
49+
3750
}

0 commit comments

Comments
 (0)