Skip to content

Commit

Permalink
refacotor: use stdlib.DataObject instead of stdClass
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 18, 2022
1 parent 5823c28 commit c0abb17
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 137 deletions.
71 changes: 52 additions & 19 deletions src/Jenkins.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private function loadJenkinsInfo(): void
*/
public function getCacheFile(): string
{
$filename = md5($this->baseUrl . $this->username) . '.json';
$filename = md5($this->baseUrl . $this->username) . '.json';

return File::join($this->cacheDir, $filename);
}
Expand Down Expand Up @@ -247,7 +247,7 @@ public function getExecutors(string $computer = '(master)'): array
throw new RuntimeException(sprintf('Error on get information for executors[%s@%s]', $i, $computer));
}

$infos = $cli->getJsonObject();
$infos = $cli->getDataObject();

$executors[] = new Jenkins\Executor($infos, $computer, $this);
}
Expand All @@ -256,6 +256,41 @@ public function getExecutors(string $computer = '(master)'): array
}

/**
* trigger hook build like use gitlab
*
* @param string $jobName
* @param array $hookData
*
* @return bool
*/
public function triggerHook(string $jobName, array $hookData = []): bool
{
$url = $this->buildUrl('/project/%s', $jobName);
$cli = $this->getHttpClient()->post($url, $hookData);

if (!$cli->isSuccess()) {
throw new RuntimeException(sprintf('Error trying to launch job "%s"(%s)', $jobName, $url));
}

return true;
}

/**
* trigger job build, can with parameters
*
* @param string $jobName
* @param array $parameters
*
* @return bool
*/
public function triggerBuild(string $jobName, array $parameters = []): bool
{
return $this->launchJob($jobName, $parameters);
}

/**
* launch job build, can with parameters
*
* @param string $jobName
* @param array $parameters
*
Expand All @@ -277,7 +312,8 @@ public function launchJob(string $jobName, array $parameters = []): bool
$cli = $this->getHttpClient()->post($url, $parameters, $headers);

if (!$cli->isSuccess()) {
throw new RuntimeException(sprintf('Error trying to launch job "%s"(%s)', $jobName, $url));
// $respStr = $cli->getResponseBody();
throw new RuntimeException(sprintf('Error trying to launch job "%s"', $jobName));
}

return true;
Expand All @@ -297,7 +333,7 @@ public function getJob(string $jobName): Job
throw new RuntimeException(sprintf('Error on get information for job %s', $jobName));
}

$infos = $cli->getJsonObject();
$infos = $cli->getDataObject();

return new Jenkins\Job($infos, $this);
}
Expand All @@ -320,7 +356,7 @@ public function getJobParams(string $jobName, string $tree = ''): Job
throw new RuntimeException(sprintf('Error on get information for job %s', $jobName));
}

$infos = $cli->getJsonObject();
$infos = $cli->getDataObject();

return new Jenkins\Job($infos, $this);
}
Expand Down Expand Up @@ -357,7 +393,7 @@ public function getQueue(): Jenkins\Queue
throw new RuntimeException('Error on get information for queues');
}

$infos = $cli->getJsonObject();
$infos = $cli->getDataObject();

return new Jenkins\Queue($infos, $this);
}
Expand Down Expand Up @@ -406,7 +442,7 @@ public function getView(string $viewName): Jenkins\View
throw new RuntimeException(sprintf('Error on get information for view %s', $viewName));
}

$infos = $cli->getJsonObject();
$infos = $cli->getDataObject();

return new Jenkins\View($infos, $this);
}
Expand Down Expand Up @@ -434,9 +470,7 @@ public function getBuild(
throw new RuntimeException(sprintf('Error on get information for build %s#%d', $job, $buildId));
}

$infos = $cli->getJsonObject();

return new Jenkins\Build($infos, $this);
return new Jenkins\Build($cli->getDataObject(), $this);
}

/**
Expand Down Expand Up @@ -466,8 +500,7 @@ public function getComputer(string $computerName): Jenkins\Computer
throw new RuntimeException(sprintf('Error on get information for computer %s', $computerName));
}

$infos = $cli->getJsonObject();
return new Jenkins\Computer($infos, $this);
return new Jenkins\Computer($cli->getDataObject(), $this);
}

/**
Expand Down Expand Up @@ -654,17 +687,17 @@ public function deleteComputer(string $computerName): void

/**
* @param string $jobName
* @param string $buildNumber
* @param int $buildNumber
*
* @return string
*/
public function getConsoleTextBuild(string $jobName, string $buildNumber): string
public function getBuildConsoleText(string $jobName, int $buildNumber): string
{
$url = $this->buildUrl('/job/%s/%s/consoleText', $jobName, $buildNumber);
$url = $this->buildUrl('/job/%s/%d/consoleText', $jobName, $buildNumber);
$cli = $this->getHttpClient()->get($url);

if (!$cli->isSuccess()) {
throw new RuntimeException('Error on get computers information');
throw new RuntimeException(sprintf('Error on get %s build#%d console text information', $jobName, $buildNumber));
}

return $cli->getResponseBody();
Expand All @@ -685,7 +718,7 @@ public function getTestReport(string $jobName, int $buildId): Jenkins\TestReport
throw new RuntimeException(sprintf('Error on get information for build %s#%d', $jobName, $buildId));
}

