Skip to content
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

Adding API endpoints to interact with custom properties #1147

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
28 changes: 28 additions & 0 deletions doc/repo/custom_properties.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
## Repo / Custom Properties API
[Back to the "Repos API"](../repos.md) | [Back to the navigation](../README.md)

For extended info see the [GitHub documentation](https://docs.github.com/en/rest/reference/repos#custom-properties-for-a-repository)

### List custom properties for a repository

```php
$properties = $client->api('repo')->properties()->all('twbs', 'bootstrap');
```

### Get a custom property for a repository

```php
$property = $client->api('repo')->properties()->show('twbs', 'bootstrap', $propertyName);
```


### Update a custom property for a repository

```php
$parameters = [
'property_name' => 'foo',
'value' => 'bar'
]

$property = $client->api('repo')->properties()->update('twbs', 'bootstrap', $params);
```
13 changes: 13 additions & 0 deletions lib/Github/Api/Repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Github\Api\Repository\Comments;
use Github\Api\Repository\Commits;
use Github\Api\Repository\Contents;
use Github\Api\Repository\CustomProperties;
use Github\Api\Repository\DeployKeys;
use Github\Api\Repository\Downloads;
use Github\Api\Repository\Forks;
Expand Down Expand Up @@ -523,6 +524,18 @@ public function statuses()
return new Statuses($this->getClient());
}

/**
* Manage the custom properties of a repository.
*
* @link https://docs.github.com/en/rest/repos/custom-properties
*
* @return CustomProperties
*/
public function customProperties()
{
return new CustomProperties($this->getClient());
}

/**
* Get the branch(es) of a repository.
*
Expand Down
63 changes: 63 additions & 0 deletions lib/Github/Api/Repository/CustomProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Github\Api\Repository;

use Github\Api\AbstractApi;
use Github\Exception\RuntimeException;

/**
* @link https://docs.github.com/en/rest/repos/custom-properties
*
* @author Ondřej Nyklíček <[email protected]>
*/
class CustomProperties extends AbstractApi
{
/**
* @param string $owner The account owner of the repository.
* @param string $repository The name of the repository.
*
* @return array|string
*/
public function all(string $owner, string $repository)
{
return $this->get('/repos/'.rawurlencode($owner).'/'.rawurlencode($repository).'/properties/values');
}

/**
* @param string $owner The account owner of the repository.
* @param string $repository The name of the repository.
* @param string $propertyName The name of the property to retrieve.
*
* @throws RuntimeException if the property is not found.
*
* @return array
*/
public function show(string $owner, string $repository, string $propertyName): array
{
$allProperties = $this->all($owner, $repository);

if (!is_array($allProperties)) {
throw new RuntimeException('Unexpected response from GitHub API.');
}

foreach ($allProperties as $property => $value) {
if ($property === $propertyName) {
return ['property_name' => $property, 'value' => $value];
}
}

throw new RuntimeException("Property [$propertyName] not found.");
}

/**
* @param string $owner The account owner of the repository.
* @param string $repository The name of the repository.
* @param array<string, mixed> $params
*
* @return array|string
*/
public function update(string $owner, string $repository, array $params)
{
return $this->patch('/repos/'.rawurlencode($owner).'/'.rawurlencode($repository).'/properties/values', $params);
}
}
88 changes: 88 additions & 0 deletions test/Github/Tests/Api/Repository/CustomPropertiesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Github\Tests\Api\Repository;

use Github\Api\Repository\CustomProperties;
use Github\Tests\Api\TestCase;

class CustomPropertiesTest extends TestCase
{
public function testAll()
{
$expectedArray = ['property1' => 'value1', 'property2' => 'value2'];

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('/repos/owner/repo/properties/values')
->willReturn($expectedArray);

$this->assertEquals($expectedArray, $api->all('owner', 'repo'));
}

public function testShowPropertyExists()
{
$allProperties = [
'property1' => 'value1',
'property2' => 'value2',
];

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('/repos/owner/repo/properties/values')
->willReturn($allProperties);

$expectedResult = [
'property_name' => 'property2',
'value' => 'value2',
];

$this->assertEquals($expectedResult, $api->show('owner', 'repo', 'property2'));
}

public function testShowPropertyDoesNotExist()
{
$allProperties = [
'property1' => 'value1',
'property2' => 'value2',
];

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('/repos/owner/repo/properties/values')
->willReturn($allProperties);

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Property [property3] not found.');

$api->show('owner', 'repo', 'property3');
}

public function testUpdate()
{
$params = [
'property1' => 'newValue1',
'property2' => 42,
];

$expectedResponse = [
'property1' => 'newValue1',
'property2' => 42,
];

$api = $this->getApiMock();
$api->expects($this->once())
->method('patch')
->with('/repos/owner/repo/properties/values', $params)
->willReturn($expectedResponse);

$this->assertEquals($expectedResponse, $api->update('owner', 'repo', $params));
}

protected function getApiClass()
{
return CustomProperties::class;
}
}
Loading