Skip to content

Commit

Permalink
up: update some params and return value type
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 16, 2022
1 parent 0b6e3ba commit 3bc90ef
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 80 deletions.
80 changes: 25 additions & 55 deletions src/Jenkins.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace PhpPkg\JenkinsClient;

use DomDocument;
use InvalidArgumentException;
use PhpPkg\JenkinsClient\Jenkins\Job;
use RuntimeException;
Expand Down Expand Up @@ -464,12 +463,12 @@ public function getBuild($job, $buildId, string $tree = 'actions[parameters,para
* @param string $job
* @param int $buildId
*
* @return null|string
* @return string
*/
public function getUrlBuild(string $job, int $buildId): ?string
public function getBuildUrl(string $job, int $buildId): string
{
return (null === $buildId) ?
$this->getUrlJob($job)
return $buildId < 1 ?
$this->getJobUrl($job)
: sprintf('%s/job/%s/%d', $this->baseUrl, $job, $buildId);
}

Expand Down Expand Up @@ -510,61 +509,34 @@ public function getUrl(): string
}

/**
* @param string $job
* @param string $jobName
*
* @return string
*/
public function getUrlJob(string $job): string
public function getJobUrl(string $jobName): string
{
return sprintf('%s/job/%s', $this->baseUrl, $job);
return sprintf('%s/job/%s', $this->baseUrl, $jobName);
}

/**
* getUrlView
* get View Url
*
* @param string $view
*
* @return string
*/
public function getUrlView(string $view): string
public function getViewUrl(string $view): string
{
return sprintf('%s/view/%s', $this->baseUrl, $view);
}

/**
* @param string $jobname
*
* @return string
*
* @throws RuntimeException
*@deprecated use getJobConfig instead
*
*/
public function retrieveXmlConfigAsString(string $jobname): string
{
return $this->getJobConfig($jobname);
}

/**
* @param string $jobname
* @param DomDocument $document
*
* @deprecated use setJobConfig instead
*/
public function setConfigFromDomDocument(string $jobname, DomDocument $document): void
{
$this->setJobConfig($jobname, $document->saveXML());
}

/**
* @param string $jobname
* @param string $jobName
* @param string $xmlConfiguration
*
* @throws InvalidArgumentException
*/
public function createJob(string $jobname, string $xmlConfiguration): void
public function createJob(string $jobName, string $xmlConfiguration): void
{
$url = sprintf('%s/createItem?name=%s', $this->baseUrl, $jobname);
$url = sprintf('%s/createItem?name=%s', $this->baseUrl, $jobName);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, 1);

Expand All @@ -582,22 +554,22 @@ public function createJob(string $jobname, string $xmlConfiguration): void
$response = curl_exec($curl);

if (curl_getinfo($curl, CURLINFO_HTTP_CODE) !== 200) {
throw new InvalidArgumentException(sprintf('Job %s already exists', $jobname));
throw new InvalidArgumentException(sprintf('Job %s already exists', $jobName));
}
if (curl_errno($curl)) {
throw new RuntimeException(sprintf('Error creating job %s', $jobname));
throw new RuntimeException(sprintf('Error creating job %s', $jobName));
}
}

/**
* @param string $jobname
* @param string $jobName
* @param $configuration
*
* @internal param string $document
*/
public function setJobConfig(string $jobname, $configuration): void
public function setJobConfig(string $jobName, $configuration): void
{
$url = sprintf('%s/job/%s/config.xml', $this->baseUrl, $jobname);
$url = sprintf('%s/job/%s/config.xml', $this->baseUrl, $jobName);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $configuration);
Expand All @@ -611,22 +583,22 @@ public function setJobConfig(string $jobname, $configuration): void
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_exec($curl);

$this->validateCurl($curl, sprintf('Error during setting configuration for job %s', $jobname));
$this->validateCurl($curl, sprintf('Error during setting configuration for job %s', $jobName));
}

/**
* @param string $jobname
* @param string $jobName
*
* @return string
*/
public function getJobConfig(string $jobname): string
public function getJobConfig(string $jobName): string
{
$url = sprintf('%s/job/%s/config.xml', $this->baseUrl, $jobname);
$url = sprintf('%s/job/%s/config.xml', $this->baseUrl, $jobName);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$ret = curl_exec($curl);

$this->validateCurl($curl, sprintf('Error during getting configuration for job %s', $jobname));
$this->validateCurl($curl, sprintf('Error during getting configuration for job %s', $jobName));

return $ret;
}
Expand Down Expand Up @@ -666,7 +638,6 @@ public function stopExecutor(Jenkins\Executor $executor): void
/**
* @param Jenkins\JobQueue $queue
*
* @throws RuntimeException
* @return void
*/
public function cancelQueue(Jenkins\JobQueue $queue): void
Expand Down Expand Up @@ -740,14 +711,14 @@ public function deleteComputer(string $computerName): void
}

