Skip to content

Commit 1d4d360

Browse files
acrobatNyholm
authored andcommitted
Missing code of conduct api endpoints (#656)
1 parent 9acf27f commit 1d4d360

File tree

8 files changed

+159
-0
lines changed

8 files changed

+159
-0
lines changed

doc/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ v3 APIs:
3131
* [Timeline](issue/timeline.md)
3232
* [Meta](meta.md)
3333
* Miscellaneous
34+
* [Code of conduct](miscellaneous/codeofconduct.md)
3435
* [Emojis](miscellaneous/emojis.md)
3536
* [Gitignore](miscellaneous/gitignore.md)
3637
* [Markdown](miscellaneous/markdown.md)

doc/miscellaneous/codeofconduct.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## CodeOfConduct API
2+
[Back to the navigation](../README.md)
3+
4+
### Lists all code of conducts.
5+
6+
```php
7+
$codeOfConducts = $client->api('codeOfConduct')->all();
8+
```
9+
10+
### Get a code of conduct.
11+
12+
```php
13+
$codeOfConducts = $client->api('codeOfConduct')->show('contributor_covenant');
14+
```

doc/repos.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,9 @@ $milestones = $client->api('repo')->milestones('ornicar', 'php-github-api');
303303
```
304304

305305
Returns a list of milestones.
306+
307+
### Get the contents of a repository's code of conduct
308+
309+
```php
310+
$codeOfConduct = $client->api('repo')->codeOfConduct('ornicar', 'php-github-api');
311+
```
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Github\Api\Miscellaneous;
4+
5+
use Github\Api\AbstractApi;
6+
use Github\Api\AcceptHeaderTrait;
7+
8+
class CodeOfConduct extends AbstractApi
9+
{
10+
use AcceptHeaderTrait;
11+
12+
public function configure()
13+
{
14+
$this->acceptHeaderValue = 'application/vnd.github.scarlet-witch-preview+json';
15+
16+
return $this;
17+
}
18+
19+
/**
20+
* List all codes of conduct.
21+
*
22+
* @link https://developer.github.com/v3/codes_of_conduct/#list-all-codes-of-conduct
23+
*
24+
* @return array
25+
*/
26+
public function all()
27+
{
28+
return $this->get('/codes_of_conduct');
29+
}
30+
31+
/**
32+
* Get an individual code of conduct
33+
*
34+
* @link https://developer.github.com/v3/codes_of_conduct/#get-an-individual-code-of-conduct
35+
*
36+
* @param string $key
37+
*
38+
* @return array
39+
*/
40+
public function show($key)
41+
{
42+
return $this->get('/codes_of_conduct/' . rawurlencode($key));
43+
}
44+
}

lib/Github/Api/Repo.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
*/
2929
class Repo extends AbstractApi
3030
{
31+
use AcceptHeaderTrait;
32+
3133
/**
3234
* Search repositories by keyword.
3335
*
@@ -601,4 +603,22 @@ public function events($username, $repository, $page = 1)
601603
{
602604
return $this->get('/repos/'.rawurldecode($username).'/'.rawurldecode($repository).'/events', ['page' => $page]);
603605
}
606+
607+
/**
608+
* Get the contents of a repository's code of conduct
609+
*
610+
* @link https://developer.github.com/v3/codes_of_conduct/#get-the-contents-of-a-repositorys-code-of-conduct
611+
*
612+
* @param string $username
613+
* @param string $repository
614+
*
615+
* @return array
616+
*/
617+
public function codeOfConduct($username, $repository)
618+
{
619+
//This api is in preview mode, so set the correct accept-header
620+
$this->acceptHeaderValue = 'application/vnd.github.scarlet-witch-preview+json';
621+
622+
return $this->get('/repos/'.rawurldecode($username).'/'.rawurldecode($repository).'/community/code_of_conduct');
623+
}
604624
}

lib/Github/Client.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* @method Api\CurrentUser me()
2424
* @method Api\Enterprise ent()
2525
* @method Api\Enterprise enterprise()
26+
* @method Api\Miscellaneous\CodeOfConduct codeOfConduct()
2627
* @method Api\Miscellaneous\Emojis emojis()
2728
* @method Api\GitData git()
2829
* @method Api\GitData gitData()
@@ -169,6 +170,9 @@ public function api($name)
169170
case 'currentUser':
170171
$api = new Api\CurrentUser($this);
171172
break;
173+
case 'codeOfConduct':
174+
$api = new Api\Miscellaneous\CodeOfConduct($this);
175+
break;
172176

173177
case 'deployment':
174178
case 'deployments':
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Github\Tests\Api\Miscellaneous;
4+
5+
use Github\Api\Miscellaneous\CodeOfConduct;
6+
use Github\Tests\Api\TestCase;
7+
8+
class CodeOfConductTest extends TestCase
9+
{
10+
/**
11+
* @test
12+
*/
13+
public function shouldGetAllCodeOfConducts()
14+
{
15+
$expectedArray = [
16+
['name' => 'CoC1'],
17+
['name' => 'CoC2'],
18+
];
19+
20+
$api = $this->getApiMock();
21+
$api->expects($this->once())
22+
->method('get')
23+
->with('/codes_of_conduct')
24+
->will($this->returnValue($expectedArray));
25+
26+
$this->assertEquals($expectedArray, $api->all());
27+
}
28+
29+
/**
30+
* @test
31+
*/
32+
public function shouldGetSingleCodeOfConducts()
33+
{
34+
$expectedArray = [
35+
'name' => 'CoC',
36+
];
37+
38+
$api = $this->getApiMock();
39+
$api->expects($this->once())
40+
->method('get')
41+
->with('/codes_of_conduct/contributor_covenant')
42+
->will($this->returnValue($expectedArray));
43+
44+
$this->assertEquals($expectedArray, $api->show('contributor_covenant'));
45+
}
46+
47+
/**
48+
* @return string
49+
*/
50+
protected function getApiClass()
51+
{
52+
return CodeOfConduct::class;
53+
}
54+
}

test/Github/Tests/Api/RepoTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,22 @@ public function shouldGetRepositoryEvents()
535535
$this->assertEquals($expectedArray, $api->events('KnpLabs', 'php-github-api', 3));
536536
}
537537

538+
/**
539+
* @test
540+
*/
541+
public function shouldGetRepositoryCodeOfConduct()
542+
{
543+
$expectedArray = array('name' => 'Contributor Covenant', 'url' => 'http://...');
544+
545+
$api = $this->getApiMock();
546+
$api->expects($this->once())
547+
->method('get')
548+
->with('/repos/KnpLabs/php-github-api/community/code_of_conduct')
549+
->will($this->returnValue($expectedArray));
550+
551+
$this->assertEquals($expectedArray, $api->codeOfConduct('KnpLabs', 'php-github-api'));
552+
}
553+
538554
/**
539555
* @return string
540556
*/

0 commit comments

Comments
 (0)