diff --git a/src/Factory.php b/src/Factory.php deleted file mode 100755 index ef1bf54..0000000 --- a/src/Factory.php +++ /dev/null @@ -1,35 +0,0 @@ -setUsername($username); - $jk->setApiToken($apiToken); - - return $jk; - } -} diff --git a/src/Jenkins.php b/src/Jenkins.php index 4277645..f237371 100755 --- a/src/Jenkins.php +++ b/src/Jenkins.php @@ -85,6 +85,21 @@ class Jenkins // extends AbstractObj */ private ?AbstractClient $httpClient = null; + /** + * @param string $baseUrl Jenkins server host URL + * @param array $config = [ + * 'enableCache' => false, + * 'cacheDir' => '', + * 'username' => '', + * 'apiToken' => '', + * 'password' => '', + * ] + */ + public static function new(string $baseUrl, array $config = []): self + { + return new self($baseUrl, $config); + } + /** * @param string $baseUrl Jenkins server host URL * @param array $config = [ diff --git a/src/JenkinsConfig.php b/src/JenkinsConfig.php new file mode 100644 index 0000000..36a7778 --- /dev/null +++ b/src/JenkinsConfig.php @@ -0,0 +1,66 @@ +hostUrl . '/job/' . $jobName; + } + + /** + * @param string $viewName + * + * @return string + */ + public function viewPageUrl(string $viewName): string + { + return $this->hostUrl . '/view/' . $viewName; + } + +} diff --git a/src/MultiJenkins.php b/src/MultiJenkins.php new file mode 100644 index 0000000..a8b95ca --- /dev/null +++ b/src/MultiJenkins.php @@ -0,0 +1,168 @@ + ['username' => '', 'apiToken' => '', 'hostUrl' => ''], + * 'prod' => ['username' => '', 'apiToken' => '', 'hostUrl' => ''], + * ] + */ + public array $envInfo = []; + + /** + * @param string $env + * + * @return Jenkins + */ + public function getJenkins(string $env = ''): Jenkins + { + return $this->create($env); + } + + /** + * @param string $env + * + * @return Jenkins + */ + public function create(string $env = ''): Jenkins + { + $jc = $this->getEnvConfig($env); + + return new Jenkins($jc->hostUrl, [ + 'enableCache' => $this->enableCache, + 'cacheDir' => $this->cacheDir, + 'username' => $jc->username, + 'apiToken' => $jc->apiToken, + 'password' => $jc->password, + ]); + } + + /** + * @param string $envName + * + * @return $this + */ + public function useEnv(string $envName): self + { + return $this->setEnvName($envName); + } + + /** + * @param string $envName + * + * @return $this + */ + public function setEnvName(string $envName): self + { + if ($envName) { + $this->envName = $envName; + } + return $this; + } + + /** + * @param string $envName + * + * @return JenkinsConfig + */ + public function getEnvConfig(string $envName = ''): JenkinsConfig + { + $defConf = $this->getDefaultConfig(); + $envName = $envName ?: $this->envName; + + if ($envName) { + if (isset($this->envInfo[$envName])) { + return JenkinsConfig::new(array_merge($defConf->toArray(), $this->envInfo[$envName])); + } + + throw new RuntimeException("get unknown env config: $envName"); + } + + return $defConf; + } + + /** + * @return JenkinsConfig + */ + public function getDefaultConfig(): JenkinsConfig + { + if (!$this->defaultConfig) { + $this->defaultConfig = JenkinsConfig::new([ + 'hostUrl' => $this->hostUrl, + 'username' => $this->username, + 'password' => $this->password, + 'apiToken' => $this->apiToken, + ]); + } + + return $this->defaultConfig; + } + + +}