mirror of
https://github.com/sitelease/sugar-cube-client.git
synced 2025-10-29 19:12:30 +01:00
Created two new API requester
+ Created a new Tags API requester to allow us to make tag related API requests + Created a new Branches API requester to allow us to make branch related API requests
This commit is contained in:
63
src/Api/Branches.php
Normal file
63
src/Api/Branches.php
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Gitea\Api;
|
||||||
|
|
||||||
|
use GuzzleHttp\Exception\ServerException;
|
||||||
|
|
||||||
|
use Gitea\Client;
|
||||||
|
use Gitea\Collections\ApiItemCollection;
|
||||||
|
use Gitea\Model\Branch;
|
||||||
|
|
||||||
|
use Gitea\Api\Abstracts\AbstractApiRequester;
|
||||||
|
|
||||||
|
class Branches extends AbstractApiRequester
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Get all tags from a particular repository
|
||||||
|
* using the repository's name and owner
|
||||||
|
*
|
||||||
|
* Example:
|
||||||
|
* ```
|
||||||
|
* // Get all tags from a repository owned by a user
|
||||||
|
* $giteaClient->tags()->fromRepository("username", "test-repository");
|
||||||
|
*
|
||||||
|
* // Get all tags from a repository owned by an organization
|
||||||
|
* $giteaClient->tags()->fromRepository("organizationName", "test-repository");
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
* @param string $owner The name of the user or organization that owns the repository
|
||||||
|
* @param string $repository The name of the repository
|
||||||
|
* @author Benjamin Blake (sitelease.ca)
|
||||||
|
*
|
||||||
|
* @return ApiItemCollection
|
||||||
|
*/
|
||||||
|
public function fromRepository(string $owner, string $repository)
|
||||||
|
{
|
||||||
|
$client = $this->getClient();
|
||||||
|
|
||||||
|
$repositoryCollection = new ApiItemCollection();
|
||||||
|
try {
|
||||||
|
$response = $this->get("repos/$owner/$repository/branches");
|
||||||
|
$statusCode = $response->getStatusCode();
|
||||||
|
$body = $response->getBody();
|
||||||
|
if ($statusCode == 200) {
|
||||||
|
$jsonItemList = json_decode($body, true);
|
||||||
|
if (count($jsonItemList) > 0) {
|
||||||
|
foreach ($jsonItemList as $jsonItem) {
|
||||||
|
$encodedItem = json_encode($jsonItem);
|
||||||
|
$itemObject = Branch::fromJson(
|
||||||
|
$this->getClient(),
|
||||||
|
$this,
|
||||||
|
json_decode($encodedItem)
|
||||||
|
);
|
||||||
|
$repositoryCollection->addItem($itemObject, $itemObject->getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $repositoryCollection;
|
||||||
|
} catch (ServerException $serverError) {
|
||||||
|
return $repositoryCollection;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
63
src/Api/Tags.php
Normal file
63
src/Api/Tags.php
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Gitea\Api;
|
||||||
|
|
||||||
|
use GuzzleHttp\Exception\ServerException;
|
||||||
|
|
||||||
|
use Gitea\Client;
|
||||||
|
use Gitea\Collections\ApiItemCollection;
|
||||||
|
use Gitea\Model\Tag;
|
||||||
|
|
||||||
|
use Gitea\Api\Abstracts\AbstractApiRequester;
|
||||||
|
|
||||||
|
class Tags extends AbstractApiRequester
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Get all tags from a particular repository
|
||||||
|
* using the repository's name and owner
|
||||||
|
*
|
||||||
|
* Example:
|
||||||
|
* ```
|
||||||
|
* // Get all tags from a repository owned by a user
|
||||||
|
* $giteaClient->tags()->fromRepository("username", "test-repository");
|
||||||
|
*
|
||||||
|
* // Get all tags from a repository owned by an organization
|
||||||
|
* $giteaClient->tags()->fromRepository("organizationName", "test-repository");
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
* @param string $owner The name of the user or organization that owns the repository
|
||||||
|
* @param string $repository The name of the repository
|
||||||
|
* @author Benjamin Blake (sitelease.ca)
|
||||||
|
*
|
||||||
|
* @return ApiItemCollection
|
||||||
|
*/
|
||||||
|
public function fromRepository(string $owner, string $repository)
|
||||||
|
{
|
||||||
|
$client = $this->getClient();
|
||||||
|
|
||||||
|
$repositoryCollection = new ApiItemCollection();
|
||||||
|
try {
|
||||||
|
$response = $this->get("repos/$owner/$repository/tags");
|
||||||
|
$statusCode = $response->getStatusCode();
|
||||||
|
$body = $response->getBody();
|
||||||
|
if ($statusCode == 200) {
|
||||||
|
$jsonItemList = json_decode($body, true);
|
||||||
|
if (count($jsonItemList) > 0) {
|
||||||
|
foreach ($jsonItemList as $jsonItem) {
|
||||||
|
$encodedItem = json_encode($jsonItem);
|
||||||
|
$itemObject = Tag::fromJson(
|
||||||
|
$this->getClient(),
|
||||||
|
$this,
|
||||||
|
json_decode($encodedItem)
|
||||||
|
);
|
||||||
|
$repositoryCollection->addItem($itemObject, $itemObject->getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $repositoryCollection;
|
||||||
|
} catch (ServerException $serverError) {
|
||||||
|
return $repositoryCollection;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user