diff --git a/src/Jenkins.php b/src/Jenkins.php index 32b0ab3..392b480 100755 --- a/src/Jenkins.php +++ b/src/Jenkins.php @@ -46,7 +46,7 @@ class Jenkins /** * @param string $baseUrl */ - public function __construct($baseUrl) + public function __construct($baseUrl) { $this->baseUrl = $baseUrl; } @@ -279,7 +279,7 @@ public function launchJob($jobName, $parameters = array()) */ public function getJob($jobName) { - $url = sprintf('%s/job/%s/api/json', $this->baseUrl, $jobName); + $url = sprintf('%s/job/%s/api/json', $this->baseUrl, Jenkins::urlSanitise($jobName)); $curl = curl_init($url); curl_setopt($curl, \CURLOPT_RETURNTRANSFER, 1); @@ -427,7 +427,9 @@ public function getBuild($job, $buildId, $tree = 'actions[parameters,parameters[ if ($tree !== null) { $tree = sprintf('?tree=%s', $tree); } + $url = sprintf('%s/job/%s/%d/api/json%s', $this->baseUrl, $job, $buildId, $tree); + $curl = curl_init($url); curl_setopt($curl, \CURLOPT_RETURNTRANSFER, 1); @@ -826,4 +828,15 @@ public function getComputerConfiguration($computerName) { return $this->execute(sprintf('/computer/%s/config.xml', $computerName), array(\CURLOPT_RETURNTRANSFER => 1,)); } + + /** + * URL sanitise a given string + * @param $string + * @return mixed|string + */ + static public function urlSanitise($string){ + $string = urlencode($string); + $string = str_replace("+", "%20", $string); + return $string; + } } diff --git a/src/Jenkins/Build.php b/src/Jenkins/Build.php index 0e87469..5f77ce1 100755 --- a/src/Jenkins/Build.php +++ b/src/Jenkins/Build.php @@ -103,6 +103,41 @@ public function getNumber() return $this->build->number; } + /** + * @return string + */ + public function getDescription() + { + return $this->build->description; + } + + public function getGitRevision() + { + foreach($this->build->actions as $action){ + if(property_exists($action, 'buildsByBranchName')){ + return $action->lastBuiltRevision->SHA1; + } + } + } + + public function getGitBranch() + { + foreach($this->build->actions as $action){ + if(property_exists($action, 'buildsByBranchName')){ + return $action->lastBuiltRevision->branch[0]->name; + } + } + } + + public function getGitRemoteURL() + { + foreach($this->build->actions as $action){ + if(property_exists($action, 'buildsByBranchName')){ + return $action->remoteUrls[0]; + } + } + } + /** * @return null|int */ diff --git a/src/Jenkins/Job.php b/src/Jenkins/Job.php index 2eb2c14..89f03e4 100755 --- a/src/Jenkins/Job.php +++ b/src/Jenkins/Job.php @@ -50,7 +50,7 @@ public function getBuilds() */ public function getJenkinsBuild($buildId) { - return $this->getJenkins()->getBuild($this->getName(), $buildId); + return $this->getJenkins()->getBuild($this->getNameURLSafe(), $buildId); } /** @@ -61,6 +61,15 @@ public function getName() return $this->job->name; } + /** + * Return a version of the name that is safe for URLs. + * @return string + */ + public function getNameURLSafe() + { + return Jenkins::urlSanitise($this->getName()); + } + /** * @return array */ @@ -110,7 +119,7 @@ public function getColor() */ public function retrieveXmlConfigAsString() { - return $this->jenkins->retrieveXmlConfigAsString($this->getName()); + return $this->jenkins->retrieveXmlConfigAsString($this->getNameURLSafe()); } /**