Skip to content

Commit 5d678fd

Browse files
committed
Add support custom properties
1 parent 663af58 commit 5d678fd

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed

lib/Github/Api/Repo.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Github\Api\Repository\Comments;
1616
use Github\Api\Repository\Commits;
1717
use Github\Api\Repository\Contents;
18+
use Github\Api\Repository\CustomProperties;
1819
use Github\Api\Repository\DeployKeys;
1920
use Github\Api\Repository\Downloads;
2021
use Github\Api\Repository\Forks;
@@ -523,6 +524,18 @@ public function statuses()
523524
return new Statuses($this->getClient());
524525
}
525526

527+
/**
528+
* Manage the custom properties of a repository.
529+
*
530+
* @link https://docs.github.com/en/rest/repos/custom-properties
531+
*
532+
* @return CustomProperties
533+
*/
534+
public function customProperties()
535+
{
536+
return new CustomProperties($this->getClient());
537+
}
538+
526539
/**
527540
* Get the branch(es) of a repository.
528541
*
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Github\Api\Repository;
4+
5+
use Github\Api\AbstractApi;
6+
use Github\Exception\RuntimeException;
7+
8+
/**
9+
* @link https://docs.github.com/en/rest/repos/custom-properties
10+
*
11+
* @author Ondřej Nyklíček <[email protected]>
12+
*/
13+
class CustomProperties extends AbstractApi
14+
{
15+
/**
16+
* @param string $owner The account owner of the repository.
17+
* @param string $repository The name of the repository.
18+
* @return array|string
19+
*/
20+
public function all(string $owner, string $repository)
21+
{
22+
return $this->get('/repos/' . rawurlencode($owner) . '/' . rawurlencode($repository) . '/properties/values');
23+
}
24+
25+
/**
26+
* @param string $owner The account owner of the repository.
27+
* @param string $repository The name of the repository.
28+
* @param string $propertyName The name of the property to retrieve.
29+
* @return array
30+
* @throws RuntimeException if the property is not found.
31+
*/
32+
public function show(string $owner, string $repository, string $propertyName)
33+
{
34+
$allProperties = $this->all($owner, $repository);
35+
36+
if(!is_array($allProperties)) {
37+
throw new RuntimeException('Unexpected response from GitHub API.');
38+
}
39+
40+
foreach ($allProperties as $property => $value) {
41+
if ($property === $propertyName) {
42+
return ['property_name' => $property, 'value' => $value];
43+
}
44+
}
45+
46+
throw new RuntimeException("Property '{$propertyName}' not found.");
47+
}
48+
49+
/**
50+
* @param string $owner The account owner of the repository.
51+
* @param string $repository The name of the repository.
52+
* @param array<string, string|int> $params
53+
* @return array|string
54+
*/
55+
public function createOrUpdate(string $owner, string $repository, array $params)
56+
{
57+
return $this->patch('/repos/' . rawurlencode($owner) . '/' . rawurlencode($repository) . '/properties/values', $params);
58+
}
59+
60+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Github\Tests\Api\Repository;
4+
5+
use Github\Api\Repository\CustomProperties;
6+
use Github\Tests\Api\TestCase;
7+
8+
class CustomPropertiesTest extends TestCase
9+
{
10+
public function testAll()
11+
{
12+
$expectedArray = ['property1' => 'value1', 'property2' => 'value2'];
13+
14+
$api = $this->getApiMock();
15+
$api->expects($this->once())
16+
->method('get')
17+
->with('/repos/owner/repo/properties/values')
18+
->willReturn($expectedArray);
19+
20+
$this->assertEquals($expectedArray, $api->all('owner', 'repo'));
21+
}
22+
23+
public function testShowPropertyExists()
24+
{
25+
$allProperties = [
26+
'property1' => 'value1',
27+
'property2' => 'value2',
28+
];
29+
30+
$api = $this->getApiMock(CustomProperties::class);
31+
$api->expects($this->once())
32+
->method('get')
33+
->with('/repos/owner/repo/properties/values')
34+
->willReturn($allProperties);
35+
36+
$expectedResult = ['property_name' =>'property2', 'value' => 'value2'];
37+
$this->assertEquals($expectedResult, $api->show('owner', 'repo', 'property2'));
38+
}
39+
40+
public function testShowPropertyDoesNotExist()
41+
{
42+
$allProperties = [
43+
'property1' => 'value1',
44+
'property2' => 'value2',
45+
];
46+
47+
$api = $this->getApiMock(CustomProperties::class);
48+
$api->expects($this->once())
49+
->method('get')
50+
->with('/repos/owner/repo/properties/values')
51+
->willReturn($allProperties);
52+
53+
$this->expectException(\RuntimeException::class);
54+
$this->expectExceptionMessage("Property 'property3' not found.");
55+
56+
$api->show('owner', 'repo', 'property3');
57+
}
58+
59+
public function testCreateOrUpdate()
60+
{
61+
$params = [
62+
'property1' => 'newValue1',
63+
'property2' => 42,
64+
];
65+
66+
$expectedResponse = [
67+
'property1' => 'newValue1',
68+
'property2' => 42,
69+
];
70+
71+
$api = $this->getApiMock(CustomProperties::class);
72+
$api->expects($this->once())
73+
->method('patch')
74+
->with('/repos/owner/repo/properties/values', $params)
75+
->willReturn($expectedResponse);
76+
77+
$this->assertEquals($expectedResponse, $api->createOrUpdate('owner', 'repo', $params));
78+
}
79+
80+
protected function getApiClass()
81+
{
82+
return CustomProperties::class;
83+
}
84+
}

0 commit comments

Comments
 (0)