$infos = $cli->getJsonObject();
$infos = $cli->getDataObject();

return new Jenkins\TestReport($this, $infos, $jobName, $buildId);
}
Expand All @@ -702,11 +735,11 @@ public function getComputers(): array
throw new RuntimeException('Error on get computers information');
}

$infos = $cli->getJsonObject();
$infos = $cli->getDataObject();

$computers = [];
foreach ($infos->computer as $computer) {
$computers[] = $this->getComputer($computer->displayName);
$computers[] = $this->getComputer($computer['displayName']);
}

return $computers;
Expand Down
27 changes: 14 additions & 13 deletions src/Jenkins/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace PhpPkg\JenkinsClient\Jenkins;

use PhpPkg\JenkinsClient\Jenkins;
use stdClass;
use Toolkit\Stdlib\Obj\DataObject;

/**
* class Build
Expand Down Expand Up @@ -51,38 +51,39 @@ class Build
public const ABORTED = 'ABORTED';

/**
* @var stdClass
* @var DataObject
*/
private stdClass $build;
private DataObject $build;

/**
* @var Jenkins
*/
private Jenkins $jenkins;

/**
* @param stdClass $build
* @param Jenkins $jenkins
* @param DataObject $build
* @param Jenkins $jenkins
*/
public function __construct(stdClass $build, Jenkins $jenkins)
public function __construct(DataObject $build, Jenkins $jenkins)
{
$this->build = $build;
$this->setJenkins($jenkins);
$this->jenkins = $jenkins;
}

/**
* @return array
*/
public function getInputParameters(): array
{
$parameters = [];

if (!property_exists($this->build->actions[0], 'parameters')) {
return $parameters;
$actions = $this->build->actions;
if (!isset($actions[0]['parameters'])) {
return [];
}

foreach ($this->build->actions[0]->parameters as $parameter) {
$parameters[$parameter->name] = $parameter->value;
// DataObject::new($actions[0]['parameters']);
$parameters = [];
foreach ($actions[0]['parameters'] as $parameter) {
$parameters[$parameter['name']] = $parameter['value'];
}

return $parameters;
Expand Down
35 changes: 21 additions & 14 deletions src/Jenkins/Computer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace PhpPkg\JenkinsClient\Jenkins;

use PhpPkg\JenkinsClient\Jenkins;
use stdClass;
use Toolkit\Stdlib\Obj\DataObject;

/**
* class Computer
Expand All @@ -21,23 +21,31 @@
class Computer
{
/**
* @var stdClass
* @var DataObject
*/
private stdClass $computer;
private DataObject $computer;

/**
* @var Jenkins
*/
private Jenkins $jenkins;

/**
* @param stdClass $computer
* @param DataObject $computer
* @param Jenkins $jenkins
*/
public function __construct(stdClass $computer, Jenkins $jenkins)
public function __construct(DataObject $computer, Jenkins $jenkins)
{
$this->computer = $computer;
$this->setJenkins($jenkins);
$this->jenkins = $jenkins;
}

/**
* @return DataObject
*/
public function getData(): DataObject
{
return $this->computer;
}

/**
Expand All @@ -59,14 +67,14 @@ public function isOffline(): bool

/**
*
* returns null when computer is launching
* returns \stdClass when computer has been put offline
* returns [] when computer is launching
* returns non-empty when computer has been put offline
*
* @return null|stdClass
* @return array
*/
public function getOfflineCause(): ?stdClass
public function getOfflineCause(): array
{
return $this->computer->offlineCause;
return $this->computer->getArray('offlineCause');
}

/**
Expand All @@ -86,8 +94,7 @@ public function toggleOffline(): Computer
*/
public function delete(): Computer
{
$this->getJenkins()
->deleteComputer($this->getName());
$this->jenkins->deleteComputer($this->getName());

return $this;
}
Expand Down Expand Up @@ -117,6 +124,6 @@ public function setJenkins(Jenkins $jenkins): Computer
*/
public function getConfiguration(): string
{
return $this->getJenkins()->getComputerConfig($this->getName());
return $this->jenkins->getComputerConfig($this->getName());
}
}
Loading

0 comments on commit c0abb17

Please sign in to comment.