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

Support for spaces in Jenkins Builds names with getNameURLSafe(), add getDescription(), getGitRevision(), getGitBranch() and getGitRemoteURL() #37

Open
wants to merge 3 commits 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
17 changes: 15 additions & 2 deletions src/Jenkins.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Jenkins
/**
* @param string $baseUrl
*/
public function __construct($baseUrl)
public function __construct($baseUrl)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change is not necessary

{
$this->baseUrl = $baseUrl;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

urlSanitize ?

$string = urlencode($string);
$string = str_replace("+", "%20", $string);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use http://us2.php.net/rawurlencode which seems to encode '+' character.

return $string;
}
}
35 changes: 35 additions & 0 deletions src/Jenkins/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,41 @@ public function getNumber()
return $this->build->number;
}

/**
* @return string
*/
public function getDescription()
{
return $this->build->description;
}

public function getGitRevision()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these properties specific to git ?
According to their name, it doesn't seem.
Can you please name these functions in a more generic way ?

{
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
*/
Expand Down
13 changes: 11 additions & 2 deletions src/Jenkins/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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
*/
Expand Down Expand Up @@ -110,7 +119,7 @@ public function getColor()
*/
public function retrieveXmlConfigAsString()
{
return $this->jenkins->retrieveXmlConfigAsString($this->getName());
return $this->jenkins->retrieveXmlConfigAsString($this->getNameURLSafe());
}

/**
Expand Down