Skip to content

Commit

Permalink
feat: support multi jenkins manage and switch
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 17, 2022
1 parent 30b7861 commit 5823c28
Show file tree
Hide file tree
Showing 4 changed files with 249 additions and 35 deletions.
35 changes: 0 additions & 35 deletions src/Factory.php

This file was deleted.

15 changes: 15 additions & 0 deletions src/Jenkins.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
66 changes: 66 additions & 0 deletions src/JenkinsConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php declare(strict_types=1);
/**
* This file is part of phppkg/jenkins-client.
*
* @link https://github.com/inhere
* @author https://github.com/inhere
* @license MIT
*/

namespace PhpPkg\JenkinsClient;

use Toolkit\Stdlib\Obj\AbstractObj;

/**
* class JenkinsConfig
*
* @author inhere
* @date 2022/11/16
*/
class JenkinsConfig extends AbstractObj
{
/**
* @var string
*/
public string $hostUrl = '';

/**
* Jenkins username
*
* @var string
*/
public string $username = '';

/**
* Jenkins user password
*
* @var string
*/
public string $password = '';

/**
* @var string
*/
public string $apiToken = '';

/**
* @param string $jobName
*
* @return string
*/
public function jobPageUrl(string $jobName): string
{
return $this->hostUrl . '/job/' . $jobName;
}

/**
* @param string $viewName
*
* @return string
*/
public function viewPageUrl(string $viewName): string
{
return $this->hostUrl . '/view/' . $viewName;
}

}
168 changes: 168 additions & 0 deletions src/MultiJenkins.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?php declare(strict_types=1);
/**
* This file is part of phppkg/jenkins-client.
*
* @link https://github.com/inhere
* @author https://github.com/inhere
* @license MIT
*/

namespace PhpPkg\JenkinsClient;

use RuntimeException;
use function array_merge;

/**
* class JenkinsFactory
*
* # API for jenkins:
*
* ## Jobs info
*
* - Jobs info:
* - /api/json?tree=jobs[*] All info
* - /api/json?tree=jobs[name] limit return fields
* - commonly fields: description,name,fullName,displayName,url,buildable,inQueue,concurrentBuild
* - show all info for a job: http://my-jenkins.dev/job/JOB_NAME/api/json?depth=0
*
* ## job property
*
* - job property parameters: /job/JOB_NAME/api/json?tree=property[parameterDefinitions[*]]
* - limit returns fields: /job/JOB_NAME/api/json?tree=property[parameterDefinitions[description,name,type,choices]]
*
* ## job builds info
*
* - latest build and full info: /job/JOB_NAME/api/json?tree=lastBuild[*]
* - latest N builds and full info: /job/JOB_NAME/api/json?tree=builds[*]{0,10}&depth=1
* - latest N builds and limit fields: /job/JOB_NAME/api/json?tree=builds[number,queueId,displayName,building,duration,timestamp]{0,6}
* - latest N builds and only field 'number': /job/JOB_NAME/api/json?tree=builds[number]{0,10}
* - build page URL: /job/JOB_NAME/1746/
* - console page URL: /job/JOB_NAME/1746/console
*
* @author inhere
*/
class MultiJenkins extends JenkinsConfig
{
private ?JenkinsConfig $defaultConfig = null;

/**
* @var bool cache jenkins info
*/
public bool $enableCache = false;

/**
* @var string cache dir
*/
public string $cacheDir = '';

/**
* @var string default job name
*/
public string $jobName = '';

/**
* @var string current env name.
*/
public string $envName = '';

/**
* can set config for diff env, multi jenkins.
*
* @var array = [
* 'dev' => ['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;
}


}

0 comments on commit 5823c28

Please sign in to comment.