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

feat: Secret Scanning Alerts #1114

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ v3 APIs:
* [Public keys](currentuser/publickeys.md)
* [Memberships](currentuser/memberships.md)
* [Enterprise](enterprise.md)
* [Secret Scanning Alert](enterprise/secret-scanning.md)
* [Gists](gists.md)
* [Comments](gists/comments.md)
* GitData
Expand Down Expand Up @@ -42,6 +43,7 @@ v3 APIs:
* [Self hosted runners](organization/actions/self_hosted_runners.md)
* [Secrets](organization/actions/secrets.md)
* [Variables](organization/actions/variables.md)
* [Secret Scanning Alert](organization/secret-scanning.md)
* [Projects](project/projects.md)
* [Columns](project/columns.md)
* [Cards](project/cards.md)
Expand Down Expand Up @@ -74,6 +76,7 @@ v3 APIs:
* [Stargazers](repo/stargazers.md)
* [Statuses](repo/statuses.md)
* [Tags](repo/tags.md)
* [Secret Scanning Alert](repo/secret-scanning.md)
* [Search](search.md)
* [Users](users.md)

Expand Down
10 changes: 10 additions & 0 deletions doc/enterprise/secret-scanning.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## Enterprise / Secret Scanning API
[Back to the "Enterprise API"](../../enterprise.md) | [Back to the navigation](../../README.md)

# List secret-scanning alerts for an Enterprise

https://docs.github.com/en/enterprise-server@3.5/rest/secret-scanning#list-secret-scanning-alerts-for-an-enterprise

```php
$alerts = $client->api('enterprise')->secretScanning()->alerts('KnpLabs');
```
10 changes: 10 additions & 0 deletions doc/organization/secret-scanning.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## Organization / Secret Scanning API
[Back to the "Organization API"](../../organization.md) | [Back to the navigation](../../README.md)

# List secret-scanning alerts for an Organization

https://docs.github.com/en/enterprise-server@3.5/rest/secret-scanning#list-secret-scanning-alerts-for-an-organization

```php
$alerts = $client->api('organization')->secretScanning()->alerts('KnpLabs');
```
37 changes: 37 additions & 0 deletions doc/repo/secret-scanning.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## Repository / Secret Scanning API
[Back to the "Repos API"](../../repos.md) | [Back to the navigation](../../README.md)

# List secret-scanning alerts for a repository

https://docs.github.com/en/enterprise-server@3.5/rest/secret-scanning#list-secret-scanning-alerts-for-a-repository

```php
$alerts = $client->api('repos')->secretScanning()->alerts('KnpLabs', 'php-github-api');
```

# Get a secret-scanning alert

https://docs.github.com/en/enterprise-server@3.5/rest/secret-scanning#get-a-secret-scanning-alert

```php
$alert = $client->api('repos')->secretScanning()->getAlert('KnpLabs', 'php-github-api', $alertNumber);
```

# Update a secret-scanning alert

https://docs.github.com/en/enterprise-server@3.5/rest/secret-scanning#update-a-secret-scanning-alert

```php
$client->api('repos')->secretScanning()->updateAlert('KnpLabs', 'php-github-api', $alertNumber, [
'state' => 'resolved',
'resolution' => 'wont-fix'
]);
```

# List Locations for a secret-scanning alert

https://docs.github.com/en/enterprise-server@3.5/rest/secret-scanning#list-locations-for-a-secret-scanning-alert

```php
$locations = $client->api('repos')->secretScanning()->locations('KnpLabs', 'php-github-api', $alertNumber);
```
9 changes: 9 additions & 0 deletions lib/Github/Api/Enterprise.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Github\Api\Enterprise\License;
use Github\Api\Enterprise\ManagementConsole;
use Github\Api\Enterprise\SecretScanning;
use Github\Api\Enterprise\Stats;
use Github\Api\Enterprise\UserAdmin;

Expand Down Expand Up @@ -48,4 +49,12 @@ public function userAdmin()
{
return new UserAdmin($this->getClient());
}

/**
* @return SecretScanning
*/
public function secretScanning(): SecretScanning
{
return new SecretScanning($this->getClient());
}
}
21 changes: 21 additions & 0 deletions lib/Github/Api/Enterprise/SecretScanning.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Github\Api\Enterprise;

use Github\Api\AbstractApi;

class SecretScanning extends AbstractApi
{
/**
* @link https://docs.github.com/en/enterprise-server@3.5/rest/secret-scanning#list-secret-scanning-alerts-for-an-enterprise
*
* @param string $enterprise
* @param array $params
*
* @return array|string
*/
public function alerts(string $enterprise, array $params = [])
{
return $this->get('/enterprises/'.rawurlencode($enterprise).'/secret-scanning/alerts', $params);
}
}
9 changes: 9 additions & 0 deletions lib/Github/Api/Organization.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Github\Api\Organization\Hooks;
use Github\Api\Organization\Members;
use Github\Api\Organization\OutsideCollaborators;
use Github\Api\Organization\SecretScanning;
use Github\Api\Organization\Teams;

/**
Expand Down Expand Up @@ -149,4 +150,12 @@ public function runners(): SelfHostedRunners
{
return new SelfHostedRunners($this->getClient());
}

/**
* @return SecretScanning
*/
public function secretScanning(): SecretScanning
{
return new SecretScanning($this->getClient());
}
}
19 changes: 19 additions & 0 deletions lib/Github/Api/Organization/SecretScanning.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Github\Api\Organization;

