Skip to content

Commit 05838a8

Browse files
iXiamm1guelpf
authored andcommitted
Add project badges API
1 parent 3ec9c85 commit 05838a8

File tree

4 files changed

+263
-0
lines changed

4 files changed

+263
-0
lines changed

lib/Gitlab/Api/Projects.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,4 +837,54 @@ public function removeShare($project_id, $group_id)
837837
{
838838
return $this->delete($this->getProjectPath($project_id, 'services/'.$group_id));
839839
}
840+
841+
/**
842+
* @param int $project_id
843+
* @return mixed
844+
*/
845+
public function badges($project_id)
846+
{
847+
return $this->get($this->getProjectPath($project_id, 'badges'));
848+
}
849+
850+
/**
851+
* @param int $project_id
852+
* @param string $badge_id
853+
* @return mixed
854+
*/
855+
public function badge($project_id, $badge_id)
856+
{
857+
return $this->get($this->getProjectPath($project_id, 'badges/' . $this->encodePath($badge_id)));
858+
}
859+
860+
/**
861+
* @param int $project_id
862+
* @param array $params
863+
* @return mixed
864+
*/
865+
public function addBadge($project_id, array $params = array())
866+
{
867+
return $this->post($this->getProjectPath($project_id, 'badges'), $params);
868+
}
869+
870+
/**
871+
* @param int $project_id
872+
* @param string $badge_id
873+
* @return mixed
874+
*/
875+
public function removeBadge($project_id, $badge_id)
876+
{
877+
return $this->delete($this->getProjectPath($project_id, 'badges/' . $this->encodePath($badge_id)));
878+
}
879+
880+
/**
881+
* @param int $project_id
882+
* @param string $badge_id
883+
* @param array $params
884+
* @return mixed
885+
*/
886+
public function updateBadge($project_id, $badge_id, array $params = array())
887+
{
888+
return $this->put($this->getProjectPath($project_id, 'badges/' . $this->encodePath($badge_id)));
889+
}
840890
}

lib/Gitlab/Model/Badge.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php namespace Gitlab\Model;
2+
3+
use Gitlab\Client;
4+
5+
/**
6+
* Class Badge
7+
*
8+
* @property-read string $link_url
9+
* @property-read string $image_url
10+
*/
11+
class Badge extends AbstractModel
12+
{
13+
/**
14+
* @var array
15+
*/
16+
protected static $properties = array(
17+
'link_url',
18+
'image_url'
19+
);
20+
21+
/**
22+
* @param Client $client
23+
* @param Project $project
24+
* @param array $data
25+
* @return Badge
26+
*/
27+
public static function fromArray(Client $client, Project $project, array $data)
28+
{
29+
$badge = new static($project, $client);
30+
31+
return $badge->hydrate($data);
32+
}
33+
34+
/**
35+
* @param Project $project
36+
* @param Client $client
37+
*/
38+
public function __construct(Project $project, Client $client = null)
39+
{
40+
$this->setClient($client);
41+
$this->setData('project', $project);
42+
}
43+
}

lib/Gitlab/Model/Project.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,4 +1200,56 @@ public function job($job_id)
12001200

12011201
return Job::fromArray($this->getClient(), $this, $data);
12021202
}
1203+
1204+
/**
1205+
* @return Badge[]
1206+
*/
1207+
public function badges()
1208+
{
1209+
$data = $this->client->projects()->badges($this->id);
1210+
1211+
$badges = array();
1212+
foreach ($data as $badge) {
1213+
$badges[] = Badge::fromArray($this->getClient(), $this, $badge);
1214+
}
1215+
1216+
return $badges;
1217+
}
1218+
1219+
/**
1220+
* @param string $link_url
1221+
* @param string $color
1222+
* @return Badge
1223+
*/
1224+
public function addBadge(array $params)
1225+
{
1226+
$data = $this->client->projects()->addBadge($this->id, $params);
1227+
1228+
return Badge::fromArray($this->getClient(), $this, $data);
1229+
}
1230+
1231+
/**
1232+
* @param string $name
1233+
* @param array $params
1234+
* @return Badge
1235+
*/
1236+
public function updateBadge($badge_id, array $params)
1237+
{
1238+
$params['badge_id'] = $badge_id;
1239+
1240+
$data = $this->client->projects()->updateBadge($this->id, $badge_id, $params);
1241+
1242+
return Badge::fromArray($this->getClient(), $this, $data);
1243+
}
1244+
1245+
/**
1246+
* @param string $name
1247+
* @return bool
1248+
*/
1249+
public function removeBadge($badge_id)
1250+
{
1251+
$this->client->projects()->removeBadge($this->id, $badge_id);
1252+
1253+
return true;
1254+
}
12031255
}

