Update to newer Github API by Hackwar · Pull Request #65 · joomla-framework/github-api · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions docs/v3-to-v4-update.md
30 changes: 26 additions & 4 deletions src/AbstractGithubObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,42 @@ public function __construct(Registry $options = null, BaseHttp $client = null)
* @return Uri
*
* @since 1.0
* @since __DEPLOY_VERSION__ Sets a default `Accept: application/vnd.github+json` header and pins
* `X-GitHub-Api-Version` (overridable via the `api.version` option, or by
* setting either header on the HTTP client before the request). The
* `gh.token.scheme` option (default `token`, unchanged) may be set to
* `Bearer` for fine-grained PATs / GitHub App installation tokens.
*/
protected function fetchUrl($path, $page = 0, $limit = 0)
{
// Get a new Uri object focusing the api url and given path.
$uri = new Uri($this->options->get('api.url') . $path);

$headers = $this->client->getOption('headers', []);

// Pin the REST API media type and version unless the consumer already set their own.
if (!isset($headers['Accept'])) {
$headers['Accept'] = 'application/vnd.github+json';
}

if (!isset($headers['X-GitHub-Api-Version'])) {
$headers['X-GitHub-Api-Version'] = $this->options->get('api.version', '2022-11-28');
}

if ($this->options->get('gh.token', false)) {
// Use oAuth authentication
$headers = $this->client->getOption('headers', []);

if (!isset($headers['Authorization'])) {
$headers['Authorization'] = 'token ' . $this->options->get('gh.token');
$this->client->setOption('headers', $headers);
// 'token' is the classic scheme; set gh.token.scheme to 'Bearer' for fine-grained
// PATs or GitHub App installation tokens, both of which GitHub also accepts as 'token'.
$scheme = $this->options->get('gh.token.scheme', 'token');

$headers['Authorization'] = $scheme . ' ' . $this->options->get('gh.token');
}
} else {
// Use basic authentication
// Note: GitHub removed username/password Basic authentication for the API in
// November 2020; this path is kept only for compatibility with Enterprise Server
// instances that may still accept it. Use gh.token for github.com.
if ($this->options->get('api.username', false)) {
$uri->setUser($this->options->get('api.username'));
}
Expand All @@ -134,6 +154,8 @@ protected function fetchUrl($path, $page = 0, $limit = 0)
}
}

$this->client->setOption('headers', $headers);

// If we have a defined page number add it to the JUri object.
if ($page > 0) {
$uri->setVar('page', (int) $page);
Expand Down
9 changes: 7 additions & 2 deletions src/Package/Authorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@
* @documentation http://developer.github.com/v3/oauth/
* @documentation http://developer.github.com/v3/oauth_authorizations/
*
* @note The methods in this class are only accessible with Basic Authentication
* @since 1.0
* @note The methods in this class are only accessible with Basic Authentication
* @since 1.0
* @deprecated __DEPLOY_VERSION__ will be removed in 5.0. GitHub removed the OAuth
* Authorizations API (and Basic Authentication for the API) in November 2020;
* every method in this class now fails against github.com. Kept only so
* existing `$github->authorization` call sites don't fatal. Use GitHub Apps / OAuth
* device flow (Package\Apps) or a personal access token via the `gh.token` option instead.
*/
class Authorization extends AbstractPackage
{
Expand Down
5 changes: 4 additions & 1 deletion src/Package/Repositories/Downloads.php