Skip to content

Commit e4dbca3

Browse files
author
artur
committed
Update for Merge Requests API:
- getting MR in different states - `labels` property for MR model
1 parent 0e42b13 commit e4dbca3

File tree

3 files changed

+159
-112
lines changed

3 files changed

+159
-112
lines changed

lib/Gitlab/Api/MergeRequests.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,29 @@
44

55
class MergeRequests extends AbstractApi
66
{
7+
const STATE_ALL = 'all';
8+
const STATE_MERGED = 'merged';
9+
const STATE_OPENED = 'opened';
10+
const STATE_CLOSED = 'closed';
11+
712
public function all($project_id, $page = 1, $per_page = self::PER_PAGE)
813
{
9-
return $this->get('projects/'.urlencode($project_id).'/merge_requests', array(
10-
'page' => $page,
11-
'per_page' => $per_page
12-
));
14+
return $this->getMrList($project_id, $page, $per_page, self::STATE_ALL);
15+
}
16+
17+
public function merged($project_id, $page = 1, $per_page = self::PER_PAGE)
18+
{
19+
return $this->getMrList($project_id, $page, $per_page, self::STATE_MERGED);
20+
}
21+
22+
public function opened($project_id, $page = 1, $per_page = self::PER_PAGE)
23+
{
24+
return $this->getMrList($project_id, $page, $per_page, self::STATE_OPENED);
25+
}
26+
27+
public function closed($project_id, $page = 1, $per_page = self::PER_PAGE)
28+
{
29+
return $this->getMrList($project_id, $page, $per_page, self::STATE_CLOSED);
1330
}
1431

1532
public function show($project_id, $mr_id)
@@ -43,4 +60,13 @@ public function addComment($project_id, $mr_id, $note)
4360
'note' => $note
4461
));
4562
}
63+
64+
protected function getMrList($project_id, $page, $per_page, $state = self::STATE_ALL)
65+
{
66+
return $this->get('projects/'.urlencode($project_id).'/merge_requests', array(
67+
'page' => $page,
68+
'per_page' => $per_page,
69+
'state' => $state
70+
));
71+
}
4672
}

lib/Gitlab/Model/MergeRequest.php

