Commit realizado el 12:13:52 08-04-2024
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
namespace DeployerForGit\Providers;
|
||||
|
||||
/**
|
||||
* Base provider class
|
||||
*/
|
||||
class BaseProvider {
|
||||
|
||||
/**
|
||||
* Repository url
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $repo_url;
|
||||
|
||||
/**
|
||||
* Repository options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $repo_url Repository url.
|
||||
* @param array $options Repository options.
|
||||
*
|
||||
* @throws \InvalidArgumentException If repository url is invalid.
|
||||
* @return void
|
||||
*/
|
||||
public function __construct( $repo_url = null, $options = array() ) {
|
||||
$this->repo_url = $repo_url;
|
||||
$this->options = $options;
|
||||
if ( is_wp_error( $this->validate_repo_url() ) ) {
|
||||
throw new \InvalidArgumentException( esc_attr( $this->validate_repo_url()->get_error_message() ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get repository url
|
||||
*
|
||||
* @return string Repository url
|
||||
*/
|
||||
public function get_repo_url() {
|
||||
return $this->repo_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate repository url
|
||||
*
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
protected function validate_repo_url() {
|
||||
// check if the repo url is empty.
|
||||
if ( empty( $this->repo_url ) ) {
|
||||
return new \WP_Error( 'empty', __( 'Repository url required', 'deployer-for-git' ) );
|
||||
}
|
||||
|
||||
// check if the repo url is a valid url.
|
||||
if ( ! filter_var( $this->repo_url, FILTER_VALIDATE_URL ) ) {
|
||||
return new \WP_Error( 'invalid', __( 'Repository url must be a url', 'deployer-for-git' ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get provider repository zip URL
|
||||
*
|
||||
* @param string $branch Branch name, default is master.
|
||||
* @return string Repository URL to download zip.
|
||||
*/
|
||||
public function get_zip_repo_url( $branch = 'master' ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get provider repository handle
|
||||
*
|
||||
* @return string Repository handle
|
||||
*/
|
||||
protected function get_handle() {
|
||||
$parsed_url = wp_parse_url( $this->repo_url );
|
||||
$parts = explode( '/', $parsed_url['path'] );
|
||||
$handle = '';
|
||||
foreach ( $parts as $index => $part ) {
|
||||
if ( $index === 0 ) {
|
||||
continue;
|
||||
}
|
||||
$handle .= $part . '/';
|
||||
|
||||
// if end of the path, remove the trailing slash.
|
||||
if ( $index === count( $parts ) - 1 ) {
|
||||
$handle = rtrim( $handle, '/' );
|
||||
}
|
||||
}
|
||||
|
||||
return $handle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get domain name from repository url
|
||||
*/
|
||||
protected function get_domain() {
|
||||
$parsed_url = wp_parse_url( $this->repo_url );
|
||||
|
||||
return $parsed_url['host'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get pretty provider name
|
||||
*
|
||||
* @return string Provider name
|
||||
*/
|
||||
public function get_pretty_name() {
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get package slug from handle
|
||||
*
|
||||
* @return string Package slug
|
||||
*/
|
||||
public function get_package_slug() {
|
||||
$handle = $this->get_handle();
|
||||
|
||||
// get the last part of the handle.
|
||||
$slug = basename( $handle );
|
||||
|
||||
return $slug;
|
||||
}
|
||||
}
|
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace DeployerForGit\Providers;
|
||||
|
||||
/**
|
||||
* Class BitbucketProvider
|
||||
*
|
||||
* @package DeployerForGit\Providers
|
||||
*/
|
||||
class BitbucketProvider extends BaseProvider {
|
||||
|
||||
/**
|
||||
* Get provider repository handle
|
||||
*
|
||||
* @return string Repository handle
|
||||
*/
|
||||
public function get_pretty_name() {
|
||||
return 'Bitbucket';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get provider repository zip URL
|
||||
*
|
||||
* @param string $branch Branch name, default is master.
|
||||
* @return string Repository URL to download zip
|
||||
*/
|
||||
public function get_zip_repo_url( $branch = 'master' ) {
|
||||
$handle = $this->get_handle();
|
||||
|
||||
return "https://bitbucket.org/{$handle}/get/{$branch}.zip";
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate repository url
|
||||
*
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
protected function validate_repo_url() {
|
||||
parent::validate_repo_url();
|
||||
|
||||
// check if string has exact format in a URL like this: https://bitbucket.org/owner/reponame .
|
||||
if ( ! preg_match( '/^https:\/\/bitbucket.org\/[a-zA-Z0-9-_]+\/[a-zA-Z0-9-_]+$/', $this->get_repo_url() ) ) {
|
||||
return new \WP_Error( 'invalid', __( 'Repository url must be a valid Bitbucket repository url', 'deployer-for-git' ) );
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace DeployerForGit\Providers;
|
||||
|
||||
/**
|
||||
* Class GiteaProvider
|
||||
*
|
||||
* @package Wp_Git_Deployer
|
||||
*/
|
||||
class GiteaProvider extends BaseProvider {
|
||||
|
||||
/**
|
||||
* Get provider repository handle
|
||||
*
|
||||
* @return string Repository handle
|
||||
*/
|
||||
public function get_pretty_name() {
|
||||
return 'Gitea';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get provider repository zip URL
|
||||
*
|
||||
* @param string $branch Branch name, default is master.
|
||||
* @return string Repository URL to download zip
|
||||
*/
|
||||
public function get_zip_repo_url( $branch = 'master' ) {
|
||||
$handle = $this->get_handle();
|
||||
|
||||
return "https://gitea.com/api/v1/repos/{$handle}/archive/{$branch}.zip";
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate repository url
|
||||
*
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
protected function validate_repo_url() {
|
||||
parent::validate_repo_url();
|
||||
|
||||
// check if string has exact format in a URL like this: https://gitea.com/owner/reponame .
|
||||
if ( ! preg_match( '/^https:\/\/gitea.com\/[a-zA-Z0-9-_]+\/[a-zA-Z0-9-_]+$/', $this->get_repo_url() ) ) {
|
||||
return new \WP_Error( 'invalid', __( 'Repository url must be a valid Gitea repository url', 'deployer-for-git' ) );
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace DeployerForGit\Providers;
|
||||
|
||||
/**
|
||||
* Class GithubProvider
|
||||
*
|
||||
* @package Wp_Git_Deployer
|
||||
*/
|
||||
class GithubProvider extends BaseProvider {
|
||||
|
||||
/**
|
||||
* Get provider repository handle
|
||||
*
|
||||
* @return string Repository handle
|
||||
*/
|
||||
public function get_pretty_name() {
|
||||
return 'GitHub';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get provider repository zip URL
|
||||
*
|
||||
* @param string $branch Branch name, default is master.
|
||||
* @return string Repository URL to download zip
|
||||
*/
|
||||
public function get_zip_repo_url( $branch = 'master' ) {
|
||||
$handle = $this->get_handle();
|
||||
|
||||
return "https://github.com/{$handle}/archive/refs/heads/{$branch}.zip";
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate repository url
|
||||
*
|
||||
* @return WP_Error|boolean
|
||||
*/
|
||||
protected function validate_repo_url() {
|
||||
parent::validate_repo_url();
|
||||
|
||||
// check if string has exact format in a URL like this: https://github.com/company/wordpress-theme .
|
||||
if ( ! preg_match( '/^https:\/\/github.com\/[a-zA-Z0-9-_]+\/[a-zA-Z0-9-_]+$/', $this->get_repo_url() ) ) {
|
||||
return new \WP_Error( 'invalid', __( 'Repository url must be a valid GitHub repository url', 'deployer-for-git' ) );
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace DeployerForGit\Providers;
|
||||
|
||||
/**
|
||||
* Class GitlabProvider
|
||||
*
|
||||
* @package DeployerForGit\Providers
|
||||
*/
|
||||
class GitlabProvider extends BaseProvider {
|
||||
|
||||
/**
|
||||
* Get provider repository handle
|
||||
*
|
||||
* @return string Repository handle
|
||||
*/
|
||||
public function get_pretty_name() {
|
||||
return 'GitLab';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get provider repository zip URL
|
||||
*
|
||||
* @param string $branch Branch name, default is master.
|
||||
* @return string Repository URL to download zip
|
||||
*/
|
||||
public function get_zip_repo_url( $branch = 'master' ) {
|
||||
$handle = $this->get_handle();
|
||||
$domain = $this->get_domain();
|
||||
$handle = rawurlencode( $handle );
|
||||
|
||||
return "https://{$domain}/api/v4/projects/{$handle}/repository/archive.zip?sha={$branch}";
|
||||
}
|
||||
|
||||
// disable the custom validation because user may use custom gitlab instance or groups which adds portions like /group/subgroup/ etc..
|
||||
// protected function validate_repo_url() {
|
||||
// return parent::validate_repo_url();.
|
||||
|
||||
// check if string has exact format in a URL like this: https://gitlab.com/owner/reponame .
|
||||
// if ( ! preg_match( '/^https?:\/\/[^\/]+(\/[^\/]+)+$/', $this->get_repo_url() ) ) {
|
||||
// return new \WP_Error( 'invalid', __( 'Repository url must be a valid GitLab repository url', 'deployer-for-git' ) );
|
||||
// }
|
||||
// }.
|
||||
}
|
@@ -0,0 +1 @@
|
||||
<?php // Silence is golden
|
Reference in New Issue
Block a user