Skip to content

Commit

Permalink
Merge pull request #350 from j0k3r/fix/github-auth
Browse files Browse the repository at this point in the history
Update the way to auth on GitHub
  • Loading branch information
j0k3r authored May 14, 2020
2 parents 9d11dc3 + 7693f34 commit 4401da8
Showing 1 changed file with 34 additions and 25 deletions.
59 changes: 34 additions & 25 deletions src/AppBundle/Extractor/Github.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace AppBundle\Extractor;

use Http\Discovery\MessageFactoryDiscovery;
use Http\Message\Authentication\BasicAuth;

class Github extends AbstractExtractor
{
protected $githubClientId;
Expand Down Expand Up @@ -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 '<div><em>Pull request on Github</em>' .
Expand All @@ -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 '<div><em>Issue on Github</em>' .
Expand All @@ -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 '';
Expand Down

0 comments on commit 4401da8

Please sign in to comment.