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

Add vulnerability alerts endpoints #1096

Merged
merged 1 commit into from
Feb 3, 2023
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
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'));
}
}