Browse Source

Added migration to repo. Added params methods to Lib\Client to create a standard for each Base class

Joachim M. Giæver 6 years ago
parent
commit
7da88509c5

+ 7 - 0
index.php

@@ -41,6 +41,7 @@ try {
     // Load every repo
     $repos = $me->repos()->load(); 
 
+
     // Loop through all of them in received order
     echo "\nNormal repo\n";
     foreach($repos->all() as $key => $repo)
@@ -130,6 +131,12 @@ try {
     foreach($repos->search(array("name" => "test-gogs-api-repo-"))->sort_by()->all() as $key => $repo)
         echo sprintf("... and deleting test repo: '%s' %s\n", $repo->name, $repo->delete() ? "true" : "false");
 
+
+    echo "\nMigrate repo 'gogs-php-api-client.git'\n";
+    $mrepo = $repos->create();
+    $mrepo->migrate("https://git.giaever.org/joachimmg/gogs-php-api-client.git", "gogs-php-api-client-migrate");
+    echo sprintf("Delete migrated repo: %s\n", $mrepo->delete());
+
     // Load all of my organizations.
     $orgs = $me->orgs()->load();
 

+ 1 - 1
src/API/Request/Base.php

@@ -144,7 +144,7 @@ namespace Gogs\API\Request {
 
             $this->json_set_property((object)$this->json_decode($ret));
 
-            return true;
+            return $this;
         }
 
         /**

+ 12 - 13
src/API/Request/Org.php

@@ -11,7 +11,7 @@ namespace Gogs\API\Request {
      *  * POST `/admin/users/username/orgs` (**Requires** admin rights. Curl will throw NotAuthorized exception if not).
      *
      * @author Joachim M. Giaever (joachim[]giaever.org)
-     * @version 0.1.3
+     * @version 0.1.4
      */
     final class Org extends User {
         public $org_description;
@@ -72,22 +72,21 @@ namespace Gogs\API\Request {
          *
          * @todo Create team within org with user
          * @param ...$args The parameter values.
-         * @return  bool
+         * @return Org
          */
         public function create(...$args) {
-            $params = array(
-                "username" => isset($args[0]) && is_string($args[0]) ? $args[0] : null,
-                "full_name" => isset($args[1]) && is_string($args[1]) ? $args[1] : null,
-                "description" => isset($args[2]) && is_string($args[2]) ? $args[2] : null,
-                "website" => isset($args[3]) && is_string($args[3]) ? $args[3] : null,
-                "location" => isset($args[4]) && is_string($args[4]) ? $args[4] : null
-            );
 
-            $params = array_filter($params, function($val) {
-                return $val != null;
-            });
+            $params = array();
 
-            Base::create($params);
+            $this->set_param($params, "username", $args, 0, "string", null);
+            $this->set_param($params, "full_name", $args, 1, "string", null);
+            $this->set_param($params, "description", $args, 2, "string", null);
+            $this->set_param($params, "website", $args, 3, "string", null);
+            $this->set_param($params, "location", $args, 4, "string", null);
+
+            $this->filter_params($params);
+
+            return Base::create($params);
         }
     }
 }

+ 1 - 1
src/API/Request/Orgs.php

@@ -44,7 +44,7 @@ namespace Gogs\API\Request {
          * leaving the programmer to create the user.
          *
          * @see Base
-         * @return User
+         * @return Org
          */
         public function create(...$args) {
 

+ 71 - 16
src/API/Request/Repo.php

@@ -14,7 +14,7 @@ namespace Gogs\API\Request {
      *  * DELETE `/repos/username/reponame`
      *
      * @author Joachim M. Giaever (joachim[]giaever.org)
-     * @version 0.1.3
+     * @version 0.1.4
      */
     final class Repo extends Base {
         
@@ -88,6 +88,9 @@ namespace Gogs\API\Request {
 
                 $this->scope = "/repos/" . ($this->owner ? $this->owner->username . "/" . $this->name : $this->full_name);
                 break;
+            case "migrate":
+                $this->scope = "/repos/migrate";
+                break;
             default:
                 return false;
             }
@@ -135,7 +138,7 @@ namespace Gogs\API\Request {
          *
          * Valid paramters:
          *
-         *  1. name
+         *  1. name, required
          *  2. description
          *  3. private (default: false)
          *  4. auto_init (default: false)
@@ -147,25 +150,77 @@ namespace Gogs\API\Request {
          *   where the required fields are first.
          *
          * @param ...$args The parameter values
-         * @return bool
+         * @return Repo
          */
         public function create(...$args) {
 
-            $params = array(
-                "name" => isset($args[0]) && is_string($args[0]) ? $args[0] : null,
-                "description" => isset($args[1]) && is_string($args[1]) ? $args[1] : null,
-                "private" => isset($args[2]) && is_bool($args[2]) ? $args[2] : false,
-                "auto_init" => isset($args[3]) && is_bool($args[3]) ? $args[3] : false,
-                "gitignores" => isset($args[4]) && is_string($args[4]) ? $args[4] : null,
-                "licence" => isset($args[5]) && is_string($args[5]) ? $args[5] : null,
-                "readme" => isset($args[6]) && is_string($args[6]) ? $args[6] : "Default"
-            );
-
-            $params = array_filter($params, function($val) {
-                return $val != null;
+            $params = array();
+
+            $this->set_param($params, "name", $args, 0,  "string", null);
+            $this->set_param($params, "description", $args, 1, "string", null);
+            $this->set_param($params, "private", $args, 2, "bool", false);
+            $this->set_param($params, "auto_init", $args, 3, "bool", false);
+            $this->set_param($params, "gitignores", $args, 4, "string", null);
+            $this->set_param($params, "license", $args, 5, "string", null);
+            $this->set_param($params, "readme", $args, 6, "string", "Default");
+            
+            $this->filter_params($params);
+
+            return parent::create($params);
+        }
+
+        /**
+         * Migrate a repository from other Git hosting sources.
+         *
+         * Valid parameters:
+         *
+         *  1. clone_addr, required
+         *  3. repo_name, required
+         *  4. auth_username
+         *  5. auth_password
+         *  6. mirror (default: false)
+         *  7. private (default: false)
+         *  8. description
+         *
+         *  **UID** will be set to `owner`. Either a User or an Organization.
+         *  **From API doc**: To migrate a repository for a organization, 
+         *  the authenticated user must be a owner of the specified organization.
+         *
+         *  This reflects the API v1 documentation, but is in an order
+         *  where the required fields as first.
+         *
+         *  @throws Exception\RequestErrorException when owner not set
+         *  @param ...$args The parameter values
+         *  @return Repo
+         */
+
+        public function migrate(...$args) {
+
+            $params = array();
+
+            if (empty($this->owner))
+                throw new Exception\RequestErrorException("Missing required userdata 'uid' or owner must be set");
+
+            $this->set_param($params, "clone_addr", $args, 0, "string", null, function(string $url) {
+                // @todo: URL/PATH validation here?
             });
+            $this->set_param($params, "repo_name", $args, 1, "string", null);
+            $this->set_param($params, "auth_username", $args, 2, "string", null);
+            $this->set_param($params, "auth_password", $args, 3, "string", null);
+            $this->set_param($params, "mirror", $args, 4, "bool", false);
+            $this->set_param($params, "private", $args, 5, "bool", false);
+            $this->set_param($params, "description", $args, 6, "string", null);
+
+            $this->set_param($params, "uid", array(), 0, "int", $this->owner->id);
+
+            $this->filter_params($params);
+
+            $this->set_scope("migrate");
+            $resp = parent::method_post($params);
+
+            $this->json_set_property($this->json_decode($resp));
 
-            parent::create($params);
+            return $this;
         }
     }
 

+ 1 - 0
src/API/Request/Repos.php

@@ -89,6 +89,7 @@ namespace Gogs\API\Request {
 
         /** 
          * @see Collection
+         * @return Repo
          */
         public function create(...$args) {
 

+ 6 - 8
src/API/Request/Token.php

@@ -12,7 +12,7 @@ namespace Gogs\API\Request {
      * as this can load them.
      * 
      * @author Joachim M. Giaever (joachim[]giaever.org)
-     * @version 0.1.1
+     * @version 0.1.2
      */
     final class Token extends Base {
         const VERSION = "0.1.1";
@@ -60,16 +60,14 @@ namespace Gogs\API\Request {
          *  This reflects the API v1 documentation.
          *
          *  @param ...$args The parameter values
-         *  @return bool
+         *  @return Token
          */
         public function create(...$args) {
-            $params = array(
-                "name" => isset($args[0]) && is_string($args[0]) ? $args[0] : null
-            );
 
-            $params = array_filter($params, function($val) {
-                return $val != null;
-            });
+            $params = array();
+
+            $this->set_param($params, "name", $args, 0, "string", null);
+            $this->filter_params($params);
 
             return parent::create($params);
         }

+ 19 - 14
src/API/Request/User.php

@@ -18,7 +18,7 @@ namespace Gogs\API\Request {
      * @see Orgs
      * 
      * @author Joachim M. Giaever (joachim[]giaever.org)
-     * @version 0.1.3
+     * @version 0.1.4
      */
     class User extends Base {
 
@@ -156,25 +156,30 @@ namespace Gogs\API\Request {
          * where the required fields are first.
          *
          * @param ...$args The parameter values.
-         * @return bool
+         * @return User
          */
         public function create(...$args) {
-            $params = array(
-                "username" => isset($args[0]) && is_string($args[0]) ? $args[0] : null,
-                "email" => isset($args[1]) && is_string($args[1]) ? $args[1] : null,
-                "source_id" => isset($args[2]) && is_numeric($args[2]) ? $args[2] : null,
-                "login_name" => isset($args[3]) && is_string($args[3]) ? $args[3] : null,
-                "password" => isset($args[4]) && is_string($args[4]) ? $args[4] : null,
-                "send_notify" => isset($args[5]) && is_bool($args[5]) ? $args[5] : null
-            );
-
-            $params = array_filter($params, function($val) {
-                return $val != null;
-            });
+
+            $params = array();
+
+            $this->set_param($params, "username", $args, 0, "string", null);
+            $this->set_param($params, "email", $args, 1, "string", null);
+            $this->set_param($params, "source_id", $args, 2, "int", null);
+            $this->set_param($params, "login_name", $args, 3, "string", null);
+            $this->set_param($params, "password", $args, 4, "string", null);
+            $this->set_param($params, "send_notify", $args, 5, "bool", null);
+
+            $this->filter_params($params);
 
             return parent::create($params);
         }
 
+        /** 
+         * Returns user tokens
+         * 
+         * @param string $password User personal password.
+         * @return Tokens
+         */
         public function tokens(string $password) {
             return new Tokens($this->url, $password, $this);
         }

+ 1 - 1
src/API/Request/Users.php

@@ -34,7 +34,7 @@ namespace Gogs\API\Request {
          * user through the user object iteself.
          *
          * @param ...$args User->create arguments
-         * @return \User
+         * @return User
          */
         public function create(...$args) {
 

+ 54 - 1
src/Lib/Curl/Client.php

@@ -27,10 +27,63 @@ namespace Gogs\Lib\Curl {
          *
          * @param string $user 
          */
-        public function basic(string $user) {
+        protected function basic(string $user) {
             $this->basic = $user;
         }
 
+        /**
+         * Set param into array
+         *
+         * The specified callback will only run if the expected
+         * parameter is set. This callback can either overwrite
+         * paramtere as passing them as reference or throw an exception
+         * to indicate invalid data.
+         *
+         * @param array &$params Array to insert to
+         * @param string $param_name Index in params-array
+         * @param array $args Arguments array
+         * @param int $index Index in arguments array
+         * @param string $type Expected type of data
+         * @param mixed $default Default if not expected type on index
+         * @param callback $f Callback method if param is set
+         */
+        protected function set_param(array &$params, string $param_name, array $args, int $index, string $type, $default = null, callable $f = null) {
+
+            switch ($type) {
+            case "str":
+                $type = "string";
+                break;
+            case "int":
+                $type = "integer";
+                break;
+            case "float":
+                $type = "double";
+                break;
+            case "bool":
+                $type = "boolean";
+                break;
+            }
+
+            $type = $type == "bool" ? "boolean" : ($type == "float" ? "double" : $type);
+            $params[$param_name] = isset($args[$index]) && gettype($args[$index]) == $type ? $args[$index] : $default;
+
+            if ($f != null && $params[$param_name] != $default)
+                $f($params[$param_name]);
+        }
+
+        /** 
+         * Filter out NULL values from parameters.
+         *
+         * Saves transferring size.
+         * 
+         * @param array &$params Parameters
+         */
+        protected function filter_params(array &$params) {
+            $params = array_filter($params, function($val) {
+                return $val != null;
+            });
+        }
+
         /** 
          * array_2_params takes an array and converts it into a
          * query string (e.g param=val&param2=val2).