Skip to content

Commit

Permalink
feature #1096 Add vulnerability alerts endpoints (andreia)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.9.x-dev branch.

Discussion
----------

Add vulnerability alerts endpoints:

- [x] Check if vulnerability alerts (dependabot alerts) are enabled for a repository (https://developer.github.com/v3/repos/#check-if-vulnerability-alerts-are-enabled-for-a-repository).
- [x] Enable vulnerability alerts (dependabot alerts) for a repository (https://developer.github.com/v3/repos/#enable-vulnerability-alerts).
- [x] Disable vulnerability alerts (dependabot alerts) for a repository (https://developer.github.com/v3/repos/#disable-vulnerability-alerts).


Commits
-------

7245b02 Add vulnerability alerts endpoints
  • Loading branch information
acrobat authored Feb 3, 2023
2 parents 28029f2 + 7245b02 commit 0a44bdc
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
24 changes: 24 additions & 0 deletions doc/repos.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,27 @@ $client->api('repo')->createFromTemplate('template-owner', 'template-repo', [
'owner' => 'name-of-new-repo-owner', // can be user or org
]);
```

### Check if vulnerability alerts (dependabot alerts) are enabled for a repository

https://developer.github.com/v3/repos/#check-if-vulnerability-alerts-are-enabled-for-a-repository

```php
$client->api('repo')->isVulnerabilityAlertsEnabled('KnpLabs', 'php-github-api');
```

### Enable vulnerability alerts (dependabot alerts)

https://developer.github.com/v3/repos/#enable-vulnerability-alerts

```php
$client->api('repo')->enableVulnerabilityAlerts('KnpLabs', 'php-github-api');
```

### Disable vulnerability alerts (dependabot alerts)

https://developer.github.com/v3/repos/#disable-vulnerability-alerts

```php
$client->api('repo')->disableVulnerabilityAlerts('KnpLabs', 'php-github-api');
```
45 changes: 45 additions & 0 deletions lib/Github/Api/Repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -843,4 +843,49 @@ public function createFromTemplate(string $templateOwner, string $templateRepo,

return $this->post('/repos/'.rawurldecode($templateOwner).'/'.rawurldecode($templateRepo).'/generate', $parameters);
}

/**
* Check if vulnerability alerts are enabled for a repository.
*
* @link https://developer.github.com/v3/repos/#check-if-vulnerability-alerts-are-enabled-for-a-repository
*
* @param string $username the username
* @param string $repository the repository
*
* @return array|string
*/
public function isVulnerabilityAlertsEnabled(string $username, string $repository)
{
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/vulnerability-alerts');
}

/**
* Enable vulnerability alerts for a repository.
*
* @link https://developer.github.com/v3/repos/#enable-vulnerability-alerts
*
* @param string $username the username
* @param string $repository the repository
*
* @return array|string
*/
public function enableVulnerabilityAlerts(string $username, string $repository)
{
return $this->put('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/vulnerability-alerts');
}

/**
* Disable vulnerability alerts for a repository.
*
* @link https://developer.github.com/v3/repos/#disable-vulnerability-alerts
*
* @param string $username the username
* @param string $repository the repository
*
* @return array|string
*/
public function disableVulnerabilityAlerts(string $username, string $repository)
{
return $this->delete('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/vulnerability-alerts');
}
}
54 changes: 54 additions & 0 deletions test/Github/Tests/Api/RepoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -722,4 +722,58 @@ protected function getApiClass()
{
return \Github\Api\Repo::class;
}

/**
* @test
*/
public function shouldCheckVulnerabilityAlertsEnabled()
{
$expectedResponse = '';

$api = $this->getApiMock();

$api
->expects($this->once())
->method('get')
->with('/repos/KnpLabs/php-github-api/vulnerability-alerts')
->will($this->returnValue($expectedResponse));

$this->assertEquals($expectedResponse, $api->isVulnerabilityAlertsEnabled('KnpLabs', 'php-github-api'));
}

/**
* @test
*/
public function shouldEnableVulnerabilityAlerts()
{
$expectedResponse = '';

$api = $this->getApiMock();

$api
->expects($this->once())
->method('put')
->with('/repos/KnpLabs/php-github-api/vulnerability-alerts')
->will($this->returnValue($expectedResponse));

$this->assertEquals($expectedResponse, $api->enableVulnerabilityAlerts('KnpLabs', 'php-github-api'));
}

/**
* @test
*/
public function shouldDisableVulnerabilityAlerts()
{
$expectedResponse = '';

$api = $this->getApiMock();

$api
->expects($this->once())
->method('delete')
->with('/repos/KnpLabs/php-github-api/vulnerability-alerts')
->will($this->returnValue($expectedResponse));

$this->assertEquals($expectedResponse, $api->disableVulnerabilityAlerts('KnpLabs', 'php-github-api'));
}
}

0 comments on commit 0a44bdc

Please sign in to comment.