class SecretScanning extends \Github\Api\AbstractApi
{
/**
* @link https://docs.github.com/en/enterprise-server@3.5/rest/secret-scanning#list-secret-scanning-alerts-for-an-organization
*
* @param string $organization
* @param array $params
*
* @return array|string
*/
public function alerts(string $organization, array $params = [])
{
return $this->get('/orgs/'.rawurlencode($organization).'/secret-scanning/alerts', $params);
}
}
9 changes: 9 additions & 0 deletions lib/Github/Api/Repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Github\Api\Repository\Projects;
use Github\Api\Repository\Protection;
use Github\Api\Repository\Releases;
use Github\Api\Repository\SecretScanning;
use Github\Api\Repository\Stargazers;
use Github\Api\Repository\Statuses;
use Github\Api\Repository\Traffic;
Expand Down Expand Up @@ -897,4 +898,12 @@ public function disableVulnerabilityAlerts(string $username, string $repository)
{
return $this->delete('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/vulnerability-alerts');
}

/**
* @return SecretScanning
*/
public function secretScanning(): SecretScanning
{
return new SecretScanning($this->getClient());
}
}
65 changes: 65 additions & 0 deletions lib/Github/Api/Repository/SecretScanning.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Github\Api\Repository;

class SecretScanning extends \Github\Api\AbstractApi
{
/**
* @link https://docs.github.com/en/enterprise-server@3.5/rest/secret-scanning#list-secret-scanning-alerts-for-a-repository
*
* @param string $username
* @param string $repository
* @param array $params
*
* @return array|string
*/
public function alerts(string $username, string $repository, array $params = [])
{
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/secret-scanning/alerts', $params);
}

/**
* @link https://docs.github.com/en/enterprise-server@3.5/rest/secret-scanning#get-a-secret-scanning-alert
*
* @param string $username
* @param string $repository
* @param int $alertNumber
*
* @return array|string
*/
public function getAlert(string $username, string $repository, int $alertNumber)
{
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/secret-scanning/alerts/'.$alertNumber);
}

/**
* @link https://docs.github.com/en/enterprise-server@3.5/rest/secret-scanning#update-a-secret-scanning-alert
*
* @param string $username
* @param string $repository
* @param int $alertNumber
* @param array $params
*
* @return array|string
*/
public function updateAlert(string $username, string $repository, int $alertNumber, array $params = [])
{
return $this->patch('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/secret-scanning/alerts/'.$alertNumber, $params);
}

/**
* @link https://docs.github.com/en/enterprise-server@3.5/rest/secret-scanning#list-locations-for-a-secret-scanning-alert
*
* @param string $username
* @param string $repository
* @param int $alertNumber
* @param array $params
*
* @return array|string
*/
public function locations(string $username, string $repository, int $alertNumber, array $params = [])
{
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/secret-scanning/alerts/'.$alertNumber.'/locations', $params);
}
}

41 changes: 41 additions & 0 deletions test/Github/Tests/Api/Enterprise/SecretScanningTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Github\Tests\Api\Enterprise;

use Github\Api\Enterprise\SecretScanning;
use Github\Tests\Api\TestCase;
use PHPUnit\Framework\MockObject\MockObject;

class SecretScanningTest extends TestCase
{
/**
* @test
*/
public function shouldGetAlerts()
{
$expectedArray = [
['number' => 1, 'state' => 'resolved', 'resolution' => 'false_positive'],
['number' => 2, 'state' => 'open', 'resolution' => null],
['number' => 3, 'state' => 'resolved', 'resolution' => 'wont_fix'],
['number' => 4, 'state' => 'resolved', 'resolution' => 'revoked'],
];

/** @var SecretScanning|MockObject $api */
$api = $this->getApiMock();

$api
->expects($this->once())
->method('get')
->with('/enterprises/KnpLabs/secret-scanning/alerts')
->will($this->returnValue($expectedArray));

$this->assertEquals($expectedArray, $api->alerts('KnpLabs', [
'state' => 'all',
]));
}

protected function getApiClass()
{
return \Github\Api\Enterprise\SecretScanning::class;
}
}
41 changes: 41 additions & 0 deletions test/Github/Tests/Api/Organization/SecretScanningTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Github\Tests\Api\Organization;

use Github\Api\Organization\SecretScanning;
use Github\Tests\Api\TestCase;
use PHPUnit\Framework\MockObject\MockObject;

class SecretScanningTest extends TestCase
{
/**
* @test
*/
public function shouldGetAlerts()
{
$expectedArray = [
['number' => 1, 'state' => 'resolved', 'resolution' => 'false_positive'],
['number' => 2, 'state' => 'open', 'resolution' => null],
['number' => 3, 'state' => 'resolved', 'resolution' => 'wont_fix'],
['number' => 4, 'state' => 'resolved', 'resolution' => 'revoked'],
];

/** @var SecretScanning|MockObject $api */
$api = $this->getApiMock();

$api
->expects($this->once())
->method('get')
->with('/orgs/KnpLabs/secret-scanning/alerts')
->will($this->returnValue($expectedArray));

$this->assertEquals($expectedArray, $api->alerts('KnpLabs', [
'state' => 'all',
]));
}

protected function getApiClass()
{
return \Github\Api\Organization\SecretScanning::class;
}
}