Skip to content

Commit 32ee5ef

Browse files
committed
User teams support: endpoints, model and methods from other models
1 parent c57c6e5 commit 32ee5ef

File tree

5 files changed

+214
-1
lines changed

5 files changed

+214
-1
lines changed

lib/Gitlab/Api/Teams.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Gitlab\Api;
4+
5+
class Teams extends AbstractApi
6+
{
7+
public function all()
8+
{
9+
return $this->get('user_teams');
10+
}
11+
12+
public function show($team_id)
13+
{
14+
return $this->get('user_teams/'.urlencode($team_id));
15+
}
16+
17+
public function create($name, $path)
18+
{
19+
return $this->post('user_teams', array(
20+
'name' => $name,
21+
'path' => $path
22+
));
23+
}
24+
25+
public function members($team_id)
26+
{
27+
return $this->get('user_teams/'.urlencode($team_id).'/members');
28+
}
29+
30+
public function member($team_id, $user_id)
31+
{
32+
return $this->get('user_teams/'.urlencode($team_id).'/members/'.urlencode($user_id));
33+
}
34+
35+
public function addMember($team_id, $user_id, $access_level)
36+
{
37+
return $this->post('user_teams/'.urlencode($team_id).'/members', array(
38+
'user_id' => $user_id,
39+
'access_level' => $access_level
40+
));
41+
}
42+
43+
public function removeMember($team_id, $user_id)
44+
{
45+
return $this->delete('user_teams/'.urlencode($team_id).'/members/'.urlencode($user_id));
46+
}
47+
48+
public function projects($team_id)
49+
{
50+
return $this->get('user_teams/'.urlencode($team_id).'/projects');
51+
}
52+
53+
public function project($team_id, $project_id)
54+
{
55+
return $this->get('user_teams/'.urlencode($team_id).'/projects/'.urlencode($project_id));
56+
}
57+
58+
public function addProject($team_id, $project_id, $greatest_access_level )
59+
{
60+
return $this->post('user_teams/'.urlencode($team_id).'/projects', array(
61+
'project_id' => $project_id,
62+
'greatest_access_level' => $greatest_access_level
63+
));
64+
}
65+
66+
public function removeProject($team_id, $project_id)
67+
{
68+
return $this->delete('user_teams/'.urlencode($team_id).'/projects/'.urlencode($project_id));
69+
}
70+
}

lib/Gitlab/Client.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ public function api($name)
106106
$api = new Api\SystemHooks($this);
107107
break;
108108

109+
case 'teams':
110+
case 'user_teams':
111+
$api = new Api\Teams($this);
112+
break;
113+
109114
default:
110115
throw new InvalidArgumentException();
111116
}

lib/Gitlab/Model/Project.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class Project extends AbstractModel
2323
'merge_requests_enabled',
2424
'wall_enabled',
2525
'wiki_enabled',
26-
'created_at'
26+
'created_at',
27+
'greatest_access_level'
2728
);
2829

2930
public static function fromArray(Client $client, array $data)
@@ -98,6 +99,20 @@ public function removeMember($user_id)
9899
return true;
99100
}
100101

102+
public function addToTeam($team_id, $greatest_access_level)
103+
{
104+
$team = new Team($team_id, $this->getClient());
105+
106+
return $team->addProject($this->id, $greatest_access_level);
107+
}
108+
109+
public function removeFromTeam($team_id)
110+
{
111+
$team = new Team($team_id, $this->getClient());
112+
113+
return $team->removeProject($this->id);
114+
}
115+
101116
public function hooks($page = 1, $per_page = Api::PER_PAGE)
102117
{
103118
$data = $this->api('projects')->hooks($this->id, $page, $per_page);

lib/Gitlab/Model/Team.php

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

lib/Gitlab/Model/User.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,18 @@ public function removeKey($id)
8484
return true;
8585
}
8686

87+
public function addToTeam($team_id, $access_level)
88+
{
89+
$team = new Team($team_id, $this->getClient());
90+
91+
return $team->addMember($this->id, $access_level);
92+
}
93+
94+
public function removeFromTeam($team_id)
95+
{
96+
$team = new Team($team_id, $this->getClient());
97+
98+
return $team->removeMember($this->id);
99+
}
100+
87101
}

0 commit comments

Comments
 (0)