Skip to content

Commit c92f1c8

Browse files
committed
Improve checks api implementation
1 parent e1c5dca commit c92f1c8

File tree

6 files changed

+365
-1
lines changed

6 files changed

+365
-1
lines changed

lib/Github/Api/Repo.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Github\Api;
44

55
use Github\Api\Repository\Checks;
6+
use Github\Api\Repository\Checks\CheckRuns;
7+
use Github\Api\Repository\Checks\CheckSuites;
68
use Github\Api\Repository\Collaborators;
79
use Github\Api\Repository\Comments;
810
use Github\Api\Repository\Commits;
@@ -335,14 +337,33 @@ public function commits()
335337
* Manage checks on a repository.
336338
*
337339
* @link https://developer.github.com/v3/checks/
340+
* @deprecated since 2.17 and will be removed in 3.0. Use the "checkRuns" or "checkSuites" api's instead.
338341
*
339342
* @return Checks
340343
*/
341344
public function checks()
342345
{
346+
@trigger_error(sprintf('The "%s" is deprecated since knp-labs/php-github-api 2.17 and will be removed in knp-labs/php-github-api 3.0. Use the "checkRuns" or "checkSuites" api\'s instead.', __METHOD__), E_USER_DEPRECATED);
347+
343348
return new Checks($this->client);
344349
}
345350

