Skip to content

Commit

Permalink
Merge pull request #54 from jwage/github-auth
Browse files Browse the repository at this point in the history
Add support for authentication when making requests to GitHub API.
  • Loading branch information
jwage authored Apr 25, 2019
2 parents d4f713f + b91e8d8 commit 5897452
Show file tree
Hide file tree
Showing 11 changed files with 302 additions and 3 deletions.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,49 @@ return [
,
];
```

## GitHub Authentication

By default it is not required to authenticate with GitHub to use this tool. But if you want
higher rate limits or want to use it with private repositories then you will need to authenticate.

### Personal Credentials

You can authenticate with your `username` and `password` or a personal access token
instead of your password using the `ChangelogGenerator\GitHubUsernamePassword` class:

```php
<?php

declare(strict_types=1);

use ChangelogGenerator\ChangelogConfig;
use ChangelogGenerator\GitHubUsernamePassword;

return [
'changelog-generator' => (new ChangelogConfig())
// ...
->setGitHubCredentials(new GitHubUsernamePassword('username', 'passwordOrToken'))
,
];
```

### OAuth Token

You can authenticate with an OAuth token as well using the `ChangelogGenerator\GitHubOAuthToken` class:

```php
<?php

declare(strict_types=1);

use ChangelogGenerator\ChangelogConfig;
use ChangelogGenerator\GitHubOAuthToken;

return [
'changelog-generator' => (new ChangelogConfig())
// ...
->setGitHubCredentials(new GitHubOAuthToken('the oauth token'))
,
];
```
15 changes: 15 additions & 0 deletions src/ChangelogGenerator/ChangelogConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class ChangelogConfig
/** @var bool */
private $showContributors = false;

/** @var GitHubCredentials|null */
private $gitHubCredentials;

/** @var mixed[] */
private $options = ['rootGitHubUrl' => self::DEFAULT_ROOT_GITHUB_URL];

Expand Down Expand Up @@ -132,6 +135,18 @@ public function setShowContributors(bool $showContributors) : self
return $this;
}

public function getGitHubCredentials() : ?GitHubCredentials
{
return $this->gitHubCredentials;
}

public function setGitHubCredentials(GitHubCredentials $gitHubCredentials) : self
{
$this->gitHubCredentials = $gitHubCredentials;

return $this;
}

/**
* @return mixed[]
*/
Expand Down
10 changes: 10 additions & 0 deletions src/ChangelogGenerator/GitHubCredentials.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace ChangelogGenerator;

interface GitHubCredentials
{
public function getAuthorizationHeader() : string;
}
23 changes: 23 additions & 0 deletions src/ChangelogGenerator/GitHubOAuthToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace ChangelogGenerator;

use function sprintf;

final class GitHubOAuthToken implements GitHubCredentials
{
/** @var string */
private $oAuthToken;

public function __construct(string $oAuthToken)
{
$this->oAuthToken = $oAuthToken;
}

public function getAuthorizationHeader() : string
{
return sprintf('token %s', $this->oAuthToken);
}
}
28 changes: 28 additions & 0 deletions src/ChangelogGenerator/GitHubUsernamePassword.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace ChangelogGenerator;

use function base64_encode;
use function sprintf;

final class GitHubUsernamePassword implements GitHubCredentials
{
/** @var string */
private $username;

/** @var string */
private $passwordOrToken;

public function __construct(string $username, string $passwordOrToken)
{
$this->username = $username;
$this->passwordOrToken = $passwordOrToken;
}

public function getAuthorizationHeader() : string
{
return sprintf('Basic %s', base64_encode(sprintf('%s:%s', $this->username, $this->passwordOrToken)));
}
}
25 changes: 23 additions & 2 deletions src/ChangelogGenerator/IssueClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use RuntimeException;
use function json_decode;
use function preg_match;
use function sprintf;

class IssueClient
{
Expand All @@ -26,12 +28,21 @@ public function __construct(
$this->client = $client;
}

public function execute(string $url) : IssueClientResponse
{
public function execute(
string $url,
?GitHubCredentials $gitHubCredentials = null
) : IssueClientResponse {
$request = $this->messageFactory
->createRequest('GET', $url)
->withAddedHeader('User-Agent', 'jwage/changelog-generator');

if ($gitHubCredentials !== null) {
$request = $request->withAddedHeader(
'Authorization',
$gitHubCredentials->getAuthorizationHeader()
);
}

$response = $this->client->sendRequest($request);

$responseBody = $response
Expand All @@ -40,6 +51,16 @@ public function execute(string $url) : IssueClientResponse

$body = json_decode($responseBody, true);

$statusCode = $response->getStatusCode();

if ($statusCode !== 200) {
throw new RuntimeException(sprintf(
'API call to GitHub failed with status code %d%s',
$statusCode,
isset($body['message']) ? sprintf(' and message "%s"', $body['message']) : ''
));
}

return new IssueClientResponse($body, $this->getNextUrl($response));
}

Expand Down
2 changes: 1 addition & 1 deletion src/ChangelogGenerator/IssueFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function fetchMilestoneIssues(ChangelogConfig $changelogConfig) : array
$url = $changelogConfig->getMilestoneIssuesUrl($label);

while (true) {
$response = $this->issueClient->execute($url);
$response = $this->issueClient->execute($url, $changelogConfig->getGitHubCredentials());

$body = $response->getBody();

Expand Down
16 changes: 16 additions & 0 deletions tests/ChangelogGenerator/Tests/ChangelogConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace ChangelogGenerator\Tests;

use ChangelogGenerator\ChangelogConfig;
use ChangelogGenerator\GitHubCredentials;
use ChangelogGenerator\GitHubUsernamePassword;
use PHPUnit\Framework\TestCase;

final class ChangelogConfigTest extends TestCase
Expand Down Expand Up @@ -140,6 +142,20 @@ public function testIsValid() : void
self::assertFalse($changelogConfig->isValid());
}

public function testGetSetGitHubCredentials() : void
{
self::assertNull($this->changelogConfig->getGitHubCredentials());

$expectedGitHubCredentials = new GitHubUsernamePassword('username', 'password');

$this->changelogConfig->setGitHubCredentials($expectedGitHubCredentials);

/** @var GitHubCredentials $gitHubCredentials */
$gitHubCredentials = $this->changelogConfig->getGitHubCredentials();

self::assertSame($expectedGitHubCredentials, $gitHubCredentials);
}

protected function setUp() : void
{
$this->user = 'jwage';
Expand Down
19 changes: 19 additions & 0 deletions tests/ChangelogGenerator/Tests/GitHubOAuthTokenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace ChangelogGenerator\Tests;

use ChangelogGenerator\GitHubOAuthToken;
use PHPUnit\Framework\TestCase;

final class GitHubOAuthTokenTest extends TestCase
{
public function testGetHttpBasicAuthorization() : void
{
self::assertSame(
'token oauthtoken',
(new GitHubOAuthToken('oauthtoken'))->getAuthorizationHeader()
);
}
}
19 changes: 19 additions & 0 deletions tests/ChangelogGenerator/Tests/GitHubUsernamePasswordTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace ChangelogGenerator\Tests;

use ChangelogGenerator\GitHubUsernamePassword;
use PHPUnit\Framework\TestCase;

final class GitHubUsernamePasswordTest extends TestCase
{
public function testGetHttpBasicAuthorization() : void
{
self::assertSame(
'Basic dXNlcm5hbWU6cGFzc3dvcmQ=',
(new GitHubUsernamePassword('username', 'password'))->getAuthorizationHeader()
);
}
}
Loading

0 comments on commit 5897452

Please sign in to comment.