Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow getting repo info by id, not just user and repo name #579

Merged
merged 5 commits into from
May 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/repos.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,18 @@ $repos = $client->api('repo')->find('chess', array('language' => 'php', 'start_p

### Get extended information about a repository

Using the username of the repository owner and the repository name:

```php
$repo = $client->api('repo')->show('KnpLabs', 'php-github-api')
```

Or by using the repository id (note that this is at time of writing an undocumented feature, see [here](https://github.com/piotrmurach/github/issues/283) and [here](https://github.com/piotrmurach/github/issues/282)):

```php
$repo = $client->api('repo')->showById(123456)
```

Returns an array of information about the specified repository.

### Get the repositories of a specific user
Expand Down
17 changes: 17 additions & 0 deletions lib/Github/Api/Repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,23 @@ public function show($username, $repository)
{
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository));
}

/**
* Get extended information about a repository by its id.
* Note: at time of writing this is an undocumented feature but GitHub support have advised that it can be relied on.
*
* @link http://developer.github.com/v3/repos/
* @link https://github.com/piotrmurach/github/issues/283
* @link https://github.com/piotrmurach/github/issues/282
*
* @param int $id the id of the repository
*
* @return array information about the repository
*/
public function showById($id)
{
return $this->get('/repositories/'.rawurlencode($id));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the ID always an integer? Why do we ned rawurlencode?

´return $this->get(sprintf('/repositories/%d', $id));`

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it should always be an integer (currently anyway). I thought about checking that they'd input an integer and if not throwing an exception, but then figured that it was better than us assuming IDs never become non-integers to just pass it over to GitHub and let them respond accordingly. Having the rawurlencode ensures that whatever is passed into the function (even if it shouldn't have been) is sent over to GitHub properly.

There probably isn't much in it as I think it's highly unlikely that GH will ever start to issue non-integer IDs, but personally I prefer this way - it's GitHub's ID, let's let them validate it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, okey. I see that @m1guelpf and @acrobat are fine with the way it is so let's not change it.

}

/**
* Create repository.
Expand Down
16 changes: 16 additions & 0 deletions test/Github/Tests/Api/RepoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ public function shouldShowRepository()

$this->assertEquals($expectedArray, $api->show('KnpLabs', 'php-github-api'));
}

/**
* @test
*/
public function shouldShowRepositoryById()
{
$expectedArray = array('id' => 123456, 'name' => 'repoName');

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('/repositories/123456')
->will($this->returnValue($expectedArray));

$this->assertEquals($expectedArray, $api->showById(123456));
}

/**
* @test
Expand Down