351+
/**
352+
* @link https://docs.github.com/en/free-pro-team@latest/rest/reference/checks#check-runs
353+
*/
354+
public function checkRuns(): CheckRuns
355+
{
356+
return new CheckRuns($this->client);
357+
}
358+
359+
/**
360+
* @link https://docs.github.com/en/free-pro-team@latest/rest/reference/checks#check-suites
361+
*/
362+
public function checkSuites(): CheckSuites
363+
{
364+
return new CheckSuites($this->client);
365+
}
366+
346367
/**
347368
* Manage the content of a repository.
348369
*

lib/Github/Api/Repository/Checks.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
use Github\Exception\MissingArgumentException;
88

99
/**
10-
* @link https://developer.github.com/v3/checks/
10+
* @link https://developer.github.com/v3/checks/
11+
* @deprecated since 2.17 and will be removed in 3.0. Use the "Github\Api\Repository\Checks\CheckRuns" or "Github\Api\Repository\Checks\CheckSuits" api classes instead.
1112
*
1213
* @author Zack Galbreath <zack.galbreath@kitware.com>
1314
*/
@@ -29,6 +30,8 @@ class Checks extends AbstractApi
2930
*/
3031
public function create($username, $repository, array $params = [])
3132
{
33+
@trigger_error(sprintf('The "%s" method is deprecated since knp-labs/php-github-api 2.17 and will be removed in knp-labs/php-github-api 3.0. Use the "Github\Api\Repository\Checks\CheckRuns::create" method instead.', __METHOD__), E_USER_DEPRECATED);
34+
3235
if (!isset($params['name'], $params['head_sha'])) {
3336
throw new MissingArgumentException(['name', 'head_sha']);
3437
}
@@ -48,6 +51,8 @@ public function create($username, $repository, array $params = [])
4851
*/
4952
public function update($username, $repository, $checkRunId, array $params = [])
5053
{
54+
@trigger_error(sprintf('The "%s" method is deprecated since knp-labs/php-github-api 2.17 and will be removed in knp-labs/php-github-api 3.0. Use the "Github\Api\Repository\Checks\CheckRuns::update" method instead.', __METHOD__), E_USER_DEPRECATED);
55+
5156
return $this->patch('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/check-runs/'.rawurlencode($checkRunId), $params);
5257
}
5358

@@ -63,6 +68,8 @@ public function update($username, $repository, $checkRunId, array $params = [])
6368
*/
6469
public function all($username, $repository, $ref, $params = [])
6570
{
71+
@trigger_error(sprintf('The "%s" method is deprecated since knp-labs/php-github-api 2.17 and will be removed in knp-labs/php-github-api 3.0. Use the "Github\Api\Repository\Checks\CheckRuns::allForReference" method instead.', __METHOD__), E_USER_DEPRECATED);
72+
6673
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/commits/'.rawurlencode($ref).'/check-runs', $params);
6774
}
6875

@@ -77,6 +84,8 @@ public function all($username, $repository, $ref, $params = [])
7784
*/
7885
public function show($username, $repository, $checkRunId)
7986
{
87+
@trigger_error(sprintf('The "%s" method is deprecated since knp-labs/php-github-api 2.17 and will be removed in knp-labs/php-github-api 3.0. Use the "Github\Api\Repository\Checks\CheckRuns::show" method instead.', __METHOD__), E_USER_DEPRECATED);
88+
8089
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/check-runs/'.rawurlencode($checkRunId));
8190
}
8291

@@ -91,6 +100,8 @@ public function show($username, $repository, $checkRunId)
91100
*/
92101
public function annotations($username, $repository, $checkRunId)
93102
{
103+
@trigger_error(sprintf('The "%s" method is deprecated since knp-labs/php-github-api 2.17 and will be removed in knp-labs/php-github-api 3.0. Use the "Github\Api\Repository\Checks\CheckRuns::annotations" method instead.', __METHOD__), E_USER_DEPRECATED);
104+
94105
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/check-runs/'.rawurlencode($checkRunId).'/annotations');
95106
}
96107
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Github\Api\Repository\Checks;
4+
5+
use Github\Api\AbstractApi;
6+
7+
/**
8+
* @link https://docs.github.com/en/free-pro-team@latest/rest/reference/checks
9+
*/
10+
class CheckRuns extends AbstractApi
11+
{
12+
/**
13+
* @link https://docs.github.com/en/free-pro-team@latest/rest/reference/checks#create-a-check-run
14+
*
15+
* @return array
16+
*/
17+
public function create(string $username, string $repository, array $params = [])
18+
{
19+
return $this->post('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/check-runs', $params);
20+
}
21+
22+
/**
23+
* @link https://docs.github.com/en/free-pro-team@latest/rest/reference/checks#get-a-check-run
24+
*
25+
* @return array
26+
*/
27+
public function show(string $username, string $repository, int $checkRunId)
28+
{
29+
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/check-runs/'.$checkRunId);
30+
}
31+
32+
/**
33+
* @link https://docs.github.com/en/free-pro-team@latest/rest/reference/checks#update-a-check-run
34+
*
35+
* @return array
36+
*/
37+
public function update(string $username, string $repository, int $checkRunId, array $params = [])
38+
{
39+
return $this->patch('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/check-runs/'.$checkRunId, $params);
40+
}
41+
42+
/**
43+
* @link https://docs.github.com/en/free-pro-team@latest/rest/reference/checks#list-check-run-annotations
44+
*
45+
* @return array
46+
*/
47+
public function annotations(string $username, string $repository, int $checkRunId)
48+
{
49+
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/check-runs/'.$checkRunId.'/annotations');
50+
}
51+
52+
/**
53+
* @link https://docs.github.com/en/free-pro-team@latest/rest/reference/checks#list-check-runs-in-a-check-suite
54+
*
55+
* @return array
56+
*/
57+
public function allForCheckSuite(string $username, string $repository, int $checkSuiteId)
58+
{
59+
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/check-suites/'.$checkSuiteId.'/check-runs');
60+
}
61+
62+
/**
63+
* @link https://docs.github.com/en/free-pro-team@latest/rest/reference/checks#list-check-runs-for-a-git-reference
64+
*
65+
* @return array
66+
*/
67+
public function allForReference(string $username, string $repository, string $ref, array $params = [])
68+
{
69+
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/commits/'.rawurlencode($ref).'/check-runs', $params);
70+
}
71+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Github\Api\Repository\Checks;
4+
5+
use Github\Api\AbstractApi;
6+
7+
/**
8+
* @link https://docs.github.com/en/free-pro-team@latest/rest/reference/checks
9+
*/
10+
class CheckSuites extends AbstractApi
11+
{
12+
/**
13+
* @link https://docs.github.com/en/free-pro-team@latest/rest/reference/checks#create-a-check-suite
14+
*
15+
* @return array
16+
*/
17+
public function create(string $username, string $repository, array $params = [])
18+
{
19+
return $this->post('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/check-suites', $params);
20+
}
21+
22+
/**
23+
* @link https://docs.github.com/en/free-pro-team@latest/rest/reference/checks#update-repository-preferences-for-check-suites
24+
*
25+
* @return array
26+
*/
27+
public function updatePreferences(string $username, string $repository, array $params = [])
28+
{
29+
return $this->patch('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/check-suites/preferences', $params);
30+
}
31+
32+
/**
33+
* @link https://docs.github.com/en/free-pro-team@latest/rest/reference/checks#get-a-check-suite
34+
*
35+
* @return array
36+
*/
37+
public function getCheckSuite(string $username, string $repository, int $checkSuiteId)
38+
{
39+
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/check-suites/'.$checkSuiteId);
40+
}
41+
42+
/**
43+
* @link https://docs.github.com/en/free-pro-team@latest/rest/reference/checks#rerequest-a-check-suite
44+
*
45+
* @return array
46+
*/
47+
public function rerequest(string $username, string $repository, int $checkSuiteId)
48+
{
49+
return $this->post('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/check-suites/'.$checkSuiteId.'/rerequest');
50+
}
51+
52+
/**
53+
* @link https://docs.github.com/en/free-pro-team@latest/rest/reference/checks#list-check-suites-for-a-git-reference
54+
*
55+
* @return array
56+
*/
57+
public function allForReference(string $username, string $repository, string $ref, array $params = [])
58+
{
59+
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/commits/'.rawurlencode($ref).'/check-suites', $params);
60+
}
61+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
namespace Github\Tests\Api\Repository\Checks;
4+
5+
use Github\Api\Repository\Checks\CheckRuns;
6+
use Github\Tests\Api\TestCase;
7+
use PHPUnit\Framework\MockObject\MockObject;
8+
9+
class CheckRunsTest extends TestCase
10+
{
11+
/**
12+
* @test
13+
*/
14+
public function shouldCreateCheck()
15+
{
16+
$expectedValue = ['state' => 'success'];
17+
$data = ['head_sha' => 'commitSHA123456', 'name' => 'my check'];
18+
19+
/** @var CheckRuns|MockObject $api */
20+
$api = $this->getApiMock();
21+
$api->expects($this->once())
22+
->method('post')
23+
->with('/repos/KnpLabs/php-github-api/check-runs', $data)
24+
->willReturn($expectedValue);
25+
26+
$this->assertEquals($expectedValue, $api->create('KnpLabs', 'php-github-api', $data));
27+
}
28+
29+
/**
30+
* @test
31+
*/
32+
public function shouldShowSingleCheckRun()
33+
{
34+
/** @var CheckRuns|MockObject $api */
35+
$api = $this->getApiMock();
36+
$api->expects($this->once())
37+
->method('get')
38+
->with('/repos/KnpLabs/php-github-api/check-runs/14');
39+
40+
$api->show('KnpLabs', 'php-github-api', 14);
41+
}
42+
43+
/**
44+
* @test
45+
*/
46+
public function shouldUpdateCheck()
47+
{
48+
$expectedValue = ['state' => 'success'];
49+
$data = ['head_sha' => 'commitSHA123456', 'name' => 'my check'];
50+
51+
/** @var CheckRuns|MockObject $api */
52+
$api = $this->getApiMock();
53+
$api->expects($this->once())
54+
->method('patch')
55+
->with('/repos/KnpLabs/php-github-api/check-runs/123', $data)
56+
->willReturn($expectedValue);
57+
58+
$this->assertEquals($expectedValue, $api->update('KnpLabs', 'php-github-api', '123', $data));
59+
}
60+
61+
/**
62+
* @test
63+
*/
64+
public function shouldListCheckRunAnnotations()
65+
{
66+
/** @var CheckRuns|MockObject $api */
67+
$api = $this->getApiMock();
68+
$api->expects($this->once())
69+
->method('get')
70+
->with('/repos/KnpLabs/php-github-api/check-runs/14/annotations');
71+
72+
$api->annotations('KnpLabs', 'php-github-api', 14);
73+
}
74+
75+
/**
76+
* @test
77+
*/
78+
public function shouldGetAllChecksForCheckSuite()
79+
{
80+
/** @var CheckRuns|MockObject $api */
81+
$api = $this->getApiMock();
82+
$api->expects($this->once())
83+
->method('get')
84+
->with('/repos/KnpLabs/php-github-api/check-suites/123/check-runs');
85+
86+
$api->allForCheckSuite('KnpLabs', 'php-github-api', 123);
87+
}
88+
89+
/**
90+
* @test
91+
*/
92+
public function shouldGetAllChecksForReference()
93+
{
94+
/** @var CheckRuns|MockObject $api */
95+
$api = $this->getApiMock();
96+
$api->expects($this->once())
97+
->method('get')
98+
->with('/repos/KnpLabs/php-github-api/commits/cb4abc15424c0015b4468d73df55efb8b60a4a3d/check-runs');
99+
100+
$api->allForReference('KnpLabs', 'php-github-api', 'cb4abc15424c0015b4468d73df55efb8b60a4a3d');
101+
}
102+
103+
protected function getApiClass(): string
104+
{
105+
return CheckRuns::class;
106+
}
107+
}

0 commit comments

Comments
 (0)