From fe50ad19e00625dc55abea9db729950efbdf4d67 Mon Sep 17 00:00:00 2001 From: Nick Poulos Date: Wed, 13 May 2020 21:02:27 -0400 Subject: [PATCH 1/9] Add matchingBranch() and matchingTags() Methods To References.php Added two new methods to GitData->References to cover the matching reference endpoints described here: https://developer.github.com/v3/git/refs/#list-matching-references --- lib/Github/Api/GitData/References.php | 28 +++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lib/Github/Api/GitData/References.php b/lib/Github/Api/GitData/References.php index c54c0c8aad0..13d77b59b3c 100644 --- a/lib/Github/Api/GitData/References.php +++ b/lib/Github/Api/GitData/References.php @@ -25,6 +25,34 @@ public function all($username, $repository) return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/git/refs'); } + /** + * Get all matching references for a particular branch of a repository. + * + * @param string $username + * @param string $repository + * @param string $branch + * + * @return array + */ + public function matchingBranch($username, $repository, $branch) + { + return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/git/matching-refs/heads/'.$branch); + } + + /** + * Get all matching references for a particular branch of a repository. + * + * @param string $username + * @param string $repository + * @param string $tag + * + * @return array + */ + public function matchingTag($username, $repository, $tag) + { + return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/git/matching-refs/tags/'.$tag); + } + /** * Get all branches of a repository. * From 791f5631a087356309e720c3911a49ce372cc380 Mon Sep 17 00:00:00 2001 From: Nick Poulos Date: Wed, 13 May 2020 21:04:43 -0400 Subject: [PATCH 2/9] Add New Tests In ReferencesTest For Our Two New Methods Add New Tests In ReferencesTest For Our Two New Methods --- .../Tests/Api/GitData/ReferencesTest.php | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/Github/Tests/Api/GitData/ReferencesTest.php b/test/Github/Tests/Api/GitData/ReferencesTest.php index 768085f69bf..49b6bcb6e60 100644 --- a/test/Github/Tests/Api/GitData/ReferencesTest.php +++ b/test/Github/Tests/Api/GitData/ReferencesTest.php @@ -71,6 +71,38 @@ public function shouldGetAllRepoReferences() $this->assertEquals($expectedValue, $api->all('l3l0', 'l3l0repo')); } + /** + * @test + */ + public function shouldGetAllMatchingBranchRepoReferences() + { + $expectedValue = [['reference' => 'some data']]; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('/repos/l3l0/l3l0repo/git/matching-refs/heads/branchName') + ->will($this->returnValue($expectedValue)); + + $this->assertEquals($expectedValue, $api->matchingBranch('l3l0', 'l3l0repo', 'branchName')); + } + + /** + * @test + */ + public function shouldGetAllMatchingTagRepoReferences() + { + $expectedValue = [['reference' => 'some data']]; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('/repos/l3l0/l3l0repo/git/matching-refs/tags/tagName') + ->will($this->returnValue($expectedValue)); + + $this->assertEquals($expectedValue, $api->matchingTag('l3l0', 'l3l0repo', 'tagName')); + } + /** * @test */ From f9f39afd69987ddc06936ef6853508f0c3e986b0 Mon Sep 17 00:00:00 2001 From: Nick Poulos Date: Fri, 15 May 2020 17:42:03 -0400 Subject: [PATCH 3/9] Apply Requested Fixes - Add rawurlencode around both the $branch and $tag parameters in matchingBranch() and matchingTag() respectively. - Add parameter type hints, and function return types for both marchingBranch() and matchingTag() --- lib/Github/Api/GitData/References.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/Github/Api/GitData/References.php b/lib/Github/Api/GitData/References.php index 13d77b59b3c..17b88ff04dc 100644 --- a/lib/Github/Api/GitData/References.php +++ b/lib/Github/Api/GitData/References.php @@ -34,9 +34,9 @@ public function all($username, $repository) * * @return array */ - public function matchingBranch($username, $repository, $branch) + public function matchingBranch(string $username, string $repository, string $branch): array { - return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/git/matching-refs/heads/'.$branch); + return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/git/matching-refs/heads/'.rawurlencode($branch)); } /** @@ -48,9 +48,9 @@ public function matchingBranch($username, $repository, $branch) * * @return array */ - public function matchingTag($username, $repository, $tag) + public function matchingTag(string $username, string $repository, string $tag): array { - return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/git/matching-refs/tags/'.$tag); + return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/git/matching-refs/tags/'.rawurlencode($tag)); } /** From 8107e1366fe4cb197515ee0200dfaa8d1fb1b7e8 Mon Sep 17 00:00:00 2001 From: Nick Poulos Date: Sun, 17 May 2020 02:18:11 -0400 Subject: [PATCH 4/9] Add Documentation For method matching() in gitdata/references.md Add Documentation For method matching() in gitdata/references.md --- doc/gitdata/references.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/doc/gitdata/references.md b/doc/gitdata/references.md index cc4ee25eb7c..8cc2b971e7e 100644 --- a/doc/gitdata/references.md +++ b/doc/gitdata/references.md @@ -7,6 +7,11 @@ $references = $client->api('gitData')->references()->all('KnpLabs', 'php-github-api'); ``` +### List Matching references +```php +$references = $client->api('gitData')->references()->matching('KnpLabs', 'php-github-api', 'heads/branchName'); // use 'tags/tagName' for third argument if ref is tag +``` + ### Show a reference ```php @@ -41,4 +46,4 @@ $references = $client->api('gitData')->references()->branches('KnpLabs', 'php-gi ### List all tags ```php $references = $client->api('gitData')->references()->tags('KnpLabs', 'php-github-api'); -``` \ No newline at end of file +``` From 7e5dfc9e305184c79bdeb6153bbd3654343f3a60 Mon Sep 17 00:00:00 2001 From: Nick Poulos Date: Sun, 17 May 2020 02:19:52 -0400 Subject: [PATCH 5/9] Refactor Dual Methods To Single, and Fix Test - In order to match existing API conventions, the two method solution for branch/tag was consolidated into one method and extra data in the third argument. - Updated ReferencesTest.php to only test the single method --- lib/Github/Api/GitData/References.php | 22 +++++-------------- .../Tests/Api/GitData/ReferencesTest.php | 22 +++---------------- 2 files changed, 8 insertions(+), 36 deletions(-) diff --git a/lib/Github/Api/GitData/References.php b/lib/Github/Api/GitData/References.php index 17b88ff04dc..e145a367e93 100644 --- a/lib/Github/Api/GitData/References.php +++ b/lib/Github/Api/GitData/References.php @@ -26,31 +26,19 @@ public function all($username, $repository) } /** - * Get all matching references for a particular branch of a repository. + * Get all matching references for the supplied reference name * - * @param string $username - * @param string $repository - * @param string $branch - * - * @return array - */ - public function matchingBranch(string $username, string $repository, string $branch): array - { - return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/git/matching-refs/heads/'.rawurlencode($branch)); - } - - /** - * Get all matching references for a particular branch of a repository. + * https://developer.github.com/v3/git/refs/#list-matching-references * * @param string $username * @param string $repository - * @param string $tag + * @param string $reference * * @return array */ - public function matchingTag(string $username, string $repository, string $tag): array + public function matching(string $username, string $repository, string $reference): array { - return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/git/matching-refs/tags/'.rawurlencode($tag)); + return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/git/matching-refs/'.rawurlencode($reference)); } /** diff --git a/test/Github/Tests/Api/GitData/ReferencesTest.php b/test/Github/Tests/Api/GitData/ReferencesTest.php index 49b6bcb6e60..60ace6fe9cf 100644 --- a/test/Github/Tests/Api/GitData/ReferencesTest.php +++ b/test/Github/Tests/Api/GitData/ReferencesTest.php @@ -74,33 +74,17 @@ public function shouldGetAllRepoReferences() /** * @test */ - public function shouldGetAllMatchingBranchRepoReferences() + public function shouldGetAllMatchingReferences() { $expectedValue = [['reference' => 'some data']]; $api = $this->getApiMock(); $api->expects($this->once()) ->method('get') - ->with('/repos/l3l0/l3l0repo/git/matching-refs/heads/branchName') + ->with('/repos/l3l0/l3l0repo/git/matching-refs/refName') ->will($this->returnValue($expectedValue)); - $this->assertEquals($expectedValue, $api->matchingBranch('l3l0', 'l3l0repo', 'branchName')); - } - - /** - * @test - */ - public function shouldGetAllMatchingTagRepoReferences() - { - $expectedValue = [['reference' => 'some data']]; - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('get') - ->with('/repos/l3l0/l3l0repo/git/matching-refs/tags/tagName') - ->will($this->returnValue($expectedValue)); - - $this->assertEquals($expectedValue, $api->matchingTag('l3l0', 'l3l0repo', 'tagName')); + $this->assertEquals($expectedValue, $api->matching('l3l0', 'l3l0repo', 'refName')); } /** From 61fe18b28a85d6ed72ecf6e22a3f08b51d41bad9 Mon Sep 17 00:00:00 2001 From: Nick Poulos Date: Sun, 17 May 2020 02:22:38 -0400 Subject: [PATCH 6/9] Remove link from DocBlock Remove link from DocBlock --- lib/Github/Api/GitData/References.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/Github/Api/GitData/References.php b/lib/Github/Api/GitData/References.php index e145a367e93..b252dfdb654 100644 --- a/lib/Github/Api/GitData/References.php +++ b/lib/Github/Api/GitData/References.php @@ -28,8 +28,6 @@ public function all($username, $repository) /** * Get all matching references for the supplied reference name * - * https://developer.github.com/v3/git/refs/#list-matching-references - * * @param string $username * @param string $repository * @param string $reference From 2856710dd6de5aa07e990752324179cee3e8485e Mon Sep 17 00:00:00 2001 From: Nick Poulos Date: Sun, 17 May 2020 13:17:13 -0400 Subject: [PATCH 7/9] Add $this->encodeReference() call Inside matching() - reference parameters for the API can contain a '/', luckily we already have a method to handle these cases --- lib/Github/Api/GitData/References.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/Github/Api/GitData/References.php b/lib/Github/Api/GitData/References.php index b252dfdb654..6c59cddbafb 100644 --- a/lib/Github/Api/GitData/References.php +++ b/lib/Github/Api/GitData/References.php @@ -36,7 +36,9 @@ public function all($username, $repository) */ public function matching(string $username, string $repository, string $reference): array { - return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/git/matching-refs/'.rawurlencode($reference)); + $reference = $this->encodeReference($reference); + + return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/git/matching-refs/'.$reference); } /** From 1fc94710eed57e143c9f2416505bc1e253fbdae9 Mon Sep 17 00:00:00 2001 From: Nick Poulos Date: Sun, 17 May 2020 13:29:32 -0400 Subject: [PATCH 8/9] Update ReferencesTest.php To Use A More Realistic Parameter Update ReferencesTest.php To Use A More Realistic Parameter --- test/Github/Tests/Api/GitData/ReferencesTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/Github/Tests/Api/GitData/ReferencesTest.php b/test/Github/Tests/Api/GitData/ReferencesTest.php index 60ace6fe9cf..70f17cde1d8 100644 --- a/test/Github/Tests/Api/GitData/ReferencesTest.php +++ b/test/Github/Tests/Api/GitData/ReferencesTest.php @@ -81,10 +81,10 @@ public function shouldGetAllMatchingReferences() $api = $this->getApiMock(); $api->expects($this->once()) ->method('get') - ->with('/repos/l3l0/l3l0repo/git/matching-refs/refName') + ->with('/repos/l3l0/l3l0repo/git/matching-refs/heads/refName') ->will($this->returnValue($expectedValue)); - $this->assertEquals($expectedValue, $api->matching('l3l0', 'l3l0repo', 'refName')); + $this->assertEquals($expectedValue, $api->matching('l3l0', 'l3l0repo', 'heads/refName')); } /** From d63d9a16f2e67ad867633d849b672bd7e9579f6e Mon Sep 17 00:00:00 2001 From: Nick Poulos Date: Sun, 17 May 2020 21:53:23 -0400 Subject: [PATCH 9/9] StyleCI Add Period StyleCI Add Period --- lib/Github/Api/GitData/References.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Github/Api/GitData/References.php b/lib/Github/Api/GitData/References.php index 6c59cddbafb..d67cbe512b9 100644 --- a/lib/Github/Api/GitData/References.php +++ b/lib/Github/Api/GitData/References.php @@ -26,7 +26,7 @@ public function all($username, $repository) } /** - * Get all matching references for the supplied reference name + * Get all matching references for the supplied reference name. * * @param string $username * @param string $repository