Skip to content

Commit a462c35

Browse files
committed
Merge pull request KnpLabs#148 from guillermoandrae/master
Added GitHub Enterprise classes, tests, and documentation.
2 parents c5475e2 + 861cf2e commit a462c35

File tree

7 files changed

+271
-3
lines changed

7 files changed

+271
-3
lines changed

doc/enterprise.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## Enterprise API
2+
[Back to the navigation](index.md)
3+
4+
Provides information about a GitHub Enterprise installation. Wraps [GitHub Enterprise API](http://developer.github.com/v3/enterprise/).
5+
6+
In order to configure the client to point to a GitHub Enterprise installation, do the following:
7+
8+
```php
9+
<?php
10+
11+
// This file is generated by Composer
12+
require_once 'vendor/autoload.php';
13+
14+
$client = new \Github\Client();
15+
16+
// Set the URL of your GitHub Enterprise installation
17+
$client->setEnterpriseUrl('https://ghe.host');
18+
19+
// Use the client as you would ordinarily
20+
$repositories = $client->api('user')->repositories('ornicar');
21+
```
22+
23+
To use the Stats and License APIs, you will need to authenticate using a GitHub Enterprise site admin account.
24+

doc/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Navigation
44
APIs:
55
* [Authorizations](authorizations.md)
66
* [Commits](commits.md)
7+
* [Enterprise](enterprise.md)
78
* [Gists](gists.md)
89
* [Issues](issues.md)
910
* [Comments](issue/comments.md)

lib/Github/Api/Enterprise.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Github\Api;
4+
5+
use Github\Api\Enterprise\Stats;
6+
use Github\Api\Enterprise\License;
7+
8+
/**
9+
* Getting information about a GitHub Enterprise instance.
10+
*
11+
* @link https://developer.github.com/v3/enterprise/
12+
* @author Joseph Bielawski <stloyd@gmail.com>
13+
* @author Guillermo A. Fisher <guillermoandraefisher@gmail.com>
14+
*/
15+
class Enterprise extends AbstractApi
16+
{
17+
/**
18+
* @return Stats
19+
*/
20+
public function stats()
21+
{
22+
return new Stats($this->client);
23+
}
24+
25+
/**
26+
* @return License
27+
*/
28+
public function license()
29+
{
30+
return new License($this->client);
31+
}
32+
}

lib/Github/Api/Enterprise/License.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Github\Api\Enterprise;
4+
5+
use Github\Api\AbstractApi;
6+
7+
class License extends AbstractApi
8+
{
9+
/**
10+
* Provides information about your Enterprise license (only available to site admins).
11+
* @link https://developer.github.com/v3/enterprise/license/
12+
*
13+
* @return array array of license information
14+
*/
15+
public function show()
16+
{
17+
return $this->get('enterprise/settings/license');
18+
}
19+
}

lib/Github/Api/Enterprise/Stats.php

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
3+
namespace Github\Api\Enterprise;
4+
5+
use Github\Api\AbstractApi;
6+
7+
class Stats extends AbstractApi
8+
{
9+
/**
10+
* Returns the number of open and closed issues
11+
*
12+
* @return array array with totals of open and closed issues
13+
*/
14+
public function issues()
15+
{
16+
return $this->show('issues');
17+
}
18+
19+
/**
20+
* Returns the number of active and inactive hooks
21+
*
22+
* @return array array with totals of active and inactive hooks
23+
*/
24+
public function hooks()
25+
{
26+
return $this->show('hooks');
27+
}
28+
29+
/**
30+
* Returns the number of open and closed milestones
31+
*
32+
* @return array array with totals of open and closed milestones
33+
*/
34+
public function milestones()
35+
{
36+
return $this->show('milestones');
37+
}
38+
39+
/**
40+
* Returns the number of organizations, teams, team members, and disabled organizations
41+
*
42+
* @return array array with totals of organizations, teams, team members, and disabled organizations
43+
*/
44+
public function orgs()
45+
{
46+
return $this->show('orgs');
47+
}
48+
49+
/**
50+
* Returns the number of comments on issues, pull requests, commits, and gists
51+
*
52+
* @return array array with totals of comments on issues, pull requests, commits, and gists
53+
*/
54+
public function comments()
55+
{
56+
return $this->show('comments');
57+
}
58+
59+
/**
60+
* Returns the number of GitHub Pages sites
61+
*
62+
* @return array array with totals of GitHub Pages sites
63+
*/
64+
public function pages()
65+
{
66+
return $this->show('pages');
67+
}
68+
69+
/**
70+
* Returns the number of suspended and admin users
71+
*
72+
* @return array array with totals of suspended and admin users
73+
*/
74+
public function users()
75+
{
76+
return $this->show('users');
77+
}
78+
79+
/**
80+
* Returns the number of private and public gists
81+
*
82+
* @return array array with totals of private and public gists
83+
*/
84+
public function gists()
85+
{
86+
return $this->show('gists');
87+
}
88+
89+
/**
90+
* Returns the number of merged, mergeable, and unmergeable pull requests
91+
*
92+
* @return array array with totals of merged, mergeable, and unmergeable pull requests
93+
*/
94+
public function pulls()
95+
{
96+
return $this->show('pulls');
97+
}
98+
99+
/**
100+
* Returns the number of organization-owned repositories, root repositories, forks, pushed commits, and wikis
101+
*
102+
* @return array array with totals of organization-owned repositories, root repositories, forks, pushed commits, and wikis
103+
*/
104+
public function repos()
105+
{
106+
return $this->show('repos');
107+
}
108+
109+
/**
110+
* Returns all of the statistics
111+
*
112+
* @return array array with all of the statistics
113+
*/
114+
public function all()
115+
{
116+
return $this->show('all');
117+
}
118+
119+
/**
120+
* @param string $type The type of statistics to show
121+
*
122+
* @return array
123+
*/
124+
public function show($type)
125+
{
126+
return $this->get('enterprise/stats/' . rawurlencode($type));
127+
}
128+
}

lib/Github/Client.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ public function api($name)
8787
$api = new Api\CurrentUser($this);
8888
break;
8989

90+
case 'ent':
91+
case 'enterprise':
92+
$api = new Api\Enterprise($this);
93+
break;
94+
9095
case 'git':
9196
case 'git_data':
9297
$api = new Api\GitData($this);
@@ -177,6 +182,17 @@ public function authenticate($tokenOrLogin, $password = null, $authMethod = null
177182
$this->getHttpClient()->authenticate($tokenOrLogin, $password, $authMethod);
178183
}
179184

185+
/**
186+
* Sets the URL of your GitHub Enterprise instance.
187+
*
188+
* @param string $enterpriseUrl URL of the API in the form of http(s)://hostname
189+
*/
190+
public function setEnterpriseUrl($enterpriseUrl)
191+
{
192+
$baseUrl = (substr($enterpriseUrl, -1) == '/') ? substr($enterpriseUrl, 0, -1) : $enterpriseUrl;
193+
$this->getHttpClient()->client->setBaseUrl($baseUrl . '/api/v3');
194+
}
195+
180196
/**
181197
* @return HttpClient
182198
*/
@@ -241,11 +257,21 @@ public function setOption($name, $value)
241257
if (!array_key_exists($name, $this->options)) {
242258
throw new InvalidArgumentException(sprintf('Undefined option called: "%s"', $name));
243259
}
244-
245-
if ('api_version' == $name && !in_array($value, array('v3', 'beta'))) {
246-
throw new InvalidArgumentException(sprintf('Invalid API version ("%s"), valid are: %s', $name, implode(', ', array('v3', 'beta'))));
260+
$supportedApiVersions = $this->getSupportedApiVersions();
261+
if ('api_version' == $name && !in_array($value, $supportedApiVersions)) {
262+
throw new InvalidArgumentException(sprintf('Invalid API version ("%s"), valid are: %s', $name, implode(', ', $supportedApiVersions)));
247263
}
248264

249265
$this->options[$name] = $value;
250266
}
267+
268+
/**
269+
* Returns an array of valid API versions supported by this client.
270+
*
271+
* @return array
272+
*/
273+
public function getSupportedApiVersions()
274+
{
275+
return array('v3', 'beta');
276+
}
251277
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* This file is part of the PHP GitHub API package.
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*/
8+
9+
namespace Github\Tests\Api;
10+
11+
class EnterpriseTest extends TestCase
12+
{
13+
/**
14+
* @test
15+
*/
16+
public function shouldGetEntepriseStatsApiObject()
17+
{
18+
$api = $this->getApiMock();
19+
20+
$this->assertInstanceOf('Github\Api\Enterprise\Stats', $api->stats());
21+
}
22+
23+
/**
24+
* @test
25+
*/
26+
public function shouldGetEnterpriseLicenseApiObject()
27+
{
28+
$api = $this->getApiMock();
29+
30+
$this->assertInstanceOf('Github\Api\Enterprise\License', $api->license());
31+
}
32+
33+
protected function getApiClass()
34+
{
35+
return 'Github\Api\Enterprise';
36+
}
37+
}
38+

0 commit comments

Comments
 (0)