Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed using full name for the Jobs #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 65 additions & 3 deletions src/Jenkins.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ public function getAllJobs()

$jobs = array();
foreach ($this->jenkins->jobs as $job) {
$jobs[$job->name] = array(
'name' => $job->name
$jobs[$job->fullName] = array(
'name' => $job->fullName
);
}

Expand All @@ -191,7 +191,7 @@ public function getJobs()

$jobs = array();
foreach ($this->jenkins->jobs as $job) {
$jobs[$job->name] = $this->getJob($job->name);
$jobs[$job->fullName] = $this->getJob($job->fullName);
}

return $jobs;
Expand Down Expand Up @@ -241,6 +241,8 @@ public function getExecutors($computer = '(master)')
*/
public function launchJob($jobName, $parameters = array())
{
$jobName = self::parseJobName($jobName);

if (0 === count($parameters)) {
$url = sprintf('%s/job/%s/build', $this->baseUrl, $jobName);
} else {
Expand Down Expand Up @@ -275,6 +277,8 @@ public function launchJob($jobName, $parameters = array())
*/
public function getJob($jobName)
{
$jobName = self::parseJobName($jobName);

$url = sprintf('%s/job/%s/api/json', $this->baseUrl, $jobName);
$curl = curl_init($url);

Expand Down Expand Up @@ -307,6 +311,8 @@ public function getJob($jobName)
*/
public function deleteJob($jobName)
{
$jobName = self::parseJobName($jobName);

$url = sprintf('%s/job/%s/doDelete', $this->baseUrl, $jobName);
$curl = curl_init($url);

Expand Down Expand Up @@ -420,6 +426,9 @@ public function getBuild($job, $buildId, $tree = 'actions[parameters,parameters[
if ($tree !== null) {
$tree = sprintf('?tree=%s', $tree);
}

$job = self::parseJobName($job);

$url = sprintf('%s/job/%s/%d/api/json%s', $this->baseUrl, $job, $buildId, $tree);
$curl = curl_init($url);

Expand Down Expand Up @@ -448,6 +457,8 @@ public function getBuild($job, $buildId, $tree = 'actions[parameters,parameters[
*/
public function getUrlBuild($job, $buildId)
{
$job = self::parseJobName($job);

return (null === $buildId) ?
$this->getUrlJob($job)
: sprintf('%s/job/%s/%d', $this->baseUrl, $job, $buildId);
Expand Down Expand Up @@ -496,6 +507,8 @@ public function getUrl()
*/
public function getUrlJob($job)
{
$job = self::parseJobName($job);

return sprintf('%s/job/%s', $this->baseUrl, $job);
}

Expand All @@ -522,6 +535,8 @@ public function getUrlView($view)
*/
public function retrieveXmlConfigAsString($jobname)
{
$jobname = self::parseJobName($jobname);

return $this->getJobConfig($jobname);
}

Expand All @@ -533,6 +548,8 @@ public function retrieveXmlConfigAsString($jobname)
*/
public function setConfigFromDomDocument($jobname, \DomDocument $document)
{
$jobname = self::parseJobName($jobname);

$this->setJobConfig($jobname, $document->saveXML());
}

Expand All @@ -544,6 +561,8 @@ public function setConfigFromDomDocument($jobname, \DomDocument $document)
*/
public function createJob($jobname, $xmlConfiguration)
{
$jobname = self::parseJobName($jobname);

$url = sprintf('%s/createItem?name=%s', $this->baseUrl, $jobname);
$curl = curl_init($url);
curl_setopt($curl, \CURLOPT_POST, 1);
Expand Down Expand Up @@ -577,6 +596,8 @@ public function createJob($jobname, $xmlConfiguration)
*/
public function setJobConfig($jobname, $configuration)
{
$jobname = self::parseJobName($jobname);

$url = sprintf('%s/job/%s/config.xml', $this->baseUrl, $jobname);
$curl = curl_init($url);
curl_setopt($curl, \CURLOPT_POST, 1);
Expand All @@ -601,6 +622,8 @@ public function setJobConfig($jobname, $configuration)
*/
public function getJobConfig($jobname)
{
$jobname = self::parseJobName($jobname);

$url = sprintf('%s/job/%s/config.xml', $this->baseUrl, $jobname);
$curl = curl_init($url);
curl_setopt($curl, \CURLOPT_RETURNTRANSFER, 1);
Expand Down Expand Up @@ -725,6 +748,8 @@ public function deleteComputer($computerName)
*/
public function getConsoleTextBuild($jobname, $buildNumber)
{
$jobname = self::parseJobName($jobname);

$url = sprintf('%s/job/%s/%s/consoleText', $this->baseUrl, $jobname, $buildNumber);
$curl = curl_init($url);
curl_setopt($curl, \CURLOPT_RETURNTRANSFER, 1);
Expand All @@ -742,6 +767,8 @@ public function getConsoleTextBuild($jobname, $buildNumber)
*/
public function getTestReport($jobName, $buildId)
{
$jobName = self::parseJobName($jobName);

$url = sprintf('%s/job/%s/%d/testReport/api/json', $this->baseUrl, $jobName, $buildId);
$curl = curl_init($url);

Expand Down Expand Up @@ -837,4 +864,39 @@ private function validateCurl($curl, $errorMessage) {
throw new \RuntimeException(sprintf('Access Denied [HTTP status code 403] to %s"', $info['url']));
}
}

/**
* Converts a job name containting '/job/' between every word
*
* @param string $jobName
*
* @return string
*/
private static function parseJobName($jobName)
{
// Example:
// 'Github/my-project/master' --> 'Github/job/my-project/job/master'

if (strpos($jobName, '/') !== false) {

$new = [];
$parts = explode('/', $jobName);
$last = count($parts) - 1;

foreach ($parts as $i => $part) {

$next = $i + 1;
$new[] = $part;

// add an extra job-item after, if next isn't a 'job' already and if it's not the last
if ($part !== 'job' && $i < $last && isset($parts[$next]) && $parts[$next] !== 'job') {
$new[] = 'job';
}
}

$jobName = implode('/', $new);
}

return $jobName;
}
}
2 changes: 1 addition & 1 deletion src/Jenkins/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function getJenkinsBuild($buildId)
*/
public function getName()
{
return $this->job->name;
return $this->job->fullName;
}

/**
Expand Down