Skip to content
Closed
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
4 changes: 2 additions & 2 deletions Tests/Package/Activity/NotificationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function testGetList()
{
$this->client->expects($this->once())
->method('get')
->with('/notifications?all=1&participating=1&since=2005-08-17T00:00:00+00:00&before=2005-08-17T00:00:00+00:00', [], 0)
->with('/notifications?all=1&participating=1&since=2005-08-17T00%3A00%3A00%2B00%3A00&before=2005-08-17T00%3A00%3A00%2B00%3A00', [], 0)
->willReturn($this->response);

$response = json_decode((string) $this->response->getBody());
Expand All @@ -71,7 +71,7 @@ public function testGetList()
*/
public function testGetListRepository()
{
$args = 'all=1&participating=1&since=2005-08-17T00:00:00+00:00&before=2005-08-17T00:00:00+00:00';
$args = 'all=1&participating=1&since=2005-08-17T00%3A00%3A00%2B00%3A00&before=2005-08-17T00%3A00%3A00%2B00%3A00';

$this->client->expects($this->once())
->method('get')
Expand Down
82 changes: 82 additions & 0 deletions Tests/Package/EncodingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

/**
* @copyright Copyright (C) 2005 - 2026 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

namespace Joomla\Github\Tests\Package;

use Joomla\Github\Package\Data\Refs;
use Joomla\Github\Package\Issues\Labels;
use Joomla\Github\Package\Pulls;
use Joomla\Github\Package\Repositories\Branches;
use Joomla\Github\Package\Repositories\Commits;
use Joomla\Github\Package\Repositories\Contents;
use Joomla\Github\Tests\Stub\GitHubTestCase;

/**
* Tests that values with reserved characters are percent-encoded in the request path and query.
*/
class EncodingTest extends GitHubTestCase
{
public function testContentsPathAndRef()
{
$this->expectGet('/repos/joomla/joomla-cms/contents/docs/a%20b%26c%3Dd%23e.md?ref=feature%2Fx%26raw%3D1');

(new Contents($this->options, $this->client))->get('joomla', 'joomla-cms', 'docs/a b&c=d#e.md', 'feature/x&raw=1');
}

public function testCommitListQueryValues()
{
$this->expectGet('/repos/joomla/joomla-cms/commits?sha=feature%2Fx%23y&path=src%2Fa%20b.php&author=a%26b');

(new Commits($this->options, $this->client))->getList('joomla', 'joomla-cms', 'feature/x#y', 'src/a b.php', 'a&b');
}

public function testDateIsEncoded()
{
$this->expectGet('/repos/joomla/joomla-cms/commits?since=2026-11-12T10%3A00%3A00%2B01%3A00');

(new Commits($this->options, $this->client))->getList('joomla', 'joomla-cms', since: new \DateTime('2026-11-12T10:00:00+01:00'));
}

public function testBranchName()
{
$this->expectGet('/repos/joomla/joomla-cms/branches/feature/fix%231');

(new Branches($this->options, $this->client))->get('joomla', 'joomla-cms', 'feature/fix#1');
}

public function testRef()
{
$this->expectGet('/repos/joomla/joomla-cms/git/refs/heads/a%3Fb');

(new Refs($this->options, $this->client))->get('joomla', 'joomla-cms', 'heads/a?b');
}

public function testLabelUpdate()
{
$this->client->expects($this->once())
->method('patch')
->with('/repos/joomla/joomla-cms/labels/needs%2Ftests')
->willReturn($this->response);

(new Labels($this->options, $this->client))->update('joomla', 'joomla-cms', 'needs/tests', 'needs tests', 'ff0000');
}

public function testPullsStateWithPaging()
{
$this->expectGet('/repos/joomla/joomla-cms/pulls?page=2&state=closed%26x%3D1');

(new Pulls($this->options, $this->client))->getList('joomla', 'joomla-cms', 'closed&x=1', 2);
}

private function expectGet(string $url): void
{
$this->client->expects($this->once())
->method('get')
->with($url)
->willReturn($this->response);
}
}
2 changes: 1 addition & 1 deletion Tests/Package/Issues/CommentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function testGetRepositoryListSince()

$this->client->expects($this->once())
->method('get')
->with('/repos/joomla/joomla-platform/issues/comments?sort=created&direction=asc&since=1966-09-15T12:34:56+00:00', [], 0)
->with('/repos/joomla/joomla-platform/issues/comments?sort=created&direction=asc&since=1966-09-15T12%3A34%3A56%2B00%3A00', [], 0)
->willReturn($this->response);

$response = json_decode((string) $this->response->getBody());
Expand Down
4 changes: 2 additions & 2 deletions Tests/Package/IssuesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ public function testGetListAll()

$this->client->expects($this->once())
->method('get')
->with('/issues?filter={filter}&state={state}&labels={labels}&sort={sort}&direction={direction}&since=2012-01-01T12:12:12+0000')
->with('/issues?filter=%7Bfilter%7D&state=%7Bstate%7D&labels=%7Blabels%7D&sort=%7Bsort%7D&direction=%7Bdirection%7D&since=2012-01-01T12%3A12%3A12%2B0000')
->willReturn($this->response);

$this->assertThat(
Expand Down Expand Up @@ -390,7 +390,7 @@ public function testGetListByRepositoryAll()
->method('get')
->with(
'/repos/joomla/joomla-platform/issues?milestone=25&state=closed&assignee=none&' .
'mentioned=joomla-jenkins&labels=bug&sort=created&direction=asc&since=2012-01-01T12:12:12+00:00'
'mentioned=joomla-jenkins&labels=bug&sort=created&direction=asc&since=2012-01-01T12%3A12%3A12%2B00%3A00'
)
->willReturn($this->response);

Expand Down
2 changes: 1 addition & 1 deletion Tests/Package/Repositories/BranchesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function testGet()
{
$this->client->expects($this->once())
->method('get')
->with('/repos/{owner}/{repo}/branches/{branch}')
->with('/repos/{owner}/{repo}/branches/%7Bbranch%7D')
->willReturn($this->response);

$this->assertThat(
Expand Down
4 changes: 2 additions & 2 deletions Tests/Package/Repositories/CommitsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public function testgetSha()
{
$this->client->expects($this->once())
->method('get')
->with('/repos/{user}/{repo}/commits/{ref}')
->with('/repos/{user}/{repo}/commits/%7Bref%7D')
->willReturn($this->response);

$this->assertThat(
Expand All @@ -194,7 +194,7 @@ public function testgetShaFailure()

$this->client->expects($this->once())
->method('get')
->with('/repos/{user}/{repo}/commits/{ref}')
->with('/repos/{user}/{repo}/commits/%7Bref%7D')
->willReturn($this->response);

$this->assertThat(
Expand Down
2 changes: 1 addition & 1 deletion Tests/Package/Repositories/DeploymentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function testGetList()
{
$this->client->expects($this->once())
->method('get')
->with('/repos/{owner}/{repo}/deployments?sha={sha}&ref={ref}&task={task}&environment={environment}')
->with('/repos/{owner}/{repo}/deployments?sha=%7Bsha%7D&ref=%7Bref%7D&task=%7Btask%7D&environment=%7Benvironment%7D')
->willReturn($this->response);

$this->assertThat(
Expand Down
2 changes: 1 addition & 1 deletion Tests/Package/Repositories/ReleasesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public function testGetByTag()
{
$this->client->expects($this->once())
->method('get')
->with('/repos/joomla/joomla-platform/releases/tags/{tag}', [], 0)
->with('/repos/joomla/joomla-platform/releases/tags/%7Btag%7D', [], 0)
->willReturn($this->response);

$response = json_decode((string) $this->response->getBody());
Expand Down
2 changes: 1 addition & 1 deletion Tests/Package/Repositories/StatusesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public function testGetCombinedStatus()
{
$this->client->expects($this->once())
->method('get')
->with('/repos/{user}/{repo}/commits/{sha}/status')
->with('/repos/{user}/{repo}/commits/%7Bsha%7D/status')
->willReturn($this->response);

$this->assertThat(
Expand Down
14 changes: 14 additions & 0 deletions docs/overview.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Github API package

TODO

## Encoding of URL values

Values that end up in the request URL are percent-encoded:

* Query values, e.g. the `$ref` of `$github->repositories->contents->get()`, the `$path` and `$author` of
`$github->repositories->commits->getList()` or a `since` date (its `+` used to be sent as is, which GitHub reads as
a space).
* Path values that can contain reserved characters: file paths, branch, tag and Git reference names, commit SHAs and
references, label names, `.gitignore` template names and search keywords. A `/` in a file path, branch or reference
name is kept as the separator; the other characters of each segment are encoded.

Pass these values unencoded. Owner, repository, user and organisation names are not encoded; GitHub only allows
letters, digits, `-`, `_` and `.` in them.
19 changes: 18 additions & 1 deletion src/AbstractGithubObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,24 @@ protected function fetchUrl($path, $page = 0, $limit = 0)
$uri->setVar('per_page', (int) $limit);
}

return $uri;
// Percent-encode the query values that the packages add.
return new EncodedQueryUri((string) $uri);
}

/**
* Percent-encode each segment of a path value, e.g. a file path, a branch or a Git reference.
*
* The slashes between the segments are kept.
*
* @param string $path The path value.
*
* @return string
*
* @since __DEPLOY_VERSION__
*/
protected static function encodePath($path)
{
return implode('/', array_map('rawurlencode', explode('/', (string) $path)));
}

/**
Expand Down
38 changes: 38 additions & 0 deletions src/EncodedQueryUri.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* Part of the Joomla Framework Github Package
*
* @copyright Copyright (C) 2005 - 2026 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

namespace Joomla\Github;

use Joomla\Uri\Uri;

/**
* URI that percent-encodes the query values it renders.
*
* Joomla\Uri\Uri decodes the query it builds from its variables, so a value containing "&", "#", "+" or a space
* would break the request or add parameters. This class encodes the query according to RFC 3986 instead.
*
* @since __DEPLOY_VERSION__
* @internal
*/
final class EncodedQueryUri extends Uri
{
/**
* Build a query from an array of variables, percent-encoded according to RFC 3986.
*
* @param array $params The query variables.
*
* @return string The encoded query.
*
* @since __DEPLOY_VERSION__
*/
protected static function buildQuery(array $params)
{
return http_build_query($params, '', '&', \PHP_QUERY_RFC3986);
}
}
8 changes: 4 additions & 4 deletions src/Package/Data/Refs.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Refs extends AbstractPackage
public function get($user, $repo, $ref)
{
// Build the request path.
$path = '/repos/' . $user . '/' . $repo . '/git/refs/' . $ref;
$path = '/repos/' . $user . '/' . $repo . '/git/refs/' . self::encodePath($ref);

// Send the request.
return $this->processResponse($this->client->get($this->fetchUrl($path)));
Expand All @@ -61,7 +61,7 @@ public function getList($user, $repo, $namespace = '', $page = 0, $limit = 0)
$path = '/repos/' . $user . '/' . $repo . '/git/refs';

if (!empty($namespace)) {
$path .= '/' . $namespace;
$path .= '/' . self::encodePath($namespace);
}

// Send the request.
Expand Down Expand Up @@ -115,7 +115,7 @@ public function create($user, $repo, $ref, $sha)
public function edit($user, $repo, $ref, $sha, $force = false)
{
// Build the request path.
$path = '/repos/' . $user . '/' . $repo . '/git/refs/' . $ref;
$path = '/repos/' . $user . '/' . $repo . '/git/refs/' . self::encodePath($ref);

// Create the data object.
$data = new \stdClass();
Expand Down Expand Up @@ -148,7 +148,7 @@ public function edit($user, $repo, $ref, $sha, $force = false)
public function delete($owner, $repo, $ref)
{
// Build the request path.
$path = '/repos/' . $owner . '/' . $repo . '/git/refs/' . $ref;
$path = '/repos/' . $owner . '/' . $repo . '/git/refs/' . self::encodePath($ref);

return $this->processResponse(
$this->client->delete($this->fetchUrl($path)),
Expand Down
4 changes: 2 additions & 2 deletions src/Package/Data/Trees.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Trees extends AbstractPackage
public function get($owner, $repo, $sha)
{
// Build the request path.
$path = '/repos/' . $owner . '/' . $repo . '/git/trees/' . $sha;
$path = '/repos/' . $owner . '/' . $repo . '/git/trees/' . self::encodePath($sha);

return $this->processResponse(
$this->client->get($this->fetchUrl($path))
Expand All @@ -55,7 +55,7 @@ public function get($owner, $repo, $sha)
public function getRecursively($owner, $repo, $sha)
{
// Build the request path.
$path = '/repos/' . $owner . '/' . $repo . '/git/trees/' . $sha . '?recursive=1';
$path = '/repos/' . $owner . '/' . $repo . '/git/trees/' . self::encodePath($sha) . '?recursive=1';

return $this->processResponse(
$this->client->get($this->fetchUrl($path))
Expand Down
2 changes: 1 addition & 1 deletion src/Package/Gitignore.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function getList()
public function get($name, $raw = false)
{
// Build the request path.
$path = '/gitignore/templates/' . $name;
$path = '/gitignore/templates/' . rawurlencode($name);

$headers = [];

Expand Down
2 changes: 1 addition & 1 deletion src/Package/Issues/Labels.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function delete($owner, $repo, $name)
public function update($user, $repo, $label, $name, $color)
{
// Build the request path.
$path = '/repos/' . $user . '/' . $repo . '/labels/' . $label;
$path = '/repos/' . $user . '/' . $repo . '/labels/' . rawurlencode($label);

// Build the request data.
$data = json_encode(
Expand Down
6 changes: 4 additions & 2 deletions src/Package/Pulls.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,15 @@ public function getList($user, $repo, $state = 'open', $page = 0, $limit = 0)
// Build the request path.
$path = '/repos/' . $user . '/' . $repo . '/pulls';

$uri = $this->fetchUrl($path, $page, $limit);

// If a state exists append it as an option.
if ($state != 'open') {
$path .= '?state=' . $state;
$uri->setVar('state', $state);
}

// Send the request.
return $this->processResponse($this->client->get($this->fetchUrl($path, $page, $limit)));
return $this->processResponse($this->client->get($uri));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Package/Repositories/Branches.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function getList($owner, $repo)
public function get($owner, $repo, $branch)
{
// Build the request path.
$path = "/repos/$owner/$repo/branches/$branch";
$path = "/repos/$owner/$repo/branches/" . self::encodePath($branch);

// Send the request.
return $this->processResponse(
Expand Down
4 changes: 2 additions & 2 deletions src/Package/Repositories/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function getListRepository($user, $repo, $page = 0, $limit = 0)
public function getList($user, $repo, $sha, $page = 0, $limit = 0)
{
// Build the request path.
$path = '/repos/' . $user . '/' . $repo . '/commits/' . $sha . '/comments';
$path = '/repos/' . $user . '/' . $repo . '/commits/' . self::encodePath($sha) . '/comments';

// Send the request.
return $this->processResponse(
Expand Down Expand Up @@ -159,7 +159,7 @@ public function delete($user, $repo, $id)
public function create($user, $repo, $sha, $comment, $line, $filepath, $position)
{
// Build the request path.
$path = '/repos/' . $user . '/' . $repo . '/commits/' . $sha . '/comments';
$path = '/repos/' . $user . '/' . $repo . '/commits/' . self::encodePath($sha) . '/comments';

$data = json_encode(
[
Expand Down
Loading
Loading