Lines changed: 126 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,126 @@
1-
<?php
2-
3-
namespace Gitlab\Model;
4-
5-
use Gitlab\Client;
6-
7-
class MergeRequest extends AbstractModel
8-
{
9-
protected static $_properties = array(
10-
'id',
11-
'iid',
12-
'target_branch',
13-
'source_branch',
14-
'project_id',
15-
'title',
16-
'closed',
17-
'merged',
18-
'author',
19-
'assignee',
20-
'project',
21-
'state',
22-
'source_project_id',
23-
'target_project_id',
24-
'upvotes',
25-
'downvotes'
26-
);
27-
28-
public static function fromArray(Client $client, Project $project, array $data)
29-
{
30-
$mr = new MergeRequest($project, $data['id'], $client);
31-
32-
if (isset($data['author'])) {
33-
$data['author'] = User::fromArray($client, $data['author']);
34-
}
35-
36-
if (isset($data['assignee'])) {
37-
$data['assignee'] = User::fromArray($client, $data['assignee']);
38-
}
39-
40-
return $mr->hydrate($data);
41-
}
42-
43-
public function __construct(Project $project, $id = null, Client $client = null)
44-
{
45-
$this->setClient($client);
46-
47-
$this->project = $project;
48-
$this->id = $id;
49-
}
50-
51-
public function show()
52-
{
53-
$data = $this->api('mr')->show($this->project->id, $this->id);
54-
55-
return MergeRequest::fromArray($this->getClient(), $this->project, $data);
56-
}
57-
58-
public function update(array $params)
59-
{
60-
$data = $this->api('mr')->update($this->project->id, $this->id, $params);
61-
62-
return MergeRequest::fromArray($this->getClient(), $this->project, $data);
63-
}
64-
65-
public function close($comment = null)
66-
{
67-
if ($comment) {
68-
$this->addComment($comment);
69-
}
70-
71-
return $this->update(array(
72-
'state_event' => 'close'
73-
));
74-
}
75-
76-
public function reopen()
77-
{
78-
return $this->update(array(
79-
'state_event' => 'reopen'
80-
));
81-
}
82-
83-
public function merged()
84-
{
85-
return $this->update(array(
86-
'state_event' => 'merge'
87-
));
88-
}
89-
90-
public function addComment($note)
91-
{
92-
$data = $this->api('mr')->addComment($this->project->id, $this->id, $note);
93-
94-
return Note::fromArray($this->getClient(), $this, $data);
95-
}
96-
97-
public function isClosed()
98-
{
99-
if (in_array($this->state, array('closed', 'merged'))) {
100-
return true;
101-
}
102-
103-
return false;
104-
}
105-
106-
}
1+
<?php
2+
3+
namespace Gitlab\Model;
4+
5+
use Gitlab\Client;
6+
7+
class MergeRequest extends AbstractModel
8+
{
9+
protected static $_properties = array(
10+
'id',
11+
'iid',
12+
'target_branch',
13+
'source_branch',
14+
'project_id',
15+
'title',
16+
'closed',
17+
'merged',
18+
'author',
19+
'assignee',
20+
'project',
21+
'state',
22+
'source_project_id',
23+
'target_project_id',
24+
'upvotes',
25+
'downvotes',
26+
'labels'
27+
);
28+
29+
public static function fromArray(Client $client, Project $project, array $data)
30+
{
31+
$mr = new static($project, $data['id'], $client);
32+
33+
if (isset($data['author'])) {
34+
$data['author'] = User::fromArray($client, $data['author']);
35+
}
36+
37+
if (isset($data['assignee'])) {
38+
$data['assignee'] = User::fromArray($client, $data['assignee']);
39+
}
40+
41+
return $mr->hydrate($data);
42+
}
43+
44+
public function __construct(Project $project, $id = null, Client $client = null)
45+
{
46+
$this->setClient($client);
47+
48+
$this->project = $project;
49+
$this->id = $id;
50+
}
51+
52+
public function show()
53+
{
54+
$data = $this->api('mr')->show($this->project->id, $this->id);
55+
56+
return static::fromArray($this->getClient(), $this->project, $data);
57+
}
58+
59+
public function update(array $params)
60+
{
61+
$data = $this->api('mr')->update($this->project->id, $this->id, $params);
62+
63+
return static::fromArray($this->getClient(), $this->project, $data);
64+
}
65+
66+
public function close($comment = null)
67+
{
68+
if ($comment) {
69+
$this->addComment($comment);
70+
}
71+
72+
return $this->update(array(
73+
'state_event' => 'close'
74+
));
75+
}
76+
77+
public function reopen()
78+
{
79+
return $this->update(array(
80+
'state_event' => 'reopen'
81+
));
82+
}
83+
84+
public function merge($message = null)
85+
{
86+
$data = $this->api('mr')->merge($this->project->id, $this->id, array('merge_commit_message' => $message));
87+
88+
return static::fromArray($this->getClient(), $this->project, $data);
89+
}
90+
91+
public function merged()
92+
{
93+
return $this->update(array(
94+
'state_event' => 'merge'
95+
));
96+
}
97+
98+
public function addComment($note)
99+
{
100+
$data = $this->api('mr')->addComment($this->project->id, $this->id, $note);
101+
102+
return Note::fromArray($this->getClient(), $this, $data);
103+
}
104+
105+
public function showComments()
106+
{
107+
$notes = array();
108+
$data = $this->api('mr')->showComments($this->project->id, $this->id);
109+
110+
foreach ($data as $note) {
111+
$notes[] = Note::fromArray($this->getClient(), $this, $note);
112+
}
113+
114+
return $notes;
115+
}
116+
117+
public function isClosed()
118+
{
119+
if (in_array($this->state, array('closed', 'merged'))) {
120+
return true;
121+
}
122+
123+
return false;
124+
}
125+
126+
}

lib/Gitlab/Model/Project.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Gitlab\Model;
44

5+
use Gitlab\Api\MergeRequests;
56
use Gitlab\Client;
67
use Gitlab\Api\AbstractApi as Api;
78

@@ -311,9 +312,9 @@ public function events()
311312
return $events;
312313
}
313314

314-
public function mergeRequests($page = 1, $per_page = Api::PER_PAGE)
315+
public function mergeRequests($page = 1, $per_page = Api::PER_PAGE, $state = MergeRequests::STATE_ALL)
315316
{
316-
$data = $this->api('mr')->all($this->id, $page, $per_page);
317+
$data = $this->api('mr')->$state($this->id, $page, $per_page);
317318

318319
$mrs = array();
319320
foreach ($data as $mr) {

0 commit comments

Comments
 (0)