From ce09c70669e734e73a6ce296651539ea3d702e19 Mon Sep 17 00:00:00 2001 From: Joseph Bielawski Date: Thu, 12 Jul 2012 09:45:34 +0200 Subject: [PATCH 1/2] Added way to parse pagination headers from GitHub. --- lib/Github/HttpClient/HttpClient.php | 46 ++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/lib/Github/HttpClient/HttpClient.php b/lib/Github/HttpClient/HttpClient.php index 21af8cb3b0a..02e6d6021bb 100644 --- a/lib/Github/HttpClient/HttpClient.php +++ b/lib/Github/HttpClient/HttpClient.php @@ -56,6 +56,11 @@ class HttpClient implements HttpClientInterface */ protected $browser; + /** + * @var array + */ + private $lastResponse; + /** * Instantiated a new http client * @@ -101,6 +106,18 @@ public function setOption($name, $value) return $this; } + /** + * @return null|array + */ + public function getPagination() + { + if (null === $this->lastResponse) { + return null; + } + + return $this->lastResponse['pagination']; + } + /** * {@inheritDoc} */ @@ -162,9 +179,9 @@ public function request($path, array $parameters = array(), $httpMethod = 'GET', )); // get encoded response - $response = $this->doRequest($url, $parameters, $httpMethod, $options); + $this->lastResponse = $this->doRequest($url, $parameters, $httpMethod, $options); - return $this->decodeResponse($response['response']); + return $this->decodeResponse($this->lastResponse['response']); } /** @@ -186,11 +203,36 @@ protected function doRequest($url, array $parameters = array(), $httpMethod = 'G return array( 'response' => $response->getContent(), 'headers' => $response->getHeaders(), + 'pagination' => $this->decodePagination($response), 'errorNumber' => '', 'errorMessage' => '' ); } + /** + * @param MessageInterface $response + * + * @return array|null + */ + protected function decodePagination(MessageInterface $response) + { + $header = $response->getHeader('Link'); + if (empty($header)) { + return null; + } + + $pagination = array(); + foreach (explode("\n", $header) as $link) { + preg_match('/<(.*)>; rel="(.*)"/i', trim($link, ','), $match); + + if (3 === count($match)) { + $pagination[$match[2]] = $match[1]; + } + } + + return $pagination; + } + /** * {@inheritDoc} */ From da6e3879b5658908c3cdf562dacbc458675586ca Mon Sep 17 00:00:00 2001 From: Joseph Bielawski Date: Tue, 17 Jul 2012 12:15:48 +0200 Subject: [PATCH 2/2] Big refactoring of structure, many new features added, many polished, and few fixes --- .travis.yml | 3 +- CHANGELOG | 123 --- LICENSE | 1 + README.markdown | 767 +----------------- behat.yml.dist | 10 - composer.json | 3 - doc/commits.md | 45 + doc/customize.md | 60 ++ doc/gists.md | 140 ++++ doc/index.md | 17 + doc/issues.md | 154 ++++ doc/pull_requests.md | 87 ++ doc/repos.md | 272 +++++++ doc/request_any_route.md | 15 + doc/security.md | 39 + doc/users.md | 161 ++++ features/bootstrap/FeatureContext.php | 99 --- features/markdown.feature | 30 - features/rate_limit.feature | 18 - lib/Github/Api/{Api.php => AbstractApi.php} | 2 +- lib/Github/Api/Commit.php | 86 -- lib/Github/Api/CurrentUser.php | 76 ++ lib/Github/Api/CurrentUser/DeployKeys.php | 85 ++ lib/Github/Api/CurrentUser/Emails.php | 64 ++ lib/Github/Api/CurrentUser/Followers.php | 62 ++ lib/Github/Api/CurrentUser/Watchers.php | 65 ++ lib/Github/Api/Gists.php | 71 ++ lib/Github/Api/GitData.php | 58 ++ lib/Github/Api/GitData/Blobs.php | 37 + lib/Github/Api/GitData/Commits.php | 27 + lib/Github/Api/GitData/References.php | 56 ++ lib/Github/Api/GitData/Tags.php | 40 + lib/Github/Api/GitData/Trees.php | 35 + lib/Github/Api/Issue.php | 230 ++---- lib/Github/Api/Issue/Comments.php | 48 ++ lib/Github/Api/Issue/Events.php | 30 + lib/Github/Api/Issue/Labels.php | 44 + lib/Github/Api/Issue/Milestones.php | 64 ++ lib/Github/Api/Markdown.php | 2 +- lib/Github/Api/Object.php | 94 --- lib/Github/Api/Organization.php | 126 +-- lib/Github/Api/Organization/Members.php | 46 ++ lib/Github/Api/Organization/Teams.php | 95 +++ lib/Github/Api/PullRequest.php | 132 +-- lib/Github/Api/PullRequest/Comments.php | 51 ++ lib/Github/Api/Repo.php | 384 +++------ lib/Github/Api/Repository/Collaborators.php | 32 + lib/Github/Api/Repository/Comments.php | 50 ++ lib/Github/Api/Repository/Commits.php | 27 + lib/Github/Api/Repository/Contents.php | 38 + lib/Github/Api/Repository/DeployKeys.php | 46 ++ lib/Github/Api/Repository/Downloads.php | 56 ++ lib/Github/Api/Repository/Forks.php | 26 + lib/Github/Api/Repository/Hooks.php | 51 ++ lib/Github/Api/Repository/Labels.php | 46 ++ lib/Github/Api/User.php | 140 +--- lib/Github/Client.php | 173 +--- .../Exception/MissingArgumentException.php | 23 + lib/Github/HttpClient/HttpClient.php | 26 +- lib/Github/HttpClient/HttpClientInterface.php | 19 +- .../Listener/{Auth.php => AuthListener.php} | 28 +- test/Github/Tests/Api/CommitTest.php | 12 +- test/Github/Tests/Api/GistTest.php | 92 +++ test/Github/Tests/Api/IssueTest.php | 165 +--- test/Github/Tests/Api/MarkdownTest.php | 26 + test/Github/Tests/Api/PullRequestTest.php | 66 +- test/Github/Tests/Api/RepoTest.php | 11 +- test/Github/Tests/ClientTest.php | 39 +- test/Github/Tests/Functional/CommitTest.php | 8 +- test/Github/Tests/Functional/GistTest.php | 73 ++ test/Github/Tests/Functional/MarkdownTest.php | 29 + test/Github/Tests/Functional/ObjectTest.php | 82 -- test/Github/Tests/Functional/RepoTest.php | 31 +- test/Github/Tests/Functional/UserTest.php | 14 +- test/Github/Tests/HttpClientTest.php | 11 +- 75 files changed, 3233 insertions(+), 2361 deletions(-) delete mode 100755 CHANGELOG delete mode 100644 behat.yml.dist create mode 100644 doc/commits.md create mode 100644 doc/customize.md create mode 100644 doc/gists.md create mode 100644 doc/index.md create mode 100644 doc/issues.md create mode 100644 doc/pull_requests.md create mode 100644 doc/repos.md create mode 100644 doc/request_any_route.md create mode 100644 doc/security.md create mode 100644 doc/users.md delete mode 100644 features/bootstrap/FeatureContext.php delete mode 100644 features/markdown.feature delete mode 100644 features/rate_limit.feature rename lib/Github/Api/{Api.php => AbstractApi.php} (96%) delete mode 100644 lib/Github/Api/Commit.php create mode 100644 lib/Github/Api/CurrentUser.php create mode 100644 lib/Github/Api/CurrentUser/DeployKeys.php create mode 100644 lib/Github/Api/CurrentUser/Emails.php create mode 100644 lib/Github/Api/CurrentUser/Followers.php create mode 100644 lib/Github/Api/CurrentUser/Watchers.php create mode 100644 lib/Github/Api/Gists.php create mode 100644 lib/Github/Api/GitData.php create mode 100644 lib/Github/Api/GitData/Blobs.php create mode 100644 lib/Github/Api/GitData/Commits.php create mode 100644 lib/Github/Api/GitData/References.php create mode 100644 lib/Github/Api/GitData/Tags.php create mode 100644 lib/Github/Api/GitData/Trees.php create mode 100644 lib/Github/Api/Issue/Comments.php create mode 100644 lib/Github/Api/Issue/Events.php create mode 100644 lib/Github/Api/Issue/Labels.php create mode 100644 lib/Github/Api/Issue/Milestones.php delete mode 100644 lib/Github/Api/Object.php create mode 100644 lib/Github/Api/Organization/Members.php create mode 100644 lib/Github/Api/Organization/Teams.php create mode 100644 lib/Github/Api/PullRequest/Comments.php create mode 100644 lib/Github/Api/Repository/Collaborators.php create mode 100644 lib/Github/Api/Repository/Comments.php create mode 100644 lib/Github/Api/Repository/Commits.php create mode 100644 lib/Github/Api/Repository/Contents.php create mode 100644 lib/Github/Api/Repository/DeployKeys.php create mode 100644 lib/Github/Api/Repository/Downloads.php create mode 100644 lib/Github/Api/Repository/Forks.php create mode 100644 lib/Github/Api/Repository/Hooks.php create mode 100644 lib/Github/Api/Repository/Labels.php create mode 100644 lib/Github/Exception/MissingArgumentException.php rename lib/Github/HttpClient/Listener/{Auth.php => AuthListener.php} (72%) create mode 100644 test/Github/Tests/Api/GistTest.php create mode 100644 test/Github/Tests/Api/MarkdownTest.php create mode 100644 test/Github/Tests/Functional/GistTest.php create mode 100644 test/Github/Tests/Functional/MarkdownTest.php delete mode 100644 test/Github/Tests/Functional/ObjectTest.php diff --git a/.travis.yml b/.travis.yml index 51e48050712..517a42a8446 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,5 +9,4 @@ before_script: - php composer.phar install --dev script: - - phpunit - - bin/behat + - phpunit --coverage-text diff --git a/CHANGELOG b/CHANGELOG deleted file mode 100755 index 7a7163092b8..00000000000 --- a/CHANGELOG +++ /dev/null @@ -1,123 +0,0 @@ -V3.2 - 2011-06-09 - - * Add pull request API thanks to https://github.com/jeanvoye - -V3.1 - 2011-02-24 - -Bugfixes release - - * Fix PHP 5.2 compatibility - * Fix bug when instantiating a client without passing a http client - * Fix bug when sending authenticated requests using curl driver - -V3.0 - 2011-02-12 - -Major refactoring, nearly BC compatible. See migration in same dir. - - * PEAR conventions for files and class names - * PEAR coding standards - * Builtin optional autoloader - * Demeter's law compliance - * PHPUnit for tests instead of lime - * Mock github responses in tests - * Allow different http drivers - -V2.12 - 2010-10-17 - - * add support for HTTP authentication - * complete repository API - * complete user API - -V2.11 - 2010-07-22 - - * add phpGitHubIssueApi->searchLabel - -v2.10 - 2010-07-10 - - * add phpGitHubRepoApi->getRepoContributors - -v2.9 - 2010-07-03 - - * add advanced repository search - -v2.8 - 2010-06-07 - - * add phpGitHubRepoApi->create - * add phpGitHubRepoApi->delete - -v2.7 - 2010-05-20 - - * add phpGitHubApiCommit->getCommit - * allow 201 as valid HTTP response code - * fix issues unit tests - -v2.6 - 2010-04-10 - - * add phpGitHubApiRepo->search - * add phpGitHubApiRepo->show - * add phpGitHubApiRepo->getUserRepos - * add phpGitHubApiRepo->getRepoTags - * add phpGitHubApiRepo->getRepoBranches - * encode url parameters properly - * move tests - * improve documentation - -v2.5 - 2010-04-08 - - * add phpGitHubApiIssue->open() - * add phpGitHubApiIssue->close() - * add phpGitHubApiIssue->reOpen() - * add phpGitHubApiIssue->update() - * add phpGitHubApiIssue->getComments() - * add phpGitHubApiIssue->addComment() - * add phpGitHubApiIssue->getLabels() - * add phpGitHubApiIssue->addLabel() - * add phpGitHubApiIssue->removeLabel() - * Issue API completed. - -v2.4 - 2010-04-08 - - * add phpGitHubApiUser->getEmails() - * add phpGitHubApiUser->addEmail() - * add phpGitHubApiUser->removeEmail() - -v2.3 - 2010-04-08 - - * add phpGitHubApiUser->follow() - * add phpGitHubApiUser->unFollow() - * improved user documentation and phpDoc - -v2.2 - 2010-04-08 - - * add phpGitHubApiUser->getFollowing() - * add phpGitHubApiUser->getFollowers() - -v2.1 - 2010-04-08 - - * add phpGitHubApiObject->getRawData() - * allow to reconfigure the request for one call only - * make response support text format - -v2.0 - 2010-04-08 - - * refactored the whole API for better extensibility and performance; BC compatibility preserved - * each API part has a dedicated class - * request instance can be injected with phpGitHubApi->setRequest($request) - * API instances can be injected with phpGitHubApi->setApi($name, $api) - * request and API instances are lazy loaded for best performance - * added phpGitHubApiUser->update method - * 100% unit test coverage - -v1.2 - 2010-04-07 - - * addition of the deAuthenticate method - * added unit tests - * refactored the request class - -v1.1 - 2010-04-06 - - * addition of GitHub Object API - -v1.0 - 2010-02-13 - - * Created initial 1.0 version diff --git a/LICENSE b/LICENSE index dd2630b84b9..0fd8dd8ea25 100755 --- a/LICENSE +++ b/LICENSE @@ -1,5 +1,6 @@ The MIT License +Copyright (c) 2012 KnpLabs Copyright (c) 2010 Thibault Duplessis Permission is hereby granted, free of charge, to any person obtaining a copy diff --git a/README.markdown b/README.markdown index 9adf5050173..92f25febd80 100755 --- a/README.markdown +++ b/README.markdown @@ -1,15 +1,8 @@ -# PHP GitHub API [WIP] +# PHP GitHub API -[![Build Status](https://secure.travis-ci.org/KnpLabs/php-github-api.png?branch=api_v3)](http://travis-ci.org/KnpLabs/php-github-api) +[![Build Status](https://secure.travis-ci.org/KnpLabs/php-github-api.png)](http://travis-ci.org/KnpLabs/php-github-api) -[WARNING] We are converting that lib into "github api v3" so some stuff is not working yet... - -A simple Object Oriented wrapper for GitHub API, written with PHP5. - -```php - $github = new Github\Client(); - $myRepos = $github->getRepoApi()->getUserRepos('ornicar'); -``` +A simple Object Oriented wrapper for GitHub API, written with PHP5. Uses [GitHub API v3](http://developer.github.com/v3/). The object API is very similar to the RESTful API. @@ -17,761 +10,71 @@ Uses [GitHub API v3](http://developer.github.com/v3/). The object API is very si * Follows PSR-0 conventions and coding standard: autoload friendly * Light and fast thanks to lazy loading of API classes -* Flexible and extensible thanks to dependency injection * Extensively tested and documented ## Requirements -* PHP >= 5.3 -* PHPUnit to run tests. +* PHP >= 5.3.2 with [cURL](http://php.net/manual/en/book.curl.php) extension, +* [Buzz](https://github.com/kriswallsmith/Buzz) library, +* (optional) PHPUnit to run tests. ## Autoload The new version of php-github-api using [composer](http://getcomposer.org). -The first step to use php-github-api is to download composer: `curl -s http://getcomposer.org/installer | php` -Then we have to install our dependencies using `php composer.phar install`, now we can use autoloader from composer by: - -```php - require_once 'vendor/autoload.php'; -``` - -TODO: More examples how to install with composer. Some example of composer.json files. Add to packagist - -> php-github-api follows the PSR-0 convention names for its classes, which means you can easily integrate php-github-api classes loading in your own autoloader. - -## instantiate a new github client - -```php - $github = new Github\Client(); -``` - -From this object, you can access to all GitHub apis, listed below. - - -## Navigation - -[Users][] | [Issues][] | [Commits][] | [Objects][] | [Repos][] | [Pull Requests][] | [Request any Route][] | [Authentication & Security][] | [Customize php-github-api][] | [Run Test Suite][] - - -## Users -Go back to the Navigation - -Searching users, getting user information and managing authenticated user account information. -Wrap [GitHub User API](http://developer.github.com/v3/users). - -### Search for users by username is depreciated cause of github api limitation. - -### Get information about a user - -```php - $user = $github->getUserApi()->show('ornicar'); -``` - -Returns an array of information about the user. - -### Update user informations - -Change user attributes: name, email, blog, company, location. Requires authentication. - -```php - $github->getUserApi()->update('ornicar', array('location' => 'France', 'blog' => 'http://diem-project.org/blog')); -``` - -Returns an array of information about the user. - -### Get users that a specific user is following - -```php - $users = $github->getUserApi()->getFollowing('ornicar'); -``` - -Returns an array of followed users. - -### Get users following a specific user - -```php - $users = $github->getUserApi()->getFollowers('ornicar'); -``` - -Returns an array of following users. - -### Follow a user - -Make the authenticated user follow a user. Requires authentication. - -```php - $github->getUserApi()->follow('symfony'); -``` - -Returns an array of followed users. - -### Unfollow a user - -Make the authenticated user unfollow a user. Requires authentication. +The first step to use php-github-api is to download composer: -```php - $github->getUserApi()->unFollow('symfony'); +```bash +$ curl -s http://getcomposer.org/installer | php ``` -Returns an array of followed users. - -### Get repos that a specific user is watching - -```php - $users = $github->getUserApi()->getWatchedRepos('ornicar'); +Then we have to install our dependencies using: +```bash +$ php composer.phar install ``` +Now we can use autoloader from Composer by: -Returns an array of watched repos. - -### Get the authenticated user emails - -```php - $emails = $github->getUserApi()->getEmails(); -``` - -Returns an array of the authenticated user emails. Requires authentication. - -### Add an email to the authenticated user - -```php - $github->getUserApi()->addEmail('my-email@provider.org'); -``` - -Returns an array of the authenticated user emails. Requires authentication. - -### Remove an email from the authenticated user - -```php - $github->getUserApi()->removeEmail('my-email@provider.org'); -``` - -Return an array of the authenticated user emails. Requires authentication. - - -## Issues -Go back to the Navigation - -Listing issues, searching, editing and closing your projects issues. -Wrap [GitHub Issue API](http://develop.github.com/p/issues.html). - -### List issues in a project - -```php - $issues = $github->getIssueApi()->getList('ornicar', 'php-github-api', 'open'); -``` - -Returns an array of issues. - -### Search issues in a project - -```php - $issues = $github->getIssueApi()->search('ornicar', 'php-github-api', 'closed', 'bug'); -``` - -Returns an array of closed issues matching the "bug" term,. - -### Get information about an issue - -```php - $issue = $github->getIssueApi()->show('ornicar', 'php-github-api', 1); -``` - -Returns an array of information about the issue. - -### Open a new issue - -```php - $github->getIssueApi()->open('ornicar', 'php-github-api', 'The issue title', 'The issue body'); -``` - -Creates a new issue in the repo "php-github-api" of the user "ornicar". -The issue is assigned to the authenticated user. Requires authentication. -Returns an array of information about the issue. - -### Close an issue - -```php - $github->getIssueApi()->close('ornicar', 'php-github-api', 4); -``` - -Closes the fourth issue of the repo "php-github-api" of the user "ornicar". Requires authentication. -Returns an array of information about the issue. - -### Reopen an issue - -```php - $github->getIssueApi()->reOpen('ornicar', 'php-github-api', 4); +```yaml +{ + "require": { + "knplabs/github-api": "*" + }, + "minimum-stability": "dev" +} ``` -Reopens the fourth issue of the repo "php-github-api" of the user "ornicar". Requires authentication. -Returns an array of information about the issue. - -### Update an issue - -```php - $github->getIssueApi()->update('ornicar', 'php-github-api', 4, array('body' => 'The new issue body')); -``` - -Updates the fourth issue of the repo "php-github-api" of the user "ornicar". Requires authentication. -Available attributes are title and body. -Returns an array of information about the issue. - -### List an issue comments - -```php - $comments = $github->getIssueApi()->getComments('ornicar', 'php-github-api', 4); -``` - -List an issue comments by username, repo and issue number. -Returns an array of issues. - -### Add a comment on an issue - -```php - $github->getIssueApi()->addComment('ornicar', 'php-github-api', 4, 'My new comment'); -``` - -Add a comment to the issue by username, repo and issue number. -The comment is assigned to the authenticated user. Requires authentication. - -### List project labels - -```php - $labels = $github->getIssueApi()->getLabels('ornicar', 'php-github-api'); -``` - -List all project labels by username and repo. -Returns an array of project labels. - -### Add a label on an issue - -```php - $github->getIssueApi()->addLabel('ornicar', 'php-github-api', 'label name', 4); -``` - -Add a label to the issue by username, repo, label name and issue number. Requires authentication. -If the label is not yet in the system, it will be created. -Returns an array of the issue labels. - -### Remove a label from an issue - -```php - $github->getIssueApi()->removeLabel('ornicar', 'php-github-api', 'label name', 4); -``` - -Remove a label from the issue by username, repo, label name and issue number. Requires authentication. -Returns an array of the issue labels. - -### Search issues matching a label - -```php - $github->getIssueApi()->searchLabel('ornicar', 'php-github-api', 'label name') -``` - -Returns an array of issues matching the given label. - - -## Commits -Go back to the Navigation - -Getting information on specific commits, the diffs they introduce, the files they've changed. -Wrap [GitHub Commit API](http://develop.github.com/p/commits.html). - -### List commits in a branch - -```php - $commits = $github->getCommitApi()->getBranchCommits('ornicar', 'php-github-api', 'master'); -``` - -Returns an array of commits. - -### List commits for a file - -```php - $commits = $github->getCommitApi()->getFileCommits('ornicar', 'php-github-api', 'master', 'README'); -``` - -Returns an array of commits. - -### Get a single commit - -```php - $commit = $github->getCommitApi()->getCommit('ornicar', 'php-github-api', '726eac09a3b44411bd86'); -``` - -Returns a single commit. - - -## Objects -Go back to the Navigation - -Getting full versions of specific files and trees in your Git repositories. Wrap [GitHub objects API](http://develop.github.com/p/objects.html). - -### List contents of a tree - -```php - $tree = $github->getObjectApi()->showTree('ornicar', 'php-github-api', '691c2ec7fd0b948042047b515886fec40fe76e2b'); -``` - -Returns an array containing a tree of the repository. - -### List all blobs of a tree - -```php - $blobs = $github->getObjectApi()->listBlobs('ornicar', 'php-github-api', '691c2ec7fd0b948042047b515886fec40fe76e2b'); -``` - -Returns an array containing the tree blobs. - -### Show the informations of a blob - -```php - $blob = $github->getObjectApi()->showBlob('ornicar', 'php-github-api', '691c2ec7fd0b948042047b515886fec40fe76e2b', 'CHANGELOG'); -``` - -Returns array of blob informations. - -### Show the raw content of an object - -```php - $rawText = $github->getObjectApi()->getRawData('ornicar', 'php-github-api', 'bd25d1e4ea7eab84b856131e470edbc21b6cd66b'); -``` - -The last parameter can be either a blob SHA1, a tree SHA1 or a commit SHA1. -Returns the raw text content of the object. - - -## Repos -Go back to the Navigation - -Searching repositories, getting repository information and managing repository information for authenticated users. -Wrap [GitHub Repo API](http://develop.github.com/p/repo.html). All methods are described on that page. - -### Search repos by keyword - -#### Simple search - -```php - $repos = $github->getRepoApi()->search('symfony'); -``` - -Returns a list of repositories. - -#### Advanced search - -You can filter the results by language. It takes the same values as the language drop down on [http://github.com/search](http://github). - -```php - $repos = $github->getRepoApi()->search('chess', 'php'); -``` - -You can specify the page number: - -```php - $repos = $github->getRepoApi()->search('chess' , 'php', 2); -``` - -### Get extended information about a repository - -```php - $repo = $github->getRepoApi()->show('ornicar', 'php-github-api') -``` - -Returns an array of information about the specified repository. - -### Get the repositories of a specific user - -```php - $repos = $github->getRepoApi()->getUserRepos('ornicar'); -``` - -Returns a list of repositories. - -### Get the repositories the authenticated user can push to - -```php - $repos = $github->getRepoApi()->getPushableRepos(); -``` - -Returns a list of repositories. - -### Create a repository - -```php - $repo = $github->getRepoApi()->create('my-new-repo', 'This is the description of a repo', 'http://my-repo-homepage.org', true); -``` - -Creates and returns a public repository named my-new-repo. - -### Update a repository - -```php - $repo = $github->getRepoApi()->setRepoInfo('username', 'my-new-repo', array('description' => 'some new description')); -``` - -The value array also accepts the parameters - * description - * homepage - * has_wiki - * has_issues - * has_downloads - -Updates and returns the repository named 'my-new-repo' that is owned by 'username'. - -### Delete a repository - -```php - $token = $github->getRepoApi()->delete('my-new-repo'); // Get the deletion token - $github->getRepoApi()->delete('my-new-repo', $token); // Confirm repository deletion -``` - -Deletes the my-new-repo repository. - -### Making a repository public or private - -```php - $github->getRepoApi()->setPublic('reponame'); - $github->getRepoApi()->setPrivate('reponame'); -``` - -Makes the 'reponame' repository public or private and returns the repository. - -### Get the deploy keys of a repository - -```php - $keys = $github->getRepoApi()->getDeployKeys('reponame'); -``` - -Returns a list of the deploy keys for the 'reponame' repository. - -### Add a deploy key to a repository - -```php - $keys = $github->getRepoApi()->addDeployKey('reponame', 'key title', $key); -``` - -Adds a key with title 'key title' to the 'reponame' repository and returns a list of the deploy keys for the repository. - -### Remove a deploy key from a repository - -```php - $keys = $github->getRepoApi()->removeDeployKey('reponame', 12345); -``` - -Removes the key with id 12345 from the 'reponame' repository and returns a list of the deploy keys for the repository. - -### Get the collaborators for a repository - -```php - $collaborators = $github->getRepoApi()->getRepoCollaborators('username', 'reponame'); -``` - -Returns a list of the collaborators for the 'reponame' repository. - -### Add a collaborator to a repository - -```php - $collaborators = $github->getRepoApi->addCollaborator('reponame', 'username'); -``` - -Adds the 'username' user as collaborator to the 'reponame' repository. - -### Remove a collaborator from a repository - -```php - $collaborators = $github->getRepoApi->removeCollaborator('reponame', 'username'); -``` - -Remove the 'username' collaborator from the 'reponame' repository. - -### Watch and unwatch a repository - -```php - $repository = $github->getRepoApi->watch('ornicar', 'php-github-api'); - $repository = $github->getRepoApi->unwatch('ornicar', 'php-github-api'); -``` - -Watches or unwatches the 'php-github-api' repository owned by 'ornicar' and returns the repository. - -### Fork a repository - -```php - $repository = $github->getRepoApi->fork('ornicar', 'php-github-api'); -``` - -Creates a fork of the 'php-github-api' owned by 'ornicar' and returns the newly created repository. - -### Get the tags of a repository - -```php - $tags = $github->getRepoApi()->getRepoTags('ornicar', 'php-github-api'); -``` - -Returns a list of tags. - -### Get the branches of a repository - -```php - $tags = $github->getRepoApi()->getRepoBranches('ornicar', 'php-github-api'); -``` - -Returns a list of branches. - -### Get the watchers of a repository - -```php - $watchers = $github->getRepoApi()->getRepoWatchers('ornicar', 'php-github-api'); -``` - -Returns list of the users watching the 'php-github-api' owned by 'ornicar'. - -### Get the network (forks) of a repository - -```php - $network = $github->getRepoApi()->getRepoNetwork('ornicar', 'php-github-api'); -``` - -Returns list of the forks of the 'php-github-api' owned by 'ornicar', including the original repository. - -### Get the languages for a repository - -```php - $contributors = $github->getRepoApi()->getRepoLanguages('ornicar', 'php-github-api'); -``` - -Returns a list of languages. - -### Get the contributors of a repository - -```php - $contributors = $github->getRepoApi()->getRepoContributors('ornicar', 'php-github-api'); -``` - -Returns a list of contributors. - -To include non GitHub users, add a third parameter to true: - -```php - $contributors = $github->getRepoApi()->getRepoContributors('ornicar', 'php-github-api', true); -``` - - - - - -## Pull Requests -Go back to the Navigation - -Lets you list pull requests for a given repository, list one pull request in particular along with its discussion, and create a pull-request. -Wraps [GitHub Pull Request API](http://develop.github.com/p/pulls.html), still tagged **BETA**. All methods are described there. - -### List all pull requests, per repository - -#### List open pull requests - -```php - $openPullRequests = $github->getPullRequestApi()->listPullRequests( "ezsystems", "ezpublish", 'open' ); -``` - -The last parameter of the listPullRequests method default to 'open'. The call above is equivalent to : - -```php - $openPullRequests = $github->getPullRequestApi()->listPullRequests( "ezsystems", "ezpublish" ); -``` - -``$openPullRequests`` contains an array of open pull-requests for this repository. - -#### List closed pull requests - -```php - $closedPullRequests = $github->getPullRequestApi()->listPullRequests( "ezsystems", "ezpublish", 'closed' ); -``` - -``$closedPullRequests`` contains an array of closed pull-requests for this repository. - -### List one pull request in particular, along with its discussion - -```php - $pullRequest15 = $github->getPullRequestApi()->show( "ezsystems", "ezpublish", 15 ); -``` - -The last parameter of this call, Pull request ID, can be either extracted from the results of the -``listPullRequests`` results ( 'number' key for a listed pull-request ), or added manually. - -The ``$pullRequest15`` array contains the same elements as every entry in the result of a ``listPullRequests`` call, plus a "discussion" key, self-explanatory. - -### Create a pull request - -A pull request can either be created by supplying both the Title & Body, OR an Issue ID. -Details regarding the content of parameters 3 and 4 of the ``create`` method are presented here : http://develop.github.com/p/pulls.html . - -#### Populated with Title and Body - -Requires authentication. - -```php - $title = "My nifty pull request"; - $body = "This pull request contains a bunch of enhancements and bug-fixes, happily shared with you"; - $github->getPullRequestApi()->create( "ezsystems", "ezpublish", "master", "testbranch", $title, $body ); -``` - -This returns the details of the pull request. - -#### Populated with Issue ID - -Requires authentication. The issue ID is provided instead of title and body. - -```php - $issueId = 15; - $github->getPullRequestApi()->create( "ezsystems", "ezpublish", "master", "testbranch", null, null, $issueId ); -``` - -This returns the details of the pull request. - - - -## Request any Route -Go back to the Navigation - -The method you need does not exist yet? -You can access any GitHub route by using the "get" and "post" methods. -For example, - -```php - $repo = $github->get('repos/show/ornicar/php-github-api'); -``` - -Returns an array describing the php-github-api repository. - -See all GitHub API routes: [http://develop.github.com/](http://develop.github.com/) - - -## Authentication & Security -Go back to the Navigation - -Most GitHub services do not require authentication, but some do. For example the methods that allow you to change properties on Repositories and some others. Therefore this step is facultative. - -### Authenticate - -GitHub provides some different ways of authentication. This API implementation implements three of them which are handled by one function: - -```php - $github->authenticate($username, $secret, $method); -``` - -$username is, of course, the username. $method is optional. The three allowed -values are: - -* Github_Client::AUTH_URL_TOKEN (default, if $method is omitted) -* Github_Client::AUTH_HTTP_TOKEN -* Github_Client::AUTH_HTTP_PASSWORD - -The required value of $secret depends on the choosen $method. For the AUTH_*_TOKEN methods, you should provide the API token here. For the AUTH_HTTP_PASSWORD, you should provide the password of the account. - -After executing the `$github->authenticate($username, $secret, $method);` method using correct credentials, all further requests are done as the given user. - -### About authentication methods - -The Github_Client::AUTH_URL_TOKEN authentication method sends the username and API token in URL parameters. The Github_Client::AUTH_HTTP_* authentication methods send their values to GitHub using HTTP Basic Authentication. Github_Client::AUTH_URL_TOKEN used to be the only available authentication method. To prevent existing applications from changing their behavior in case of an API upgrade, this method is choosen as the default for this API implementation. Note however that GitHub describes this method as deprecated. In most case you should use the Github_Client::AUTH_HTTP_TOKEN instead. - -### Deauthenticate - -If you want to stop new requests from being authenticated, you can use the deAuthenticate method. - -```php - $github->deAuthenticate(); -``` - - -## Customize php-github-api -Go back to the Navigation - -The library is highly configurable and extensible thanks to dependency injection. - -### Configure the http client - -Wanna change, let's say, the http client User Agent? - -```php - $github->getHttpClient()->setOption('user_agent', 'My new User Agent'); -``` - -See all available options in Github/HttpClient/HttpClient.php - -### Inject a new http client instance - -php-github-api provides a curl-based implementation of a http client. -If you want to use your own http client implementation, inject it to the Github\Client instance: - -```php - // create a custom http client - class MyHttpClient extends Github\HttpClient\HttpClient - { - public function doRequest($url, array $parameters = array(), $httpMethod = 'GET', array $options = array()) - { - // send the request and return the raw response - } - } -``` - -> Your http client implementation may not extend Github_HttpClient, but only implement Github_HttpClientInterface. - -You can now inject your http client through Github\Client constructor: - -```php - $github = new Github\Client(new MyHttpClient()); -``` +> php-github-api follows the PSR-0 convention names for its classes, which means you can easily integrate php-github-api classes loading in your own autoloader. -Or to an existing Github_Client instance: +## Basic usage of `php-github-api` client ```php - $github->setHttpClient(new MyHttpClient()); -``` - -### Inject a new API part instance +setApi('user', new MyGithubApiUser($github)); +$client = new Github\Client(); +$repositories = $client->api('user')->repositories('ornicar'); ``` - -## Run Test Suite -Go back to the Navigation +From `$client` object, you can access to all GitHub. -The code is unit tested. To run tests on your machine, from a CLI, run +## Documentation - phpunit +See the `doc` directory for more detailed documentation. ## Credits -This library borrows ideas, code and tests from [phptwitterbot](http://code.google.com/p/phptwitterbot/). - -### Contributors hall of fame +### Contributors Hall of Fame +- Thanks to [Thibault Duplessis aka. ornicar](http://github.com/ornicar) for his first version of this library. - Thanks to [noloh](http://github.com/noloh) for his contribution on the Object API. - Thanks to [bshaffer](http://github.com/bshaffer) for his contribution on the Repo API. - Thanks to [Rolf van de Krol](http://github.com/rolfvandekrol) for his countless contributions. - Thanks to [Nicolas Pastorino](http://github.com/jeanvoye) for his contribution on the Pull Request API. +- Thanks to [Edoardo Rivello](http://github.com/erivello) for his contribution on the Gists API. Thanks to GitHub for the high quality API and documentation. +## License -[Users]: #users -[Issues]: #issues -[Commits]: #commits -[Objects]: #objects -[Repos]: #repos -[Pull Requests]: #pull_requests -[Request any Route]: #request_any_route -[Authentication & Security]: #authentication_and_security -[Customize php-github-api]: #customize -[Run Test Suite]: #run_test_suite +php-github-api is licensed under the MIT License - see the LICENSE file for details diff --git a/behat.yml.dist b/behat.yml.dist deleted file mode 100644 index fd80785e519..00000000000 --- a/behat.yml.dist +++ /dev/null @@ -1,10 +0,0 @@ -default: - paths: - features: features - bootstrap: features/bootstrap - formatter: - name: progress - - context: - parameters: - base_url: "http://api.github.com" diff --git a/composer.json b/composer.json index bcd5eefb18a..05a7ad20479 100644 --- a/composer.json +++ b/composer.json @@ -21,9 +21,6 @@ "ext-curl": "*", "kriswallsmith/buzz": "0.7" }, - "require-dev": { - "behat/behat": "2.4.*" - }, "autoload": { "psr-0": { "Github": "lib/" } }, diff --git a/doc/commits.md b/doc/commits.md new file mode 100644 index 00000000000..703784d76e4 --- /dev/null +++ b/doc/commits.md @@ -0,0 +1,45 @@ +## Commits API +[Back to the navigation](index.md) + +Getting information on specific commits, the diffs they introduce, the files they've changed. +Wrap [GitHub Commit API](http://developer.github.com/v3/git/commits/). + +### List commits in a branch + +```php +api('repo')->commits()->all('KnpLabs', 'php-github-api', array('sha' => 'master')); +``` + +Returns an array of commits. + +### List commits for a file + +```php +api('repo')->commits()->all('KnpLabs', 'php-github-api', array('sha' => 'master', 'path' => 'README')); +``` + +Returns an array of commits. + +### Get a single commit + +```php +api('repo')->commits()->show('KnpLabs', 'php-github-api', '839e5185da9434753db47959bee16642bb4f2ce4'); +``` + +Returns a single commit. + +### Compare commits + +```php +api('repo')->commits()->compare('KnpLabs', 'php-github-api', '839e5185da9434753db47959bee16642bb4f2ce4', 'b24a89060ca3f337c9b8c4fd2c929f60a5f2e33a'); +``` + +Returns an array of commits. diff --git a/doc/customize.md b/doc/customize.md new file mode 100644 index 00000000000..056fd8b2daf --- /dev/null +++ b/doc/customize.md @@ -0,0 +1,60 @@ +## Customize `php-github-api` and testing +[Back to the navigation](index.md) + +### Configure the http client + +Wanna change, let's say, the http client User Agent? + +```php +getHttpClient()->setOption('user_agent', 'My new User Agent'); +``` + +See all available options in `Github/HttpClient/HttpClient.php` + +### Inject a new http client instance + +`php-github-api` provides a curl-based implementation of a http client. +If you want to use your own http client implementation, inject it to the `Github\Client` instance: + +```php + Your http client implementation may not extend `Github\HttpClient\HttpClient`, but only implement `Github\HttpClient\HttpClientInterface`. + +You can now inject your http client through `Github\Client` constructor: + +```php +setHttpClient(new MyHttpClient()); +``` + +### Run Test Suite + +The code is unit tested, there are also some functional tests. To run tests on your machine, from a CLI, run + +```bash +$ phpunit +``` diff --git a/doc/gists.md b/doc/gists.md new file mode 100644 index 00000000000..d97c6118558 --- /dev/null +++ b/doc/gists.md @@ -0,0 +1,140 @@ +## Gists API +[Back to the navigation](index.md) + +Creating, editing, deleting and listing gists. Wraps [GitHub Gists API](http://developer.github.com/v3/gists/). + +#### List all public gists. + +```php +api('gists')->all('public'); +``` + +#### List the authenticated user’s starred gists. + +```php +authenticate(); +$gists = $github->api('gists')->all('starred'); +``` + +Requires authentication. + +#### List the authenticated user’s gists or if called anonymously, this will return all public gists. + +```php +authenticate(); +$gists = $github->api('gists')->all(); +``` + +#### Get a single gist + +```php +api('gists')->show(1); +``` + +#### Create a gist + +```php + array( + 'filename.txt' => array( + 'content' => 'txt file content' + ), + ), + 'public' => true, + 'description' => 'This is an optional description' +); + +$gist = $github->api('gists')->create($data); +``` + +Creates and returns a public gist. + +#### Update a gist + +You can update ``description``. + +```php + 'This is new description' +); + +$gist = $github->api('gists')->update($data); +``` + +You can update ``content`` of a previous file's version. + +```php + array( + 'filename.txt' => array( + 'content' => 'updated txt file content' + ), + ), +); +$gist = $github->api('gists')->update(1234, $data); +``` + +You can update the ``filename`` of a previous file's version. + +```php + array( + 'filename.txt' => array( + 'filename' => 'new-filename.txt' + ), + ), +); +$gist = $github->api('gists')->update(1234, $data); +``` + +You can add a new file to the gist. + +```php + array( + 'new-filename.php' => array( + 'content' => 'a new file content' + ), + ), +); +$gist = $github->api('gists')->update(1234, $data); +``` + +You can remove a file from the gist. + +```php + array( + 'filename.txt' => null, + ), +); +$gist = $github->api('gists')->update(1234, $data); +``` + +#### Delete a gist + +```php +api('gists')->remove(1234); +``` diff --git a/doc/index.md b/doc/index.md new file mode 100644 index 00000000000..a72d8b82021 --- /dev/null +++ b/doc/index.md @@ -0,0 +1,17 @@ +Navigation +========== + +API list: + +* [Commits](commits.md) +* [Gists](gists.md) +* [Issues](issues.md) +* [Pull Requests](pull_requests.md) +* [Repositories](repos.md) +* [Users](users.md) + +Additional features: + +* [Authentication & Security](security.md) +* [Request any Route](request_any_route.md) +* [Customize `php-github-api` and testing](customize.md) diff --git a/doc/issues.md b/doc/issues.md new file mode 100644 index 00000000000..2bac3c479c0 --- /dev/null +++ b/doc/issues.md @@ -0,0 +1,154 @@ +## Issues API +[Back to the navigation](index.md) + +Listing issues, searching, editing and closing your projects issues. +Wrap [GitHub Issue API](http://developer.github.com/v3/issues/). + +### List issues in a project + +```php +api('issue')->all('KnpLabs', 'php-github-api', array('state' => 'open')); +``` + +Returns an array of issues. + +### Search issues in a project + +```php +api('issue')->find('KnpLabs', 'php-github-api', 'closed', 'bug'); +``` + +Returns an array of closed issues matching the "bug" term. + +### Get information about an issue + +```php +api('issue')->show('KnpLabs', 'php-github-api', 1); +``` + +Returns an array of information about the issue. + +### Open a new issue + +```php +authenticate(); +$client->api('issue')->create('KnpLabs', 'php-github-api', array('title' => 'The issue title', 'body' => 'The issue body'); +``` + +Creates a new issue in the repo "php-github-api" of the user "KnpLabs". +The issue is assigned to the authenticated user. Requires authentication. +Returns an array of information about the issue. + +### Close an issue + +```php +authenticate(); +$client->api('issue')->update('KnpLabs', 'php-github-api', 4, array('state' => 'closed')); +``` + +Closes the fourth issue of the repo "php-github-api" of the user "KnpLabs". Requires authentication. +Returns an array of information about the issue. + +### Reopen an issue + +```php +authenticate(); +$client->api('issue')->update('KnpLabs', 'php-github-api', 4, array('state' => 'open')); +``` + +Reopens the fourth issue of the repo "php-github-api" of the user "ornicar". Requires authentication. +Returns an array of information about the issue. + +### Update an issue + +```php +authenticate(); +$client->api('issue')->update('KnpLabs', 'php-github-api', 4, array('body' => 'The new issue body')); +``` + +Updates the fourth issue of the repo "php-github-api" of the user "KnpLabs". Requires authentication. +Available attributes are title and body. +Returns an array of information about the issue. + +### List an issue comments + +```php +api('issue')->comments()->all('KnpLabs', 'php-github-api', 4); +``` + +List an issue comments by username, repo and issue number. +Returns an array of issues. + +### Add a comment on an issue + +```php +authenticate(); +$client->api('issue')->comments()->create('KnpLabs', 'php-github-api', 4, array('body' => 'My new comment')); +``` + +Add a comment to the issue by username, repo and issue number. +The comment is assigned to the authenticated user. Requires authentication. + +### List project labels + +```php +api('issue')->labels()->all('KnpLabs', 'php-github-api'); +``` + +List all project labels by username and repo. +Returns an array of project labels. + +### Add a label on an issue + +```php +authenticate(); +$client->api('issue')->labels()->add('KnpLabs', 'php-github-api', 4, 'label name'); +``` + +Add a label to the issue by username, repo, label name and issue number. Requires authentication. +If the label is not yet in the system, it will be created. +Returns an array of the issue labels. + +### Remove a label from an issue + +```php +authenticate(); +$client->api('issue')->labels()->remove('KnpLabs', 'php-github-api', 4, 'label name'); +``` + +Remove a label from the issue by username, repo, label name and issue number. Requires authentication. +Returns an array of the issue labels. + +### Search issues matching a label + +```php +api('issue')->all('KnpLabs', 'php-github-api', array('labels' => 'label name')); +``` + +Returns an array of issues matching the given label. diff --git a/doc/pull_requests.md b/doc/pull_requests.md new file mode 100644 index 00000000000..0f3ea3a5047 --- /dev/null +++ b/doc/pull_requests.md @@ -0,0 +1,87 @@ +## Pull Requests API +[Back to the navigation](index.md) + +Lets you list pull requests for a given repository, list one pull request in particular along with its discussion, and create a pull-request. +Wraps [GitHub Pull Request API](http://developer.github.com/v3/pulls/), still tagged **BETA**. All methods are described there. + +### List all pull requests, per repository + +#### List open pull requests + +```php +api('pull_request')->all('ezsystems', 'ezpublish', 'open'); +``` + +The last parameter of the listPullRequests method default to 'open'. The call above is equivalent to: + +```php +api('pull_request')->all('ezsystems', 'ezpublish'); +``` + +``$openPullRequests`` contains an array of open pull-requests for this repository. + +#### List closed pull requests + +```php +api('pull_request')->all('ezsystems', 'ezpublish', 'closed'); +``` + +``$closedPullRequests`` contains an array of closed pull-requests for this repository. + +### List one pull request in particular, along with its discussion + +```php +api('pull_request')->show('ezsystems', 'ezpublish', 15); +``` + +The last parameter of this call, Pull Request ID. + +The ``$pullRequest`` array contains the same elements as every entry in the result of a ``all()`` call, plus a "discussion" key, self-explanatory. + +### Create a pull request + +A pull request can either be created by supplying both the Title & Body, OR an Issue ID. +Details regarding the content of parameters 3 and 4 of the ``create``. + +#### Populated with Title and Body + +Requires authentication. + +```php +authenticate(); +$pullRequest = $client->api('pull_request')->create('ezsystems', 'ezpublish', array( + 'base' => 'master', + 'head' => 'testbranch', + 'title' => 'My nifty pull request', + 'body' => 'This pull request contains a bunch of enhancements and bug-fixes, happily shared with you' +); +``` + +This returns the details of the pull request. + +#### Populated with Issue ID + +Requires authentication. The issue ID is provided instead of title and body. + +```php +authenticate(); +$pullRequest = $client->api('pull_request')->create('ezsystems', 'ezpublish', array( + 'base' => 'master', + 'head' => 'testbranch', + 'issue' => 15 +); +``` + +This returns the details of the pull request. diff --git a/doc/repos.md b/doc/repos.md new file mode 100644 index 00000000000..ec827300add --- /dev/null +++ b/doc/repos.md @@ -0,0 +1,272 @@ +## Repositories API +[Back to the navigation](index.md) + +Searching repositories, getting repository information and managing repository information for authenticated users. +Wrap [GitHub Repo API](http://developer.github.com/v3/repos/). All methods are described on that page. + +### Search repos by keyword + +#### Simple search + +```php +api('repo')->find('symfony'); +``` + +Returns a list of repositories. + +#### Advanced search + +You can filter the results by language. It takes the same values as the language drop down on [http://github.com/search](http://github). + +```php +api('repo')->find('chess', array('language' => 'php')); +``` + +You can specify the page number: + +```php +api('repo')->find('chess', array('language' => 'php', 'starting_page' => 2)); +``` + +### Get extended information about a repository + +```php +api('repo')->show('KnpLabs', 'php-github-api') +``` + +Returns an array of information about the specified repository. + +### Get the repositories of a specific user + +```php +api('user')->repositories('KnpLabs'); +``` + +Returns a list of repositories. + +### Create a repository + +```php +authenticate(); +$repo = $client->api('repo')->create('my-new-repo', 'This is the description of a repo', 'http://my-repo-homepage.org', true); +``` + +Creates and returns a public repository named my-new-repo. + +### Update a repository + +```php +authenticate(); +$repo = $client->api('repo')->update('username', 'my-new-repo', array('description' => 'some new description')); +``` + +The value array also accepts the parameters +* `description` +* `homepage` +* `has_wiki` +* `has_issues` +* `has_downloads` + +Updates and returns the repository named 'my-new-repo' that is owned by 'username'. + +### Delete a repository + +```php +authenticate(); +$client->api('repo')->delete('username', 'my-new-repo'); // Get the deletion token +``` + +Deletes the my-new-repo repository. + +### Making a repository public or private + +```php +authenticate(); +// Make repo public +$client->api('repo')->update('username', 'reponame', array('private' => false)); +// Make repo private +$client->api('repo')->update('username', 'reponame', array('private' => true)); +``` + +Makes the 'reponame' repository public or private and returns the repository. + +### Get the deploy keys of a repository + +```php +api('repo')->keys()->all('username', 'reponame'); +``` + +Returns a list of the deploy keys for the 'reponame' repository. + +### Add a deploy key to a repository + +```php +authenticate(); +$key = $client->api('repo')->keys()->create('username', 'reponame', array('title' => 'key title', 'key' => 12345)); +``` + +Adds a key with title 'key title' to the 'reponame' repository and returns a list of the deploy keys for the repository. + +### Remove a deploy key from a repository + +```php +authenticate(); +$client->api('repo')->keys()->remove('username', 'reponame', 12345); +``` + +Removes the key with id 12345 from the 'reponame' repository and returns a list of the deploy keys for the repository. + +### Get the collaborators for a repository + +```php +api('repo')->collaborators()->all('username', 'reponame'); +``` + +Returns a list of the collaborators for the 'reponame' repository. + +### Add a collaborator to a repository + +```php +api('repo')->collaborators()->add('username', 'reponame', 'KnpLabs'); +``` + +Adds the 'username' user as collaborator to the 'reponame' repository. + +### Remove a collaborator from a repository + +```php +api('repo')->collaborators()->remove('username', 'reponame', 'KnpLabs'); +``` + +Remove the 'username' collaborator from the 'reponame' repository. + +### Watch and unwatch a repository + +```php +authenticate(); +$client->api('current_user')->watchers()->watch('ornicar', 'php-github-api'); +$client->api('current_user')->watchers()->unwatch('ornicar', 'php-github-api'); +``` + +Watches or unwatches the 'php-github-api' repository owned by 'ornicar' and returns the repository. + +### Fork a repository + +```php +authenticate(); +$repository = $client->api('repo')->forks()->create('ornicar', 'php-github-api'); +``` + +Creates a fork of the 'php-github-api' owned by 'ornicar' and returns the newly created repository. + +### Get the tags of a repository + +```php +api('repo')->tags('ornicar', 'php-github-api'); +``` + +Returns a list of tags. + +### Get the branches of a repository + +```php +api('repo')->branches('ornicar', 'php-github-api'); +``` + +Returns a list of branches. + +### Get single branch of a repository + +```php +api('repo')->branches('ornicar', 'php-github-api', 'master'); +``` + +Returns a branch data. + +### Get the watchers of a repository + +```php +api('repo')->watchers('ornicar', 'php-github-api'); +``` + +Returns list of the users watching the 'php-github-api' owned by 'ornicar'. + +### Get the network (forks) of a repository + +```php +api('repo')->forks()->all('ornicar', 'php-github-api'); +``` + +Returns list of the forks of the 'php-github-api' owned by 'ornicar', including the original repository. + +### Get the languages for a repository + +```php +api('repo')->languages('ornicar', 'php-github-api'); +``` + +Returns a list of languages. + +### Get the contributors of a repository + +```php +api('repo')->contributors('ornicar', 'php-github-api'); +``` + +Returns a list of contributors. + +To include non GitHub users, add a third parameter to true: + +```php +api('repo')->contributors('ornicar', 'php-github-api', true); +``` diff --git a/doc/request_any_route.md b/doc/request_any_route.md new file mode 100644 index 00000000000..2ac77b57bf9 --- /dev/null +++ b/doc/request_any_route.md @@ -0,0 +1,15 @@ +## Request any Route +[Back to the navigation](index.md) + +The method you need does not exist yet? You can access any GitHub route by using the "get" and "post" methods. +For example: + +```php +get('repos/show/ornicar/php-github-api'); +``` + +Returns an array describing the "php-github-api" repository. + +See all GitHub API routes: [http://develop.github.com/](http://develop.github.com/) diff --git a/doc/security.md b/doc/security.md new file mode 100644 index 00000000000..f9711c93afb --- /dev/null +++ b/doc/security.md @@ -0,0 +1,39 @@ +## Authentication & Security +[Back to the navigation](index.md) + +Most GitHub services do not require authentication, but some do. For example the methods that allow you to change +properties on Repositories and some others. Therefore this step is facultative. + +### Authenticate + +GitHub provides some different ways of authentication. This API implementation implements three of them which are handled by one function: + +```php +authenticate($username, $password, $method); +``` + +`$username` is, of course, the username, and guess what should contain `$password`. The `$method` +can contain one of the three allowed values: + +* `Github\Client::AUTH_URL_TOKEN` +* `Github\Client::AUTH_HTTP_TOKEN` +* `Github\Client::AUTH_HTTP_PASSWORD` + +The required value of `$password` depends on the chosen `$method`. For the `Github\Client::AUTH_*_TOKEN` methods, +you should provide the API token in `$username` variable (`$password` is omitted in this particular case). For the +`Github\Client::AUTH_HTTP_PASSWORD`, you should provide the password of the account. + +After executing the `$client->authenticate($username, $secret, $method);` method using correct credentials, +all further requests are done as the given user. + +### About authentication methods + +The `Github\Client::AUTH_URL_TOKEN` authentication method sends the username and API token in URL parameters. +The `Github_Client::AUTH_HTTP_*` authentication methods send their values to GitHub using HTTP Basic Authentication. +`Github\Client::AUTH_URL_TOKEN` used to be the only available authentication method. To prevent existing applications +from changing their behavior in case of an API upgrade, this method is chosen as the default for this API implementation. + +Note however that GitHub describes this method as deprecated. In most case you should use the +`Github\Client::AUTH_HTTP_TOKEN` instead. diff --git a/doc/users.md b/doc/users.md new file mode 100644 index 00000000000..88829158b64 --- /dev/null +++ b/doc/users.md @@ -0,0 +1,161 @@ +## Users API +[Back to the navigation](index.md) + +Searching users, getting user information and managing authenticated user account information. +Wrap [GitHub User API](http://developer.github.com/v3/users/). + +### Search for users by keyword + +```php +api('user')->find('KnpLabs'); +``` + +Returns an array of found users. + +### Get information about a user + +```php +api('user')->show('KnpLabs'); +``` + +Returns an array of information about the user. + +### Update user informations + +Change user attributes: name, email, blog, company, location. Requires authentication. + +```php +authenticate(); +$client->api('current_user')->update(array( + 'location' => 'France', + 'blog' => 'http://diem-project.org/blog' +)); +``` + +Returns an array of information about the user. + +### Get users that a specific user is following + +```php +api('user')->following('KnpLabs'); +``` + +Returns an array of followed users. + +For authenticated user use: + +```php +authenticate(); +$users = $client->api('current_user')->follow()->all(); +``` + +### Get users following a specific user + +```php +api('user')->followers('KnpLabs'); +``` + +Returns an array of following users. + +For authenticated user use: + +```php +authenticate(); +$users = $client->api('current_user')->followers(); +``` + +### Follow a user + +Make the authenticated user follow a user. Requires authentication. + +```php +authenticate(); +$client->api('current_user')->follow()->follow('symfony'); +``` + +Returns an array of followed users. + +### Unfollow a user + +Make the authenticated user unfollow a user. Requires authentication. + +```php +authenticate(); +$client->api('current_user')->follow()->unfollow('symfony'); +``` + +Returns an array of followed users. + +### Get repos that a specific user is watching + +```php +api('user')->watched('ornicar'); +``` + +For authenticated user use: + +```php +authenticate(); +$users = $client->api('current_user')->watched(); +``` + +Returns an array of watched repos. + +### Get the authenticated user emails + +```php +authenticate(); +$emails = $client->api('current_user')->emails()->all(); +``` + +Returns an array of the authenticated user emails. Requires authentication. + +### Add an email to the authenticated user + +```php +authenticate(); +$emails = $client->api('current_user')->emails()->add('my-email@provider.org'); +// or add few emails at once +$emails = $client->api('current_user')->emails()->add(array('first@provider.org', 'second@provider.org')); +``` + +Returns an array of the authenticated user emails. Requires authentication. + +### Remove an email from the authenticated user + +```php +authenticate(); +$emails = $client->api('current_user')->emails()->remove('my-email@provider.org'); +// or remove few emails at once +$emails = $client->api('current_user')->emails()->remove(array('first@provider.org', 'second@provider.org')); +``` + +Return an array of the authenticated user emails. Requires authentication. diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php deleted file mode 100644 index eb93dee7541..00000000000 --- a/features/bootstrap/FeatureContext.php +++ /dev/null @@ -1,99 +0,0 @@ -client = new Client(); - } - - /** - * @AfterScenario - */ - public function clearLastResponse() - { - $this->lastResponse = null; - } - - /** - * @When /^(?:|I )send a request for rate limit information$/ - */ - public function iSendRequestForRateLimitInformation() - { - $this->lastResponse = $this->client->getRateLimit(); - } - - /** - * @Then /^(?:|I )should not reach my rate limit$/ - */ - public function iShouldNotReachMyRateLimit() - { - $remainingCalls = $this->client->getHttpClient()->remainingCalls; - - assertNotNull($remainingCalls); - assertTrue(5000 > $remainingCalls); - assertTrue(0 < $remainingCalls); - } - - /** - * @When /^(?:|I )send a markdown data:$/ - */ - public function iSendMarkdownData(PyStringNode $jsonString) - { - $data = json_decode($jsonString->getRaw(), true); - - $this->lastResponse = $this->client->getMarkdownApi()->render( - $data['text'], - isset($data['mode']) ? $data['mode'] : null, - isset($data['context']) ? $data['context'] : null - ); - } - - /** - * @Then /^(?:the )?response should equal to html:$/ - */ - public function theResponseShouldEqualToHtml(PyStringNode $htmlString) - { - $expected = $htmlString->getRaw(); - $actual = $this->lastResponse; - - assertEquals($expected, $actual, "Failed asserting that equals to \n".print_r($actual, true)); - } - - /** - * @Then /^(?:the )?response should equal to json:$/ - */ - public function theResponseShouldEqualToJson(PyStringNode $jsonString) - { - $expected = json_decode($jsonString->getRaw(), true); - $actual = $this->lastResponse; - - if (null === $expected) { - throw new \RuntimeException( - "Can not convert etalon to json:\n".$jsonString->getRaw() - ); - } - - assertEquals($expected, $actual, "Failed asserting that equals to \n".print_r($actual, true)); - } -} diff --git a/features/markdown.feature b/features/markdown.feature deleted file mode 100644 index 4347188139a..00000000000 --- a/features/markdown.feature +++ /dev/null @@ -1,30 +0,0 @@ -Feature: Markdown rendering - In order to get rendered markdown file - As an anonymous user - I need to be able to post data to /markdown resource - - Scenario: Get rendered markdown file - When I send a markdown data: - """ - { - "text": "Hello world github/linguist#1 **cool**, and #1!" - } - """ - Then the response should equal to html: - """ -