/**
* @param string $jobname
* @param string $jobName
* @param string $buildNumber
*
* @return string
*/
public function getConsoleTextBuild(string $jobname, string $buildNumber): string
public function getConsoleTextBuild(string $jobName, string $buildNumber): string
{
$url = sprintf('%s/job/%s/%s/consoleText', $this->baseUrl, $jobname, $buildNumber);
$url = sprintf('%s/job/%s/%s/consoleText', $this->baseUrl, $jobName, $buildNumber);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

Expand All @@ -760,7 +731,6 @@ public function getConsoleTextBuild(string $jobname, string $buildNumber): strin
*
* @return array|Jenkins\TestReport
* @internal param string $buildNumber
*
*/
public function getTestReport(string $jobName, $buildId): array|Jenkins\TestReport
{
Expand Down
40 changes: 22 additions & 18 deletions src/Jenkins/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
use PhpPkg\JenkinsClient\Jenkins;
use stdClass;

/**
* class Build
*
* @author inhere
* @date 2022/11/16
*/
class Build
{
/**
Expand Down Expand Up @@ -85,19 +91,19 @@ public function getInputParameters(): array
/**
* @return int
*/
public function getTimestamp(): float|int
public function getTimestamp(): int
{
//division par 1000 => pas de millisecondes
return $this->build->timestamp / 1000;
//division par 1000 => pas de milliseconds
return (int)($this->build->timestamp / 1000);
}

/**
* @return int
*/
public function getDuration(): float|int
public function getDuration(): int
{
//division par 1000 => pas de millisecondes
return $this->build->duration / 1000;
//division par 1000 => pas de milliseconds
return (int)($this->build->duration / 1000);
}

/**
Expand All @@ -122,22 +128,22 @@ public function getProgress(): ?int
}

/**
* @return float|null
* @return int
*/
public function getEstimatedDuration(): float|int|null
public function getEstimatedDuration(): int
{
//since version 1.461 estimatedDuration is displayed in jenkins's api
//we can use it witch is more accurate than calcule ourselves
//but older versions need to continue to work, so in case of estimated
//duration is not found we fallback to calcule it.
if (property_exists($this->build, 'estimatedDuration')) {
return $this->build->estimatedDuration / 1000;
return (int)($this->build->estimatedDuration / 1000);
}

$duration = null;
$duration = 0;
$progress = $this->getProgress();
if (null !== $progress && $progress >= 0) {
$duration = ceil((time() - $this->getTimestamp()) / ($progress / 100));
$duration = (int)ceil((time() - $this->getTimestamp()) / ($progress / 100));
}

return $duration;
Expand All @@ -163,20 +169,18 @@ public function getRemainingExecutionTime(): ?int
}

/**
* @return null|string
* @return string
*/
public function getResult(): ?string
public function getResult(): string
{
$result = match ($this->build->result) {
return match ($this->build->result) {
'FAILURE' => self::FAILURE,
'SUCCESS' => self::SUCCESS,
'UNSTABLE' => self::UNSTABLE,
'ABORTED' => self::ABORTED,
'WAITING' => self::WAITING,
default => self::RUNNING,
};

return $result;
}

/**
Expand Down Expand Up @@ -227,9 +231,9 @@ public function getJenkins(): Jenkins
/**
* @param Jenkins $jenkins
*
* @return Build|Job
* @return Build
*/
public function setJenkins(Jenkins $jenkins): Job|static
public function setJenkins(Jenkins $jenkins): static
{
$this->jenkins = $jenkins;

Expand Down
6 changes: 6 additions & 0 deletions src/Jenkins/Computer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
use PhpPkg\JenkinsClient\Jenkins;
use stdClass;

/**
* class Computer
*
* @author inhere
* @date 2022/11/16
*/
class Computer
{
/**
Expand Down
6 changes: 6 additions & 0 deletions src/Jenkins/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
use PhpPkg\JenkinsClient\Jenkins;
use stdClass;

/**
* class Executor
*
* @author inhere
* @date 2022/11/16
*/
class Executor
{
/**
Expand Down
6 changes: 6 additions & 0 deletions src/Jenkins/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
use RuntimeException;
use stdClass;

/**
* class Job
*
* @author inhere
* @date 2022/11/16
*/
class Job
{
/**
Expand Down
6 changes: 6 additions & 0 deletions src/Jenkins/JobQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
use PhpPkg\JenkinsClient\Jenkins;
use stdClass;

/**
* class JobQueue
*
* @author inhere
* @date 2022/11/16
*/
class JobQueue
{
/**
Expand Down
20 changes: 13 additions & 7 deletions src/Jenkins/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
use PhpPkg\JenkinsClient\Jenkins;
use stdClass;

/**
* class View
*
* @author inhere
* @date 2022/11/16
*/
class View
{
/**
Expand Down Expand Up @@ -45,17 +51,17 @@ public function getName(): string
/**
* @return string
*/
public function getDescription(): ?string
public function getDescription(): string
{
return $this->view->description ?? null;
return $this->view->description ?? '';
}

/**
* @return string
*/
public function getURL(): ?string
public function getURL(): string
{
return $this->view->url ?? null;
return $this->view->url ?? '';
}

/**
Expand All @@ -73,7 +79,7 @@ public function getJobs(): array
}

/**
* getColor
* get color
*
* @return string
*/
Expand All @@ -90,13 +96,13 @@ public function getColor(): string
}

/**
* getColorPriority
* get Color Priority
*
* @param string $color
*
* @return int
*/
protected function getColorPriority(string $color): ?int
protected function getColorPriority(string $color): int
{
switch ($color) {
default:
Expand Down

0 comments on commit 3bc90ef

Please sign in to comment.