diff --git a/lib/scm/gitea.class.php b/lib/scm/gitea.class.php index d64adf54d9..4712f4f69d 100644 --- a/lib/scm/gitea.class.php +++ b/lib/scm/gitea.class.php @@ -12,15 +12,26 @@ class Gitea * @param string $username * @param string $password * @param string $encoding + * @param object $repo * @access public * @return void */ - public function __construct($client, $root, $username, $password, $encoding = 'UTF-8') + public function __construct($client, $root, $username, $password, $encoding = 'UTF-8', $repo = null) { putenv('LC_CTYPE=en_US.UTF-8'); $this->client = $client; $this->root = rtrim($root, DIRECTORY_SEPARATOR); + if(!realpath($this->root) and !empty($repo)) + { + global $app; + $project = $app->control->loadModel('gitea')->apiGetSingleProject($repo->serviceHost, $repo->serviceProject); + if(isset($project->tokenCloneUrl)) + { + $cmd = 'git clone --progress -v "' . $project->tokenCloneUrl . '" "' . $this->root . '"'; + exec($cmd); + } + } $branch = isset($_COOKIE['repoBranch']) ? $_COOKIE['repoBranch'] : ''; if($branch) diff --git a/lib/scm/gitlab.class.php b/lib/scm/gitlab.class.php index 583b0b1475..a27cb2259d 100644 --- a/lib/scm/gitlab.class.php +++ b/lib/scm/gitlab.class.php @@ -7,15 +7,16 @@ class gitlab /** * Construct * - * @param string $client gitlab api url. - * @param string $root id of gitlab project. - * @param string $username null - * @param string $password token of gitlab api. - * @param string $encoding + * @param string $client gitlab api url. + * @param string $root id of gitlab project. + * @param string $username null + * @param string $password token of gitlab api. + * @param string $encoding + * @param object $repo * @access public * @return void */ - public function __construct($client, $root, $username, $password, $encoding = 'UTF-8') + public function __construct($client, $root, $username, $password, $encoding = 'UTF-8', $repo = null) { $this->client = $client; $this->root = rtrim($root, '/') . '/'; diff --git a/lib/scm/gitrepo.class.php b/lib/scm/gitrepo.class.php index bb39bcb417..aaafee2639 100644 --- a/lib/scm/gitrepo.class.php +++ b/lib/scm/gitrepo.class.php @@ -12,10 +12,11 @@ class GitRepo * @param string $username * @param string $password * @param string $encoding + * @param object $repo * @access public * @return void */ - public function __construct($client, $root, $username, $password, $encoding = 'UTF-8') + public function __construct($client, $root, $username, $password, $encoding = 'UTF-8', $repo = null) { putenv('LC_CTYPE=en_US.UTF-8'); diff --git a/lib/scm/scm.class.php b/lib/scm/scm.class.php index 4389aa26ab..950c6b740a 100644 --- a/lib/scm/scm.class.php +++ b/lib/scm/scm.class.php @@ -15,7 +15,7 @@ class scm $className = $repo->SCM; if($className == 'Git') $className = 'GitRepo'; if(!class_exists($className)) require(strtolower($className) . '.class.php'); - $this->engine = new $className($repo->client, $repo->path, $repo->account, $repo->password, $repo->encoding); + $this->engine = new $className($repo->client, $repo->path, $repo->account, $repo->password, $repo->encoding, $repo); } /** diff --git a/lib/scm/subversion.class.php b/lib/scm/subversion.class.php index 1bddd6baa1..120e5d4a88 100644 --- a/lib/scm/subversion.class.php +++ b/lib/scm/subversion.class.php @@ -18,10 +18,11 @@ class Subversion * @param string $account * @param string $password * @param string $encoding + * @param object $repo * @access public * @return void */ - public function __construct($client, $root, $account, $password, $encoding = 'UTF-8') + public function __construct($client, $root, $account, $password, $encoding = 'UTF-8', $repo = null) { putenv('LC_CTYPE=en_US.UTF-8'); $this->root = str_replace(array('%3A', '%2F', '+'), array(':', '/', ' '), urlencode(rtrim($root, '/'))); diff --git a/module/gitea/model.php b/module/gitea/model.php index 5ee5469e49..14c2fa89ef 100644 --- a/module/gitea/model.php +++ b/module/gitea/model.php @@ -384,6 +384,10 @@ class giteaModel extends model $project->path_with_namespace = $project->full_name; $project->http_url_to_repo = $project->html_url; $project->name_with_namespace = $project->full_name; + + $gitea = $this->getByID($giteaID); + $oauth = "oauth2:{$gitea->token}@"; + $project->tokenCloneUrl = preg_replace('/(http(s)?:\/\/)/', "\$1$oauth", $project->html_url); } return $project; @@ -600,36 +604,4 @@ class giteaModel extends model return $newBranches; } - - /** - * Check remote url. - * - * @param int $giteaID - * @param string $project - * @param string $client - * @param string $path - * @access public - * @return bool - */ - public function checkRemoteUrl($giteaID, $project, $client, $path) - { - if(chdir($path)) - { - $url = new stdclass(); - $remote = execCmd(escapeCmd("$client remote -v"), 'array'); - $pregHttp = '/http(s)?:\/\/(www\.)?[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+(:\d+)*(\/\w+)*\.git/'; - $pregSSH = '/ssh:\/\/git@[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+(:\d+)*(\/\w+)*\.git/'; - - if(preg_match($pregHttp, $remote[0], $matches)) $url->http = $matches[0]; - if(preg_match($pregSSH, $remote[0], $matches)) $url->ssh = $matches[0]; - - $project = $this->apiGetSingleProject($giteaID, $project); - if(isset($project->clone_url)) - { - if($project->clone_url == $url->http or $project->ssh_url == $url->ssh) return true; - } - } - - return false; - } } diff --git a/module/repo/js/create.js b/module/repo/js/create.js index 1c2d0a00f2..4699724d33 100644 --- a/module/repo/js/create.js +++ b/module/repo/js/create.js @@ -79,7 +79,7 @@ function scmChanged(scm) $('tr.service').toggle(true); if(scm == 'Gitea') { - $('tr.hide-service').toggle(true); + $('tr.hide-service:not(".hide-gitea")').toggle(true); } else { diff --git a/module/repo/js/edit.js b/module/repo/js/edit.js index 6f8167d17c..f9b94dc757 100644 --- a/module/repo/js/edit.js +++ b/module/repo/js/edit.js @@ -63,7 +63,7 @@ function scmChanged(scm, isFirstRequest = false) $('tr.service').toggle(true); if(scm == 'Gitea') { - $('tr.hide-service').toggle(true); + $('tr.hide-service:not(".hide-gitea")').toggle(true); } else { diff --git a/module/repo/lang/de.php b/module/repo/lang/de.php index 4f5484b589..54c8aad4ea 100644 --- a/module/repo/lang/de.php +++ b/module/repo/lang/de.php @@ -199,7 +199,7 @@ $lang->repo->error->linkedJob = "Deletion of the repository failed. The curr $lang->repo->error->clientPath = "The client installation directory cannot have spaces!"; $lang->repo->error->notFound = "The repository %s’s URL %s does not exist. Please confirm if this repository has been deleted from the local server."; $lang->repo->error->noWritable = '%s is not writable! Please check the privilege, or download will not be done.'; -$lang->repo->error->unconnected = 'The directory does not match the repository'; +$lang->repo->error->noCloneAddr = 'The repository clone address was not found'; $lang->repo->syncTips = 'You may find the reference about how to set Git sync from here.'; $lang->repo->encodingsTips = "The encodings of comments can be comma separated values, e.g. utf-8."; diff --git a/module/repo/lang/en.php b/module/repo/lang/en.php index a29be6e3c7..5d9b5bbd87 100644 --- a/module/repo/lang/en.php +++ b/module/repo/lang/en.php @@ -199,7 +199,7 @@ $lang->repo->error->linkedJob = "Deletion of the repository failed. The curr $lang->repo->error->clientPath = "The client installation directory cannot have spaces!"; $lang->repo->error->notFound = "The repository %s’s URL %s does not exist. Please confirm if this repository has been deleted from the local server."; $lang->repo->error->noWritable = '%s is not writable! Please check the privilege, or download will not be done.'; -$lang->repo->error->unconnected = 'The directory does not match the repository'; +$lang->repo->error->noCloneAddr = 'The repository clone address was not found'; $lang->repo->syncTips = 'You may find the reference about how to set Git sync from here.'; $lang->repo->encodingsTips = "The encodings of comments can be comma separated values, e.g. utf-8."; diff --git a/module/repo/lang/fr.php b/module/repo/lang/fr.php index 5e77a34092..ff2f8c8246 100644 --- a/module/repo/lang/fr.php +++ b/module/repo/lang/fr.php @@ -199,7 +199,7 @@ $lang->repo->error->linkedJob = "Deletion of the repository failed. The curr $lang->repo->error->clientPath = "The client installation directory cannot have spaces!"; $lang->repo->error->notFound = "The repository %s’s URL %s does not exist. Please confirm if this repository has been deleted from the local server."; $lang->repo->error->noWritable = '%s is not writable! Please check the privilege, or download will not be done.'; -$lang->repo->error->unconnected = 'The directory does not match the repository'; +$lang->repo->error->noCloneAddr = 'The repository clone address was not found'; $lang->repo->syncTips = 'Vous pouvez trouver la référence sur la façon de définir la synchronisation Git à partir de la page se trouvant ici.'; $lang->repo->encodingsTips = "Les encodages des commentaires de validation peuvent être des valeurs séparées par des virgules,ex: utf-8"; diff --git a/module/repo/lang/vi.php b/module/repo/lang/vi.php index 92d29cc587..16eb83b534 100644 --- a/module/repo/lang/vi.php +++ b/module/repo/lang/vi.php @@ -199,7 +199,7 @@ $lang->repo->error->linkedJob = "Deletion of the repository failed. The curr $lang->repo->error->clientPath = "The client installation directory cannot have spaces!"; $lang->repo->error->notFound = "The repository %s’s URL %s does not exist. Please confirm if this repository has been deleted from the local server."; $lang->repo->error->noWritable = '%s is not writable! Please check the privilege, or download will not be done.'; -$lang->repo->error->unconnected = 'The directory does not match the repository'; +$lang->repo->error->noCloneAddr = 'The repository clone address was not found'; $lang->repo->syncTips = 'Bạn có thể tìm tham khảo làm sao thiết lập đồng bộ Git từ here.'; $lang->repo->encodingsTips = "The encodings of commit comments, can be comma separated values,ví dụ: utf-8"; diff --git a/module/repo/lang/zh-cn.php b/module/repo/lang/zh-cn.php index d3fa3981e7..f94930c892 100644 --- a/module/repo/lang/zh-cn.php +++ b/module/repo/lang/zh-cn.php @@ -199,7 +199,7 @@ $lang->repo->error->linkedJob = "删除代码库失败,当前代码库与 $lang->repo->error->clientPath = "客户端安装目录不能有空格!"; $lang->repo->error->notFound = "代码库『%s』路径 %s 不存在,请确认此代码库是否已在本地服务器被删除"; $lang->repo->error->noWritable = '%s 不可写!请检查该目录权限,否则无法下载。'; -$lang->repo->error->unconnected = '目录地址与仓库不匹配'; +$lang->repo->error->noCloneAddr = '该项目克隆地址未找到'; $lang->repo->syncTips = '请参照这里,设置代码库定时同步。'; $lang->repo->encodingsTips = "提交日志的编码,可以用逗号连接起来的多个,比如utf-8。"; diff --git a/module/repo/model.php b/module/repo/model.php index 9456f0b0ea..817276d1e8 100644 --- a/module/repo/model.php +++ b/module/repo/model.php @@ -221,15 +221,6 @@ class repoModel extends model $data->acl = empty($data->acl) ? '' : json_encode($data->acl); - if($data->SCM == 'Gitea') - { - $relation = $this->loadModel('gitea')->checkRemoteUrl($data->serviceHost, $data->serviceProject, $data->client, $data->path); - if(!$relation) - { - dao::$errors['path'] = $this->lang->repo->error->unconnected; - return false; - } - } if($data->SCM == 'Subversion') { $scm = $this->app->loadClass('scm'); @@ -1424,7 +1415,29 @@ class repoModel extends model return false; } } - elseif(in_array($scm, array('Git', 'Gitea'))) + elseif($scm == 'Gitea') + { + if($this->post->name != '' and $this->post->serviceProject != '') + { + $project = $this->loadModel('gitea')->apiGetSingleProject($this->post->serviceHost, $this->post->serviceProject); + if(isset($project->tokenCloneUrl)) + { + $path = dirname(dirname(dirname(__FILE__))) . '/tmp/repo/' . $this->post->name . '_gitea'; + if(!realpath($path)) + { + $cmd = 'git clone --progress -v "' . $project->tokenCloneUrl . '" "' . $path . '"'; + exec($cmd); + } + $_POST['path'] = $path; + } + else + { + dao::$errors['serviceProject'] = $this->lang->repo->error->noCloneAddr; + return false; + } + } + } + elseif($scm == 'Git') { if(!is_dir($path)) { diff --git a/module/repo/view/create.html.php b/module/repo/view/create.html.php index 897b84c4f4..298d1b935f 100644 --- a/module/repo/view/create.html.php +++ b/module/repo/view/create.html.php @@ -45,7 +45,7 @@ - + repo->path; ?> diff --git a/module/repo/view/edit.html.php b/module/repo/view/edit.html.php index db31220799..c19a57b702 100644 --- a/module/repo/view/edit.html.php +++ b/module/repo/view/edit.html.php @@ -49,7 +49,7 @@ name, "class='form-control'"); ?> - + repo->path; ?> path, "class='form-control'"); ?>