test/Gitlab/Tests/Api/ProjectsTest.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,6 +1601,124 @@ public function possibleAccessLevels()
16011601
];
16021602
}
16031603

1604+
public function getBadgeExpectedArray()
1605+
{
1606+
return [
1607+
[
1608+
"id" => 1,
1609+
"link_url" => "http://example.com/ci_status.svg?project=%{project_path}&ref=%{default_branch}",
1610+
"image_url" => "https://shields.io/my/badge",
1611+
"rendered_link_url" => "http://example.com/ci_status.svg?project=example-org/example-project&ref=master",
1612+
"rendered_image_url" => "https://shields.io/my/badge",
1613+
"kind" => "project"
1614+
],
1615+
[
1616+
"id" => 2,
1617+
"link_url" => "http://example.com/ci_status.svg?project=%{project_path}&ref=%{default_branch}",
1618+
"image_url" => "https://shields.io/my/badge",
1619+
"rendered_link_url" => "http://example.com/ci_status.svg?project=example-org/example-project&ref=master",
1620+
"rendered_image_url" => "https://shields.io/my/badge",
1621+
"kind" => "group"
1622+
],
1623+
];
1624+
}
1625+
/**
1626+
* @test
1627+
*/
1628+
public function shouldGetBadges()
1629+
{
1630+
$expectedArray = $this->getBadgeExpectedArray();
1631+
1632+
$api = $this->getApiMock();
1633+
$api->expects($this->once())
1634+
->method('get')
1635+
->with('projects/1/badges')
1636+
->will($this->returnValue($expectedArray))
1637+
;
1638+
1639+
$this->assertEquals($expectedArray, $api->badges(1));
1640+
}
1641+
1642+
/**
1643+
* @test
1644+
*/
1645+
public function shouldGetBadge()
1646+
{
1647+
$expectedBadgesArray = $this->getBadgeExpectedArray();
1648+
$expectedArray = array(
1649+
$expectedBadgesArray[0]
1650+
);
1651+
1652+
$api = $this->getApiMock();
1653+
$api->expects($this->once())
1654+
->method('get')
1655+
->with('projects/1/badges/1')
1656+
->will($this->returnValue($expectedArray))
1657+
;
1658+
1659+
$this->assertEquals($expectedArray, $api->badge(1, 1));
1660+
}
1661+
1662+
/**
1663+
* @test
1664+
*/
1665+
public function shouldAddBadge()
1666+
{
1667+
$link_url = 'http://example.com/ci_status.svg?project=%{project_path}&ref=%{default_branch}';
1668+
$image_url = 'https://shields.io/my/badge';
1669+
$expectedArray = array(
1670+
'id' => 3,
1671+
'link_url' => $link_url,
1672+
'image_url' => $image_url,
1673+
);
1674+
1675+
$api = $this->getApiMock();
1676+
$api->expects($this->once())
1677+
->method('post')
1678+
->with('projects/1/badges', array('link_url' => $link_url, 'image_url' => $image_url))
1679+
->will($this->returnValue($expectedArray))
1680+
;
1681+
1682+
$this->assertEquals($expectedArray, $api->addBadge(1, array('link_url' => $link_url, 'image_url' => $image_url)));
1683+
}
1684+
1685+
/**
1686+
* @test
1687+
*/
1688+
public function shouldUpdateBadge()
1689+
{
1690+
$image_url = 'https://shields.io/my/new/badge';
1691+
$expectedArray = array(
1692+
'id' => 2,
1693+
);
1694+
1695+
$api = $this->getApiMock();
1696+
$api->expects($this->once())
1697+
->method('put')
1698+
->with('projects/1/badges/2')
1699+
->will($this->returnValue($expectedArray))
1700+
;
1701+
1702+
$this->assertEquals($expectedArray, $api->updateBadge(1, 2, array('image_url' => $image_url)));
1703+
}
1704+
1705+
/**
1706+
* @test
1707+
*/
1708+
public function shouldRemoveBadge()
1709+
{
1710+
$expectedBool = true;
1711+
1712+
$api = $this->getApiMock();
1713+
$api->expects($this->once())
1714+
->method('delete')
1715+
->with('projects/1/badges/1')
1716+
->will($this->returnValue($expectedBool))
1717+
;
1718+
1719+
$this->assertEquals($expectedBool, $api->removeBadge(1, 1));
1720+
}
1721+
16041722
protected function getApiClass()
16051723
{
16061724
return 'Gitlab\Api\Projects';

0 commit comments

Comments
 (0)