From 7693f347568ff006e031f85d555c29294935a83f Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Thu, 14 May 2020 11:32:26 +0200 Subject: [PATCH] Update the way to auth on GitHub Because it's deprecated now. See https://developer.github.com/changes/2019-11-05-deprecated-passwords-and-authorizations-api/#authenticating-using-query-parameters --- src/AppBundle/Extractor/Github.php | 59 +++++++++++++++++------------- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/src/AppBundle/Extractor/Github.php b/src/AppBundle/Extractor/Github.php index 34a449e1..dadddaf2 100644 --- a/src/AppBundle/Extractor/Github.php +++ b/src/AppBundle/Extractor/Github.php @@ -2,6 +2,9 @@ namespace AppBundle\Extractor; +use Http\Discovery\MessageFactoryDiscovery; +use Http\Message\Authentication\BasicAuth; + class Github extends AbstractExtractor { protected $githubClientId; @@ -72,16 +75,20 @@ public function getContent() return ''; } + $authentication = new BasicAuth($this->githubClientId, $this->githubClientSecret); + $messageFactory = MessageFactoryDiscovery::find(); + if (null !== $this->pullNumber) { try { - $response = $this->client - ->get( - 'https://api.github.com/repos/' . $this->githubRepo . '/pulls/' . $this->pullNumber . '?client_id=' . $this->githubClientId . '&client_secret=' . $this->githubClientSecret, - [ - 'Accept' => 'application/vnd.github.v3.html+json', - 'User-Agent' => 'f43.me / Github Extractor', - ] - ); + $request = $messageFactory->createRequest( + 'GET', + 'https://api.github.com/repos/' . $this->githubRepo . '/pulls/' . $this->pullNumber, + [ + 'Accept' => 'application/vnd.github.v3.html+json', + 'User-Agent' => 'f43.me / Github Extractor', + ] + ); + $response = $this->client->sendRequest($authentication->authenticate($request)); $data = $this->jsonDecode($response); return '
Pull request on Github' . @@ -104,14 +111,15 @@ public function getContent() if (null !== $this->issueNumber) { try { - $response = $this->client - ->get( - 'https://api.github.com/repos/' . $this->githubRepo . '/issues/' . $this->issueNumber . '?client_id=' . $this->githubClientId . '&client_secret=' . $this->githubClientSecret, - [ - 'Accept' => 'application/vnd.github.v3.html+json', - 'User-Agent' => 'f43.me / Github Extractor', - ] - ); + $request = $messageFactory->createRequest( + 'GET', + 'https://api.github.com/repos/' . $this->githubRepo . '/issues/' . $this->issueNumber, + [ + 'Accept' => 'application/vnd.github.v3.html+json', + 'User-Agent' => 'f43.me / Github Extractor', + ] + ); + $response = $this->client->sendRequest($authentication->authenticate($request)); $data = $this->jsonDecode($response); return '
Issue on Github' . @@ -130,15 +138,16 @@ public function getContent() } try { - return (string) $this->client - ->get( - 'https://api.github.com/repos/' . $this->githubRepo . '/readme?client_id=' . $this->githubClientId . '&client_secret=' . $this->githubClientSecret, - [ - 'Accept' => 'application/vnd.github.v3.html', - 'User-Agent' => 'f43.me / Github Extractor', - ] - ) - ->getBody(); + $request = $messageFactory->createRequest( + 'GET', + 'https://api.github.com/repos/' . $this->githubRepo . '/readme', + [ + 'Accept' => 'application/vnd.github.v3.html+json', + 'User-Agent' => 'f43.me / Github Extractor', + ] + ); + + return (string) $this->client->sendRequest($authentication->authenticate($request))->getBody(); } catch (\Exception $e) { // Github will return a 404 if no readme are found return '';