Skip to content

Add support for creating and updating GitHub checks #788

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
Apr 23, 2019
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
1 change: 1 addition & 0 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 31 additions & 0 deletions doc/repo/checks.md
Original file line number Diff line number Diff line change
@@ -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);
```
58 changes: 58 additions & 0 deletions lib/Github/Api/Repository/Checks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Github\Api\Repository;

use Github\Api\AbstractApi;
use Github\Api\AcceptHeaderTrait;
use Github\Exception\MissingArgumentException;

/**
* @link https://developer.github.com/v3/checks/
*
* @author Zack Galbreath <[email protected]>
*/
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);
}
}
80 changes: 80 additions & 0 deletions test/Github/Tests/Api/Repository/ChecksTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Github\Tests\Api\Repository;

use Github\Tests\Api\TestCase;

class ChecksTest extends TestCase
{
/**
* @test
* @expectedException \Github\Exception\MissingArgumentException
*/
public function shouldNotCreateWithoutHeadSha()
{
$data = ['name' => '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;
}
}