Skip to content

Added missing api methods for the integrations installation api #527

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

Merged
merged 1 commit into from
Feb 21, 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
26 changes: 26 additions & 0 deletions doc/integrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,29 @@ To create an access token on behalf of a user with id 456 use:
```php
$token = $client->api('integrations')->createInstallationToken(123, 456);
```

### Find all installations

Find all installations for the authenticated integration.
```php
Installations = $client->api('integrations')->findInstallations();
```

### List repositories

List repositories that are accessible to the authenticated installation.
```php
$repositories = $client->api('integrations')->listRepositories(456);
```

### Add repository to installation
Add a single repository to an installation.
```php
$client->api('integrations')->addRepository(123);
```

### Remove repository from installation
Remove a single repository from an installation.
```php
$client->api('integrations')->removeRepository(123);
```
56 changes: 56 additions & 0 deletions lib/Github/Api/Integrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,60 @@ public function createInstallationToken($installationId, $userId = null)

return $this->post('/installations/'.rawurlencode($installationId).'/access_tokens', $parameters);
}

/**
* Find all installations for the authenticated integration.
*
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add @link https://developer.github.com/v3/integrations/#find-installations

* @link https://developer.github.com/v3/integrations/#find-installations
*
* @return array
*/
public function findInstallations()
{
return $this->get('/integration/installations');
}

/**
* List repositories that are accessible to the authenticated installation.
*
Copy link
Collaborator

Choose a reason for hiding this comment

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

* @link https://developer.github.com/v3/integrations/installations/#list-repositories
*
* @param int $userId
*
* @return array
*/
public function listRepositories($userId)
Copy link
Collaborator

Choose a reason for hiding this comment

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

$userId should be optional, right?

Copy link
Collaborator Author

@acrobat acrobat Feb 14, 2017

Choose a reason for hiding this comment

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

No if you call the api without the userId you just get a 403 error irc. The repositories are also always checked for the current installation and the repo's the user has access to. I guess github will change this api route in the future so the userId is part of the uri.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Okey. Good. I guess I misunderstood the docs.

{
return $this->get('/installation/repositories', ['user_id' => $userId]);
}

/**
* Add a single repository to an installation.
*
Copy link
Collaborator

Choose a reason for hiding this comment

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

* @link https://developer.github.com/v3/integrations/installations/#add-repository-to-installation
*
* @param int $installationId
* @param int $repositoryId
*
* @return array
*/
public function addRepository($installationId, $repositoryId)
{
return $this->put('/installations/'.rawurlencode($installationId).'/repositories/'.rawurlencode($repositoryId));
}

/**
* Remove a single repository from an installation.
*
Copy link
Collaborator

Choose a reason for hiding this comment

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

* @link https://developer.github.com/v3/integrations/installations/#remove-repository-from-installation
*
* @param int $installationId
* @param int $repositoryId
*
* @return array
*/
public function removeRepository($installationId, $repositoryId)
{
return $this->delete('/installations/'.rawurlencode($installationId).'/repositories/'.rawurlencode($repositoryId));
}
}
2 changes: 2 additions & 0 deletions lib/Github/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
* @method Api\GitData gitData()
* @method Api\Gists gist()
* @method Api\Gists gists()
* @method Api\Integrations integration()
* @method Api\Integrations integrations()
* @method Api\Issue issue()
* @method Api\Issue issues()
* @method Api\Markdown markdown()
Expand Down
73 changes: 73 additions & 0 deletions test/Github/Tests/Api/IntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Github\Tests\Api;

class IntegrationTest extends TestCase
{
/**
* @test
*/
public function shouldFindRepositoriesForIntegration()
{
$result = ['installation1', 'installation2'];

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('/integration/installations')
->willReturn($result);

$this->assertEquals($result, $api->findInstallations());
}

/**
* @test
*/
public function shouldGetRepositoriesFromInstallation()
{
$result = ['repo1', 'repo2'];

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('/installation/repositories', ['user_id' => '1234'])
->willReturn($result);

$this->assertEquals($result, $api->listRepositories('1234'));
}

/**
* @test
*/
public function shouldAddRepositoryToInstallation()
{
$api = $this->getApiMock();
$api->expects($this->once())
->method('put')
->with('/installations/1234/repositories/5678');

$api->addRepository('1234', '5678');
}

/**
* @test
*/
public function shouldRemoveRepositoryToInstallation()
{
$api = $this->getApiMock();
$api->expects($this->once())
->method('delete')
->with('/installations/1234/repositories/5678');

$api->removeRepository('1234', '5678');
}


/**
* @return string
*/
protected function getApiClass()
{
return \Github\Api\Integrations::class;
}
}