Skip to content

Commit 8c5d3bb

Browse files
bobeaganNyholm
authored andcommitted
Add configure() support for issues (#532)
* Add configure() support for issues Borrowed from issue comments, but changed the default bodyType to "raw" since the documentation indicates that is the default when no specific media type is passed: https://developer.github.com/v3/media/#comment-body-properties * add more configure methods to address missing custom media types - also fixes the Issue/Comments default configure value - also adds support for polaris-preview in PullRequest - also adds support for squirrel-girl-preview in PullRequest/Comments - replaces switch case with in_array in Repository/Comments to standardize * switch order of configure values for PullRequest and PullRequest/Comments Users would more commonly be interested in changing the bodyType rather than apiVersion Also, added missing @param values * update configure() to be chainable per #544 * revert existing bodyTypes that were defaulting to "full" instead of "raw" Per @Nyholm "I think the best solution is to accept that we made a mistake before by keep the default body type to full for the comments API. In general we should align with the docs. Making a new major release just for this fix is not a good solution. What we should do is to create a new issue suggesting this change and add that issue in a 3.0 milestone. So in short: Just change this back to full and I'll be happy to merge."
1 parent 9b66e47 commit 8c5d3bb

File tree

7 files changed

+149
-16
lines changed

7 files changed

+149
-16
lines changed

lib/Github/Api/Gist/Comments.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,35 @@
33
namespace Github\Api\Gist;
44

55
use Github\Api\AbstractApi;
6+
use Github\Api\AcceptHeaderTrait;
67

78
/**
89
* @link https://developer.github.com/v3/gists/comments/
910
* @author Kayla Daniels <[email protected]>
1011
*/
1112
class Comments extends AbstractApi
1213
{
14+
use AcceptHeaderTrait;
15+
16+
/**
17+
* Configure the body type.
18+
*
19+
* @link https://developer.github.com/v3/gists/comments/#custom-media-types
20+
* @param string|null $bodyType
21+
*
22+
* @return self
23+
*/
24+
public function configure($bodyType = null)
25+
{
26+
if (!in_array($bodyType, array('text', 'html', 'full'))) {
27+
$bodyType = 'raw';
28+
}
29+
30+
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s+json', $this->client->getApiVersion(), $bodyType);
31+
32+
return $this;
33+
}
34+
1335
/**
1436
* Get all comments for a gist.
1537
*

lib/Github/Api/Gists.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,27 @@
1414
*/
1515
class Gists extends AbstractApi
1616
{
17+
use AcceptHeaderTrait;
18+
19+
/**
20+
* Configure the body type.
21+
*
22+
* @link https://developer.github.com/v3/gists/#custom-media-types
23+
* @param string|null $bodyType
24+
*
25+
* @return self
26+
*/
27+
public function configure($bodyType = null)
28+
{
29+
if (!in_array($bodyType, array('base64'))) {
30+
$bodyType = 'raw';
31+
}
32+
33+
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s', $this->client->getApiVersion(), $bodyType);
34+
35+
return $this;
36+
}
37+
1738
public function all($type = null)
1839
{
1940
if (!in_array($type, array('public', 'starred'))) {

lib/Github/Api/Issue.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,27 @@
1919
*/
2020
class Issue extends AbstractApi
2121
{
22+
use AcceptHeaderTrait;
23+
24+
/**
25+
* Configure the body type.
26+
*
27+
* @link https://developer.github.com/v3/issues/#custom-media-types
28+
* @param string|null $bodyType
29+
*
30+
* @return self
31+
*/
32+
public function configure($bodyType = null)
33+
{
34+
if (!in_array($bodyType, array('text', 'html', 'full'))) {
35+
$bodyType = 'raw';
36+
}
37+
38+
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s+json', $this->client->getApiVersion(), $bodyType);
39+
40+
return $this;
41+
}
42+
2243
/**
2344
* List issues by username, repo and state.
2445
*

lib/Github/Api/PullRequest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,36 @@
1515
*/
1616
class PullRequest extends AbstractApi
1717
{
18+
use AcceptHeaderTrait;
19+
20+
/**
21+
* Configure the body type.
22+
*
23+
* @link https://developer.github.com/v3/pulls/#custom-media-types
24+
* @param string|null $bodyType
25+
* @param string|null $apiVersion
26+
*
27+
* @return self
28+
*/
29+
public function configure($bodyType = null, $apiVersion = null)
30+
{
31+
if (!in_array($apiVersion, array('polaris-preview'))) {
32+
$apiVersion = $this->client->getApiVersion();
33+
}
34+
35+
if (!in_array($bodyType, array('text', 'html', 'full', 'diff', 'patch'))) {
36+
$bodyType = 'raw';
37+
}
38+
39+
if (!in_array($bodyType, array('diff', 'patch'))) {
40+
$bodyType .= '+json';
41+
}
42+
43+
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s', $this->client->getApiVersion(), $bodyType);
44+
45+
return $this;
46+
}
47+
1848
/**
1949
* Get a listing of a project's pull requests by the username, repository and (optionally) state.
2050
*

lib/Github/Api/PullRequest/Comments.php

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

55
use Github\Api\AbstractApi;
6+
use Github\Api\AcceptHeaderTrait;
67
use Github\Exception\MissingArgumentException;
78

89
/**
@@ -11,6 +12,32 @@
1112
*/
1213
class Comments extends AbstractApi
1314
{
15+
use AcceptHeaderTrait;
16+
17+
/**
18+
* Configure the body type.
19+
*
20+
* @link https://developer.github.com/v3/pulls/comments/#custom-media-types
21+
* @param string|null $bodyType
22+
* @param string|null @apiVersion
23+
*
24+
* @return self
25+
*/
26+
public function configure($bodyType = null, $apiVersion = null)
27+
{
28+
if (!in_array($apiVersion, array('squirrel-girl-preview'))) {
29+
$apiVersion = $this->client->getApiVersion();
30+
}
31+
32+
if (!in_array($bodyType, array('text', 'html', 'full'))) {
33+
$bodyType = 'raw';
34+
}
35+
36+
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s+json', $apiVersion, $bodyType);
37+
38+
return $this;
39+
}
40+
1441
public function all($username, $repository, $pullRequest = null)
1542
{
1643
if (null !== $pullRequest) {

lib/Github/Api/Repository/Comments.php

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,20 @@ class Comments extends AbstractApi
1616
use AcceptHeaderTrait;
1717

1818
/**
19+
* Configure the body type.
20+
*
21+
* @link https://developer.github.com/v3/repos/comments/#custom-media-types
1922
* @param string|null $bodyType
2023
*
2124
* @return self
2225
*/
2326
public function configure($bodyType = null)
2427
{
25-
switch ($bodyType) {
26-
case 'raw':
27-
$header = sprintf('Accept: application/vnd.github.%s.raw+json', $this->client->getApiVersion());
28-
break;
29-
30-
case 'text':
31-
$header = sprintf('Accept: application/vnd.github.%s.text+json', $this->client->getApiVersion());
32-
break;
33-
34-
case 'html':
35-
$header = sprintf('Accept: application/vnd.github.%s.html+json', $this->client->getApiVersion());
36-
break;
37-
38-
default:
39-
$header = sprintf('Accept: application/vnd.github.%s.full+json', $this->client->getApiVersion());
28+
if (!in_array($bodyType, array('raw', 'text', 'html'))) {
29+
$bodyType = 'full';
4030
}
4131

42-
$this->acceptHeaderValue = $header;
32+
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s+json', $this->client->getApiVersion(), $bodyType);
4333

4434
return $this;
4535
}

lib/Github/Api/Repository/Contents.php

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

55
use Github\Api\AbstractApi;
6+
use Github\Api\AcceptHeaderTrait;
67
use Github\Exception\InvalidArgumentException;
78
use Github\Exception\ErrorException;
89
use Github\Exception\MissingArgumentException;
@@ -14,6 +15,27 @@
1415
*/
1516
class Contents extends AbstractApi
1617
{
18+
use AcceptHeaderTrait;
19+
20+
/**
21+
* Configure the body type.
22+
*
23+
* @link https://developer.github.com/v3/repo/contents/#custom-media-types
24+
* @param string|null $bodyType
25+
*
26+
* @return self
27+
*/
28+
public function configure($bodyType = null)
29+
{
30+
if (!in_array($bodyType, array('html', 'object'))) {
31+
$bodyType = 'raw';
32+
}
33+
34+
$this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s', $this->client->getApiVersion(), $bodyType);
35+
36+
return $this;
37+
}
38+
1739
/**
1840
* Get content of README file in a repository.
1941
*

0 commit comments

Comments
 (0)