mirror of
https://github.com/sitelease/sugar-cube-client.git
synced 2025-10-31 20:12:29 +01:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5deac6cf55 | |||
| 714d079287 | |||
| ea64578fd9 | |||
| dbdff87baf | |||
| 785d0b43a2 | |||
| f468b67335 | |||
| d37650a882 | |||
| e60312fab3 | |||
| cc6bd39f82 |
@ -25,7 +25,7 @@
|
||||
"php": ">=7.2.0",
|
||||
"ext-curl": "*",
|
||||
"ext-json": "*",
|
||||
"cedx/enum": "^7.4.0",
|
||||
"myclabs/php-enum": "^1.8",
|
||||
"guzzlehttp/guzzle": "~6.0"
|
||||
},
|
||||
"require-dev": {
|
||||
|
||||
@ -112,28 +112,73 @@ class Repositories extends AbstractAllApiRequester
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the raw contents of a file stored inside a repository
|
||||
* Get a repository using its ID
|
||||
*
|
||||
* Example:
|
||||
* ```
|
||||
* $client->repositories()->getByID($repoId);
|
||||
* ```
|
||||
*
|
||||
* @param string repoId The ID of the repository
|
||||
* @return Repository
|
||||
*/
|
||||
public function getById(int $repoId)
|
||||
{
|
||||
$client = $this->getClient();
|
||||
try {
|
||||
$response = $this->get("repositories/$repoId");
|
||||
$statusCode = $response->getStatusCode();
|
||||
$body = (string) $response->getBody();
|
||||
if ($statusCode == 200) {
|
||||
return Repository::fromJson(
|
||||
$client,
|
||||
$this,
|
||||
json_decode($body)
|
||||
);
|
||||
}
|
||||
return false;
|
||||
|
||||
} catch (ServerException $serverError) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the contents of a file in a certain in repository commit/branch/tag
|
||||
* using the repository's name and owner
|
||||
*
|
||||
* Example:
|
||||
* ```
|
||||
* $client->repositories()->getRawFile($owner, $repoName, "README.md");
|
||||
* $client->repositories()->getFileContents($owner, $repoName, "README.md", "v2.0.0");
|
||||
* ```
|
||||
*
|
||||
* @param string $owner The owner of the repository
|
||||
* @param string $repoName The name of the repository
|
||||
* @param string $filepath The path to the raw file (relative to the repository root)
|
||||
* @param string $filepath The path to the file (relative to the repository root)
|
||||
* @param string $ref The name of the commit/branch/tag. Default the repository’s default branch (usually master)
|
||||
* @return string
|
||||
*/
|
||||
public function getRawFile(string $owner, string $repoName, string $filepath)
|
||||
public function getFileContents(string $owner, string $repoName, string $filepath, string $ref="")
|
||||
{
|
||||
$client = $this->getClient();
|
||||
try {
|
||||
$response = $this->get("repos/$owner/$repoName/raw/$filepath");
|
||||
if ($ref !== "") {
|
||||
$response = $this->get("repos/$owner/$repoName/contents/$filepath",[
|
||||
"ref" => $ref
|
||||
]);
|
||||
} else {
|
||||
$response = $this->get("repos/$owner/$repoName/contents/$filepath");
|
||||
}
|
||||
|
||||
$statusCode = $response->getStatusCode();
|
||||
$body = (string) $response->getBody();
|
||||
if ($statusCode == 200) {
|
||||
return $body;
|
||||
$body = (string) $response->getBody();
|
||||
$jsonObj = json_decode($body, true);
|
||||
if (array_key_exists("content", $jsonObj)) {
|
||||
$base64FileContents = $jsonObj["content"];
|
||||
$fileContents = base64_decode($base64FileContents);
|
||||
return $fileContents;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
|
||||
@ -66,7 +66,7 @@ class Client implements RequestChainableInterface
|
||||
{
|
||||
// Append a slash to any URL that doesn't end in '/'
|
||||
if (!$this->endsWith($giteaURL, '/')) {
|
||||
$giteaURL += "/";
|
||||
$giteaURL .= "/";
|
||||
}
|
||||
$this->giteaURL = $giteaURL;
|
||||
$this->authToken = $authToken;
|
||||
|
||||
@ -203,7 +203,7 @@ class Owner extends AbstractApiModel {
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getIsAdmin(): boolean {
|
||||
public function getIsAdmin(): bool {
|
||||
return $this->isAdmin;
|
||||
}
|
||||
|
||||
|
||||
@ -1,11 +1,13 @@
|
||||
<?php declare(strict_types=1);
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gitea\Model;
|
||||
|
||||
use Enum\{EnumTrait};
|
||||
use MyCLabs\Enum\Enum;
|
||||
|
||||
/** Defines the state of a Gitea status. */
|
||||
final class StatusState {
|
||||
use EnumTrait;
|
||||
final class StatusState extends Enum {
|
||||
|
||||
/** @var string The status is an error. */
|
||||
const error = 'error';
|
||||
|
||||
@ -44,9 +44,9 @@ class Tag extends AbstractApiModel {
|
||||
if (count($args) >= 2) {
|
||||
$id = $args[0];
|
||||
$name = $args[1];
|
||||
if (!is_numeric($id)) {
|
||||
if (!is_string($id)) {
|
||||
$argumentType = gettype($id);
|
||||
throw new InvalidArgumentException("The \"__construct()\" function requires the 3rd parameter to be of the integer type, but a \"$argumentType\" was passed in");
|
||||
throw new InvalidArgumentException("The \"__construct()\" function requires the 3rd parameter to be of the string type, but a \"$argumentType\" was passed in");
|
||||
}
|
||||
if (!is_string($name)) {
|
||||
$argumentType = gettype($name);
|
||||
@ -71,7 +71,7 @@ class Tag extends AbstractApiModel {
|
||||
$newTag = new static(
|
||||
$client,
|
||||
$caller,
|
||||
isset($map->id) && is_numeric($map->id) ? $map->id : -1,
|
||||
isset($map->id) && is_string($map->id) ? $map->id : -1,
|
||||
isset($map->name) && is_string($map->name) ? $map->name : ''
|
||||
);
|
||||
$newTag->setTarballURL(isset($map->tarball_url) && is_string($map->tarball_url) ? new Uri($map->tarball_url) : null);
|
||||
@ -104,7 +104,7 @@ class Tag extends AbstractApiModel {
|
||||
* Gets the tag identifier.
|
||||
* @return int The tag identifier.
|
||||
*/
|
||||
function getId(): int {
|
||||
function getId(): string {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
|
||||
@ -1,11 +1,10 @@
|
||||
<?php declare(strict_types=1);
|
||||
namespace Gitea\Model;
|
||||
|
||||
use Enum\{EnumTrait};
|
||||
use MyCLabs\Enum\Enum;
|
||||
|
||||
/** Defines the permission of a team. */
|
||||
final class TeamPermission {
|
||||
use EnumTrait;
|
||||
final class TeamPermission extends Enum {
|
||||
|
||||
/** @var string The team has the administrator permission. */
|
||||
const admin = 'admin';
|
||||
|
||||
@ -59,9 +59,10 @@ class PushEvent extends AbstractApiModel {
|
||||
* @param array $server The HTTP SERVER array for the push event
|
||||
* @param string $body The raw data from the request body
|
||||
* @param string $secretKey The secret key to from your server
|
||||
* @return void
|
||||
* @param bool $skipSecretValidation If set to true, secret key validation will be skipped (used for newer versions of Gitea)
|
||||
* @return bool
|
||||
*/
|
||||
public static function validateRequest(array $server, string $body, string $secretKey)
|
||||
public static function validateRequest(array $server, string $body, string $secretKey, bool $skipSecretValidation = false)
|
||||
{
|
||||
// Validate request protocol
|
||||
if ($server['REQUEST_METHOD'] != 'POST') {
|
||||
@ -80,6 +81,7 @@ class PushEvent extends AbstractApiModel {
|
||||
throw new \RuntimeException("FAILED: Empty Body - The request has an empty body");
|
||||
}
|
||||
|
||||
if (!$skipSecretValidation) {
|
||||
// Validate header signature
|
||||
$headerSignature = isset($server['HTTP_X_GITEA_SIGNATURE']) ? $server['HTTP_X_GITEA_SIGNATURE'] : '';
|
||||
if (empty($headerSignature)) {
|
||||
@ -93,6 +95,7 @@ class PushEvent extends AbstractApiModel {
|
||||
if ($headerSignature != $payload_signature) {
|
||||
throw new \RuntimeException("FAILED: Access Denied - The push event's secret does not match the expected secret");
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -82,7 +82,7 @@ if ($repository) {
|
||||
}
|
||||
|
||||
// print("Getting contents of \"composer.json\" file \n\n");
|
||||
// $rawFile = $giteaClient->repositories()->getRawFile("Sitelease", "sl-theme-recipe", "composer.json");
|
||||
// $rawFile = $giteaClient->repositories()->getFileContents("Sitelease", "sl-theme-recipe", "composer.json");
|
||||
// if ($rawFile) {
|
||||
// var_dump(json_encode($rawFile));
|
||||
// print("\n\n");
|
||||
@ -91,7 +91,7 @@ if ($repository) {
|
||||
// }
|
||||
|
||||
// print("Getting contents of \"composer.json\" file \n\n");
|
||||
// $rawFile = $giteaClient->repositories()->getRawFile("Sitelease", "sl-theme-recipe", "composer.json");
|
||||
// $rawFile = $giteaClient->repositories()->getFileContents("Sitelease", "sl-theme-recipe", "composer.json");
|
||||
// if ($rawFile) {
|
||||
// var_dump($rawFile);
|
||||
// print("\n\n");
|
||||
|
||||
Reference in New Issue
Block a user