Skip to content

Commit 8c26dc8

Browse files
authored
Merge pull request #788 from zackgalbreath/github_checks
Add support for creating and updating GitHub checks
2 parents 497292c + f253ab0 commit 8c26dc8

File tree

4 files changed

+170
-0
lines changed

4 files changed

+170
-0
lines changed

doc/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ v3 APIs:
4646
* [Comments](pull_request/comments.md)
4747
* [Rate Limits](rate_limits.md)
4848
* [Repositories](repos.md)
49+
* [Checks](repo/checks.md)
4950
* [Contents](repo/contents.md)
5051
* [Deployments](repo/deployments.md)
5152
* [Protection](repo/protection.md)

doc/repo/checks.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
## Repo / Checks API
2+
[Back to the "Repos API"](../repos.md) | [Back to the navigation](../README.md)
3+
4+
### Create a check for a commit
5+
6+
[Visit GitHub for a full of list of parameters and their descriptions.](https://developer.github.com/v3/checks/runs/#create-a-check-run)
7+
8+
```php
9+
$params = [
10+
'name' => 'my check', # required
11+
'head_sha' => $commitSha, # required
12+
'status' => 'pending',
13+
'details_url' => 'https://nimbleci.com/...',
14+
'output' => {...}
15+
];
16+
$checks = $client->api('repo')->checks()->create('NimbleCI', 'docker-web-tester-behat', $params);
17+
```
18+
19+
### Update an existing check on a commit
20+
21+
https://developer.github.com/v3/checks/runs/#update-a-check-run
22+
23+
```php
24+
$params = [
25+
'name' => 'my check',
26+
'status' => 'pending',
27+
'details_url' => 'https://nimbleci.com/...',
28+
'output' => {...}
29+
];
30+
$checks = $client->api('repo')->checks()->create('NimbleCI', 'docker-web-tester-behat', $checkRunId, $params);
31+
```

lib/Github/Api/Repository/Checks.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Github\Api\Repository;
4+
5+
use Github\Api\AbstractApi;
6+
use Github\Api\AcceptHeaderTrait;
7+
use Github\Exception\MissingArgumentException;
8+
9+
/**
10+
* @link https://developer.github.com/v3/checks/
11+
*
12+
* @author Zack Galbreath <zack.galbreath@kitware.com>
13+
*/
14+
class Checks extends AbstractApi
15+
{
16+
use AcceptHeaderTrait;
17+
18+
/**
19+
* @link https://developer.github.com/v3/checks/runs/#create-a-check-run
20+
*
21+
* @param string $username
22+
* @param string $repository
23+
* @param array $params
24+
*
25+
* @throws MissingArgumentException
26+
*
27+
* @return array
28+
*/
29+
public function create($username, $repository, array $params = [])
30+
{
31+
if (!isset($params['name'], $params['head_sha'])) {
32+
throw new MissingArgumentException(['name', 'head_sha']);
33+
}
34+
35+
// This api is in preview mode, so set the correct accept-header.
36+
$this->acceptHeaderValue = 'application/vnd.github.antiope-preview+json';
37+
38+
return $this->post('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/check-runs', $params);
39+
}
40+
41+
/**
42+
* @link https://developer.github.com/v3/checks/runs/#update-a-check-run
43+
*
44+
* @param string $username
45+
* @param string $repository
46+
* @param string $checkRunId
47+
* @param array $params
48+
*
49+
* @return array
50+
*/
51+
public function update($username, $repository, $checkRunId, array $params = [])
52+
{
53+
// This api is in preview mode, so set the correct accept-header.
54+
$this->acceptHeaderValue = 'application/vnd.github.antiope-preview+json';
55+
56+
return $this->patch('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/check-runs/'.rawurlencode($checkRunId), $params);
57+
}
58+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace Github\Tests\Api\Repository;
4+
5+
use Github\Tests\Api\TestCase;
6+
7+
class ChecksTest extends TestCase
8+
{
9+
/**
10+
* @test
11+
* @expectedException \Github\Exception\MissingArgumentException
12+
*/
13+
public function shouldNotCreateWithoutHeadSha()
14+
{
15+
$data = ['name' => 'my check'];
16+
17+
$api = $this->getApiMock();
18+
$api->expects($this->never())
19+
->method('post');
20+
21+
$api->create('KnpLabs', 'php-github-api', $data);
22+
}
23+
24+
/**
25+
* @test
26+
* @expectedException \Github\Exception\MissingArgumentException
27+
*/
28+
public function shouldNotCreateWithoutName()
29+
{
30+
$data = ['head_sha' => 'commitSHA123456'];
31+
32+
$api = $this->getApiMock();
33+
$api->expects($this->never())
34+
->method('post');
35+
36+
$api->create('KnpLabs', 'php-github-api', $data);
37+
}
38+
39+
/**
40+
* @test
41+
*/
42+
public function shouldCreateCheck()
43+
{
44+
$expectedValue = ['state' => 'success'];
45+
$data = ['head_sha' => 'commitSHA123456', 'name' => 'my check'];
46+
47+
$api = $this->getApiMock();
48+
$api->expects($this->once())
49+
->method('post')
50+
->with('/repos/KnpLabs/php-github-api/check-runs', $data)
51+
->will($this->returnValue($expectedValue));
52+
53+
$this->assertEquals($expectedValue, $api->create('KnpLabs', 'php-github-api', $data));
54+
}
55+
56+
/**
57+
* @test
58+
*/
59+
public function shouldUpdateCheck()
60+
{
61+
$expectedValue = ['state' => 'success'];
62+
$data = ['head_sha' => 'commitSHA123456', 'name' => 'my check'];
63+
64+
$api = $this->getApiMock();
65+
$api->expects($this->once())
66+
->method('patch')
67+
->with('/repos/KnpLabs/php-github-api/check-runs/123', $data)
68+
->will($this->returnValue($expectedValue));
69+
70+
$this->assertEquals($expectedValue, $api->update('KnpLabs', 'php-github-api', '123', $data));
71+
}
72+
73+
/**
74+
* @return string
75+
*/
76+
protected function getApiClass()
77+
{
78+
return \Github\Api\Repository\Checks::class;
79+
}
80+
}

0 commit comments

Comments
 (0)