Hello world github/linguist#1 cool, and #1!

- """ - - Scenario: Get rendered markdown file with context - When I send a markdown data: - """ - { - "text": "Hello world KnpLabs/KnpBundles#1 **cool**, and #1!", - "mode": "gfm", - "context": "KnpLabs/KnpMenu" - } - """ - Then the response should equal to html: - """ -

Hello world KnpLabs/KnpBundles#1 cool, and #1!

- """ diff --git a/features/rate_limit.feature b/features/rate_limit.feature deleted file mode 100644 index 0ef143658ff..00000000000 --- a/features/rate_limit.feature +++ /dev/null @@ -1,18 +0,0 @@ -Feature: Rate Limit - In order to get rate limit information - As an anonymous user - I need to be able to make calls to /rate_limit resource - - Scenario: Get rate limit - When I send a request for rate limit information - Then I should not reach my rate limit -# Commented because GH limit is reset once per hour -# Then the response should equal to json: -# """ -# { -# "rate": { -# "remaining": 4999, -# "limit": 5000 -# } -# } -# """ diff --git a/lib/Github/Api/Api.php b/lib/Github/Api/AbstractApi.php similarity index 96% rename from lib/Github/Api/Api.php rename to lib/Github/Api/AbstractApi.php index d80204ad6de..63194a74c66 100644 --- a/lib/Github/Api/Api.php +++ b/lib/Github/Api/AbstractApi.php @@ -10,7 +10,7 @@ * @author Thibault Duplessis * @author Joseph Bielawski */ -abstract class Api implements ApiInterface +abstract class AbstractApi implements ApiInterface { /** * The client diff --git a/lib/Github/Api/Commit.php b/lib/Github/Api/Commit.php deleted file mode 100644 index 5219a870c16..00000000000 --- a/lib/Github/Api/Commit.php +++ /dev/null @@ -1,86 +0,0 @@ - - * @author Joseph Bielawski - */ -class Commit extends Api -{ - /** - * List commits by username, repo and branch - * @link http://developer.github.com/v3/repos/commits/ - * - * @param string $username the username - * @param string $repo the repo - * @param string $branch the branch - * @return array list of users found - */ - public function getBranchCommits($username, $repo, $branch) - { - $url = 'repos/'.urlencode($username).'/'.urlencode($repo).'/commits'; - - $sha = $this->getBranchSha($username, $repo, $branch); - if (null !== $sha) { - $url .= '?sha='.urlencode($sha); - } - - return $this->get($url); - } - - /** - * List commits by username, repo, branch and path - * @link http://developer.github.com/v3/repos/commits/ - * - * @param string $username the username - * @param string $repo the repo - * @param string $branch the branch - * @param string $path the path - * @return array list of users found - */ - public function getFileCommits($username, $repo, $branch, $path) - { - $branchSha = $this->getBranchSha($username, $repo, $branch); - - return $this->get('repos/'.urlencode($username).'/'.urlencode($repo).'/commits?path='.urlencode($path).'&sha='.urlencode($branchSha)); - } - - /** - * Show a specific commit - * @link http://developer.github.com/v3/repos/commits/#get-a-single-commit - * - * @param string $username the username - * @param string $repo the repo - * @param string $sha the commit sha - * @return array data from commit - */ - public function getCommit($username, $repo, $sha) - { - return $this->get('repos/'.urlencode($username).'/'.urlencode($repo).'/commits/'.urlencode($sha)); - } - - /** - * Fetch branch sha from branch name - * @link http://developer.github.com/v3/git/refs/#get-a-reference - * - * @param string $username - * @param string $repoName - * @param string $branchName - * @return null|string - */ - private function getBranchSha($username, $repoName, $branchName) - { - $info = $this->get('repos/'.urlencode($username).'/'.urlencode($repoName).'/git/refs/'.urlencode($branchName)); - - if (!isset($info['ref'])) { - return null; - } - - return $info['object']['sha']; - } -} diff --git a/lib/Github/Api/CurrentUser.php b/lib/Github/Api/CurrentUser.php new file mode 100644 index 00000000000..dd457c02cac --- /dev/null +++ b/lib/Github/Api/CurrentUser.php @@ -0,0 +1,76 @@ + + */ +class CurrentUser extends AbstractApi +{ + public function show() + { + return $this->get('user'); + } + + public function update(array $params) + { + return $this->patch('user', $params); + } + + /** + * @return DeployKeys + */ + public function keys() + { + return new DeployKeys($this->client); + } + + /** + * @return Emails + */ + public function emails() + { + return new Emails($this->client); + } + + /** + * @return Followers + */ + public function follow() + { + return new Followers($this->client); + } + + public function issues(array $params = array()) + { + return $this->get('issues', array_merge(array('page' => 1), $params)); + } + + public function followers($page = 1) + { + return $this->get('user/followers', array( + 'page' => $page + )); + } + + /** + * @return Watchers + */ + public function watchers() + { + return new Watchers($this->client); + } + + public function watched($page = 1) + { + return $this->get('user/watched', array( + 'page' => $page + )); + } +} diff --git a/lib/Github/Api/CurrentUser/DeployKeys.php b/lib/Github/Api/CurrentUser/DeployKeys.php new file mode 100644 index 00000000000..75f64e8e9f4 --- /dev/null +++ b/lib/Github/Api/CurrentUser/DeployKeys.php @@ -0,0 +1,85 @@ + + */ +class DeployKeys extends AbstractApi +{ + /** + * List deploy keys for the authenticated user + * @link http://developer.github.com/v3/repos/keys/ + * + * @return array + */ + public function all() + { + return $this->get('user/keys'); + } + + /** + * Shows deploy key for the authenticated user + * @link http://developer.github.com/v3/repos/keys/ + * + * @param string $id + * @return array + */ + public function show($id) + { + return $this->get('user/keys/'.urlencode($id)); + } + + /** + * Adds deploy key for the authenticated user + * @link http://developer.github.com/v3/repos/keys/ + * + * @param array $params + * @return array + * + * @throws MissingArgumentException + */ + public function create(array $params) + { + if (!isset($params['title'], $params['key'])) { + throw new MissingArgumentException(array('title', 'key')); + } + + return $this->post('user/keys', $params); + } + + /** + * Updates deploy key for the authenticated user + * @link http://developer.github.com/v3/repos/keys/ + * + * @param string $id + * @param array $params + * @return array + * + * @throws MissingArgumentException + */ + public function update($id, array $params) + { + if (!isset($params['title'], $params['key'])) { + throw new MissingArgumentException(array('title', 'key')); + } + + return $this->patch('user/keys/'.urlencode($id), $params); + } + + /** + * Removes deploy key for the authenticated user + * @link http://developer.github.com/v3/repos/keys/ + * + * @param string $id + * @return array + */ + public function remove($id) + { + return $this->delete('user/keys/'.urlencode($id)); + } +} diff --git a/lib/Github/Api/CurrentUser/Emails.php b/lib/Github/Api/CurrentUser/Emails.php new file mode 100644 index 00000000000..f1e59e50dd9 --- /dev/null +++ b/lib/Github/Api/CurrentUser/Emails.php @@ -0,0 +1,64 @@ + + */ +class Emails extends AbstractApi +{ + /** + * List emails for the authenticated user + * @link http://developer.github.com/v3/repos/emails/ + * + * @return array + */ + public function all() + { + return $this->get('user/emails'); + } + + /** + * Adds one or more email for the authenticated user + * @link http://developer.github.com/v3/repos/emails/ + * + * @param string|array $emails + * @return array + * + * @throws InvalidArgumentException + */ + public function add($emails) + { + if (is_string($emails)) { + $emails = array($emails); + } elseif (0 === count($emails)) { + throw new InvalidArgumentException(); + } + + return $this->post('user/emails', $emails); + } + + /** + * Removes one or more email for the authenticated user + * @link http://developer.github.com/v3/repos/emails/ + * + * @param string|array $emails + * @return array + * + * @throws InvalidArgumentException + */ + public function remove($emails) + { + if (is_string($emails)) { + $emails = array($emails); + } elseif (0 === count($emails)) { + throw new InvalidArgumentException(); + } + + return $this->delete('user/emails/', $emails); + } +} diff --git a/lib/Github/Api/CurrentUser/Followers.php b/lib/Github/Api/CurrentUser/Followers.php new file mode 100644 index 00000000000..d513e5ff251 --- /dev/null +++ b/lib/Github/Api/CurrentUser/Followers.php @@ -0,0 +1,62 @@ + + */ +class Followers extends AbstractApi +{ + /** + * List followed users by the authenticated user + * @link http://developer.github.com/v3/repos/followers/ + * + * @param integer $page + * @return array + */ + public function all($page = 1) + { + return $this->get('user/following', array( + 'page' => $page + )); + } + + /** + * Check that the authenticated user follows a user + * @link http://developer.github.com/v3/repos/followers/ + * + * @param string $username the username to follow + * @return array + */ + public function check($username) + { + return $this->get('user/following/'.urlencode($username)); + } + + /** + * Make the authenticated user follow a user + * @link http://developer.github.com/v3/repos/followers/ + * + * @param string $username the username to follow + * @return array + */ + public function follow($username) + { + return $this->put('user/following/'.urlencode($username)); + } + + /** + * Make the authenticated user un-follow a user + * @link http://developer.github.com/v3/repos/followers/ + * + * @param string $username the username to un-follow + * @return array + */ + public function unfollow($username) + { + return $this->delete('user/following/'.urlencode($username)); + } +} diff --git a/lib/Github/Api/CurrentUser/Watchers.php b/lib/Github/Api/CurrentUser/Watchers.php new file mode 100644 index 00000000000..1277ff34f9c --- /dev/null +++ b/lib/Github/Api/CurrentUser/Watchers.php @@ -0,0 +1,65 @@ + + */ +class Watchers extends AbstractApi +{ + /** + * List repositories watched by the authenticated user + * @link http://developer.github.com/v3/repos/watching/ + * + * @param integer $page + * @return array + */ + public function all($page = 1) + { + return $this->get('user/watched', array( + 'page' => $page + )); + } + + /** + * Check that the authenticated user watches a repository + * @link http://developer.github.com/v3/repos/watching/ + * + * @param string $username the user who owns the repo + * @param string $repository the name of the repo + * @return array + */ + public function check($username, $repository) + { + return $this->get('user/watched/'.urlencode($username).'/'.urlencode($repository)); + } + + /** + * Make the authenticated user watch a repository + * @link http://developer.github.com/v3/repos/watching/ + * + * @param string $username the user who owns the repo + * @param string $repository the name of the repo + * @return array + */ + public function watch($username, $repository) + { + return $this->put('user/watched/'.urlencode($username).'/'.urlencode($repository)); + } + + /** + * Make the authenticated user unwatch a repository + * @link http://developer.github.com/v3/repos/watching/ + * + * @param string $username the user who owns the repo + * @param string $repository the name of the repo + * @return array + */ + public function unwatch($username, $repository) + { + return $this->delete('user/watched/'.urlencode($username).'/'.urlencode($repository)); + } +} diff --git a/lib/Github/Api/Gists.php b/lib/Github/Api/Gists.php new file mode 100644 index 00000000000..b0a59960094 --- /dev/null +++ b/lib/Github/Api/Gists.php @@ -0,0 +1,71 @@ + + * @author Edoardo Rivello + */ +class Gists extends AbstractApi +{ + public function all($type = null) + { + if (!in_array($type, array('public', 'starred'))) { + return $this->get('gists'); + } + + return $this->get('gists/'.urlencode($type)); + } + + public function show($number) + { + return $this->get('gists/'.urlencode($number)); + } + + public function create(array $params) + { + if (!isset($params['files']) || (!is_array($params['files']) || 0 === count($params['files']))) { + throw new MissingArgumentException('files'); + } + + $params['public'] = (boolean) $params['public']; + + return $this->post('gists', $params); + } + + public function update($id, array $params) + { + return $this->patch('gists/'.urlencode($id), $params); + } + + public function fork($id) + { + return $this->post('gists/'.urlencode($id).'/fork'); + } + + public function remove($id) + { + return $this->delete('gists/'.urlencode($id)); + } + + public function check($id) + { + return $this->get('gists/'.urlencode($id).'/star'); + } + + public function star($id) + { + return $this->put('gists/'.urlencode($id).'/star'); + } + + public function unstar($id) + { + return $this->delete('gists/'.urlencode($id).'/star'); + } +} diff --git a/lib/Github/Api/GitData.php b/lib/Github/Api/GitData.php new file mode 100644 index 00000000000..21395a8ba7a --- /dev/null +++ b/lib/Github/Api/GitData.php @@ -0,0 +1,58 @@ + + */ +class GitData extends AbstractApi +{ + /** + * @return Blobs + */ + public function blobs() + { + return new Blobs($this->client); + } + + /** + * @return Commits + */ + public function commits() + { + return new Commits($this->client); + } + + /** + * @return References + */ + public function references() + { + return new References($this->client); + } + + /** + * @return Tags + */ + public function tags() + { + return new Tags($this->client); + } + + /** + * @return Trees + */ + public function trees() + { + return new Trees($this->client); + } +} diff --git a/lib/Github/Api/GitData/Blobs.php b/lib/Github/Api/GitData/Blobs.php new file mode 100644 index 00000000000..3dd1d984242 --- /dev/null +++ b/lib/Github/Api/GitData/Blobs.php @@ -0,0 +1,37 @@ + + */ +class Blobs extends AbstractApi +{ + public function show($username, $repository, $sha, $raw = false) + { + if ($raw) { + $this->client->setHeaders(array('Accept: application/vnd.github.v3.raw')); + } + + $response = $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/git/blobs/'.urlencode($sha)); + + if ($raw) { + $this->client->clearHeaders(); + } + + return $response; + } + + public function create($username, $repository, array $params) + { + if (!isset($params['content'], $params['encoding'])) { + throw new MissingArgumentException(array('content', 'encoding')); + } + + return $this->post('repos/'.urlencode($username).'/'.urlencode($repository).'/git/blobs', $params); + } +} diff --git a/lib/Github/Api/GitData/Commits.php b/lib/Github/Api/GitData/Commits.php new file mode 100644 index 00000000000..8aaefe3ddf4 --- /dev/null +++ b/lib/Github/Api/GitData/Commits.php @@ -0,0 +1,27 @@ + + */ +class Commits extends AbstractApi +{ + public function show($username, $repository, $sha) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/commits/'.urlencode($sha)); + } + + public function create($username, $repository, array $params) + { + if (!isset($params['message'], $params['tree'], $params['parents'])) { + throw new MissingArgumentException(array('message', 'tree', 'parents')); + } + + return $this->post('repos/'.urlencode($username).'/'.urlencode($repository).'/git/commits', $params); + } +} diff --git a/lib/Github/Api/GitData/References.php b/lib/Github/Api/GitData/References.php new file mode 100644 index 00000000000..89886084660 --- /dev/null +++ b/lib/Github/Api/GitData/References.php @@ -0,0 +1,56 @@ + + */ +class References extends AbstractApi +{ + public function all($username, $repository) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/git/refs'); + } + + public function branches($username, $repository) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/git/refs/heads'); + } + + public function tags($username, $repository) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/git/refs/tags'); + } + + public function show($username, $repository, $reference) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/git/refs/'.urlencode($reference)); + } + + public function create($username, $repository, array $params) + { + if (!isset($params['ref'], $params['sha'])) { + throw new MissingArgumentException(array('ref', 'sha')); + } + + return $this->post('repos/'.urlencode($username).'/'.urlencode($repository).'/git/refs', $params); + } + + public function update($username, $repository, $reference, array $params) + { + if (!isset($params['sha'])) { + throw new MissingArgumentException('sha'); + } + + return $this->patch('repos/'.urlencode($username).'/'.urlencode($repository).'/git/refs/'.urlencode($reference), $params); + } + + public function remove($username, $repository, $reference) + { + return $this->delete('repos/'.urlencode($username).'/'.urlencode($repository).'/git/refs/'.urlencode($reference)); + } +} diff --git a/lib/Github/Api/GitData/Tags.php b/lib/Github/Api/GitData/Tags.php new file mode 100644 index 00000000000..376a6d6ee4f --- /dev/null +++ b/lib/Github/Api/GitData/Tags.php @@ -0,0 +1,40 @@ + + */ +class Tags extends AbstractApi +{ + public function all($username, $repository) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/git/refs/tags'); + } + + public function show($username, $repository, $sha) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/git/tags/'.urlencode($sha)); + } + + public function create($username, $repository, array $params) + { + if (!isset($params['tag'], $params['message'], $params['object'], $params['type'])) { + throw new MissingArgumentException(array('tag', 'message', 'object', 'type')); + } + + if (!isset($params['tagger'])) { + throw new MissingArgumentException('tagger'); + } + + if (!isset($params['tagger']['name'], $params['tagger']['email'], $params['tagger']['date'])) { + throw new MissingArgumentException(array('tagger.name', 'tagger.email', 'tagger.date')); + } + + return $this->post('repos/'.urlencode($username).'/'.urlencode($repository).'/git/tags', $params); + } +} diff --git a/lib/Github/Api/GitData/Trees.php b/lib/Github/Api/GitData/Trees.php new file mode 100644 index 00000000000..09c9f0eb9e3 --- /dev/null +++ b/lib/Github/Api/GitData/Trees.php @@ -0,0 +1,35 @@ + + */ +class Trees extends AbstractApi +{ + public function show($username, $repository, $sha, $recursive = false) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/git/trees/'.urlencode($sha), array('recursive' => $recursive ? 1 : null)); + } + + public function create($username, $repository, array $params) + { + if (!isset($params['tree'])) { + throw new MissingArgumentException('tree'); + } + if (!isset($params['tree']['path'], $params['tree']['mode'], $params['tree']['type'])) { + throw new MissingArgumentException(array('tree.path', 'tree.mode', 'tree.type')); + } + + // If `sha` is not set, `content` is required + if (!isset($params['tree']['sha']) && !isset($params['tree']['content'])) { + throw new MissingArgumentException('tree.content'); + } + + return $this->post('repos/'.urlencode($username).'/'.urlencode($repository).'/git/trees', $params); + } +} diff --git a/lib/Github/Api/Issue.php b/lib/Github/Api/Issue.php index 07a3d301628..56aa327ed47 100644 --- a/lib/Github/Api/Issue.php +++ b/lib/Github/Api/Issue.php @@ -2,83 +2,67 @@ namespace Github\Api; +use Github\Api\Issue\Comments; +use Github\Api\Issue\Events; +use Github\Api\Issue\Labels; +use Github\Api\Issue\Milestones; +use Github\Exception\MissingArgumentException; + /** * Listing issues, searching, editing and closing your projects issues. * - * @link http://develop.github.com/p/issues.html - * @author Thibault Duplessis - * @license MIT License + * @link http://develop.github.com/p/issues.html + * @author Thibault Duplessis + * @author Joseph Bielawski */ -class Issue extends Api +class Issue extends AbstractApi { /** * List issues by username, repo and state * @link http://developer.github.com/v3/issues/ * - * @param string $username the username - * @param string $repo the repo - * @param string $state the issue state, can be open or closed - * @param array $parameters the additional parameters like milestone, assignee, lables, sort, direction - * @return array list of issues found + * @param string $username the username + * @param string $repository the repository + * @param array $params the additional parameters like milestone, assignees, labels, sort, direction + * @return array list of issues found */ - public function getList($username, $repo, $state = null, $parameters = array()) + public function all($username, $repository, array $params = array()) { - $url = 'repos/'.urlencode($username).'/'.urlencode($repo).'/issues'; - if ($state) { - $parameters['state'] = $state; - } - if ($parameters) { - $url .= '?'.http_build_query($parameters); - } - - return $this->get($url); + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/issues', array_merge(array('page' => 1), $params)); } /** * Search issues by username, repo, state and keyword * @link http://developer.github.com/v3/search/#search-issues * - * @param string $username the username - * @param string $repo the repo - * @param string $state the issue state, can be open or closed - * @param string $keyword the keyword to filter issues by + * @param string $username the username + * @param string $repository the repository + * @param string $state the issue state, can be open or closed + * @param string $keyword the keyword to filter issues by * - * @return array list of issues found + * @return array list of issues found */ - public function search($username, $repo, $state, $keyword) + public function find($username, $repository, $state, $keyword) { if (!in_array($state, array('open', 'closed'))) { $state = 'open'; } - return $this->get('legacy/issues/search/'.urlencode($username).'/'.urlencode($repo).'/'.urlencode($state).'/'.urlencode($keyword)); - } - - /** - * Search issues by label - * - * @param string $username the username - * @param string $repo the repo - * @param string $label the label to filter issues by - * @return array list of issues found - */ - public function searchLabel($username, $repo, $label) - { - throw new \BadMethodCallException('Method cannot be implemented using new api version'); + return $this->get('legacy/issues/search/'.urlencode($username).'/'.urlencode($repository).'/'.urlencode($state).'/'.urlencode($keyword)); } /** * Get extended information about an issue by its username, repo and number * @link http://developer.github.com/v3/issues/ * - * @param string $username the username - * @param string $repo the repo - * @param string $number the issue number - * @return array information about the issue + * @param string $username the username + * @param string $repository the repository + * @param string $id the issue number + * @return array information about the issue */ - public function show($username, $repo, $number) + public function show($username, $repository, $id) { - return $this->get('repos/'.urlencode($username).'/'.urlencode($repo).'/issues/'.urlencode($number)); + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/issues/'.urlencode($id)); } /** @@ -86,165 +70,79 @@ public function show($username, $repo, $number) * The issue is assigned to the authenticated user. Requires authentication. * @link http://developer.github.com/v3/issues/ * - * @param string $username the username - * @param string $repo the repo - * @param string $title the new issue title - * @param string $body the new issue body - * @return array information about the issue - */ - public function open($username, $repo, $title, $body) - { - return $this->post('repos/'.urlencode($username).'/'.urlencode($repo).'/issues', array( - 'title' => $title, - 'body' => $body - )); - } - - /** - * Close an existing issue by username, repo and issue number. Requires authentication. - * @link http://developer.github.com/v3/issues/ + * @param string $username the username + * @param string $repository the repository + * @param array $params the new issue data + * @return array information about the issue * - * @param string $username the username - * @param string $repo the repo - * @param string $number the issue number - * @return array information about the issue + * @throws MissingArgumentException */ - public function close($username, $repo, $number) + public function create($username, $repository, array $params) { - return $this->update($username, $repo, $number, array('state' => 'closed')); + if (!isset($params['title'], $params['body'])) { + throw new MissingArgumentException(array('title', 'body')); + } + + return $this->post('repos/'.urlencode($username).'/'.urlencode($repository).'/issues', $params); } /** - * Update issue informations by username, repo and issue number. Requires authentication. + * Update issue information's by username, repo and issue number. Requires authentication. * @link http://developer.github.com/v3/issues/ * * @param string $username the username - * @param string $repo the repo - * @param string $number the issue number - * @param array $data key=>value user attributes to update. + * @param string $repository the repository + * @param string $id the issue number + * @param array $params key=>value user attributes to update. * key can be title or body * @return array information about the issue */ - public function update($username, $repo, $number, array $data) + public function update($username, $repository, $id, array $params) { - return $this->patch('repos/'.urlencode($username).'/'.urlencode($repo).'/issues/'.urlencode($number), $data); + return $this->patch('repos/'.urlencode($username).'/'.urlencode($repository).'/issues/'.urlencode($id), $params); } /** - * Reopen an existing issue by username, repo and issue number. Requires authentication. - * @link http://developer.github.com/v3/issues/ - * - * @param string $username the username - * @param string $repo the repo - * @param string $number the issue number - * @return array informations about the issue - */ - public function reOpen($username, $repo, $number) - { - return $this->update($username, $repo, $number, array('state' => 'open')); - } - - /** - * List an issue comments by username, repo and issue number - * @link http://developer.github.com/v3/issues/comments/ - * - * @param string $username the username - * @param string $repo the repo - * @param string $number the issue number - * @return array list of issue comments - */ - public function getComments($username, $repo, $number) - { - return $this->get('repos/'.urlencode($username).'/'.urlencode($repo).'/issues/'.urlencode($number).'/comments'); - } - - /** - * Get an issue comments by username, repo, issue number and comment id - * @link http://developer.github.com/v3/issues/comments/ - * - * @param string $username the username - * @param string $repo the repo - * @param string $id the comment id - * @return array list of issue comments - */ - public function getComment($username, $repo, $id) - { - return $this->get('repos/'.urlencode($username).'/'.urlencode($repo).'/issues/comments/'.urlencode($id)); - } - - /** - * Add a comment to the issue by username, repo and issue number + * List an issue comments * @link http://developer.github.com/v3/issues/comments/ * - * @param string $username the username - * @param string $repo the repo - * @param string $number the issue number - * @param string $body the comment body - * @return array the created comment - */ - public function addComment($username, $repo, $number, $body) - { - return $this->post('repos/'.urlencode($username).'/'.urlencode($repo).'/issues/'.urlencode($number).'/comments', array( - 'body' => $body - )); - } - - /** - * List all project labels by username and repo - * @link http://developer.github.com/v3/issues/labels/ - * - * @param string $username the username - * @param string $repo the repo - * @return array list of project labels + * @return Comments */ - public function getLabels($username, $repo) + public function comments() { - return $this->get('repos/'.urlencode($username).'/'.urlencode($repo).'/labels'); + return new Comments($this->client); } /** - * Get project label by username and repo - * @link http://developer.github.com/v3/issues/labels/ + * List all project events + * @link http://developer.github.com/v3/issues/events/ * - * @param string $username the username - * @param string $repo the repo - * @param string $name the label name - * @return array list of project labels + * @return Events */ - public function getLabel($username, $repo, $name) + public function events() { - return $this->get('repos/'.urlencode($username).'/'.urlencode($repo).'/labels/'.urlencode($name)); + return new Events($this->client); } /** - * Add a label to the issue by username, repo and issue number. Requires authentication. + * List all project labels * @link http://developer.github.com/v3/issues/labels/ * - * @param string $username the username - * @param string $repo the repo - * @param string $labelName the label name - * @param string $labelColor the label color - * @return array list of issue labels + * @return Labels */ - public function addLabel($username, $repo, $labelName, $labelColor) + public function labels() { - return $this->post('repos/'.urlencode($username).'/'.urlencode($repo).'/labels', array( - 'name' => $labelName, - 'color' => $labelColor - )); + return new Labels($this->client); } /** - * Remove a label from the issue by username, repo, issue number and label name. Requires authentication. - * @link http://developer.github.com/v3/issues/labels/ + * List all project milestones + * @link http://developer.github.com/v3/issues/milestones/ * - * @param string $username the username - * @param string $repo the repo - * @param string $labelName the label name - * @return array list of issue labels + * @return Milestones */ - public function removeLabel($username, $repo, $labelName) + public function milestones() { - return $this->delete('repos/'.urlencode($username).'/'.urlencode($repo).'/labels/'.urlencode($labelName)); + return new Milestones($this->client); } } diff --git a/lib/Github/Api/Issue/Comments.php b/lib/Github/Api/Issue/Comments.php new file mode 100644 index 00000000000..01e5dc742c5 --- /dev/null +++ b/lib/Github/Api/Issue/Comments.php @@ -0,0 +1,48 @@ + + */ +class Comments extends AbstractApi +{ + public function all($username, $repository, $issue, $page = 1) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/issues/'.urlencode($issue).'/comments', array( + 'page' => $page + )); + } + + public function show($username, $repository, $comment) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/issues/comments/'.urlencode($comment)); + } + + public function create($username, $repository, $issue, array $params) + { + if (!isset($params['body'])) { + throw new MissingArgumentException('body'); + } + + return $this->post('repos/'.urlencode($username).'/'.urlencode($repository).'/issues/'.urlencode($issue).'/comments', $params); + } + + public function update($username, $repository, $comment, array $params) + { + if (!isset($params['body'])) { + throw new MissingArgumentException('body'); + } + + return $this->patch('repos/'.urlencode($username).'/'.urlencode($repository).'/issues/comments/'.urlencode($comment), $params); + } + + public function remove($username, $repository, $comment) + { + return $this->delete('repos/'.urlencode($username).'/'.urlencode($repository).'/issues/comments/'.urlencode($comment)); + } +} diff --git a/lib/Github/Api/Issue/Events.php b/lib/Github/Api/Issue/Events.php new file mode 100644 index 00000000000..dab5ae520c5 --- /dev/null +++ b/lib/Github/Api/Issue/Events.php @@ -0,0 +1,30 @@ + + */ +class Events extends AbstractApi +{ + public function all($username, $repository, $issue = null, $page = 1) + { + if (null === $issue) { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/issues/'.urlencode($issue).'/events', array( + 'page' => $page + )); + } + + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/issues/events', array( + 'page' => $page + )); + } + + public function show($username, $repository, $event) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/issues/events/'.urlencode($event)); + } +} diff --git a/lib/Github/Api/Issue/Labels.php b/lib/Github/Api/Issue/Labels.php new file mode 100644 index 00000000000..55e68273d3a --- /dev/null +++ b/lib/Github/Api/Issue/Labels.php @@ -0,0 +1,44 @@ + + */ +class Labels extends AbstractApi +{ + public function all($username, $repository, $issue) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/issues/'.urlencode($issue).'/labels'); + } + + public function add($username, $repository, $issue, $labels) + { + if (is_string($labels)) { + $labels = array($labels); + } elseif (0 === count($labels)) { + throw new InvalidArgumentException(); + } + + return $this->post('repos/'.urlencode($username).'/'.urlencode($repository).'/issues/'.urlencode($issue).'/labels', $labels); + } + + public function replace($username, $repository, $issue, array $params) + { + return $this->put('repos/'.urlencode($username).'/'.urlencode($repository).'/issues/'.urlencode($issue).'/labels', $params); + } + + public function remove($username, $repository, $issue, $label) + { + return $this->delete('repos/'.urlencode($username).'/'.urlencode($repository).'/issues/'.urlencode($issue).'/labels/'.urlencode($label)); + } + + public function clear($username, $repository, $issue) + { + return $this->delete('repos/'.urlencode($username).'/'.urlencode($repository).'/issues/'.urlencode($issue).'/labels'); + } +} diff --git a/lib/Github/Api/Issue/Milestones.php b/lib/Github/Api/Issue/Milestones.php new file mode 100644 index 00000000000..b76f7d9a6f4 --- /dev/null +++ b/lib/Github/Api/Issue/Milestones.php @@ -0,0 +1,64 @@ + + */ +class Milestones extends AbstractApi +{ + public function all($username, $repository, array $params = array()) + { + if (!in_array($params['state'], array('open', 'closed'))) { + $params['state'] = 'open'; + } + if (!in_array($params['sort'], array('due_date', 'completeness'))) { + $params['sort'] = 'due_date'; + } + if (!in_array($params['direction'], array('asc', 'desc'))) { + $params['direction'] = 'desc'; + } + + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/milestones', array_merge(array('page' => 1, 'state' => 'open', 'sort' => 'due_date', 'direction' => 'desc'), $params)); + } + + public function show($username, $repository, $id) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/milestones/'.urlencode($id)); + } + + public function create($username, $repository, array $params) + { + if (!isset($params['title'])) { + throw new MissingArgumentException('title'); + } + if (isset($params['state']) && !in_array($params['state'], array('open', 'closed'))) { + $params['state'] = 'open'; + } + + return $this->post('repos/'.urlencode($username).'/'.urlencode($repository).'/milestones', $params); + } + + public function update($username, $repository, $milestone, array $params) + { + if (isset($params['state']) && !in_array($params['state'], array('open', 'closed'))) { + $params['state'] = 'open'; + } + + return $this->patch('repos/'.urlencode($username).'/'.urlencode($repository).'/milestones/'.urlencode($milestone), $params); + } + + public function remove($username, $repository, $milestone) + { + return $this->delete('repos/'.urlencode($username).'/'.urlencode($repository).'/milestones/'.urlencode($milestone)); + } + + public function labels($username, $repository, $milestone) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/milestones/'.urlencode($milestone).'/labels'); + } +} diff --git a/lib/Github/Api/Markdown.php b/lib/Github/Api/Markdown.php index 3ae3fbd5593..8620ee86757 100644 --- a/lib/Github/Api/Markdown.php +++ b/lib/Github/Api/Markdown.php @@ -8,7 +8,7 @@ * @link http://developer.github.com/v3/markdown/ * @author Joseph Bielawski */ -class Markdown extends Api +class Markdown extends AbstractApi { /** * @param string $text diff --git a/lib/Github/Api/Object.php b/lib/Github/Api/Object.php deleted file mode 100644 index 6e55c6cd51a..00000000000 --- a/lib/Github/Api/Object.php +++ /dev/null @@ -1,94 +0,0 @@ - - * @license MIT License - */ -class Object extends Api -{ - /** - * Get a listing of the root tree of a project by the username, repo, and tree SHA - * http://develop.github.com/p/object.html#trees - * - * @param string $username the username - * @param string $repo the repo - * @param string $treeSHA the tree sha - * @param boolean $resursive the recursive flag - * @return array root tree of the project - */ - public function showTree($username, $repo, $treeSHA, $recursive = false) - { - $url = 'repos/'.urlencode($username).'/'.urlencode($repo).'/git/trees/'.urlencode($treeSHA); - if ($recursive) { - $url .= '?recursive=1'; - } - - return $this->get($url); - } - - /** - * Lists the data blobs of a tree by tree SHA - * - * @param string $username the username - * @param string $repo the repo - * @param string $treeSHA the tree sha - * @param string $path the path - * @return array data blobs of tree - */ - public function listBlobs($username, $repo, $treeSHA) - { - $tree = $this->showTree($username, $repo, $treeSHA, true); - - if (isset($tree['tree'])) { - $blobs = array_filter($tree['tree'], function ($element) { - return 'blob' === $element['type']; - }); - - return $blobs; - } - } - - /** - * Get the data about a blob by tree SHA and file path. - * - * @param string $username the username - * @param string $repo the repo - * @param string $treeSHA the tree sha - * @param string $path the path - * @return array data blob of tree and path - */ - public function showBlob($username, $repo, $treeSHA, $path) - { - $tree = $this->showTree($username, $repo, $treeSHA, true); - - if (isset($tree['tree'])) { - $blobs = array_filter($tree['tree'], function ($element) use ($path) { - return $path === $element['path']; - }); - - return reset($blobs); - } - } - - /** - * Returns the raw text content of the object. - * - * @param string $username the username - * @param string $repo the repo - * @param string $objectSHA the object sha can be either a blob SHA1, a tree SHA1 or a commit SHA1 - * @return string raw text content of the blob, tree or commit object - */ - public function getRawData($username, $repo, $objectSHA) - { - $this->client->setHeaders(array('Accept: application/vnd.github.v3.raw')); - $response = $this->get('repos/'.urlencode($username).'/'.urlencode($repo).'/git/blobs/'.urlencode($objectSHA)); - $this->client->clearHeaders(); - - return $response; - } -} diff --git a/lib/Github/Api/Organization.php b/lib/Github/Api/Organization.php index 2b3b40ad7a5..29f23ccf2d7 100644 --- a/lib/Github/Api/Organization.php +++ b/lib/Github/Api/Organization.php @@ -2,133 +2,65 @@ namespace Github\Api; -use Github\Exception\InvalidArgumentException; +use Github\Api\Organization\Members; +use Github\Api\Organization\Teams; /** - * Searching organizations, getting organization information - * and managing authenticated organization account information. + * Getting organization information and managing authenticated organization account information. * - * @link http://develop.github.com/p/orgs.html - * @author Antoine Berranger - * @license MIT License + * @link http://developer.github.com/v3/orgs/ + * @author Antoine Berranger + * @author Joseph Bielawski */ -class Organization extends Api +class Organization extends AbstractApi { - const ADMIN = "admin"; - const PUSH = "push"; - const PULL = "pull"; - /** * Get extended information about an organization by its name - * http://develop.github.com/p/orgs.html + * @link http://developer.github.com/v3/orgs/#get * - * @param string $name the organization to show - * @return array informations about the organization - */ - public function show($name) - { - return $this->get('orgs/'.urlencode($name)); - } - - /** - * List all repositories across all the organizations that you can access - * @link http://developer.github.com/v3/repos/#list-organization-repositories + * @param string $organization the organization to show * - * @param string $name the user name - * @param string $type the type of repositories - * @return array the repositories + * @return array informations about the organization */ - public function getAllRepos($name, $type = 'all') + public function show($organization) { - return $this->get('orgs/'.urlencode($name).'/repos?type='.urlencode($type)); + return $this->get('orgs/'.urlencode($organization)); } - /** - * List all public repositories of any other organization - * @link http://developer.github.com/v3/repos/#list-organization-repositories - * - * @param string $name the organization name - * @return array the repositories - */ - public function getPublicRepos($name) + public function update($organization, array $params) { - return $this->getAllRepos($name, 'public'); + return $this->patch('orgs/'.urlencode($organization), $params); } /** - * List all public members of that organization - * @link http://developer.github.com/v3/orgs/members/#list-members - * - * @param string $name the organization name - * @return array the members - */ - public function getPublicMembers($name) - { - return $this->get('orgs/'.urlencode($name).'/members'); - } - - /** - * Check that user is in that organization - * @link http://developer.github.com/v3/orgs/members/#get-member + * List all repositories across all the organizations that you can access + * @link http://developer.github.com/v3/repos/#list-organization-repositories * - * @param string $name the organization name - * @param string $user the user - * @return array the members - */ - public function getPublicMember($name, $user) - { - return $this->get('orgs/'.urlencode($name).'/members/'.urlencode($user)); - } - - /** - * List all teams of that organization - * @link http://developer.github.com/v3/orgs/teams/#list-teams + * @param string $organization the user name + * @param string $type the type of repositories * - * @param string $name the organization name - * @return array the teams + * @return array the repositories */ - public function getTeams($name) + public function repositories($organization, $type = 'all') { - return $this->get('orgs/'.urlencode($name).'/teams'); + return $this->get('orgs/'.urlencode($organization).'/repos', array( + 'type' => $type + )); } /** - * Get team with given id of that organization - * @link http://developer.github.com/v3/orgs/teams/#get-team - * - * @param string $name the organization name - * @param string $id id of team - * @return array the team + * @return Members */ - public function getTeam($name, $id) + public function members() { - return $this->get('orgs/'.urlencode($name).'/teams/'.urlencode($id)); + return new Members($this->client); } /** - * Add a team to that organization - * @link http://developer.github.com/v3/orgs/teams/#create-team - * - * @param string $organization the organization name - * @param string $team name of the new team - * @param string $permission its permission [PULL|PUSH|ADMIN] - * @param array $repositories (optional) its repositories names - * - * @return array the teams - * - * @throws InvalidArgumentException + * @return Teams */ - public function addTeam($organization, $team, $permission = 'pull', array $repositories = array()) + public function teams() { - if (!in_array($permission, array(self::ADMIN, self::PUSH, self::PULL))) { - throw new InvalidArgumentException("Invalid value for the permission variable"); - } - - return $this->post('orgs/'.urlencode($organization).'/teams', array( - 'name' => $team, - 'permission' => $permission, - 'repo_names' => $repositories - )); + return new Teams($this->client); } - } diff --git a/lib/Github/Api/Organization/Members.php b/lib/Github/Api/Organization/Members.php new file mode 100644 index 00000000000..7aa9e29bf1b --- /dev/null +++ b/lib/Github/Api/Organization/Members.php @@ -0,0 +1,46 @@ + + */ +class Members extends AbstractApi +{ + public function all($organization, $type = null) + { + if (null === $type) { + return $this->get('orgs/'.urlencode($organization).'/members'); + } + + return $this->get('orgs/'.urlencode($organization).'/public_members'); + } + + public function show($organization, $username) + { + return $this->get('orgs/'.urlencode($organization).'/members/'.urlencode($username)); + } + + public function check($organization, $username) + { + return $this->get('orgs/'.urlencode($organization).'/public_members/'.urlencode($username)); + } + + public function publicize($organization, $username) + { + return $this->put('orgs/'.urlencode($organization).'/public_members/'.urlencode($username)); + } + + public function conceal($organization, $username) + { + return $this->delete('orgs/'.urlencode($organization).'/public_members/'.urlencode($username)); + } + + public function remove($organization, $username) + { + return $this->delete('orgs/'.urlencode($organization).'/members/'.urlencode($username)); + } +} diff --git a/lib/Github/Api/Organization/Teams.php b/lib/Github/Api/Organization/Teams.php new file mode 100644 index 00000000000..5310b5df0a6 --- /dev/null +++ b/lib/Github/Api/Organization/Teams.php @@ -0,0 +1,95 @@ + + */ +class Teams extends AbstractApi +{ + public function all($organization) + { + return $this->get('orgs/'.urlencode($organization).'/teams'); + } + + public function show($organization, $team) + { + return $this->get('orgs/'.urlencode($organization).'/teams/'.urlencode($team)); + } + + public function create($organization, array $params) + { + if (!isset($params['name'])) { + throw new MissingArgumentException('name'); + } + if (isset($params['repo_names']) && !is_array($params['repo_names'])) { + $params['repo_names'] = array($params['repo_names']); + } + if (isset($params['permission']) && !in_array($params['permission'], array('pull', 'push', 'admin'))) { + $params['permission'] = 'pull'; + } + + return $this->post('orgs/'.urlencode($organization).'/teams', $params); + } + + public function update($team, array $params) + { + if (!isset($params['name'])) { + throw new MissingArgumentException('name'); + } + if (isset($params['permission']) && !in_array($params['permission'], array('pull', 'push', 'admin'))) { + $params['permission'] = 'pull'; + } + + return $this->patch('teams/'.urlencode($team), $params); + } + + public function remove($team) + { + return $this->delete('teams/'.urlencode($team)); + } + + public function members($team) + { + return $this->get('teams/'.urlencode($team).'/members'); + } + + public function check($team, $username) + { + return $this->get('teams/'.urlencode($team).'/members/'.urlencode($username)); + } + + public function addMember($team, $username) + { + return $this->put('teams/'.urlencode($team).'/members/'.urlencode($username)); + } + + public function removeMember($team, $username) + { + return $this->delete('teams/'.urlencode($team).'/members/'.urlencode($username)); + } + + public function repositories($team) + { + return $this->get('teams/'.urlencode($team).'/repos'); + } + + public function repository($team, $username, $repository) + { + return $this->get('teams/'.urlencode($team).'/repos/'.urlencode($username).'/'.urlencode($repository)); + } + + public function addRepository($team, $username, $repository) + { + return $this->put('teams/'.urlencode($team).'/repos/'.urlencode($username).'/'.urlencode($repository)); + } + + public function removeRepository($team, $username, $repository) + { + return $this->delete('teams/'.urlencode($team).'/repos/'.urlencode($username).'/'.urlencode($repository)); + } +} diff --git a/lib/Github/Api/PullRequest.php b/lib/Github/Api/PullRequest.php index 9c62353aa78..5cb9b6d96ec 100644 --- a/lib/Github/Api/PullRequest.php +++ b/lib/Github/Api/PullRequest.php @@ -2,78 +2,116 @@ namespace Github\Api; +use Github\Api\PullRequest\Comments; +use Github\Exception\MissingArgumentException; + /** * API for accessing Pull Requests from your Git/Github repositories. * - * @link http://develop.github.com/p/pulls.html - * @author Nicolas Pastorino - * @license MIT License + * @link http://developer.github.com/v3/pulls/ + * @author Joseph Bielawski */ -class PullRequest extends Api +class PullRequest extends AbstractApi { /** - * Get a listing of a project's pull requests by the username, repo, and optionally state. - * @link http://developer.github.com/v3/pulls/ + * Get a listing of a project's pull requests by the username, repository and (optionally) state. + * @link http://developer.github.com/v3/pulls/ + * + * @param string $username the username + * @param string $repository the repository + * @param string $state the state of the fetched pull requests. + * The API seems to automatically default to 'open' * - * @param string $username the username - * @param string $repo the repo - * @param string $state the state of the fetched pull requests. - * The API seems to automatically default to 'open' - * @return array array of pull requests for the project + * @return array array of pull requests for the project */ - public function listPullRequests($username, $repo, $state = null) + public function all($username, $repository, $state = null) { - $url = 'repos/'.urlencode($username).'/'.urlencode($repo).'/pulls'; - if ($state) { - $url .= '?state='.urlencode($state); - } - - return $this->get($url); + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/pulls', array('state' => $state)); } /** * Show all details of a pull request, including the discussions. - * @link http://developer.github.com/v3/pulls/ + * @link http://developer.github.com/v3/pulls/ * - * @param string $username the username - * @param string $repo the repo - * @param string $id the ID of the pull request for which details are retrieved - * @return array array of pull requests for the project + * @param string $username the username + * @param string $repository the repository + * @param string $id the ID of the pull request for which details are retrieved + * + * @return array array of pull requests for the project */ - public function show($username, $repo, $id) + public function show($username, $repository, $id) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/pulls/'.urlencode($id)); + } + + public function commits($username, $repository, $id) { - return $this->get('repos/'.urlencode($username).'/'.urlencode($repo).'/pulls/'.urlencode($id)); + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/pulls/'.urlencode($id).'/commits'); + } + + public function files($username, $repository, $id) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/pulls/'.urlencode($id).'/files'); + } + + public function comments() + { + return new Comments($this->client); } /** * Create a pull request + * @link http://developer.github.com/v3/pulls/ + * + * @param string $username the username + * @param string $repository the repository + * @param array $params A String of the branch or commit SHA that you want your changes to be pulled to. + * A String of the branch or commit SHA of your changes. Typically this will be a branch. + * If the branch is in a fork of the original repository, specify the username first: + * "my-user:some-branch". The String title of the Pull Request. The String body of + * the Pull Request. The issue number. Used when title and body is not set. * - * @link http://developer.github.com/v3/pulls/ - * @param string $username the username - * @param string $repo the repo - * @param string $base A String of the branch or commit SHA that you want your changes to be pulled to. - * @param string $head A String of the branch or commit SHA of your changes. - * Typically this will be a branch. If the branch is in a fork of the original repository, - * specify the username first: "my-user:some-branch". - * @param string $title The String title of the Pull Request. Used in pair with $body. - * @param string $body The String body of the Pull Request. Used in pair with $title. - * @param string $issueNumber The issue number. Used when title and body is not set. - * @return array array of pull requests for the project + * @return array + * + * @throws MissingArgumentException */ - public function create($username, $repo, $base, $head, $title, $body = null, $issueNumber = null) + public function create($username, $repository, array $params) { - $input = array( - 'head' => $head, - 'base' => $base, - ); + // Two ways to create PR, using issue or title + if (!isset($params['issue']) && !isset($params['title'])) { + throw new MissingArgumentException(array('issue', 'title')); + } + + if (!isset($params['base'], $params['head'])) { + throw new MissingArgumentException(array('base', 'head')); + } - if ($title || $body) { - $input['title'] = $title; - $input['body'] = $body; - } else { - $input['issue'] = $issueNumber; + // If `issue` is not sent, then `body` must be sent + if (!isset($params['issue']) && !isset($params['body'])) { + throw new MissingArgumentException(array('issue', 'body')); } - return $this->post('repos/'.urlencode($username).'/'.urlencode($repo).'/pulls', $input); + return $this->post('repos/'.urlencode($username).'/'.urlencode($repository).'/pulls', $params); + } + + public function update($username, $repository, array $params) + { + if (isset($params['state']) && !in_array($params['state'], array('open', 'closed'))) { + $params['state'] = 'open'; + } + + return $this->patch('repos/'.urlencode($username).'/'.urlencode($repository).'/pulls', $params); + } + + public function merged($username, $repository, $id) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/pulls/'.urlencode($id).'/merge'); + } + + public function merge($username, $repository, $id, $message = null) + { + return $this->put('repos/'.urlencode($username).'/'.urlencode($repository).'/pulls/'.urlencode($id).'/merge', array( + 'commit_message' => $message + )); } } diff --git a/lib/Github/Api/PullRequest/Comments.php b/lib/Github/Api/PullRequest/Comments.php new file mode 100644 index 00000000000..1951be8b2ea --- /dev/null +++ b/lib/Github/Api/PullRequest/Comments.php @@ -0,0 +1,51 @@ + + */ +class Comments extends AbstractApi +{ + public function all($username, $repository, $pullRequest) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/pulls/'.urlencode($pullRequest).'/comments'); + } + + public function show($username, $repository, $comment) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/pulls/comments/'.urlencode($comment)); + } + + public function create($username, $repository, $sha, array $params) + { + if (!isset($params['body'])) { + throw new MissingArgumentException('body'); + } + + // If `in_reply_to` is set, other options are not necessary anymore + if (!isset($params['in_reply_to']) && !isset($params['commit_id'], $params['path'], $params['position'])) { + throw new MissingArgumentException(array('commit_id', 'path', 'position')); + } + + return $this->post('repos/'.urlencode($username).'/'.urlencode($repository).'/pulls/'.urlencode($sha).'/comments', $params); + } + + public function update($username, $repository, $comment, array $params) + { + if (!isset($params['body'])) { + throw new MissingArgumentException('body'); + } + + return $this->patch('repos/'.urlencode($username).'/'.urlencode($repository).'/pulls/comments/'.urlencode($comment), $params); + } + + public function remove($username, $repository, $comment) + { + return $this->delete('repos/'.urlencode($username).'/'.urlencode($repository).'/pulls/comments/'.urlencode($comment)); + } +} diff --git a/lib/Github/Api/Repo.php b/lib/Github/Api/Repo.php index c011656f9a5..9b53301400e 100644 --- a/lib/Github/Api/Repo.php +++ b/lib/Github/Api/Repo.php @@ -2,97 +2,72 @@ namespace Github\Api; +use Github\Api\Repository\Collaborators; +use Github\Api\Repository\Comments; +use Github\Api\Repository\Commits; +use Github\Api\Repository\Contents; +use Github\Api\Repository\DeployKeys; +use Github\Api\Repository\Downloads; +use Github\Api\Repository\Forks; +use Github\Api\Repository\Hooks; +use Github\Api\Repository\Labels; + /** * Searching repositories, getting repository information * and managing repository information for authenticated users. * - * @link http://develop.github.com/p/repos.html - * @author Thibault Duplessis - * @license MIT License + * @link http://developer.github.com/v3/repos/ + * @author Joseph Bielawski + * @author Thibault Duplessis */ -class Repo extends Api +class Repo extends AbstractApi { /** * Search repositories by keyword: * @link http://developer.github.com/v3/search/#search-repositories * - * @param string $keyword the search query - * @param string $language takes the same values as the language drop down on http://github.com/search - * @param integer $startPage the page number - * - * @return array list of founded repositories - */ - public function search($keyword, $language = null, $startPage = 1) - { - $url = 'legacy/repos/search/'.urlencode($keyword); - - $params = array(); - if (null !== $language) { - $params['language'] = $language; - } - if (1 !== $startPage) { - $params['start_page'] = $startPage; - } - - if (!empty($params)) { - $url .= '?' . http_build_query($params); - } - - return $this->get($url); - } - - /** - * Get a list of the repositories that the authenticated user can push to + * @param string $keyword the search query + * @param array $params * - * @return array list of repositories + * @return array list of founded repositories */ - public function getPushableRepos() + public function find($keyword, array $params) { - throw new \BadMethodCallException('Method cannot be implemented using new api version'); + return $this->get('legacy/repos/search/'.urlencode($keyword), array_merge(array('start_page' => 1), $params)); } /** - * Get the repositories of a user + * Get extended information about a repository by its username and repository name * @link http://developer.github.com/v3/repos/ * - * @param string $username the username - * @return array list of the user repos - */ - public function getUserRepos($username) - { - return $this->get('users/'.urlencode($username).'/repos'); - } - - /** - * Get extended information about a repository by its username and repo name - * @link http://developer.github.com/v3/repos/ + * @param string $username the user who owns the repository + * @param string $repository the name of the repository * - * @param string $username the user who owns the repo - * @param string $repo the name of the repo - * @return array informations about the repo + * @return array informations about the repository */ - public function show($username, $repo) + public function show($username, $repository) { - return $this->get('repos/'.urlencode($username).'/'.urlencode($repo)); + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository)); } /** - * Create repo + * Create repository * @link http://developer.github.com/v3/repos/ * - * @param string $name name of the repository - * @param string $description repo description - * @param string $homepage homepage url - * @param bool $public 1 for public, 0 for private - * @return array returns repo data + * @param string $name name of the repository + * @param string $description repository description + * @param string $homepage homepage url + * @param boolean $public 1 for public, 0 for private + * + * @return array returns repository data */ public function create($name, $description = '', $homepage = '', $public = true) { return $this->post('user/repos', array( - 'name' => $name, + 'name' => $name, 'description' => $description, - 'homepage' => $homepage, - 'private' => !$public + 'homepage' => $homepage, + 'private' => !$public )); } @@ -100,263 +75,171 @@ public function create($name, $description = '', $homepage = '', $public = true) * Set information of a repository * @link http://developer.github.com/v3/repos/ * - * @param string $username the user who owns the repo - * @param string $repo the name of the repo - * @param array $values the key => value pairs to post - * @return array informations about the repo - */ - public function setRepoInfo($username, $repo, $values) - { - return $this->patch('repos/'.urlencode($username).'/'.urlencode($repo), $values); - } - - /** - * Set the visibility of a repository to public - * @link http://developer.github.com/v3/repos/ + * @param string $username the user who owns the repository + * @param string $repository the name of the repository + * @param array $values the key => value pairs to post * - * @param string $username the user who owns the repo - * @param string $repo the name of the repo - * @return array informations about the repo + * @return array informations about the repository */ - public function setPublic($username, $repo) + public function update($username, $repository, array $values) { - $this->setRepoInfo($username, $repo, array('private' => false)); + return $this->patch('repos/'.urlencode($username).'/'.urlencode($repository), $values); } /** - * Set the visibility of a repository to private - * @link http://developer.github.com/v3/repos/ + * Manage the collaborators of a repository + * @link http://developer.github.com/v3/repos/collaborators/ * - * @param string $username the user who owns the repo - * @param string $repo the name of the repo - * @return array informations about the repo + * @return Collaborators */ - public function setPrivate($username, $repo) + public function collaborators() { - $this->setRepoInfo($username, $repo, array('private' => true)); + return new Collaborators($this->client); } /** - * Get the list of deploy keys for a repository - * @link http://developer.github.com/v3/repos/keys/ + * Manage the comments of a repository + * @link http://developer.github.com/v3/repos/comments/ * - * @param string $username the user who owns the repo - * @param string $repo the name of the repo - * @return array the list of deploy keys + * @return Comments */ - public function getDeployKeys($username, $repo) + public function comments() { - return $this->get('repos/'.urlencode($username).'/'.urlencode($repo).'/keys'); + return new Comments($this->client); } /** - * Add a deploy key for a repository - * @link http://developer.github.com/v3/repos/keys/ + * Manage the commits of a repository + * @link http://developer.github.com/v3/repos/commits/ * - * @param string $username the user who owns the repo - * @param string $repo the name of the repo - * @param string $title the title of the key - * @param string $key the public key data - * @return array the list of deploy keys + * @return Commits */ - public function addDeployKey($username, $repo, $title, $key) + public function commits() { - return $this->post('repos/'.urlencode($username).'/'.urlencode($repo).'/keys', array( - 'title' => $title, - 'key' => $key - )); + return new Commits($this->client); } /** - * Delete a deploy key from a repository + * Manage the deploy keys of a repository * @link http://developer.github.com/v3/repos/keys/ * - * @param string $username the user who owns the repo - * @param string $repo the name of the repo - * @param string $id the the id of the key to remove - * @return array the list of deploy keys - */ - public function removeDeployKey($username, $repo, $id) - { - return $this->delete('repos/'.urlencode($username).'/'.urlencode($repo).'/keys/'.urlencode($id)); - } - - /** - * Get the collaborators of a repository - * @link http://developer.github.com/v3/repos/collaborators/ - * - * @param string $username the user who owns the repo - * @param string $repo the name of the repo - * @return array list of the repo collaborators - */ - public function getRepoCollaborators($username, $repo) - { - return $this->get('repos/'.urlencode($username).'/'.urlencode($repo).'/collaborators'); - } - - /** - * Get the collaborator of a repository - * @link http://developer.github.com/v3/repos/collaborators/ - * - * @param string $username the user who owns the repo - * @param string $repo the name of the repo - * @param string $user the user which we seek - * @return array list of the repo collaborators + * @return DeployKeys */ - public function getRepoCollaborator($username, $repo, $user) + public function keys() { - return $this->get('repos/'.urlencode($username).'/'.urlencode($repo).'/collaborators/'.urlencode($user)); + return new DeployKeys($this->client); } /** - * Add a collaborator to a repository - * @link http://developer.github.com/v3/repos/collaborators/ - * - * @param string $username the user who owns the repo - * @param string $repo the name of the repo - * @param string $user the user who should be added as a collaborator - * @return array list of the repo collaborators - */ - public function addRepoCollaborator($username, $repo, $user) - { - return $this->put('repos/'.urlencode($username).'/'.urlencode($repo).'/collaborators/'.urlencode($user)); - } - - /** - * Delete a collaborator from a repository - * @link http://developer.github.com/v3/repos/collaborators/ + * Manage the forks of a repository + * @link http://developer.github.com/v3/repos/forks/ * - * @param string $username the user who owns the repo - * @param string $repo the name of the repo - * @param string $user the user who should be removed as a collaborator - * @return array list of the repo collaborators + * @return Forks */ - public function removeRepoCollaborator($repo, $username, $user) + public function forks() { - return $this->delete('repos/'.urlencode($username).'/'.urlencode($repo).'/collaborators/'.urlencode($user)); + return new Forks($this->client); } /** - * Make the authenticated user watch a repository - * @link http://developer.github.com/v3/repos/watching/ + * Manage the hooks of a repository + * @link http://developer.github.com/v3/issues/jooks/ * - * @param string $username the user who owns the repo - * @param string $repo the name of the repo - * @return array informations about the repo + * @return Hooks */ - public function watch($username, $repo) + public function hooks() { - return $this->put('user/watched/'.urlencode($username).'/'.urlencode($repo)); + return new Hooks($this->client); } /** - * Make the authenticated user unwatch a repository - * @link http://developer.github.com/v3/repos/watching/ + * Manage the labels of a repository + * @link http://developer.github.com/v3/issues/labels/ * - * @param string $username the user who owns the repo - * @param string $repo the name of the repo - * @return array informations about the repo + * @return Labels */ - public function unwatch($username, $repo) + public function labels() { - return $this->delete('user/watched/'.urlencode($username).'/'.urlencode($repo)); + return new Labels($this->client); } /** - * Make the authenticated user fork a repository - * @link http://developer.github.com/v3/repos/forks/ + * @param string $username + * @param string $repository + * @param integer $page * - * @param string $username the user who owns the repo - * @param string $repo the name of the repo - * @return array informations about the newly forked repo + * @return array */ - public function fork($username, $repo) + public function watchers($username, $repository, $page = 1) { - return $this->post('repos/'.urlencode($username).'/'.urlencode($repo).'/forks'); + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/watchers', array( + 'page' => $page + )); } /** * Get the tags of a repository * @link http://developer.github.com/v3/repos/ * - * @param string $username the user who owns the repo - * @param string $repo the name of the repo - * @return array list of the repo tags + * @param string $username the user who owns the repository + * @param string $repository the name of the repository + * + * @return array list of the repository tags */ - public function getRepoTags($username, $repo) + public function tags($username, $repository) { - return $this->get('repos/'.urlencode($username).'/'.urlencode($repo).'/tags'); + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/tags'); } /** - * Get the branches of a repository + * Get the branch(es) of a repository * @link http://developer.github.com/v3/repos/ * - * @param string $username the username - * @param string $repo the name of the repo - * @return array list of the repo branches - */ - public function getRepoBranches($username, $repo) - { - return $this->get('repos/'.urlencode($username).'/'.urlencode($repo).'/branches'); - } - - /** - * Get the watchers of a repository - * @link http://developer.github.com/v3/repos/watching/ + * @param string $username the username + * @param string $repository the name of the repository + * @param string $branch the name of the branch * - * @param string $username the user who owns the repo - * @param string $repo the name of the repo - * @return array list of the repo watchers + * @return array list of the repository branches */ - public function getRepoWatchers($username, $repo) + public function branches($username, $repository, $branch = null) { - return $this->get('repos/'.urlencode($username).'/'.urlencode($repo).'/watchers'); - } + $url = 'repos/'.urlencode($username).'/'.urlencode($repository).'/branches'; + if (null !== $branch) { + $url .= '/'.urlencode($branch); + } - /** - * Get the network (a list of forks) of a repository - * http://develop.github.com/p/repo.html - * - * @param string $username the user who owns the repo - * @param string $repo the name of the repo - * @return array list of the repo forks - */ - public function getRepoNetwork($username, $repo) - { - throw new \BadMethodCallException('Method cannot be implemented using new api version'); + return $this->get($url); } /** * Get the language breakdown of a repository * @link http://developer.github.com/v3/repos/ * - * @param string $username the user who owns the repo - * @param string $repo the name of the repo - * @return array list of the languages + * @param string $username the user who owns the repository + * @param string $repository the name of the repository + * + * @return array list of the languages */ - public function getRepoLanguages($username, $repo) + public function languages($username, $repository) { - return $this->get('repos/'.urlencode($username).'/'.urlencode($repo).'/languages'); + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/languages'); } /** * Get the contributors of a repository * @link http://developer.github.com/v3/repos/ * - * @param string $username the user who owns the repo - * @param string $repo the name of the repo - * @param boolean $includingAnonymous by default, the list only shows GitHub users. You can include non-users too by setting this to true - * @return array list of the repo contributors + * @param string $username the user who owns the repository + * @param string $repository the name of the repository + * @param boolean $includingAnonymous by default, the list only shows GitHub users. + * You can include non-users too by setting this to true + * @return array list of the repo contributors */ - public function getRepoContributors($username, $repo, $includingAnonymous = false) + public function contributors($username, $repository, $includingAnonymous = false) { - $url = 'repos/'.urlencode($username).'/'.urlencode($repo).'/contributors'; - if ($includingAnonymous) { - $url .= '?anon=1'; - } - - return $this->get($url); + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/contributors', array( + 'anon' => $includingAnonymous ?: null + )); } /** @@ -364,56 +247,41 @@ public function getRepoContributors($username, $repo, $includingAnonymous = fals * @link http://developer.github.com/v3/repos/ * * @param string $username the user who owns the repo - * @param string $repo the name of the repo + * @param string $repository the name of the repo * * @return array list of the languages */ - public function getRepoTeams($username, $repo) + public function teams($username, $repository) { - return $this->get('repos/'.urlencode($username).'/'.urlencode($repo).'/teams'); + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/teams'); } /** * Get contents of any file or directory in a repository * @link http://developer.github.com/v3/repos/contents/ * - * @param string $username the user who owns the repo - * @param string $repo the name of the repo + * @param string $username the user who owns the repository + * @param string $repository the name of the repository * @param string $path path to file or directory * * @return array information for file | information for each item in directory */ - public function getRepoContents($username, $repo, $path) + public function contents($username, $repository, $path) { - return $this->get('repos/'.urlencode($username).'/'.urlencode($repo).'/contents/'.$path); + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/contents/'.$path); } /** * Get the downloads for selected repository * @link http://developer.github.com/v3/repos/downloads/#list-downloads-for-a-repository * - * @param string $username the user who owns the repo - * @param string $repo the name of the repo - * - * @return array - */ - public function getRepoDownloads($username, $repo) - { - return $this->get('repos/'.urlencode($username).'/'.urlencode($repo).'/downloads'); - } - - /** - * Delete a download in selected repository - * @link http://developer.github.com/v3/repos/downloads/#delete-a-download - * - * @param string $username the user who owns the repo - * @param string $repo the name of the repo - * @param integer $id the id of the download file + * @param string $username the user who owns the repository + * @param string $repository the name of the repository * * @return array */ - public function deleteRepoDownload($username, $repo, $id) + public function downloads($username, $repository) { - return $this->delete('repos/'.urlencode($username).'/'.urlencode($repo).'/downloads/'.urlencode($id)); + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/downloads'); } } diff --git a/lib/Github/Api/Repository/Collaborators.php b/lib/Github/Api/Repository/Collaborators.php new file mode 100644 index 00000000000..987a148d167 --- /dev/null +++ b/lib/Github/Api/Repository/Collaborators.php @@ -0,0 +1,32 @@ + + */ +class Collaborators extends AbstractApi +{ + public function all($username, $repository) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/collaborators'); + } + + public function check($username, $repository, $collaborator) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/collaborators/'.urlencode($collaborator)); + } + + public function add($username, $repository, $collaborator) + { + return $this->put('repos/'.urlencode($username).'/'.urlencode($repository).'/collaborators/'.urlencode($collaborator)); + } + + public function remove($username, $repository, $collaborator) + { + return $this->delete('repos/'.urlencode($username).'/'.urlencode($repository).'/collaborators/'.urlencode($collaborator)); + } +} diff --git a/lib/Github/Api/Repository/Comments.php b/lib/Github/Api/Repository/Comments.php new file mode 100644 index 00000000000..55642a427f8 --- /dev/null +++ b/lib/Github/Api/Repository/Comments.php @@ -0,0 +1,50 @@ + + */ +class Comments extends AbstractApi +{ + public function all($username, $repository, $sha = null) + { + if (null === $sha) { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/comments'); + } + + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/commits/'.urlencode($sha).'/comments'); + } + + public function show($username, $repository, $comment) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/comments/'.urlencode($comment)); + } + + public function create($username, $repository, $sha, array $params) + { + if (!isset($params['body'], $params['commit_id'], $params['line'], $params['path'], $params['position'])) { + throw new MissingArgumentException(array('body', 'commit_id', 'line', 'path', 'position')); + } + + return $this->post('repos/'.urlencode($username).'/'.urlencode($repository).'/commits/'.urlencode($sha).'/comments', $params); + } + + public function update($username, $repository, $comment, array $params) + { + if (!isset($params['body'])) { + throw new MissingArgumentException('body'); + } + + return $this->patch('repos/'.urlencode($username).'/'.urlencode($repository).'/comments/'.urlencode($comment), $params); + } + + public function remove($username, $repository, $comment) + { + return $this->delete('repos/'.urlencode($username).'/'.urlencode($repository).'/comments/'.urlencode($comment)); + } +} diff --git a/lib/Github/Api/Repository/Commits.php b/lib/Github/Api/Repository/Commits.php new file mode 100644 index 00000000000..030231e0b1c --- /dev/null +++ b/lib/Github/Api/Repository/Commits.php @@ -0,0 +1,27 @@ + + */ +class Commits extends AbstractApi +{ + public function all($username, $repository, array $params) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/commits', $params); + } + + public function compare($username, $repository, $base, $head) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/compare/'.urlencode($base).'...'.urlencode($head)); + } + + public function show($username, $repository, $sha) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/commits/'.urlencode($sha)); + } +} diff --git a/lib/Github/Api/Repository/Contents.php b/lib/Github/Api/Repository/Contents.php new file mode 100644 index 00000000000..21fdf413171 --- /dev/null +++ b/lib/Github/Api/Repository/Contents.php @@ -0,0 +1,38 @@ + + */ +class Contents extends AbstractApi +{ + public function readme($username, $repository, $reference = null) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/contents/readme', array( + 'ref' => $reference + )); + } + + public function show($username, $repository, $path, $reference = null) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/contents/'.urlencode($path), array( + 'ref' => $reference + )); + } + + public function archive($username, $repository, $format, $reference = null) + { + if (!in_array($format, array('tarball', 'zipball'))) { + $format = 'tarball'; + } + + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/'.urlencode($format), array( + 'ref' => $reference + )); + } +} diff --git a/lib/Github/Api/Repository/DeployKeys.php b/lib/Github/Api/Repository/DeployKeys.php new file mode 100644 index 00000000000..b25b1d04780 --- /dev/null +++ b/lib/Github/Api/Repository/DeployKeys.php @@ -0,0 +1,46 @@ + + */ +class DeployKeys extends AbstractApi +{ + public function all($username, $repository) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/keys'); + } + + public function show($username, $repository, $id) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/keys/'.urlencode($id)); + } + + public function create($username, $repository, array $params) + { + if (!isset($params['title'], $params['key'])) { + throw new MissingArgumentException(array('title', 'key')); + } + + return $this->post('repos/'.urlencode($username).'/'.urlencode($repository).'/keys', $params); + } + + public function update($username, $repository, $id, array $params) + { + if (!isset($params['title'], $params['key'])) { + throw new MissingArgumentException(array('title', 'key')); + } + + return $this->patch('repos/'.urlencode($username).'/'.urlencode($repository).'/keys/'.urlencode($id), $params); + } + + public function remove($username, $repository, $id) + { + return $this->delete('repos/'.urlencode($username).'/'.urlencode($repository).'/keys/'.urlencode($id)); + } +} diff --git a/lib/Github/Api/Repository/Downloads.php b/lib/Github/Api/Repository/Downloads.php new file mode 100644 index 00000000000..43112197517 --- /dev/null +++ b/lib/Github/Api/Repository/Downloads.php @@ -0,0 +1,56 @@ + + */ +class Downloads extends AbstractApi +{ + /** + * List downloads in selected repository + * @link http://developer.github.com/v3/repos/downloads/ + * + * @param string $username the user who owns the repo + * @param string $repository the name of the repo + * + * @return array + */ + public function all($username, $repository) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/downloads'); + } + + /** + * Get a download in selected repository + * @link http://developer.github.com/v3/repos/downloads/ + * + * @param string $username the user who owns the repo + * @param string $repository the name of the repo + * @param integer $id the id of the download file + * + * @return array + */ + public function show($username, $repository, $id) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/downloads/'.urlencode($id)); + } + + /** + * Delete a download in selected repository + * @link http://developer.github.com/v3/repos/downloads/#delete-a-download + * + * @param string $username the user who owns the repo + * @param string $repository the name of the repo + * @param integer $id the id of the download file + * + * @return array + */ + public function remove($username, $repository, $id) + { + return $this->delete('repos/'.urlencode($username).'/'.urlencode($repository).'/downloads/'.urlencode($id)); + } +} diff --git a/lib/Github/Api/Repository/Forks.php b/lib/Github/Api/Repository/Forks.php new file mode 100644 index 00000000000..2a88c330f12 --- /dev/null +++ b/lib/Github/Api/Repository/Forks.php @@ -0,0 +1,26 @@ + + */ +class Forks extends AbstractApi +{ + public function all($username, $repository, array $params = array()) + { + if (isset($params['sort']) && !in_array($params['sort'], array('newest', 'oldest', 'watchers'))) { + $params['sort'] = 'newest'; + } + + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/forks', array_merge(array('page' => 1), $params)); + } + + public function create($username, $repository, array $params = array()) + { + return $this->post('repos/'.urlencode($username).'/'.urlencode($repository).'/forks', $params); + } +} diff --git a/lib/Github/Api/Repository/Hooks.php b/lib/Github/Api/Repository/Hooks.php new file mode 100644 index 00000000000..7c8b696b79d --- /dev/null +++ b/lib/Github/Api/Repository/Hooks.php @@ -0,0 +1,51 @@ + + */ +class Hooks extends AbstractApi +{ + public function all($username, $repository) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/hooks'); + } + + public function show($username, $repository, $id) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/hooks/'.urlencode($id)); + } + + public function create($username, $repository, array $params) + { + if (!isset($params['name'], $params['config'])) { + throw new MissingArgumentException(array('name', 'config')); + } + + return $this->post('repos/'.urlencode($username).'/'.urlencode($repository).'/hooks', $params); + } + + public function update($username, $repository, $id, array $params) + { + if (!isset($params['name'], $params['config'])) { + throw new MissingArgumentException(array('name', 'config')); + } + + return $this->patch('repos/'.urlencode($username).'/'.urlencode($repository).'/hooks/'.urlencode($id), $params); + } + + public function test($username, $repository, $id) + { + return $this->post('repos/'.urlencode($username).'/'.urlencode($repository).'/hooks/'.urlencode($id).'/test'); + } + + public function remove($username, $repository, $id) + { + return $this->delete('repos/'.urlencode($username).'/'.urlencode($repository).'/hooks/'.urlencode($id)); + } +} diff --git a/lib/Github/Api/Repository/Labels.php b/lib/Github/Api/Repository/Labels.php new file mode 100644 index 00000000000..5ff2305ae7e --- /dev/null +++ b/lib/Github/Api/Repository/Labels.php @@ -0,0 +1,46 @@ + + */ +class Labels extends AbstractApi +{ + public function all($username, $repository) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/labels'); + } + + public function show($username, $repository, $label) + { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/labels/'.urlencode($label)); + } + + public function create($username, $repository, array $params) + { + if (!isset($params['name'], $params['color'])) { + throw new MissingArgumentException(array('name', 'color')); + } + + return $this->post('repos/'.urlencode($username).'/'.urlencode($repository).'/labels', $params); + } + + public function update($username, $repository, $label, array $params) + { + if (!isset($params['name'], $params['color'])) { + throw new MissingArgumentException(array('name', 'color')); + } + + return $this->patch('repos/'.urlencode($username).'/'.urlencode($repository).'/labels/'.urlencode($label), $params); + } + + public function remove($username, $repository, $label) + { + return $this->delete('repos/'.urlencode($username).'/'.urlencode($repository).'/labels/'.urlencode($label)); + } +} diff --git a/lib/Github/Api/User.php b/lib/Github/Api/User.php index eee23790e09..9ec9ef962ae 100644 --- a/lib/Github/Api/User.php +++ b/lib/Github/Api/User.php @@ -4,13 +4,12 @@ /** * Searching users, getting user information - * and managing authenticated user account information. * - * @link http://develop.github.com/p/users.html - * @author Thibault Duplessis - * @license MIT License + * @link http://developer.github.com/v3/users/ + * @author Joseph Bielawski + * @author Thibault Duplessis */ -class User extends Api +class User extends AbstractApi { /** * Search users by username: @@ -20,153 +19,80 @@ class User extends Api * * @return array list of users found */ - public function search($keyword) + public function find($keyword) { return $this->get('legacy/user/search/'.urlencode($keyword)); } /** * Get extended information about a user by its username - * http://develop.github.com/p/users.html#getting_user_information + * @link http://developer.github.com/v3/users/ * - * @param string $username the username to show - * @return array informations about the user + * @param string $username the username to show + * @return array informations about the user */ public function show($username) { return $this->get('users/'.urlencode($username)); } - /** - * Update user informations. Requires authentication. - * http://developer.github.com/v3/users/ - * - * @param array $data key=>value user attributes to update. - * key can be name, email, blog, company or location - * @return array informations about the user - */ - public function update(array $data) - { - return $this->patch('user', $data); - } - /** * Request the users that a specific user is following - * http://developer.github.com/v3/users/followers/ + * @link http://developer.github.com/v3/users/followers/ * - * @param string $username the username - * @return array list of followed users + * @param string $username the username + * @return array list of followed users */ - public function getFollowing($username) + public function following($username) { return $this->get('users/'.urlencode($username).'/following'); } /** * Request the users following a specific user - * http://developer.github.com/v3/users/followers/ + * @link http://developer.github.com/v3/users/followers/ * - * @param string $username the username - * @return array list of following users + * @param string $username the username + * @return array list of following users */ - public function getFollowers($username) + public function followers($username) { return $this->get('users/'.urlencode($username).'/followers'); } /** - * Make the authenticated user follow the specified user. Requires authentication. - * http://developer.github.com/v3/users/followers/ + * Request the repository that a specific user is watching + * @link http://developer.github.com/v3/repos/watching/ * - * @param string $username the username to follow - * @return array list of followed users + * @param string $username the username + * @return array list of watched repositories */ - public function follow($username) - { - return $this->put('user/following/'.urlencode($username)); - } - - /** - * Make the authenticated user unfollow the specified user. Requires authentication. - * http://developer.github.com/v3/users/followers/ - * - * @param string $username the username to unfollow - * @return array list of followed users - */ - public function unFollow($username) - { - return $this->delete('user/following/'.urlencode($username)); - } - - /** - * Request the repos that a specific user is watching - * http://develop.github.com/p/users.html#watched_repos - * - * @param string $username the username - * @return array list of watched repos - */ - public function getWatchedRepos($username) + public function watched($username) { return $this->get('users/'.urlencode($username).'/watched'); } /** - * Get the authenticated user public keys. Requires authentication - * - * @return array list of public keys of the user - */ - public function getKeys() - { - return $this->get('user/keys'); - } - - /** - * Add a public key to the authenticated user. Requires authentication. - * - * @return array list of public keys of the user - */ - public function addKey($title, $key) - { - return $this->post('user/keys', array('title' => $title, 'key' => $key)); - } - - /** - * Remove a public key from the authenticated user. Requires authentication. - * - * @return array list of public keys of the user - */ - public function removeKey($id) - { - return $this->delete('user/keys/'.urlencode($id)); - } - - /** - * Get the authenticated user emails. Requires authentication. - * - * @return array list of authenticated user emails - */ - public function getEmails() - { - return $this->get('user/emails'); - } - - /** - * Add an email to the authenticated user. Requires authentication. + * Get the repositories of a user + * @link http://developer.github.com/v3/repos/ * - * @return array list of authenticated user emails + * @param string $username the username + * @return array list of the user repositories */ - public function addEmail($email) + public function repositories($username) { - return $this->post('user/emails', array($email)); + return $this->get('users/'.urlencode($username).'/repos'); } /** - * Remove an email from the authenticated user. Requires authentication. + * Get the public gists for a user + * @link http://developer.github.com/v3/gists/ * - * @return array list of authenticated user emails + * @param string $username the username + * @return array list of the user gists */ - public function removeEmail($email) + public function gists($username) { - return $this->delete('user/emails', array($email)); + return $this->get('users/'.urlencode($username).'/gists'); } } diff --git a/lib/Github/Client.php b/lib/Github/Client.php index da418ee0075..84a6876abbb 100644 --- a/lib/Github/Client.php +++ b/lib/Github/Client.php @@ -39,14 +39,14 @@ class Client * * @var HttpClientInterface */ - protected $httpClient = null; + private $httpClient = null; /** * The list of loaded API instances * * @var array */ - protected $apis = array(); + private $apis = array(); /** * HTTP Headers @@ -72,9 +72,9 @@ public function __construct(HttpClientInterface $httpClient = null) * @param string $secret GitHub private token or Github password if $method == AUTH_HTTP_PASSWORD * @param null|string $method One of the AUTH_* class constants */ - public function authenticate($login, $secret, $method = null) + public function authenticate($login, $secret = null, $method = null) { - $this->getHttpClient()->setOption('auth_method', $method ?: self::AUTH_URL_TOKEN); + $this->getHttpClient()->setOption('auth_method', $method); if ($method === self::AUTH_HTTP_PASSWORD) { $this @@ -85,14 +85,8 @@ public function authenticate($login, $secret, $method = null) } else { $this->getHttpClient()->setOption('token', $secret); } - } - /** - * De-authenticate a user for all next requests - */ - public function deAuthenticate() - { - $this->authenticate(null, null, null); + $this->getHttpClient()->authenticate(); } /** @@ -184,115 +178,60 @@ public function setHttpClient(HttpClientInterface $httpClient) } /** - * Get the user API + * @param string $name * - * @return Api\User the user API - */ - public function getUserApi() - { - if (!isset($this->apis['user'])) { - $this->apis['user'] = new Api\User($this); - } - - return $this->apis['user']; - } - - /** - * Get the issue API + * @return ApiInterface * - * @return Api\Issue the issue API + * @throws \InvalidArgumentException */ - public function getIssueApi() + public function api($name) { - if (!isset($this->apis['issue'])) { - $this->apis['issue'] = new Api\Issue($this); - } + if (!isset($this->apis[$name])) { + switch ($name) { + case 'current_user': + $api = new Api\CurrentUser($this); + break; - return $this->apis['issue']; - } + case 'git_data': + $api = new Api\GitData($this); + break; - /** - * Get the commit API - * - * @return Api\Commit the commit API - */ - public function getCommitApi() - { - if (!isset($this->apis['commit'])) { - $this->apis['commit'] = new Api\Commit($this); - } + case 'gists': + $api = new Api\Gists($this); + break; - return $this->apis['commit']; - } + case 'issue': + $api = new Api\Issue($this); + break; - /** - * Get the repo API - * - * @return Api\Repo the repo API - */ - public function getRepoApi() - { - if (!isset($this->apis['repo'])) { - $this->apis['repo'] = new Api\Repo($this); - } + case 'markdown': + $api = new Api\Markdown($this); + break; - return $this->apis['repo']; - } + case 'organization': + $api = new Api\Organization($this); + break; - /** - * Get the organization API - * - * @return Api\Organization the object API - */ - public function getOrganizationApi() - { - if (!isset($this->apis['organization'])) { - $this->apis['organization'] = new Api\Organization($this); - } + case 'pull_request': + $api = new Api\PullRequest($this); + break; - return $this->apis['organization']; - } + case 'repo': + $api = new Api\Repo($this); + break; - /** - * Get the object API - * - * @return Api\Object the object API - */ - public function getObjectApi() - { - if (!isset($this->apis['object'])) { - $this->apis['object'] = new Api\Object($this); - } - - return $this->apis['object']; - } - - /** - * Get the pull request API - * - * @return Api\PullRequest the pull request API - */ - public function getPullRequestApi() - { - if (!isset($this->apis['pullrequest'])) { - $this->apis['pullrequest'] = new Api\PullRequest($this); - } + case 'user': + $api = new Api\User($this); + break; - return $this->apis['pullrequest']; - } + default: + throw new \InvalidArgumentException(); + } - /** - * Get the markdown API - * - * @return Api\Markdown the markdown API - */ - public function getMarkdownApi() - { - if (!isset($this->apis['markdown'])) { - $this->apis['markdown'] = new Api\Markdown($this); + $this->apis[$name] = $api; } - return $this->apis['markdown']; + return $this->apis[$name]; } /** @@ -303,32 +242,6 @@ public function getRateLimit() return $this->get('rate_limit'); } - /** - * Inject an API instance - * - * @param string $name the API name - * @param ApiInterface $api the API instance - * - * @return Client - */ - public function setApi($name, ApiInterface $instance) - { - $this->apis[$name] = $instance; - - return $this; - } - - /** - * Get any API - * - * @param string $name the API name - * @return ApiInterface the API instance - */ - public function getApi($name) - { - return $this->apis[$name]; - } - /** * Clears used headers */ diff --git a/lib/Github/Exception/MissingArgumentException.php b/lib/Github/Exception/MissingArgumentException.php new file mode 100644 index 00000000000..24f172b1c5d --- /dev/null +++ b/lib/Github/Exception/MissingArgumentException.php @@ -0,0 +1,23 @@ + + */ +class MissingArgumentException extends \ErrorException +{ + /** + * @param string|array $required + */ + public function __construct($required) + { + if (is_string($required)) { + $required = array($required); + } + + parent::__construct(sprintf('One or more of required ("%s") parameters is missing!', implode('", "', $required))); + } +} diff --git a/lib/Github/HttpClient/HttpClient.php b/lib/Github/HttpClient/HttpClient.php index 02e6d6021bb..c29e165c80d 100644 --- a/lib/Github/HttpClient/HttpClient.php +++ b/lib/Github/HttpClient/HttpClient.php @@ -75,17 +75,19 @@ public function __construct(array $options = array(), Browser $browser = null) $this->browser->getClient()->setTimeout($this->options['timeout']); $this->browser->getClient()->setVerifyPeer(false); - if (null !== $this->options['login'] || null !== $this->options['token']) { - if (null !== $this->options['token']) { - $options = array($this->options['token']); - } else { - $options = array($this->options['login'], $this->options['password']); - } + } - $this->browser->addListener( - new AuthListener($this->options['auth_method'], $options) - ); - } + /** + * {@inheritDoc} + */ + public function authenticate() + { + $this->browser->addListener( + new AuthListener( + $this->options['auth_method'], + array($this->options['login'], $this->options['password'], $this->options['token']) + ) + ); } /** @@ -123,7 +125,9 @@ public function getPagination() */ public function get($path, array $parameters = array(), array $options = array()) { - return $this->request($path, $parameters, 'GET', $options); + $path .= (false === strpos($path, '?') ? '?' : '&').http_build_query($parameters, '', '&'); + + return $this->request($path, array(), 'GET', $options); } /** diff --git a/lib/Github/HttpClient/HttpClientInterface.php b/lib/Github/HttpClient/HttpClientInterface.php index adee517d262..436ecfe3da4 100644 --- a/lib/Github/HttpClient/HttpClientInterface.php +++ b/lib/Github/HttpClient/HttpClientInterface.php @@ -19,7 +19,7 @@ interface HttpClientInterface * * @return array Data */ - function get($path, array $parameters = array(), array $options = array()); + public function get($path, array $parameters = array(), array $options = array()); /** * Send a POST request @@ -30,7 +30,7 @@ function get($path, array $parameters = array(), array $options = array()); * * @return array Data */ - function post($path, array $parameters = array(), array $options = array()); + public function post($path, array $parameters = array(), array $options = array()); /** * Send a PATCH request @@ -41,7 +41,7 @@ function post($path, array $parameters = array(), array $options = array()); * * @return array Data */ - function patch($path, array $parameters = array(), array $options = array()); + public function patch($path, array $parameters = array(), array $options = array()); /** * Send a PUT request @@ -51,7 +51,7 @@ function patch($path, array $parameters = array(), array $options = array()); * * @return array Data */ - function put($path, array $options = array()); + public function put($path, array $options = array()); /** * Send a DELETE request @@ -62,7 +62,12 @@ function put($path, array $options = array()); * * @return array Data */ - function delete($path, array $parameters = array(), array $options = array()); + public function delete($path, array $parameters = array(), array $options = array()); + + /** + * Authenticate a user for all next requests + */ + public function authenticate(); /** * Change an option value. @@ -72,12 +77,12 @@ function delete($path, array $parameters = array(), array $options = array()); * * @return HttpClientInterface The current object instance */ - function setOption($name, $value); + public function setOption($name, $value); /** * Set HTTP headers * * @param array $headers */ - function setHeaders(array $headers); + public function setHeaders(array $headers); } diff --git a/lib/Github/HttpClient/Listener/Auth.php b/lib/Github/HttpClient/Listener/AuthListener.php similarity index 72% rename from lib/Github/HttpClient/Listener/Auth.php rename to lib/Github/HttpClient/Listener/AuthListener.php index 364c932b498..98415735c1c 100644 --- a/lib/Github/HttpClient/Listener/Auth.php +++ b/lib/Github/HttpClient/Listener/AuthListener.php @@ -10,6 +10,9 @@ use Buzz\Message\RequestInterface; use Buzz\Util\Url; +/** + * @author Joseph Bielawski + */ class AuthListener implements ListenerInterface { /** @@ -24,33 +27,44 @@ class AuthListener implements ListenerInterface /** * @param string $method * @param array $options - * - * @throws InvalidArgumentException */ public function __construct($method, array $options) { - if (!isset($options['token']) || (!isset($options['login'], $options['password']))) { - throw new InvalidArgumentException('You need to set OAuth token, or username with password!'); - } - $this->method = $method; $this->options = $options; } /** * {@inheritDoc} + * + * @throws InvalidArgumentException */ public function preSend(RequestInterface $request) { + // Skip by default + if (null === $this->method) { + return; + } + switch ($this->method) { case Client::AUTH_HTTP_PASSWORD: + if (!isset($this->options['login'], $this->options['password'])) { + throw new InvalidArgumentException('You need to set username with password!'); + } $request->addHeader('Authorization: Basic '. base64_encode($this->options['login'] .':'. $this->options['password'])); break; + case Client::AUTH_HTTP_TOKEN: + if (!isset($this->options['token'])) { + throw new InvalidArgumentException('You need to set OAuth token!'); + } $request->addHeader('Authorization: token '. $this->options['token']); break; + case Client::AUTH_URL_TOKEN: - default: + if (!isset($this->options['token'])) { + throw new InvalidArgumentException('You need to set OAuth token!'); + } $url = $request->getUrl(); if ('GET' === $request->getMethod()) { diff --git a/test/Github/Tests/Api/CommitTest.php b/test/Github/Tests/Api/CommitTest.php index 8caac04e831..e8f05f86f12 100644 --- a/test/Github/Tests/Api/CommitTest.php +++ b/test/Github/Tests/Api/CommitTest.php @@ -12,20 +12,18 @@ public function testGetInvalidBranchCommits() $expectedValue = array('commit' => array(), 'comitter'); - $api->expects($this->at(0)) - ->method('get') - ->will($this->returnValue(array('sha' => '123'))); + $data = array('sha' => 'v3'); - $api->expects($this->at(1)) + $api->expects($this->at(0)) ->method('get') - ->with($this->equalTo('repos/ornicar/php-github-api/commits')) + ->with('repos/ornicar/php-github-api/commits', $data) ->will($this->returnValue($expectedValue)); - $this->assertEquals($expectedValue, $api->getBranchCommits('ornicar', 'php-github-api', 'v3')); + $this->assertEquals($expectedValue, $api->all('ornicar', 'php-github-api', $data)); } protected function getApiClass() { - return 'Github\Api\Commit'; + return 'Github\Api\Repository\Commits'; } } diff --git a/test/Github/Tests/Api/GistTest.php b/test/Github/Tests/Api/GistTest.php new file mode 100644 index 00000000000..7f4f26ae5c6 --- /dev/null +++ b/test/Github/Tests/Api/GistTest.php @@ -0,0 +1,92 @@ +getApiMock(); + + $api->expects($this->once()) + ->method('get') + ->with('gists/starred'); + + $api->all('starred'); + } + + /** + * @test + */ + public function shouldCreateAnonymousGist() + { + $api = $this->getApiMock(); + + $input = array( + 'description' => '', + 'public' => false, + 'files' => array( + 'filename.txt' => array( + 'content' => 'content' + ) + ) + ); + + $api->expects($this->once()) + ->method('post') + ->with('gists', $input); + + $api->create($input); + } + + /** + * @test + */ + public function shouldUpdateGist() + { + $api = $this->getApiMock(); + + $input = array( + 'description' => 'jimbo', + 'files' => array( + 'filename.txt' => array( + 'filename' => 'new_name.txt', + 'content' => 'content' + ), + 'filename_new.txt' => array( + 'content' => 'content new' + ) + ) + ); + + $api->expects($this->once()) + ->method('patch') + ->with('gists/5', $input); + + $api->update(5, $input); + } + + /** + * @test + */ + public function shouldDeleteGist() + { + $api = $this->getApiMock(); + + $api->expects($this->once()) + ->method('delete') + ->with('gists/5'); + + $api->remove(5); + } + + protected function getApiClass() + { + return 'Github\Api\Gists'; + } +} diff --git a/test/Github/Tests/Api/IssueTest.php b/test/Github/Tests/Api/IssueTest.php index 66630a4ecfb..3443e04bc6f 100644 --- a/test/Github/Tests/Api/IssueTest.php +++ b/test/Github/Tests/Api/IssueTest.php @@ -13,11 +13,18 @@ public function shouldBuildValidQueryForGetList() { $api = $this->getApiMock(); + $data = array( + 'state' => 'open' + ); + $sentData = $data + array( + 'page' => 1 + ); + $api->expects($this->once()) ->method('get') - ->with('repos/ornicar/php-github-api/issues?state=open'); + ->with('repos/ornicar/php-github-api/issues', $sentData); - $api->getList('ornicar', 'php-github-api', 'open'); + $api->all('ornicar', 'php-github-api', $data); } /** @@ -27,184 +34,94 @@ public function shouldBuildValidQueryForGetListWithAdditionalParameters() { $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('repos/ornicar/php-github-api/issues?milestone=%2A&assignee=l3l0&mentioned=l3l0&labels=bug%2C%40high&sort=created&direction=asc&state=open'); - - $api->getList('ornicar', 'php-github-api', 'open', array( + $data = array( + 'state' => 'open', 'milestone' => '*', 'assignee' => 'l3l0', 'mentioned' => 'l3l0', 'labels' => 'bug,@high', 'sort' => 'created', 'direction' => 'asc' - )); - } + ); - /** - * @test - */ - public function shouldBuildValidQueryForShow() - { - $api = $this->getApiMock(); + $sentData = $data + array( + 'page' => 1 + ); $api->expects($this->once()) ->method('get') - ->with('repos/ornicar/php-github-api/issues/14'); - - $api->show('ornicar', 'php-github-api', 14); - } - - /** - * @test - */ - public function shouldBuildValidQueryForOpen() - { - $api = $this->getApiMock(); - - $api->expects($this->once()) - ->method('post') - ->with('repos/ornicar/php-github-api/issues', array( - 'title' => 'some title', - 'body' => 'some body' - )); - - $api->open('ornicar', 'php-github-api', 'some title', 'some body'); - } - - /** - * @test - */ - public function shouldBuildValidQueryForClose() - { - $api = $this->getApiMock(); - - $api->expects($this->once()) - ->method('patch') - ->with('repos/ornicar/php-github-api/issues/14', array( - 'state' => 'closed', - )); - - $api->close('ornicar', 'php-github-api', 14); - } - - /** - * @test - */ - public function shouldBuildValidQueryForReOpen() - { - $api = $this->getApiMock(); - - $api->expects($this->once()) - ->method('patch') - ->with('repos/ornicar/php-github-api/issues/14', array( - 'state' => 'open', - )); + ->with('repos/ornicar/php-github-api/issues', $sentData); - $api->reOpen('ornicar', 'php-github-api', 14); + $api->all('ornicar', 'php-github-api', $data); } /** * @test */ - public function shouldBuildValidQueryForGetComments() + public function shouldBuildValidQueryForShow() { $api = $this->getApiMock(); $api->expects($this->once()) ->method('get') - ->with('repos/ornicar/php-github-api/issues/14/comments'); + ->with('repos/ornicar/php-github-api/issues/14'); - $api->getComments('ornicar', 'php-github-api', 14); + $api->show('ornicar', 'php-github-api', 14); } /** * @test */ - public function shouldBuildValidQueryForGetComment() + public function shouldBuildValidQueryForOpen() { $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('repos/ornicar/php-github-api/issues/comments/666'); - - $api->getComment('ornicar', 'php-github-api', 666); - } - - /** - * @test - */ - public function shouldBuildValidQueryForAddComment() - { - $api = $this->getApiMock(); + $data = array( + 'title' => 'some title', + 'body' => 'some body' + ); $api->expects($this->once()) ->method('post') - ->with('repos/ornicar/php-github-api/issues/14/comments', array( - 'body' => 'some body' - )); + ->with('repos/ornicar/php-github-api/issues', $data); - $api->addComment('ornicar', 'php-github-api', 14, 'some body'); + $api->create('ornicar', 'php-github-api', $data); } /** * @test */ - public function shouldBuildValidQueryForGetLabels() + public function shouldBuildValidQueryForClose() { $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('repos/ornicar/php-github-api/labels'); - - $api->getLabels('ornicar', 'php-github-api'); - } - - /** - * @test - */ - public function shouldBuildValidQueryForGetLabel() - { - $api = $this->getApiMock(); + $data = array( + 'state' => 'closed', + ); $api->expects($this->once()) - ->method('get') - ->with('repos/ornicar/php-github-api/labels/my-label'); + ->method('patch') + ->with('repos/ornicar/php-github-api/issues/14', $data); - $api->getLabel('ornicar', 'php-github-api', 'my-label'); + $api->update('ornicar', 'php-github-api', 14, $data); } /** * @test */ - public function shouldBuildValidQueryForAddLabel() + public function shouldBuildValidQueryForReOpen() { $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('repos/ornicar/php-github-api/labels', array( - 'name' => 'my-label', - 'color' => 'FFFFFF' - )); - - $api->addLabel('ornicar', 'php-github-api', 'my-label', 'FFFFFF'); - } - - /** - * @test - */ - public function shouldBuildValidQueryForRemoveLabel() - { - $api = $this->getApiMock(); + $data = array( + 'state' => 'open', + ); $api->expects($this->once()) - ->method('delete') - ->with('repos/ornicar/php-github-api/labels/my-label'); + ->method('patch') + ->with('repos/ornicar/php-github-api/issues/14', $data); - $api->removeLabel('ornicar', 'php-github-api', 'my-label'); + $api->update('ornicar', 'php-github-api', 14, $data); } protected function getApiClass() diff --git a/test/Github/Tests/Api/MarkdownTest.php b/test/Github/Tests/Api/MarkdownTest.php new file mode 100644 index 00000000000..a74797922cf --- /dev/null +++ b/test/Github/Tests/Api/MarkdownTest.php @@ -0,0 +1,26 @@ +getApiMock(); + + $api->expects($this->once()) + ->method('post') + ->with('markdown', array('text' => $input, 'mode' => 'markdown')); + + $api->render($input); + } + + protected function getApiClass() + { + return 'Github\Api\Markdown'; + } +} diff --git a/test/Github/Tests/Api/PullRequestTest.php b/test/Github/Tests/Api/PullRequestTest.php index e610b3f5a05..bd84a96cb4b 100644 --- a/test/Github/Tests/Api/PullRequestTest.php +++ b/test/Github/Tests/Api/PullRequestTest.php @@ -18,24 +18,24 @@ public function shouldCreateValidQueryForListPullRequests() ->method('get') ->with('repos/ezsystems/ezpublish/pulls'); - $api->listPullRequests('ezsystems', 'ezpublish' ); + $api->all('ezsystems', 'ezpublish'); - $api = $this->getApiMock(); // 2. Test with last parameter set to 'open' + $api = $this->getApiMock(); $api->expects($this->once()) ->method('get') - ->with('repos/ezsystems/ezpublish/pulls?state=open'); + ->with('repos/ezsystems/ezpublish/pulls', array('state' => 'open')); - $api->listPullRequests('ezsystems', 'ezpublish', 'open' ); + $api->all('ezsystems', 'ezpublish', 'open'); - $api = $this->getApiMock(); // 2. Test with last parameter set to 'closed' + $api = $this->getApiMock(); $api->expects($this->once()) ->method('get') - ->with('repos/ezsystems/ezpublish/pulls?state=closed'); + ->with('repos/ezsystems/ezpublish/pulls', array('state' => 'closed')); - $api->listPullRequests('ezsystems', 'ezpublish', 'closed' ); + $api->all('ezsystems', 'ezpublish', 'closed'); } /** @@ -49,7 +49,7 @@ public function shouldCreateValidQueryForShow() ->method('get') ->with('repos/ezsystems/ezpublish/pulls/15'); - $api->show('ezsystems', 'ezpublish', '15' ); + $api->show('ezsystems', 'ezpublish', '15'); } /** @@ -60,49 +60,33 @@ public function shouldCreateValidQueryForCreate() // 1. Testing with body & title $api = $this->getApiMock(); + $data = array( + 'base' => 'master', + 'head' => 'virtualtestbranch', + 'title' => 'TITLE: Testing pull-request creation from PHP Github API', + 'body' => 'BODY: Testing pull-request creation from PHP Github API' + ); + $api->expects($this->once()) ->method('post') - ->with('repos/ezsystems/ezpublish/pulls', - array( 'base' => 'master', - 'head' => 'virtualtestbranch', - 'title' => 'TITLE : Testing pull-request creation from PHP Gituhub API', - 'body' => 'BODY: Testing pull-request creation from PHP Gituhub API' - ) - ); - - $api->create('ezsystems', 'ezpublish', 'master', 'virtualtestbranch', - 'TITLE : Testing pull-request creation from PHP Gituhub API', - 'BODY: Testing pull-request creation from PHP Gituhub API' ); + ->with('repos/ezsystems/ezpublish/pulls', $data); + $api->create('ezsystems', 'ezpublish', $data); // 2. Testing with issue ID $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('post') - ->with('repos/ezsystems/ezpublish/pulls', - array( 'base' => 'master', - 'head' => 'virtualtestbranch', - 'issue' => 25 - ) - ); - - $api->create('ezsystems', 'ezpublish', 'master', 'virtualtestbranch', null, null, 25 ); - - // 3. Testing with title and issue ID - $api = $this->getApiMock(); + $data = array( + 'base' => 'master', + 'head' => 'virtualtestbranch', + 'issue' => 25 + ); $api->expects($this->once()) ->method('post') - ->with('repos/ezsystems/ezpublish/pulls', - array( 'base' => 'master', - 'head' => 'virtualtestbranch', - 'title' => 'some title', - 'body' => null - ) - ); - - $api->create('ezsystems', 'ezpublish', 'master', 'virtualtestbranch', 'some title', null, 25 ); + ->with('repos/ezsystems/ezpublish/pulls', $data); + + $api->create('ezsystems', 'ezpublish', $data); } protected function getApiClass() diff --git a/test/Github/Tests/Api/RepoTest.php b/test/Github/Tests/Api/RepoTest.php index 506ed292bc4..ed9c2c42cea 100644 --- a/test/Github/Tests/Api/RepoTest.php +++ b/test/Github/Tests/Api/RepoTest.php @@ -6,14 +6,15 @@ class RepoTest extends ApiTestCase { - /** - * @expectedException BadMethodCallException - */ - public function testThatPushableReposIsNotSupported() + public function testShow() { $api = $this->getApiMock(); - $api->getPushableRepos(); + $api->expects($this->once()) + ->method('get') + ->with('repos/KnpLabs/php-github-api'); + + $api->show('KnpLabs', 'php-github-api'); } protected function getApiClass() diff --git a/test/Github/Tests/ClientTest.php b/test/Github/Tests/ClientTest.php index 1b79d87adea..83a1ebf4ff1 100644 --- a/test/Github/Tests/ClientTest.php +++ b/test/Github/Tests/ClientTest.php @@ -6,14 +6,14 @@ class ClientTest extends \PHPUnit_Framework_TestCase { - public function testInstanciateWithoutHttpClient() + public function testInstanceWithoutHttpClient() { $client = new Client(); $this->assertInstanceOf('Github\HttpClient\HttpClientInterface', $client->getHttpClient()); } - public function testInstanciateWithHttpClient() + public function testInstanceWithHttpClient() { $httpClient = $this->getHttpClientMock(); $client = new Client($httpClient); @@ -36,18 +36,6 @@ public function testAuthenticate() $client->authenticate($login, $secret, $method); } - public function testDeauthenticate() - { - $client = $this->getClientMockBuilder() - ->setMethods(array('authenticate')) - ->getMock(); - $client->expects($this->once()) - ->method('authenticate') - ->with(null, null, null); - - $client->deAuthenticate(); - } - public function testGet() { $path = '/some/path'; @@ -78,23 +66,26 @@ public function testPost() $client->post($path, $parameters, $options); } - public function testDefaultApi() + public function testRateLimit() { - $client = new Client(); + $limit = 666; + + $httpClient = $this->getHttpClientMock(); + $httpClient->expects($this->once()) + ->method('get') + ->with('rate_limit') + ->will($this->returnValue($limit)); + + $client = new Client($httpClient); - $this->assertInstanceOf('Github\Api\User', $client->getUserApi()); + $this->assertEquals($limit, $client->getRateLimit()); } - public function testInjectApi() + public function testUserApiInstance() { $client = new Client(); - $userApiMock = $this->getMockBuilder('Github\Api\ApiInterface') - ->getMock(); - - $client->setApi('user', $userApiMock); - - $this->assertSame($userApiMock, $client->getUserApi()); + $this->assertInstanceOf('Github\Api\User', $client->api('user')); } protected function getClientMockBuilder() diff --git a/test/Github/Tests/Functional/CommitTest.php b/test/Github/Tests/Functional/CommitTest.php index 3f3809dad96..c41759258d1 100644 --- a/test/Github/Tests/Functional/CommitTest.php +++ b/test/Github/Tests/Functional/CommitTest.php @@ -16,7 +16,7 @@ public function shouldRetrieveCommitsForRepositoryBranch() $branch = 'master'; $github = new Client(); - $commits = $github->getCommitApi()->getBranchCommits($username, $repo, $branch); + $commits = $github->api('repo')->commits()->all($username, $repo, array('sha' => $branch)); $commit = array_pop($commits); $this->assertArrayHasKey('url', $commit); @@ -35,7 +35,7 @@ public function shouldRetrieveCommitBySha() $repo = 'php-github-api'; $github = new Client(); - $commit = $github->getCommitApi()->getCommit($username, $repo, '6df3adf5bd16745299c6429e163265daed430fa1'); + $commit = $github->api('repo')->commits()->show($username, $repo, '6df3adf5bd16745299c6429e163265daed430fa1'); $this->assertArrayHasKey('url', $commit); $this->assertArrayHasKey('committer', $commit); @@ -52,10 +52,10 @@ public function shouldRetrieveCommitsForFile() { $username = 'KnpLabs'; $repo = 'php-github-api'; - $branch = 'api_v3'; + $branch = 'master'; $github = new Client(); - $commits = $github->getCommitApi()->getFileCommits($username, $repo, $branch, 'composer.json'); + $commits = $github->api('repo')->commits()->all($username, $repo, array('sha' => $branch, 'path' => 'composer.json')); $commit = array_pop($commits); $this->assertArrayHasKey('url', $commit); diff --git a/test/Github/Tests/Functional/GistTest.php b/test/Github/Tests/Functional/GistTest.php new file mode 100644 index 00000000000..7c231153d7c --- /dev/null +++ b/test/Github/Tests/Functional/GistTest.php @@ -0,0 +1,73 @@ +api('gists')->all(); + $gist = array_pop($gists); + + $this->assertArrayHasKey('url', $gist); + $this->assertArrayHasKey('files', $gist); + $this->assertArrayHasKey('comments', $gist); + $this->assertArrayHasKey('created_at', $gist); + $this->assertArrayHasKey('updated_at', $gist); + $this->assertArrayHasKey('user', $gist); + } + + /** + * @test + */ + public function shouldNotGetStarredListWithoutAuthorization() + { + $github = new Client(); + $response = $github->api('gists')->all('starred'); + + $this->assertEquals('Requires authentication', $response['message']); + } + + /** + * @test + */ + public function shouldRetrievePublicGistsList() + { + $github = new Client(); + $gists = $github->api('gists')->all('public'); + $gist = array_pop($gists); + + $this->assertArrayHasKey('url', $gist); + $this->assertArrayHasKey('files', $gist); + $this->assertArrayHasKey('comments', $gist); + $this->assertArrayHasKey('created_at', $gist); + $this->assertArrayHasKey('updated_at', $gist); + $this->assertArrayHasKey('user', $gist); + } + + /** + * @test + */ + public function shouldRetrieveGistById() + { + $id = 1; + + $github = new Client(); + $gist = $github->api('gists')->show($id); + + $this->assertArrayHasKey('url', $gist); + $this->assertArrayHasKey('files', $gist); + $this->assertArrayHasKey('comments', $gist); + $this->assertArrayHasKey('created_at', $gist); + $this->assertArrayHasKey('updated_at', $gist); + $this->assertArrayHasKey('user', $gist); + $this->assertArrayHasKey('gistfile1.txt', $gist['files']); + $this->assertEquals('schacon', $gist['user']['login']); + } +} diff --git a/test/Github/Tests/Functional/MarkdownTest.php b/test/Github/Tests/Functional/MarkdownTest.php new file mode 100644 index 00000000000..4c76cd618ab --- /dev/null +++ b/test/Github/Tests/Functional/MarkdownTest.php @@ -0,0 +1,29 @@ +api('markdown'); + + $input = 'Hello world github/linguist#1 **cool**, and #1!'; + $output = '

