From 962bebad260f4b9ecb58c545f0a03b0e39d1c757 Mon Sep 17 00:00:00 2001 From: Bob Eagan Date: Fri, 3 Mar 2017 16:28:51 -0700 Subject: [PATCH 1/5] 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 --- lib/Github/Api/Issue.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/Github/Api/Issue.php b/lib/Github/Api/Issue.php index f1f41d92ed1..193217659c0 100644 --- a/lib/Github/Api/Issue.php +++ b/lib/Github/Api/Issue.php @@ -18,6 +18,22 @@ */ class Issue extends AbstractApi { + use AcceptHeaderTrait; + + /** + * Configure the body type. + * + * @link https://developer.github.com/v3/issues/#custom-media-types + * @param string|null $bodyType + */ + public function configure($bodyType = null) + { + if (!in_array($bodyType, array('text', 'html', 'full'))) { + $bodyType = 'raw'; + } + $this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s+json', $this->client->getApiVersion(), $bodyType); + } + /** * List issues by username, repo and state. * From 8380ce4b6c9c383d07b5344e34ac712aaf0140b9 Mon Sep 17 00:00:00 2001 From: Bob Eagan Date: Mon, 6 Mar 2017 11:10:03 -0700 Subject: [PATCH 2/5] 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 --- lib/Github/Api/Gist/Comments.php | 18 ++++++++++++++++++ lib/Github/Api/Gists.php | 17 +++++++++++++++++ lib/Github/Api/Issue.php | 1 + lib/Github/Api/Issue/Comments.php | 4 ++-- lib/Github/Api/PullRequest.php | 25 +++++++++++++++++++++++++ lib/Github/Api/PullRequest/Comments.php | 22 ++++++++++++++++++++++ lib/Github/Api/Repository/Comments.php | 25 +++++++++---------------- lib/Github/Api/Repository/Contents.php | 18 ++++++++++++++++++ 8 files changed, 112 insertions(+), 18 deletions(-) diff --git a/lib/Github/Api/Gist/Comments.php b/lib/Github/Api/Gist/Comments.php index 521e4d03bb4..4160cbdef4b 100644 --- a/lib/Github/Api/Gist/Comments.php +++ b/lib/Github/Api/Gist/Comments.php @@ -3,6 +3,7 @@ namespace Github\Api\Gist; use Github\Api\AbstractApi; +use Github\Api\AcceptHeaderTrait; /** * @link https://developer.github.com/v3/gists/comments/ @@ -10,6 +11,23 @@ */ class Comments extends AbstractApi { + use AcceptHeaderTrait; + + /** + * Configure the body type. + * + * @link https://developer.github.com/v3/gists/comments/#custom-media-types + * @param string|null $bodyType + */ + public function configure($bodyType = null) + { + if (!in_array($bodyType, array('text', 'html', 'full'))) { + $bodyType = 'raw'; + } + + $this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s+json', $this->client->getApiVersion(), $bodyType); + } + /** * Get all comments for a gist. * diff --git a/lib/Github/Api/Gists.php b/lib/Github/Api/Gists.php index 9710e6de683..9d4c360def7 100644 --- a/lib/Github/Api/Gists.php +++ b/lib/Github/Api/Gists.php @@ -14,6 +14,23 @@ */ class Gists extends AbstractApi { + use AcceptHeaderTrait; + + /** + * Configure the body type. + * + * @link https://developer.github.com/v3/gists/#custom-media-types + * @param string|null $bodyType + */ + public function configure($bodyType = null) + { + if (!in_array($bodyType, array('base64'))) { + $bodyType = 'raw'; + } + + $this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s', $this->client->getApiVersion(), $bodyType); + } + public function all($type = null) { if (!in_array($type, array('public', 'starred'))) { diff --git a/lib/Github/Api/Issue.php b/lib/Github/Api/Issue.php index 193217659c0..fd138a52f3f 100644 --- a/lib/Github/Api/Issue.php +++ b/lib/Github/Api/Issue.php @@ -31,6 +31,7 @@ public function configure($bodyType = null) if (!in_array($bodyType, array('text', 'html', 'full'))) { $bodyType = 'raw'; } + $this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s+json', $this->client->getApiVersion(), $bodyType); } diff --git a/lib/Github/Api/Issue/Comments.php b/lib/Github/Api/Issue/Comments.php index e72b964f02e..c8317478155 100644 --- a/lib/Github/Api/Issue/Comments.php +++ b/lib/Github/Api/Issue/Comments.php @@ -23,8 +23,8 @@ class Comments extends AbstractApi */ public function configure($bodyType = null) { - if (!in_array($bodyType, array('raw', 'text', 'html'))) { - $bodyType = 'full'; + if (!in_array($bodyType, array('text', 'html', 'full'))) { + $bodyType = 'raw'; } $this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s+json', $this->client->getApiVersion(), $bodyType); diff --git a/lib/Github/Api/PullRequest.php b/lib/Github/Api/PullRequest.php index 52cc77fe348..f820a2e334c 100644 --- a/lib/Github/Api/PullRequest.php +++ b/lib/Github/Api/PullRequest.php @@ -14,6 +14,31 @@ */ class PullRequest extends AbstractApi { + use AcceptHeaderTrait; + + /** + * Configure the body type. + * + * @link https://developer.github.com/v3/pulls/#custom-media-types + * @param string|null $bodyType + */ + public function configure($apiVersion = null, $bodyType = null) + { + if (!in_array($apiVersion, array('polaris-preview'))) { + $apiVersion = $this->client->getApiVersion(); + } + + if (!in_array($bodyType, array('text', 'html', 'full', 'diff', 'patch'))) { + $bodyType = 'raw'; + } + + if (!in_array($bodyType, array('diff', 'patch'))) { + $bodyType .= '+json'; + } + + $this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s', $this->client->getApiVersion(), $bodyType); + } + /** * Get a listing of a project's pull requests by the username, repository and (optionally) state. * diff --git a/lib/Github/Api/PullRequest/Comments.php b/lib/Github/Api/PullRequest/Comments.php index b2de809ed7a..9e2985ba032 100644 --- a/lib/Github/Api/PullRequest/Comments.php +++ b/lib/Github/Api/PullRequest/Comments.php @@ -3,6 +3,7 @@ namespace Github\Api\PullRequest; use Github\Api\AbstractApi; +use Github\Api\AcceptHeaderTrait; use Github\Exception\MissingArgumentException; /** @@ -11,6 +12,27 @@ */ class Comments extends AbstractApi { + use AcceptHeaderTrait; + + /** + * Configure the body type. + * + * @link https://developer.github.com/v3/pulls/comments/#custom-media-types + * @param string|null $bodyType + */ + public function configure($apiVersion = null, $bodyType = null) + { + if (!in_array($apiVersion, array('squirrel-girl-preview'))) { + $apiVersion = $this->client->getApiVersion(); + } + + if (!in_array($bodyType, array('text', 'html', 'full'))) { + $bodyType = 'raw'; + } + + $this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s+json', $apiVersion, $bodyType); + } + public function all($username, $repository, $pullRequest = null) { if (null !== $pullRequest) { diff --git a/lib/Github/Api/Repository/Comments.php b/lib/Github/Api/Repository/Comments.php index b1d9abe7cbe..f8f0516ea2b 100644 --- a/lib/Github/Api/Repository/Comments.php +++ b/lib/Github/Api/Repository/Comments.php @@ -15,26 +15,19 @@ class Comments extends AbstractApi { use AcceptHeaderTrait; + /** + * Configure the body type. + * + * @link https://developer.github.com/v3/repos/comments/#custom-media-types + * @param string|null $bodyType + */ public function configure($bodyType = null) { - switch ($bodyType) { - case 'raw': - $header = sprintf('Accept: application/vnd.github.%s.raw+json', $this->client->getApiVersion()); - break; - - case 'text': - $header = sprintf('Accept: application/vnd.github.%s.text+json', $this->client->getApiVersion()); - break; - - case 'html': - $header = sprintf('Accept: application/vnd.github.%s.html+json', $this->client->getApiVersion()); - break; - - default: - $header = sprintf('Accept: application/vnd.github.%s.full+json', $this->client->getApiVersion()); + if (!in_array($bodyType, array('text', 'html', 'full'))) { + $bodyType = 'raw'; } - $this->acceptHeaderValue = $header; + $this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s+json', $this->client->getApiVersion(), $bodyType); } public function all($username, $repository, $sha = null) diff --git a/lib/Github/Api/Repository/Contents.php b/lib/Github/Api/Repository/Contents.php index 064f68490ff..2f6b6658d1f 100644 --- a/lib/Github/Api/Repository/Contents.php +++ b/lib/Github/Api/Repository/Contents.php @@ -3,6 +3,7 @@ namespace Github\Api\Repository; use Github\Api\AbstractApi; +use Github\Api\AcceptHeaderTrait; use Github\Exception\InvalidArgumentException; use Github\Exception\ErrorException; use Github\Exception\MissingArgumentException; @@ -14,6 +15,23 @@ */ class Contents extends AbstractApi { + use AcceptHeaderTrait; + + /** + * Configure the body type. + * + * @link https://developer.github.com/v3/repo/contents/#custom-media-types + * @param string|null $bodyType + */ + public function configure($bodyType = null) + { + if (!in_array($bodyType, array('html', 'object'))) { + $bodyType = 'raw'; + } + + $this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s', $this->client->getApiVersion(), $bodyType); + } + /** * Get content of README file in a repository. * From a45ce63538e4b90c733da36fc24433d9861755ef Mon Sep 17 00:00:00 2001 From: Bob Eagan Date: Thu, 23 Mar 2017 09:03:23 -0700 Subject: [PATCH 3/5] 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 --- lib/Github/Api/PullRequest.php | 3 ++- lib/Github/Api/PullRequest/Comments.php | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/Github/Api/PullRequest.php b/lib/Github/Api/PullRequest.php index f820a2e334c..29e07f58302 100644 --- a/lib/Github/Api/PullRequest.php +++ b/lib/Github/Api/PullRequest.php @@ -21,8 +21,9 @@ class PullRequest extends AbstractApi * * @link https://developer.github.com/v3/pulls/#custom-media-types * @param string|null $bodyType + * @param string|null $apiVersion */ - public function configure($apiVersion = null, $bodyType = null) + public function configure($bodyType = null, $apiVersion = null) { if (!in_array($apiVersion, array('polaris-preview'))) { $apiVersion = $this->client->getApiVersion(); diff --git a/lib/Github/Api/PullRequest/Comments.php b/lib/Github/Api/PullRequest/Comments.php index 9e2985ba032..feaa4e1c1a5 100644 --- a/lib/Github/Api/PullRequest/Comments.php +++ b/lib/Github/Api/PullRequest/Comments.php @@ -19,8 +19,9 @@ class Comments extends AbstractApi * * @link https://developer.github.com/v3/pulls/comments/#custom-media-types * @param string|null $bodyType + * @param string|null @apiVersion */ - public function configure($apiVersion = null, $bodyType = null) + public function configure($bodyType = null, $apiVersion = null) { if (!in_array($apiVersion, array('squirrel-girl-preview'))) { $apiVersion = $this->client->getApiVersion(); From 07cb9e4fae9e09f7079ef8e2730e35de5f3c698c Mon Sep 17 00:00:00 2001 From: Bob Eagan Date: Mon, 27 Mar 2017 10:01:54 -0700 Subject: [PATCH 4/5] update configure() to be chainable per #544 --- lib/Github/Api/Gist/Comments.php | 4 ++++ lib/Github/Api/Gists.php | 4 ++++ lib/Github/Api/Issue.php | 4 ++++ lib/Github/Api/PullRequest.php | 4 ++++ lib/Github/Api/PullRequest/Comments.php | 4 ++++ lib/Github/Api/Repository/Contents.php | 4 ++++ 6 files changed, 24 insertions(+) diff --git a/lib/Github/Api/Gist/Comments.php b/lib/Github/Api/Gist/Comments.php index 4160cbdef4b..e77428c1a53 100644 --- a/lib/Github/Api/Gist/Comments.php +++ b/lib/Github/Api/Gist/Comments.php @@ -18,6 +18,8 @@ class Comments extends AbstractApi * * @link https://developer.github.com/v3/gists/comments/#custom-media-types * @param string|null $bodyType + * + * @return self */ public function configure($bodyType = null) { @@ -26,6 +28,8 @@ public function configure($bodyType = null) } $this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s+json', $this->client->getApiVersion(), $bodyType); + + return $this; } /** diff --git a/lib/Github/Api/Gists.php b/lib/Github/Api/Gists.php index 9d4c360def7..c661dfebbc2 100644 --- a/lib/Github/Api/Gists.php +++ b/lib/Github/Api/Gists.php @@ -21,6 +21,8 @@ class Gists extends AbstractApi * * @link https://developer.github.com/v3/gists/#custom-media-types * @param string|null $bodyType + * + * @return self */ public function configure($bodyType = null) { @@ -29,6 +31,8 @@ public function configure($bodyType = null) } $this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s', $this->client->getApiVersion(), $bodyType); + + return $this; } public function all($type = null) diff --git a/lib/Github/Api/Issue.php b/lib/Github/Api/Issue.php index 9b01cc505a0..fa79ffa63cc 100644 --- a/lib/Github/Api/Issue.php +++ b/lib/Github/Api/Issue.php @@ -26,6 +26,8 @@ class Issue extends AbstractApi * * @link https://developer.github.com/v3/issues/#custom-media-types * @param string|null $bodyType + * + * @return self */ public function configure($bodyType = null) { @@ -34,6 +36,8 @@ public function configure($bodyType = null) } $this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s+json', $this->client->getApiVersion(), $bodyType); + + return $this; } /** diff --git a/lib/Github/Api/PullRequest.php b/lib/Github/Api/PullRequest.php index 95673e05633..a47509197c3 100644 --- a/lib/Github/Api/PullRequest.php +++ b/lib/Github/Api/PullRequest.php @@ -23,6 +23,8 @@ class PullRequest extends AbstractApi * @link https://developer.github.com/v3/pulls/#custom-media-types * @param string|null $bodyType * @param string|null $apiVersion + * + * @return self */ public function configure($bodyType = null, $apiVersion = null) { @@ -39,6 +41,8 @@ public function configure($bodyType = null, $apiVersion = null) } $this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s', $this->client->getApiVersion(), $bodyType); + + return $this; } /** diff --git a/lib/Github/Api/PullRequest/Comments.php b/lib/Github/Api/PullRequest/Comments.php index feaa4e1c1a5..161c629909e 100644 --- a/lib/Github/Api/PullRequest/Comments.php +++ b/lib/Github/Api/PullRequest/Comments.php @@ -20,6 +20,8 @@ class Comments extends AbstractApi * @link https://developer.github.com/v3/pulls/comments/#custom-media-types * @param string|null $bodyType * @param string|null @apiVersion + * + * @return self */ public function configure($bodyType = null, $apiVersion = null) { @@ -32,6 +34,8 @@ public function configure($bodyType = null, $apiVersion = null) } $this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s+json', $apiVersion, $bodyType); + + return $this; } public function all($username, $repository, $pullRequest = null) diff --git a/lib/Github/Api/Repository/Contents.php b/lib/Github/Api/Repository/Contents.php index 2f6b6658d1f..387413bb053 100644 --- a/lib/Github/Api/Repository/Contents.php +++ b/lib/Github/Api/Repository/Contents.php @@ -22,6 +22,8 @@ class Contents extends AbstractApi * * @link https://developer.github.com/v3/repo/contents/#custom-media-types * @param string|null $bodyType + * + * @return self */ public function configure($bodyType = null) { @@ -30,6 +32,8 @@ public function configure($bodyType = null) } $this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s', $this->client->getApiVersion(), $bodyType); + + return $this; } /** From f2107373c6f17e9ce6f9262bea8b908ca26ef36c Mon Sep 17 00:00:00 2001 From: Bob Eagan Date: Mon, 27 Mar 2017 10:10:53 -0700 Subject: [PATCH 5/5] 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." --- lib/Github/Api/Issue/Comments.php | 4 ++-- lib/Github/Api/Repository/Comments.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/Github/Api/Issue/Comments.php b/lib/Github/Api/Issue/Comments.php index 410b788e388..c860c74d64e 100644 --- a/lib/Github/Api/Issue/Comments.php +++ b/lib/Github/Api/Issue/Comments.php @@ -25,8 +25,8 @@ class Comments extends AbstractApi */ public function configure($bodyType = null) { - if (!in_array($bodyType, array('text', 'html', 'full'))) { - $bodyType = 'raw'; + if (!in_array($bodyType, array('raw', 'text', 'html'))) { + $bodyType = 'full'; } $this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s+json', $this->client->getApiVersion(), $bodyType); diff --git a/lib/Github/Api/Repository/Comments.php b/lib/Github/Api/Repository/Comments.php index aa5a79e17f6..4e993720296 100644 --- a/lib/Github/Api/Repository/Comments.php +++ b/lib/Github/Api/Repository/Comments.php @@ -25,8 +25,8 @@ class Comments extends AbstractApi */ public function configure($bodyType = null) { - if (!in_array($bodyType, array('text', 'html', 'full'))) { - $bodyType = 'raw'; + if (!in_array($bodyType, array('raw', 'text', 'html'))) { + $bodyType = 'full'; } $this->acceptHeaderValue = sprintf('application/vnd.github.%s.%s+json', $this->client->getApiVersion(), $bodyType);