Skip to content

Commit

Permalink
Fixed #203
Browse files Browse the repository at this point in the history
 o added methods getJobXml and updateJob with
   folder parameters.

Patch applied of RainerW <[email protected]>
  • Loading branch information
khmarbaise committed Nov 26, 2016
1 parent e759bfd commit 9ba5525
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 57 deletions.
14 changes: 14 additions & 0 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@

### API Changes

* [Fixed Issue 203][issue-203] with [pull request #204][pull-204]
of RainerW <[email protected]>

Added methods getJobXml and updateJob with folder parameter.

```java
public class JenkinsServer {
String getJobXml(FolderJob folder, String jobName);
void updateJob(FolderJob folder, String jobName, String jobXml, boolean crumbFlag);
}
```

* [Fixed Issue 207][issue-207]

Added
Expand Down Expand Up @@ -705,11 +717,13 @@ TestReport testReport = mavenJob.getLastSuccessfulBuild().getTestReport();
[issue-200]: https://github.com/jenkinsci/java-client-api/issues/200
[issue-201]: https://github.com/jenkinsci/java-client-api/issues/201
[issue-202]: https://github.com/jenkinsci/java-client-api/issues/202
[issue-203]: https://github.com/jenkinsci/java-client-api/issues/203
[issue-207]: https://github.com/jenkinsci/java-client-api/issues/207
[pull-123]: https://github.com/jenkinsci/java-client-api/pull/123
[pull-149]: https://github.com/jenkinsci/java-client-api/pull/149
[pull-158]: https://github.com/jenkinsci/java-client-api/pull/158
[pull-163]: https://github.com/jenkinsci/java-client-api/pull/163
[pull-204]: https://github.com/jenkinsci/java-client-api/pull/204
[jissue-35002]: https://issues.jenkins-ci.org/browse/JENKINS-35002
[jissue-35108]: https://issues.jenkins-ci.org/browse/JENKINS-35108
[jissue-38787]: https://issues.jenkins-ci.org/browse/JENKINS-38787
Expand Down
130 changes: 73 additions & 57 deletions jenkins-client/src/main/java/com/offbytwo/jenkins/JenkinsServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,7 @@ public Map<String, Job> getJobs(String view) throws IOException {
* @throws IOException
*/
public Map<String, Job> getJobs(FolderJob folder, String view) throws IOException {
String path = "/";
if (folder != null) {
path = folder.getUrl();
}
String path = toBaseUrl(folder);
Class<? extends MainView> viewClass = MainView.class;
if (view != null) {
path = path + "view/" + EncodingUtils.encode(view) + "/";
Expand Down Expand Up @@ -187,11 +184,7 @@ public Map<String, View> getViews() throws IOException {
* @throws IOException
*/
public Map<String, View> getViews(FolderJob folder) throws IOException {
String path = "/";
if (folder != null) {
path = folder.getUrl();
}
List<View> views = client.get(path + "?depth=1", MainView.class).getViews();
List<View> views = client.get(toBaseUrl(folder) + "?depth=1", MainView.class).getViews();
return Maps.uniqueIndex(views, new Function<View, String>() {
@Override
public String apply(View view) {
Expand Down Expand Up @@ -235,13 +228,8 @@ public View getView(String name) throws IOException {
* @throws IOException
*/
public View getView(FolderJob folder, String name) throws IOException {
String path = "/";
if (folder != null) {
path = folder.getUrl();
}

try {
View resultView = client.get(path + "view/" + EncodingUtils.encode(name) + "/", View.class);
View resultView = client.get(toViewBaseUrl(folder,name) + "/", View.class);
resultView.setClient(client);

// TODO: Think about the following? Does there exists a simpler/more
Expand Down Expand Up @@ -280,12 +268,8 @@ public JobWithDetails getJob(String jobName) throws IOException {
* @throws IOException
*/
public JobWithDetails getJob(FolderJob folder, String jobName) throws IOException {
String path = "/";
if (folder != null) {
path = folder.getUrl();
}
try {
JobWithDetails job = client.get(path + "job/" + EncodingUtils.encode(jobName), JobWithDetails.class);
JobWithDetails job = client.get(toJobBaseUrl(folder,jobName), JobWithDetails.class);
job.setClient(client);

return job;
Expand All @@ -304,12 +288,8 @@ public MavenJobWithDetails getMavenJob(String jobName) throws IOException {
}

public MavenJobWithDetails getMavenJob(FolderJob folder, String jobName) throws IOException {
String path = "/";
if (folder != null) {
path = folder.getUrl();
}
try {
MavenJobWithDetails job = client.get(path + "job/" + EncodingUtils.encode(jobName),
MavenJobWithDetails job = client.get(toJobBaseUrl(folder, jobName),
MavenJobWithDetails.class);
job.setClient(client);

Expand Down Expand Up @@ -382,11 +362,7 @@ public void createJob(FolderJob folder, String jobName, String jobXml) throws IO
* @throws IOException
*/
public void createJob(FolderJob folder, String jobName, String jobXml, Boolean crumbFlag) throws IOException {
String path = "/";
if (folder != null) {
path = folder.getUrl();
}
client.post_xml(path + "createItem?name=" + EncodingUtils.encodeParam(jobName), jobXml, crumbFlag);
client.post_xml( toBaseUrl(folder) + "createItem?name=" + EncodingUtils.encodeParam(jobName), jobXml, crumbFlag);
}

/**
Expand Down Expand Up @@ -428,11 +404,7 @@ public void createView(FolderJob folder, String viewName, String viewXml) throws
* @throws IOException
*/
public void createView(FolderJob folder, String viewName, String viewXml, Boolean crumbFlag) throws IOException {
String path = "/";
if (folder != null) {
path = folder.getUrl();
}
client.post_xml(path + "createView?name=" + EncodingUtils.encodeParam(viewName), viewXml, crumbFlag);
client.post_xml(toBaseUrl(folder) + "createView?name=" + EncodingUtils.encodeParam(viewName), viewXml, crumbFlag);
}

/**
Expand Down Expand Up @@ -468,15 +440,11 @@ public void createFolder(FolderJob folder, String jobName) throws IOException {
* @throws IOException
*/
public void createFolder(FolderJob folder, String jobName, Boolean crumbFlag) throws IOException {
String path = "/";
if (folder != null) {
path = folder.getUrl();
}
// https://gist.github.com/stuart-warren/7786892 was slightly helpful
// here
ImmutableMap<String, String> params = ImmutableMap.of("mode", "com.cloudbees.hudson.plugins.folder.Folder",
"name", EncodingUtils.encodeParam(jobName), "from", "", "Submit", "OK");
client.post_form(path + "createItem?", params, crumbFlag);
client.post_form(toBaseUrl(folder) + "createItem?", params, crumbFlag);
}

/**
Expand All @@ -486,7 +454,18 @@ public void createFolder(FolderJob folder, String jobName, Boolean crumbFlag) th
* @throws IOException
*/
public String getJobXml(String jobName) throws IOException {
return client.get("/job/" + EncodingUtils.encode(jobName) + "/config.xml");
return getJobXml(null,jobName);
}

/**
* Get the xml description of an existing job
*
* @return the new job object
* @throws IOException
*/
public String getJobXml(FolderJob folder, String jobName) throws IOException
{
return client.get(toJobBaseUrl(folder, jobName) + "/config.xml");
}

/**
Expand Down Expand Up @@ -557,15 +536,29 @@ public void updateView(String viewName, String viewXml, boolean crumbFlag) throw
/**
* Update the xml description of an existing job
*
* @return the new job object
* @throws IOException
*/
public void updateJob(String jobName, String jobXml) throws IOException {
this.updateJob(jobName, jobXml, true);
}

/**
* Update the xml description of an existing job
*
* @throws IOException
*/
public void updateJob(String jobName, String jobXml, boolean crumbFlag) throws IOException {
client.post_xml("/job/" + EncodingUtils.encode(jobName) + "/config.xml", jobXml, crumbFlag);
updateJob(null,jobName,jobXml,crumbFlag);
}

/**
* Update the xml description of an existing job
*
* @throws IOException
*/
public void updateJob(FolderJob folder, String jobName, String jobXml, boolean crumbFlag) throws IOException
{
client.post_xml(toJobBaseUrl(folder, jobName) + "/config.xml", jobXml, crumbFlag);
}

public void addStringParam(String jobName, String name, String description, String defaultValue)
Expand Down Expand Up @@ -630,11 +623,7 @@ public void deleteJob(FolderJob folder, String jobName) throws IOException {
* in case of problems.
*/
public void deleteJob(FolderJob folder, String jobName, boolean crumbFlag) throws IOException {
String path = "/";
if (folder != null) {
path = folder.getUrl();
}
client.post(path + "/job/" + EncodingUtils.encode(jobName) + "/doDelete", crumbFlag);
client.post(toJobBaseUrl(folder, jobName) + "/doDelete", crumbFlag);
}

/*
Expand Down Expand Up @@ -825,14 +814,41 @@ public void renameJob(FolderJob folder, String oldJobName, String newJobName) th
*/
public void renameJob(FolderJob folder, String oldJobName, String newJobName, Boolean crumbFlag)
throws IOException {

String path = "/";
if (folder != null) {
path = folder.getUrl();
}
client.post(path + "job/" + EncodingUtils.encode(oldJobName) + "/doRename?newName="
+ EncodingUtils.encodeParam(newJobName), crumbFlag);

}
client.post( toJobBaseUrl(folder, oldJobName) + "/doRename?newName=" + EncodingUtils.encodeParam(newJobName), crumbFlag);
}

/**
* Helper to create a base url in case a folder is given
* @param folder the folder or {@code null}
* @return
*/
private String toBaseUrl(FolderJob folder)
{
String path = "/";
if (folder != null) {
path = folder.getUrl();
}
return path;
}

/**
* Helper to create the base url for a job, with or without a given folder
* @param folder the folder or {@code null}
* @return
*/
private String toJobBaseUrl(FolderJob folder, String jobName)
{
return toBaseUrl(folder) + "job/" + EncodingUtils.encode(jobName);
}

/**
* Helper to create the base url for a view, with or without a given folder
* @param folder the folder or {@code null}
* @return
*/
private String toViewBaseUrl(FolderJob folder, String name)
{
return toBaseUrl(folder) + "view/" + EncodingUtils.encode(name);
}

}

0 comments on commit 9ba5525

Please sign in to comment.