diff --git a/doc/enterprise.md b/doc/enterprise.md new file mode 100644 index 00000000000..b4673061e74 --- /dev/null +++ b/doc/enterprise.md @@ -0,0 +1,24 @@ +## Enterprise API +[Back to the navigation](index.md) + +Provides information about a GitHub Enterprise installation. Wraps [GitHub Enterprise API](http://developer.github.com/v3/enterprise/). + +In order to configure the client to point to a GitHub Enterprise installation, do the following: + +```php +setEnterpriseUrl('https://ghe.host'); + +// Use the client as you would ordinarily +$repositories = $client->api('user')->repositories('ornicar'); +``` + +To use the Stats and License APIs, you will need to authenticate using a GitHub Enterprise site admin account. + diff --git a/doc/index.md b/doc/index.md index 1f447d6e457..2fd1cf8b521 100644 --- a/doc/index.md +++ b/doc/index.md @@ -4,6 +4,7 @@ Navigation APIs: * [Authorizations](authorizations.md) * [Commits](commits.md) +* [Enterprise](enterprise.md) * [Gists](gists.md) * [Issues](issues.md) * [Comments](issue/comments.md) diff --git a/lib/Github/Api/Enterprise.php b/lib/Github/Api/Enterprise.php new file mode 100644 index 00000000000..38e7cbff44c --- /dev/null +++ b/lib/Github/Api/Enterprise.php @@ -0,0 +1,32 @@ + + * @author Guillermo A. Fisher + */ +class Enterprise extends AbstractApi +{ + /** + * @return Stats + */ + public function stats() + { + return new Stats($this->client); + } + + /** + * @return License + */ + public function license() + { + return new License($this->client); + } +} diff --git a/lib/Github/Api/Enterprise/License.php b/lib/Github/Api/Enterprise/License.php new file mode 100644 index 00000000000..27785ca14e3 --- /dev/null +++ b/lib/Github/Api/Enterprise/License.php @@ -0,0 +1,19 @@ +get('enterprise/settings/license'); + } +} diff --git a/lib/Github/Api/Enterprise/Stats.php b/lib/Github/Api/Enterprise/Stats.php new file mode 100644 index 00000000000..934401b769c --- /dev/null +++ b/lib/Github/Api/Enterprise/Stats.php @@ -0,0 +1,128 @@ +show('issues'); + } + + /** + * Returns the number of active and inactive hooks + * + * @return array array with totals of active and inactive hooks + */ + public function hooks() + { + return $this->show('hooks'); + } + + /** + * Returns the number of open and closed milestones + * + * @return array array with totals of open and closed milestones + */ + public function milestones() + { + return $this->show('milestones'); + } + + /** + * Returns the number of organizations, teams, team members, and disabled organizations + * + * @return array array with totals of organizations, teams, team members, and disabled organizations + */ + public function orgs() + { + return $this->show('orgs'); + } + + /** + * Returns the number of comments on issues, pull requests, commits, and gists + * + * @return array array with totals of comments on issues, pull requests, commits, and gists + */ + public function comments() + { + return $this->show('comments'); + } + + /** + * Returns the number of GitHub Pages sites + * + * @return array array with totals of GitHub Pages sites + */ + public function pages() + { + return $this->show('pages'); + } + + /** + * Returns the number of suspended and admin users + * + * @return array array with totals of suspended and admin users + */ + public function users() + { + return $this->show('users'); + } + + /** + * Returns the number of private and public gists + * + * @return array array with totals of private and public gists + */ + public function gists() + { + return $this->show('gists'); + } + + /** + * Returns the number of merged, mergeable, and unmergeable pull requests + * + * @return array array with totals of merged, mergeable, and unmergeable pull requests + */ + public function pulls() + { + return $this->show('pulls'); + } + + /** + * Returns the number of organization-owned repositories, root repositories, forks, pushed commits, and wikis + * + * @return array array with totals of organization-owned repositories, root repositories, forks, pushed commits, and wikis + */ + public function repos() + { + return $this->show('repos'); + } + + /** + * Returns all of the statistics + * + * @return array array with all of the statistics + */ + public function all() + { + return $this->show('all'); + } + + /** + * @param string $type The type of statistics to show + * + * @return array + */ + public function show($type) + { + return $this->get('enterprise/stats/' . rawurlencode($type)); + } +} diff --git a/lib/Github/Client.php b/lib/Github/Client.php index 0ec20c21aa4..f9846d4f637 100644 --- a/lib/Github/Client.php +++ b/lib/Github/Client.php @@ -87,6 +87,11 @@ public function api($name) $api = new Api\CurrentUser($this); break; + case 'ent': + case 'enterprise': + $api = new Api\Enterprise($this); + break; + case 'git': case 'git_data': $api = new Api\GitData($this); @@ -173,6 +178,17 @@ public function authenticate($tokenOrLogin, $password = null, $authMethod = null $this->getHttpClient()->authenticate($tokenOrLogin, $password, $authMethod); } + /** + * Sets the URL of your GitHub Enterprise instance. + * + * @param string $enterpriseUrl URL of the API in the form of http(s)://hostname + */ + public function setEnterpriseUrl($enterpriseUrl) + { + $baseUrl = (substr($enterpriseUrl, -1) == '/') ? substr($enterpriseUrl, 0, -1) : $enterpriseUrl; + $this->getHttpClient()->client->setBaseUrl($baseUrl . '/api/v3'); + } + /** * @return HttpClient */ @@ -237,11 +253,21 @@ public function setOption($name, $value) if (!array_key_exists($name, $this->options)) { throw new InvalidArgumentException(sprintf('Undefined option called: "%s"', $name)); } - - if ('api_version' == $name && !in_array($value, array('v3', 'beta'))) { - throw new InvalidArgumentException(sprintf('Invalid API version ("%s"), valid are: %s', $name, implode(', ', array('v3', 'beta')))); + $supportedApiVersions = $this->getSupportedApiVersions(); + if ('api_version' == $name && !in_array($value, $supportedApiVersions)) { + throw new InvalidArgumentException(sprintf('Invalid API version ("%s"), valid are: %s', $name, implode(', ', $supportedApiVersions))); } $this->options[$name] = $value; } + + /** + * Returns an array of valid API versions supported by this client. + * + * @return array + */ + public function getSupportedApiVersions() + { + return array('v3', 'beta'); + } } diff --git a/test/Github/Tests/Api/EnterpriseTest.php b/test/Github/Tests/Api/EnterpriseTest.php new file mode 100644 index 00000000000..d284bb59282 --- /dev/null +++ b/test/Github/Tests/Api/EnterpriseTest.php @@ -0,0 +1,38 @@ +getApiMock(); + + $this->assertInstanceOf('Github\Api\Enterprise\Stats', $api->stats()); + } + + /** + * @test + */ + public function shouldGetEnterpriseLicenseApiObject() + { + $api = $this->getApiMock(); + + $this->assertInstanceOf('Github\Api\Enterprise\License', $api->license()); + } + + protected function getApiClass() + { + return 'Github\Api\Enterprise'; + } +} + \ No newline at end of file