From 09416c4ce1461412ef22e3c5b31241413b01e06b Mon Sep 17 00:00:00 2001 From: Michael Gerdemann Date: Sat, 18 Jan 2020 09:18:45 +0100 Subject: [PATCH 1/2] fix: Filter only `null` values Creates an AbstractEndpoint to provide a method to filter only `null` values. --- Classes/Endpoint/AbstractEndpoint.php | 27 +++++++++++++++++++++++++++ Classes/Endpoint/Miscellaneous.php | 4 ++-- Classes/Endpoint/Repositories.php | 6 +++--- 3 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 Classes/Endpoint/AbstractEndpoint.php diff --git a/Classes/Endpoint/AbstractEndpoint.php b/Classes/Endpoint/AbstractEndpoint.php new file mode 100644 index 0000000..d69b432 --- /dev/null +++ b/Classes/Endpoint/AbstractEndpoint.php @@ -0,0 +1,27 @@ + $text, 'wiki' => $wiki ]; - $options['json'] = array_filter($options['json']); + $options['json'] = $this->removeNullValues($options['json']); $response = $this->client->request(self::BASE_URI . '/markdown', 'POST', $options); return (string)$response->getBody(); diff --git a/Classes/Endpoint/Repositories.php b/Classes/Endpoint/Repositories.php index 1b2b4dd..cf52e67 100644 --- a/Classes/Endpoint/Repositories.php +++ b/Classes/Endpoint/Repositories.php @@ -9,7 +9,7 @@ use Avency\Gitea\Client; /** * Repositories endpoint */ -class Repositories implements EndpointInterface +class Repositories extends AbstractEndpoint implements EndpointInterface { const BASE_URI = 'api/v1/repos'; @@ -79,7 +79,7 @@ class Repositories implements EndpointInterface ] ]; - $options['json'] = array_filter($options['json']); + $options['json'] = $this->removeNullValues($options['json']); $response = $this->client->request(self::BASE_URI . '/migrate', 'POST', $options); return \GuzzleHttp\json_decode($response->getBody(), true); } @@ -134,7 +134,7 @@ class Repositories implements EndpointInterface 'sort' => $sort, 'order' => $order, ]; - $options['query'] = array_filter($options['query']); + $options['query'] = $this->removeNullValues($options['query']); $response = $this->client->request(self::BASE_URI . '/search', 'GET', $options); return \GuzzleHttp\json_decode($response->getBody(), true); } From 6993ac7c59746fa5a54efefb270af4b13d1dba7b Mon Sep 17 00:00:00 2001 From: Michael Gerdemann Date: Sat, 18 Jan 2020 09:19:17 +0100 Subject: [PATCH 2/2] feat: Create Admin-Endpoint Creates Admin endpoint with all API methods. --- Classes/Client.php | 7 +- Classes/Endpoint/Admin.php | 281 +++++++++++++++++++++++++++++++++++++ README.md | 16 +-- 3 files changed, 295 insertions(+), 9 deletions(-) create mode 100644 Classes/Endpoint/Admin.php diff --git a/Classes/Client.php b/Classes/Client.php index 6a710f7..9ee193a 100644 --- a/Classes/Client.php +++ b/Classes/Client.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Avency\Gitea; +use Avency\Gitea\Endpoint\Admin; use Avency\Gitea\Endpoint\EndpointInterface; use Avency\Gitea\Endpoint\Miscellaneous; use Avency\Gitea\Endpoint\Repositories; @@ -13,8 +14,9 @@ use Psr\Http\Message\ResponseInterface; /** * Gitea Client * - * @method Repositories repositories() + * @method Admin admin() * @method Miscellaneous miscellaneous() + * @method Repositories repositories() */ class Client { @@ -73,6 +75,9 @@ class Client */ public function request(string $uri = '', string $method = 'GET', array $options = []): ResponseInterface { + if (!empty($this->config['query']) && !empty($options['query'])) { + $options['query'] = array_merge($this->config['query'], $options['query']); + } return $this->httpClient->request($method, $uri, $options); } diff --git a/Classes/Endpoint/Admin.php b/Classes/Endpoint/Admin.php new file mode 100644 index 0000000..bef190e --- /dev/null +++ b/Classes/Endpoint/Admin.php @@ -0,0 +1,281 @@ +client = $client; + } + + /** + * @param int|null $page + * @param int|null $limit + * @return array + */ + public function getOrgs(int $page = null, int $limit = null): array + { + $options['query'] = [ + 'page' => $page, + 'limit' => $limit, + ]; + $options['query'] = $this->removeNullValues($options['query']); + + $response = $this->client->request(self::BASE_URI . '/orgs', 'GET', $options); + return \GuzzleHttp\json_decode($response->getBody(), true); + } + + /** + * @return array + */ + public function getUsers(): array + { + $response = $this->client->request(self::BASE_URI . '/users'); + return \GuzzleHttp\json_decode($response->getBody(), true); + } + + /** + * @param string $username + * @param string $password + * @param string $email + * @param string|null $fullName + * @param string|null $loginName + * @param bool $mustChangePassword + * @param bool $sendNotify + * @param int|null $sourceId + * @return array + */ + public function createUser( + string $username, + string $password, + string $email, + string $fullName = null, + string $loginName = null, + bool $mustChangePassword = true, + bool $sendNotify = true, + int $sourceId = null + ): array + { + $options['json'] = [ + 'username' => $username, + 'password' => $password, + 'email' => $email, + 'full_name' => $fullName, + 'login_name' => $loginName, + 'must_change_password' => $mustChangePassword, + 'send_notify' => $sendNotify, + 'source_id' => $sourceId, + ]; + $options['json'] = $this->removeNullValues($options['json']); + + $response = $this->client->request(self::BASE_URI . '/users', 'POST', $options); + return \GuzzleHttp\json_decode($response->getBody(), true); + } + + /** + * @param string $username + * @param string $email + * @param string|null $password + * @param bool|null $active + * @param bool|null $admin + * @param bool|null $allowCreateOrganization + * @param bool|null $allowGitHook + * @param bool|null $allowImportLocal + * @param string|null $fullName + * @param string|null $location + * @param string|null $loginName + * @param int|null $maxRepoCreation + * @param bool|null $mustChangePassword + * @param bool|null $prohibitLogin + * @param int|null $sourceId + * @param string|null $website + * @return array + */ + public function updateUser( + string $username, + string $email, + string $password = null, + bool $active = null, + bool $admin = null, + bool $allowCreateOrganization = null, + bool $allowGitHook = null, + bool $allowImportLocal = null, + string $fullName = null, + string $location = null, + string $loginName = null, + int $maxRepoCreation = null, + bool $mustChangePassword = null, + bool $prohibitLogin = null, + int $sourceId = null, + string $website = null + ): array + { + $options['json'] = [ + 'email' => $email, + 'password' => $password, + 'active' => $active, + 'admin' => $admin, + 'allow_create_organization' => $allowCreateOrganization, + 'allow_git_hook' => $allowGitHook, + 'allow_import_local' => $allowImportLocal, + 'full_name' => $fullName, + 'location' => $location, + 'login_name' => $loginName, + 'max_repo_creation' => $maxRepoCreation, + 'must_change_password' => $mustChangePassword, + 'prohibit_login' => $prohibitLogin, + 'source_id' => $sourceId, + 'website' => $website, + ]; + $options['json'] = $this->removeNullValues($options['json']); + + $response = $this->client->request(self::BASE_URI . '/users/' . $username, 'PATCH', $options); + return \GuzzleHttp\json_decode($response->getBody(), true); + } + + /** + * @param string $username + * @return bool + */ + public function deleteUser(string $username): bool + { + $response = $this->client->request(self::BASE_URI . '/users/' . $username, 'DELETE'); + return true; + } + + /** + * @param string $username + * @param string $title + * @param string $key + * @param bool|null $readOnly + * @return array + */ + public function addKeyToUser( + string $username, + string $title, + string $key, + bool $readOnly = null + ): array + { + $options['json'] = [ + 'title' => $title, + 'key' => $key, + 'read_only' => $readOnly, + ]; + $options['json'] = $this->removeNullValues($options['json']); + + $response = $this->client->request(self::BASE_URI . '/users/' . $username . '/keys', 'POST', $options); + return \GuzzleHttp\json_decode($response->getBody(), true); + } + + /** + * @param string $username + * @param string $title + * @param string $key + * @param bool|null $readOnly + * @return bool + */ + public function deleteKeyFromUser( + string $username, + int $id + ): bool + { + $response = $this->client->request(self::BASE_URI . '/users/' . $username . '/keys/' . $id, 'DELETE'); + return true; + } + + /** + * @param string $username + * @param string $orgUsername + * @param string|null $description + * @param string|null $fullName + * @param string|null $location + * @param bool|null $repoAdminChangeTeamAccess + * @param string|null $visibility // public, limited, private + * @param string|null $website + * @return array + */ + public function createOrgWithUser( + string $username, + string $orgUsername, + string $description = null, + string $fullName = null, + string $location = null, + bool $repoAdminChangeTeamAccess = null, + string $visibility = null, + string $website = null + ): array + { + $options['json'] = [ + 'username' => $orgUsername, + 'description' => $description, + 'full_name' => $fullName, + 'location' => $location, + 'repo_admin_change_team_access' => $repoAdminChangeTeamAccess, + 'visibility' => $visibility, + 'website' => $website, + ]; + $options['json'] = $this->removeNullValues($options['json']); + + $response = $this->client->request(self::BASE_URI . '/users/' . $username . '/orgs', 'POST', $options); + return \GuzzleHttp\json_decode($response->getBody(), true); + } + + /** + * @param string $username + * @param string $repoName + * @param bool|null $auto_init + * @param string|null $description + * @param string|null $gitignores + * @param string|null $issue_labels + * @param string|null $license + * @param bool|null $private + * @param string|null $readme + * @return array + */ + public function createUserRepository( + string $username, + string $repoName, + bool $auto_init = null, + string $description = null, + string $gitignores = null, + string $issue_labels = null, + string $license = null, + bool $private = null, + string $readme = null + ): array + { + $options['json'] = [ + 'name' => $repoName, + 'auto_init' => $auto_init, + 'description' => $description, + 'gitignores' => $gitignores, + 'issue_labels' => $issue_labels, + 'license' => $license, + 'private' => $private, + 'readme' => $readme, + ]; + $options['json'] = $this->removeNullValues($options['json']); + + $response = $this->client->request(self::BASE_URI . '/users/' . $username . '/repos', 'POST', $options); + return \GuzzleHttp\json_decode($response->getBody(), true); + } +} diff --git a/README.md b/README.md index faef0b5..2385b78 100644 --- a/README.md +++ b/README.md @@ -82,14 +82,14 @@ This project is licensed under the MIT License - see the Status | Method | Endpoint --- | --- | --- -❌ | GET | /admin/orgs -❌ | POST | /admin/users -❌ | DELETE | /admin/users/{username} -❌ | PATCH | /admin/users/{username} -❌ | POST | /admin/users/{username}/keys -❌ | DELETE | /admin/users/{username}/keys/{id} -❌ | POST | /admin/users/{username}/orgs -❌ | POST | /admin/users/{username}/repos +✅ | GET | /admin/orgs +✅ | POST | /admin/users +✅ | DELETE | /admin/users/{username} +✅ | PATCH | /admin/users/{username} +✅ | POST | /admin/users/{username}/keys +✅ | DELETE | /admin/users/{username}/keys/{id} +✅ | POST | /admin/users/{username}/orgs +✅ | POST | /admin/users/{username}/repos #### Miscellaneous