Hello world github/linguist#1 cool, and #1!

'; + $html = $api->render($input); + + $this->assertEquals($output, $html); + + $input = 'Hello world KnpLabs/KnpBundles#1 **cool**, and #1!'; + $output = '

Hello world KnpLabs/KnpBundles#1 cool, and #1!

'; + $html = $api->render($input, 'gfm' , 'KnpLabs/KnpMenu'); + + $this->assertEquals($output, $html); + } +} diff --git a/test/Github/Tests/Functional/ObjectTest.php b/test/Github/Tests/Functional/ObjectTest.php deleted file mode 100644 index 1073e3a37ea..00000000000 --- a/test/Github/Tests/Functional/ObjectTest.php +++ /dev/null @@ -1,82 +0,0 @@ -getObjectApi()->getRawData($username, $repo, $sha); - - $this->assertRegexp('/Copyright/', $data); - } - - /** - * @test - */ - public function shouldShowHeadTree() - { - $username = 'KnpLabs'; - $repo = 'php-github-api'; - $sha = 'HEAD'; - - $github = new Client(); - $tree = $github->getObjectApi()->showTree($username, $repo, $sha); - - $this->assertArrayHasKey('url', $tree); - $this->assertArrayHasKey('sha', $tree); - $this->assertArrayHasKey('tree', $tree); - } - - /** - * @test - */ - public function shouldListBlobsFromTree() - { - $username = 'KnpLabs'; - $repo = 'php-github-api'; - $sha = 'HEAD'; - - $github = new Client(); - $blobs = $github->getObjectApi()->listBlobs($username, $repo, $sha); - - $blob = reset($blobs); - - $this->assertArrayHasKey('url', $blob); - $this->assertEquals('blob', $blob['type']); - $this->assertArrayHasKey('size', $blob); - $this->assertArrayHasKey('path', $blob); - $this->assertArrayHasKey('sha', $blob); - $this->assertArrayHasKey('mode', $blob); - } - - /** - * @test - */ - public function shouldShowBlob() - { - $username = 'KnpLabs'; - $repo = 'php-github-api'; - $sha = 'a9300ff6c7da0b70913b9a39d59a416b0f6c0c5a'; - - $github = new Client(); - $blob = $github->getObjectApi()->showBlob($username, $repo, $sha, 'CHANGELOG'); - - $this->assertArrayHasKey('url', $blob); - $this->assertEquals('blob', $blob['type']); - $this->assertEquals('CHANGELOG', $blob['path']); - $this->assertArrayHasKey('size', $blob); - $this->assertArrayHasKey('sha', $blob); - $this->assertArrayHasKey('mode', $blob); - } -} diff --git a/test/Github/Tests/Functional/RepoTest.php b/test/Github/Tests/Functional/RepoTest.php index f14ad8cd324..cdd2fc0f212 100644 --- a/test/Github/Tests/Functional/RepoTest.php +++ b/test/Github/Tests/Functional/RepoTest.php @@ -15,7 +15,7 @@ public function shouldRetrieveContributorsList() $repo = 'php-github-api'; $github = new Client(); - $contributors = $github->getRepoApi()->getRepoContributors($username, $repo); + $contributors = $github->api('repo')->contributors($username, $repo); $contributor = array_pop($contributors); $this->assertArrayHasKey('url', $contributor); @@ -26,33 +26,6 @@ public function shouldRetrieveContributorsList() $this->assertArrayHasKey('id', $contributor); } - /** - * @test - */ - public function shouldGetUserRepositories() - { - $username = 'KnpLabs'; - - $github = new Client(); - $repos = $github->getRepoApi()->getUserRepos($username); - $repo = array_pop($repos); - - $this->assertArrayHasKey('id', $repo); - $this->assertArrayHasKey('name', $repo); - $this->assertArrayHasKey('description', $repo); - $this->assertArrayHasKey('url', $repo); - $this->assertArrayHasKey('has_wiki', $repo); - $this->assertArrayHasKey('has_issues', $repo); - $this->assertArrayHasKey('forks', $repo); - $this->assertArrayHasKey('updated_at', $repo); - $this->assertArrayHasKey('created_at', $repo); - $this->assertArrayHasKey('pushed_at', $repo); - $this->assertArrayHasKey('open_issues', $repo); - $this->assertArrayHasKey('ssh_url', $repo); - $this->assertArrayHasKey('git_url', $repo); - $this->assertArrayHasKey('svn_url', $repo); - } - /** * @test */ @@ -62,7 +35,7 @@ public function shouldShowRepo() $repo = 'php-github-api'; $github = new Client(); - $repo = $github->getRepoApi()->show($username, $repo); + $repo = $github->api('repo')->show($username, $repo); $this->assertArrayHasKey('id', $repo); $this->assertArrayHasKey('name', $repo); diff --git a/test/Github/Tests/Functional/UserTest.php b/test/Github/Tests/Functional/UserTest.php index 73ede18a456..35dc42e507f 100644 --- a/test/Github/Tests/Functional/UserTest.php +++ b/test/Github/Tests/Functional/UserTest.php @@ -14,7 +14,7 @@ public function shouldShowUserData() $username = 'KnpLabs'; $github = new Client(); - $user = $github->getUserApi()->show($username); + $user = $github->api('user')->show($username); $this->assertArrayHasKey('id', $user); $this->assertEquals('KnpLabs', $user['login']); @@ -36,7 +36,7 @@ public function shouldShowUserData() public function shouldNotUpdateUserWithoutAuthorization() { $github = new Client(); - $response = $github->getUserApi()->update(array('email' => 'leszek.prabucki@gmail.com')); + $response = $github->api('current_user')->update(array('email' => 'leszek.prabucki@gmail.com')); $this->assertEquals('Requires authentication', $response['message']); } @@ -49,7 +49,7 @@ public function shouldGetUsersWhoUserIsFollowing() $username = 'l3l0'; $github = new Client(); - $users = $github->getUserApi()->getFollowing($username); + $users = $github->api('user')->following($username); $user = array_pop($users); $this->assertArrayHasKey('id', $user); @@ -64,7 +64,7 @@ public function shouldGetFollowersUsers() $username = 'KnpLabs'; $github = new Client(); - $users = $github->getUserApi()->getFollowers($username); + $users = $github->api('user')->followers($username); $user = array_pop($users); $this->assertArrayHasKey('id', $user); @@ -77,7 +77,7 @@ public function shouldGetFollowersUsers() public function shouldNotFollowUserWithoutAuthorization() { $github = new Client(); - $response = $github->getUserApi()->follow('KnpLabs'); + $response = $github->api('current_user')->follow()->follow('KnpLabs'); $this->assertEquals('Requires authentication', $response['message']); } @@ -88,7 +88,7 @@ public function shouldNotFollowUserWithoutAuthorization() public function shouldNotUnfollowUserWithoutAuthorization() { $github = new Client(); - $response = $github->getUserApi()->unfollow('KnpLabs'); + $response = $github->api('current_user')->follow()->unfollow('KnpLabs'); $this->assertEquals('Requires authentication', $response['message']); } @@ -101,7 +101,7 @@ public function shouldGetReposBeingWatched() $username = 'l3l0'; $github = new Client(); - $repos = $github->getUserApi()->getWatchedRepos($username); + $repos = $github->api('user')->watched($username); $repo = array_pop($repos); $this->assertArrayHasKey('id', $repo); diff --git a/test/Github/Tests/HttpClientTest.php b/test/Github/Tests/HttpClientTest.php index 3dd9bf7d23e..ab78b2dd542 100644 --- a/test/Github/Tests/HttpClientTest.php +++ b/test/Github/Tests/HttpClientTest.php @@ -6,7 +6,7 @@ class HttpClientTest extends \PHPUnit_Framework_TestCase { - public function testInstanciateWithOptions() + public function testInstanceWithOptions() { $httpClient = new TestDriver(array( 'timeout' => 33 @@ -27,7 +27,7 @@ public function testGet() ->getMock(); $httpClient->expects($this->once()) ->method('request') - ->with($path, $parameters, 'GET', $options); + ->with('/some/path?a=b', array(), 'GET', $options); $httpClient->get($path, $parameters, $options); } @@ -83,13 +83,6 @@ protected function doRequest($url, array $parameters = array(), $httpMethod = 'G { } - /** - * Get an option value. - * - * @param string $name The option name - * - * @return mixed The option value - */ public function getOption($name, $default = null) { return isset($this->options[$name]) ? $this->options[$name] : $default;