diff --git a/app/Concern/InitApplicationTrait.php b/app/Concern/InitApplicationTrait.php index 25ed2ba..b121ade 100644 --- a/app/Concern/InitApplicationTrait.php +++ b/app/Concern/InitApplicationTrait.php @@ -5,6 +5,7 @@ use Inhere\Kite\Common\GitAPI\GitHubV3API; use Inhere\Kite\Common\GitAPI\GitLabV4API; use Inhere\Kite\Kite; +use Inhere\Kite\Lib\Jenkins\JenkinsClient; use Monolog\Handler\RotatingFileHandler; use Monolog\Logger; use PhpPkg\EasyTpl\EasyTemplate; @@ -122,5 +123,10 @@ protected function registerComServices(ObjectBox $box): void $config = $this->getArrayParam('github'); return new GitHubV3API($config); }); + + $box->set('jenkins', function () { + $config = $this->getArrayParam('jenkins'); + return new JenkinsClient($config); + }); } } diff --git a/app/Console/Command/ToolCommand.php b/app/Console/Command/ToolCommand.php index 2cb83b8..cf9f44f 100644 --- a/app/Console/Command/ToolCommand.php +++ b/app/Console/Command/ToolCommand.php @@ -13,6 +13,7 @@ use Inhere\Console\IO\Input; use Inhere\Console\IO\Output; use Inhere\Kite\Console\SubCmd\OpenCmd; +use Inhere\Kite\Console\SubCmd\ToolCmd\HashHmacCommand; /** * Class ToolCommand @@ -26,6 +27,7 @@ protected function subCommands(): array { return [ OpenCmd::class, + HashHmacCommand::class, ]; } diff --git a/app/Console/SubCmd/ToolCmd/HashHmacCommand.php b/app/Console/SubCmd/ToolCmd/HashHmacCommand.php new file mode 100644 index 0000000..fd6ec23 --- /dev/null +++ b/app/Console/SubCmd/ToolCmd/HashHmacCommand.php @@ -0,0 +1,61 @@ +flags->addOptByRule($name, $rule); + $this->flags->addArg('str', 'want to signed string. allow: @c', 'string', true); + $this->flags->addOpt('algo', 'a', 'Name of selected hashing algorithm. eg: md5, sha256', 'string', false, 'sha256'); + $this->flags->addOpt( + 'key', 'k', + 'Shared secret key used for generating the HMAC variant of the message digest.', + 'string', true); + } + + protected function execute(Input $input, Output $output) + { + $fs = $this->flags; + + $secKey = $fs->getOpt('key'); + $rawStr = $fs->getArg('str'); + $rawStr = ContentsAutoReader::readFrom($rawStr); + + $algoName = $fs->getOpt('algo'); + $output->aList([ + 'key' => $secKey, + 'algo' => $algoName, + ]); + $output->colored('RAW STRING:'); + $output->println($rawStr); + + if ($algoName === 'md5') { + $signStr = md5($rawStr); + } else { + $signStr = hash_hmac($algoName, $rawStr, $secKey); + } + + $signStr = strtoupper($signStr); + + $output->colored('SIGN:'); + $output->println($signStr); + } +} diff --git a/app/Kite.php b/app/Kite.php index 9238d7b..2706c39 100644 --- a/app/Kite.php +++ b/app/Kite.php @@ -17,6 +17,7 @@ use Inhere\Kite\Console\Component\AutoSetProxyEnv; use Inhere\Kite\Console\Plugin\PluginManager; use Inhere\Kite\Http\WebApplication; +use Inhere\Kite\Lib\Jenkins\JenkinsClient; use Inhere\Kite\Lib\Jump\QuickJump; use Inhere\Route\Dispatcher\Dispatcher; use Inhere\Route\Router; @@ -36,6 +37,7 @@ * @method static Router webRouter() * @method static Dispatcher dispatcher() * @method static ScriptRunner scriptRunner() + * @method static JenkinsClient jenkins() * * @see Kite::__callStatic() for quick get object */ diff --git a/app/Lib/Jenkins/JenkinsClient.php b/app/Lib/Jenkins/JenkinsClient.php index a12900f..61517d4 100644 --- a/app/Lib/Jenkins/JenkinsClient.php +++ b/app/Lib/Jenkins/JenkinsClient.php @@ -2,40 +2,126 @@ namespace Inhere\Kite\Lib\Jenkins; -use Psr\Http\Client\ClientInterface; -use Toolkit\Stdlib\Obj\Traits\QuickInitTrait; +use PhpPkg\Http\Client\AbstractClient; +use PhpPkg\Http\Client\Client; +use Toolkit\Stdlib\Obj\AbstractObj; +use function strtr; /** * class JenkinsClient * * @author inhere */ -class JenkinsClient +class JenkinsClient extends AbstractObj { - use QuickInitTrait; + /** + * @var AbstractClient|null + */ + private ?AbstractClient $httpClient; + + /** + * @var string + */ + public string $hostUrl = ''; /** - * @var ClientInterface + * Jenkins username + * + * @var string + */ + public string $username = ''; + + /** + * @var string */ - private ClientInterface $httpClient; + public string $apiToken = ''; /** + * eg: /some + * * @var string */ - public string $baseUrl = ''; + public string $folderPath = ''; + + /** + * @var string + */ + public string $jobName = ''; + + /** + * @param string $jobName + * + * @return $this + */ + public function withJobName(string $jobName): self + { + $this->jobName = $jobName; + return $this; + } + + /** + * @param array $params + * + * @return string + */ + public function buildWithParams(array $params): string + { + $tpl = '{folderPath}/job/{name}/buildWithParameters'; + $url = $this->buildUrl($tpl); + $cli = $this->getHttpClient()->post($url, $params); + + return $cli->getResponseBody(); + } + + public function buildNoParams(): self + { + $tpl = '{folderPath}/job/{name}/build'; + $url = $this->buildUrl($tpl); + + return $this; + } /** - * @return ClientInterface + * @param string $pathTpl + * + * @return string */ - public function getHttpClient(): ClientInterface + public function buildUrl(string $pathTpl): string { + return $this->hostUrl . strtr($pathTpl, [ + '{folderPath}' => $this->folderPath, + '{name}' => $this->jobName, + ]); + } + + /** + * @param string $jobName + * + * @return string + */ + public function jobPageUrl(string $jobName = ''): string + { + $jobName = $jobName ?: $this->jobName; + + return $this->hostUrl . '/job/' . $jobName; + } + + /** + * @return AbstractClient + */ + public function getHttpClient(): AbstractClient + { + if (!$this->httpClient) { + $this->httpClient = Client::factory([]); + } + return $this->httpClient; } /** - * @param ClientInterface $httpClient + * @param AbstractClient $httpClient */ - public function setHttpClient(ClientInterface $httpClient): void + public function setHttpClient(AbstractClient $httpClient): void { $this->httpClient = $httpClient; } diff --git a/config/config.php b/config/config.php index c46be55..90e75fc 100644 --- a/config/config.php +++ b/config/config.php @@ -5,25 +5,25 @@ $basePath = Inhere\Kite\Kite::basePath(); return [ - 'app' => [ + 'app' => [ ], - 'logger' => [ + 'logger' => [ 'name' => 'Kite', 'logfile' => OS::userCacheDir('kite.log'), ], - 'git' => [ + 'git' => [ // remote 'mainRemote' => 'main', 'forkRemote' => 'origin', // 'auto-sign' => true, // 'sign-text' => 'inhere ', ], - 'gitflow' => [ + 'gitflow' => [ // remote 'mainRemote' => 'main', 'forkRemote' => 'origin', ], - 'gitlab' => [ + 'gitlab' => [ // remote 'mainRemote' => 'main', 'forkRemote' => 'origin', @@ -36,7 +36,7 @@ // gitlab api config 'baseUrl' => '', ], - 'github' => [ + 'github' => [ // remote 'mainRemote' => 'main', 'forkRemote' => 'origin', @@ -44,15 +44,15 @@ 'defaultGroup' => 'swoft', 'defaultForkGroup' => 'ulue', 'redirectGit' => [ - '*' + '*' ], // github api config // 'baseUrl' => '', ], - 'osEnv' => [ + 'osEnv' => [ // env settings ], - 'osPathEnv' => [ + 'osPathEnv' => [ // os path env settings // '/path/to/my-tool/bin', ], @@ -61,8 +61,13 @@ // 'http_proxy' => 'http://127.0.0.1:1081', // 'https_proxy' => 'http://127.0.0.1:1081', ], + 'jenkins' => [ + 'hostUrl' => getenv('JK_HOST_URL') ?: '', + 'username' => getenv('JK_UNAME') ?: '', + 'apiToken' => getenv('JK_API_TOKEN') ?: '', + ], // tool command usage docs - 'manDocs' => [ + 'manDocs' => [ // if 'lang' not setting, will read from ENV. // 'lang' => 'en', 'fallbackLang' => 'en', @@ -82,16 +87,16 @@ ], ], /** @see \Inhere\Kite\Component\ScriptRunner */ - 'scriptRunner' => [ - 'enable' => true, + 'scriptRunner' => [ + 'enable' => true, ], - 'scriptDirs' => [ + 'scriptDirs' => [ // BASE_PATH . '/script', $basePath . '/script', $basePath . '/custom/script', ], // custom scripts for quick run an command - 'scripts' => require 'scripts.php', + 'scripts' => require 'scripts.php', // command aliases. element is: alias command => real command - 'aliases' => require 'aliases.php', + 'aliases' => require 'aliases.php', ];