Skip to content

Commit

Permalink
Adding API endpoints to interact with custom properties
Browse files Browse the repository at this point in the history
  • Loading branch information
ONyklicek committed Jan 7, 2025
1 parent 5d678fd commit dcd8ded
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
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);
```
9 changes: 4 additions & 5 deletions lib/Github/Api/Repository/CustomProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function all(string $owner, string $repository)
* @return array
* @throws RuntimeException if the property is not found.
*/
public function show(string $owner, string $repository, string $propertyName)
public function show(string $owner, string $repository, string $propertyName): array
{
$allProperties = $this->all($owner, $repository);

Expand All @@ -43,18 +43,17 @@ public function show(string $owner, string $repository, string $propertyName)
}
}

throw new RuntimeException("Property '{$propertyName}' not found.");
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, string|int> $params
* @param array<string, mixed> $params
* @return array|string
*/
public function createOrUpdate(string $owner, string $repository, array $params)
public function update(string $owner, string $repository, array $params)
{
return $this->patch('/repos/' . rawurlencode($owner) . '/' . rawurlencode($repository) . '/properties/values', $params);
}

}
12 changes: 6 additions & 6 deletions test/Github/Tests/Api/Repository/CustomPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function testShowPropertyExists()
'property2' => 'value2',
];

$api = $this->getApiMock(CustomProperties::class);
$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('/repos/owner/repo/properties/values')
Expand All @@ -44,19 +44,19 @@ public function testShowPropertyDoesNotExist()
'property2' => 'value2',
];

$api = $this->getApiMock(CustomProperties::class);
$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.");
$this->expectExceptionMessage("Property [property3] not found.");

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

public function testCreateOrUpdate()
public function testUpdate()
{
$params = [
'property1' => 'newValue1',
Expand All @@ -68,13 +68,13 @@ public function testCreateOrUpdate()
'property2' => 42,
];

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

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

protected function getApiClass()
Expand Down

0 comments on commit dcd8ded

Please sign in to comment.