diff --git a/doc/README.md b/doc/README.md index c4c3562df9b..3d3d3570b8b 100644 --- a/doc/README.md +++ b/doc/README.md @@ -46,6 +46,7 @@ v3 APIs: * [Comments](pull_request/comments.md) * [Rate Limits](rate_limits.md) * [Repositories](repos.md) + * [Checks](repo/checks.md) * [Contents](repo/contents.md) * [Deployments](repo/deployments.md) * [Protection](repo/protection.md) diff --git a/doc/repo/checks.md b/doc/repo/checks.md new file mode 100644 index 00000000000..04cd6144bf7 --- /dev/null +++ b/doc/repo/checks.md @@ -0,0 +1,31 @@ +## Repo / Checks API +[Back to the "Repos API"](../repos.md) | [Back to the navigation](../README.md) + +### Create a check for a commit + +[Visit GitHub for a full of list of parameters and their descriptions.](https://developer.github.com/v3/checks/runs/#create-a-check-run) + +```php +$params = [ + 'name' => 'my check', # required + 'head_sha' => $commitSha, # required + 'status' => 'pending', + 'details_url' => 'https://nimbleci.com/...', + 'output' => {...} +]; +$checks = $client->api('repo')->checks()->create('NimbleCI', 'docker-web-tester-behat', $params); +``` + +### Update an existing check on a commit + +https://developer.github.com/v3/checks/runs/#update-a-check-run + +```php +$params = [ + 'name' => 'my check', + 'status' => 'pending', + 'details_url' => 'https://nimbleci.com/...', + 'output' => {...} +]; +$checks = $client->api('repo')->checks()->create('NimbleCI', 'docker-web-tester-behat', $checkRunId, $params); +``` diff --git a/lib/Github/Api/Repository/Checks.php b/lib/Github/Api/Repository/Checks.php new file mode 100644 index 00000000000..fcc7059d2d5 --- /dev/null +++ b/lib/Github/Api/Repository/Checks.php @@ -0,0 +1,58 @@ + + */ +class Checks extends AbstractApi +{ + use AcceptHeaderTrait; + + /** + * @link https://developer.github.com/v3/checks/runs/#create-a-check-run + * + * @param string $username + * @param string $repository + * @param array $params + * + * @throws MissingArgumentException + * + * @return array + */ + public function create($username, $repository, array $params = []) + { + if (!isset($params['name'], $params['head_sha'])) { + throw new MissingArgumentException(['name', 'head_sha']); + } + + // This api is in preview mode, so set the correct accept-header. + $this->acceptHeaderValue = 'application/vnd.github.antiope-preview+json'; + + return $this->post('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/check-runs', $params); + } + + /** + * @link https://developer.github.com/v3/checks/runs/#update-a-check-run + * + * @param string $username + * @param string $repository + * @param string $checkRunId + * @param array $params + * + * @return array + */ + public function update($username, $repository, $checkRunId, array $params = []) + { + // This api is in preview mode, so set the correct accept-header. + $this->acceptHeaderValue = 'application/vnd.github.antiope-preview+json'; + + return $this->patch('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/check-runs/'.rawurlencode($checkRunId), $params); + } +} diff --git a/test/Github/Tests/Api/Repository/ChecksTest.php b/test/Github/Tests/Api/Repository/ChecksTest.php new file mode 100644 index 00000000000..23eb545141a --- /dev/null +++ b/test/Github/Tests/Api/Repository/ChecksTest.php @@ -0,0 +1,80 @@ + 'my check']; + + $api = $this->getApiMock(); + $api->expects($this->never()) + ->method('post'); + + $api->create('KnpLabs', 'php-github-api', $data); + } + + /** + * @test + * @expectedException \Github\Exception\MissingArgumentException + */ + public function shouldNotCreateWithoutName() + { + $data = ['head_sha' => 'commitSHA123456']; + + $api = $this->getApiMock(); + $api->expects($this->never()) + ->method('post'); + + $api->create('KnpLabs', 'php-github-api', $data); + } + + /** + * @test + */ + public function shouldCreateCheck() + { + $expectedValue = ['state' => 'success']; + $data = ['head_sha' => 'commitSHA123456', 'name' => 'my check']; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('post') + ->with('/repos/KnpLabs/php-github-api/check-runs', $data) + ->will($this->returnValue($expectedValue)); + + $this->assertEquals($expectedValue, $api->create('KnpLabs', 'php-github-api', $data)); + } + + /** + * @test + */ + public function shouldUpdateCheck() + { + $expectedValue = ['state' => 'success']; + $data = ['head_sha' => 'commitSHA123456', 'name' => 'my check']; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('patch') + ->with('/repos/KnpLabs/php-github-api/check-runs/123', $data) + ->will($this->returnValue($expectedValue)); + + $this->assertEquals($expectedValue, $api->update('KnpLabs', 'php-github-api', '123', $data)); + } + + /** + * @return string + */ + protected function getApiClass() + { + return \Github\Api\Repository\Checks::class; + } +}