Skip to content
Merged
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
4 changes: 4 additions & 0 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Release 0.3.8 (NOT RELEASED YET)

* [Refactor Issue 291][issue-291]

Useful utility methods refactored into utility classes.

* [Fixed Issue 282][issue-282]

`NullPointerException` may be thrown if `upstreamUrl` is `null` when
Expand Down
108 changes: 19 additions & 89 deletions jenkins-client/src/main/java/com/offbytwo/jenkins/JenkinsServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

Expand All @@ -27,6 +26,7 @@
import com.google.common.collect.Maps;
import com.offbytwo.jenkins.client.JenkinsHttpClient;
import com.offbytwo.jenkins.client.util.EncodingUtils;
import com.offbytwo.jenkins.client.util.UrlUtils;
import com.offbytwo.jenkins.helper.JenkinsVersion;
import com.offbytwo.jenkins.model.Build;
import com.offbytwo.jenkins.model.Computer;
Expand Down Expand Up @@ -155,7 +155,7 @@ public Map<String, Job> getJobs(String view) throws IOException {
* @throws IOException in case of an error.
*/
public Map<String, Job> getJobs(FolderJob folder, String view) throws IOException {
String path = toBaseUrl(folder);
String path = UrlUtils.toBaseUrl(folder);
Class<? extends MainView> viewClass = MainView.class;
if (view != null) {
path = path + "view/" + EncodingUtils.encode(view) + "/";
Expand Down Expand Up @@ -192,7 +192,7 @@ public Map<String, View> getViews() throws IOException {
public Map<String, View> getViews(FolderJob folder) throws IOException {
// This is much better than using &depth=2
// http://localhost:8080/api/json?pretty&tree=views[name,url,jobs[name,url]]
List<View> views = client.get(toBaseUrl(folder) + "?tree=views[name,url,jobs[name,url]]", MainView.class).getViews();
List<View> views = client.get(UrlUtils.toBaseUrl(folder) + "?tree=views[name,url,jobs[name,url]]", MainView.class).getViews();
return Maps.uniqueIndex(views, new Function<View, String>() {
@Override
public String apply(View view) {
Expand Down Expand Up @@ -232,7 +232,7 @@ public View getView(String name) throws IOException {
*/
public View getView(FolderJob folder, String name) throws IOException {
try {
View resultView = client.get(toViewBaseUrl(folder, name) + "/", View.class);
View resultView = client.get(UrlUtils.toViewBaseUrl(folder, name) + "/", View.class);
resultView.setClient(client);

// TODO: Think about the following? Does there exists a simpler/more
Expand Down Expand Up @@ -262,7 +262,7 @@ public View getView(FolderJob folder, String name) throws IOException {
* @throws IOException in case of an error.
*/
public JobWithDetails getJob(String jobName) throws IOException {
return getJob(null, parseFullName(jobName));
return getJob(null, UrlUtils.toFullJobPath(jobName));
}

/**
Expand All @@ -275,7 +275,7 @@ public JobWithDetails getJob(String jobName) throws IOException {
*/
public JobWithDetails getJob(FolderJob folder, String jobName) throws IOException {
try {
JobWithDetails job = client.get(toJobBaseUrl(folder, jobName), JobWithDetails.class);
JobWithDetails job = client.get(UrlUtils.toJobBaseUrl(folder, jobName), JobWithDetails.class);
job.setClient(client);

return job;
Expand All @@ -290,12 +290,12 @@ public JobWithDetails getJob(FolderJob folder, String jobName) throws IOExceptio
}

public MavenJobWithDetails getMavenJob(String jobName) throws IOException {
return getMavenJob(null, parseFullName(jobName));
return getMavenJob(null, UrlUtils.toFullJobPath(jobName));
}

public MavenJobWithDetails getMavenJob(FolderJob folder, String jobName) throws IOException {
try {
MavenJobWithDetails job = client.get(toJobBaseUrl(folder, jobName), MavenJobWithDetails.class);
MavenJobWithDetails job = client.get(UrlUtils.toJobBaseUrl(folder, jobName), MavenJobWithDetails.class);
job.setClient(client);

return job;
Expand Down Expand Up @@ -381,7 +381,7 @@ public void createJob(FolderJob folder, String jobName, String jobXml) throws IO
* @throws IOException in case of an error.
*/
public void createJob(FolderJob folder, String jobName, String jobXml, Boolean crumbFlag) throws IOException {
client.post_xml(toBaseUrl(folder) + "createItem?name=" + EncodingUtils.encodeParam(jobName), jobXml, crumbFlag);
client.post_xml(UrlUtils.toBaseUrl(folder) + "createItem?name=" + EncodingUtils.encodeParam(jobName), jobXml, crumbFlag);
}

/**
Expand Down Expand Up @@ -433,7 +433,7 @@ public void createView(FolderJob folder, String viewName, String viewXml) throws
* @throws IOException in case of an error.
*/
public void createView(FolderJob folder, String viewName, String viewXml, Boolean crumbFlag) throws IOException {
client.post_xml(toBaseUrl(folder) + "createView?name=" + EncodingUtils.encodeParam(viewName), viewXml,
client.post_xml(UrlUtils.toBaseUrl(folder) + "createView?name=" + EncodingUtils.encodeParam(viewName), viewXml,
crumbFlag);
}

Expand Down Expand Up @@ -484,7 +484,7 @@ public void createFolder(FolderJob folder, String jobName, Boolean crumbFlag) th
// here
ImmutableMap<String, String> params = ImmutableMap.of("mode", "com.cloudbees.hudson.plugins.folder.Folder",
"name", EncodingUtils.encodeParam(jobName), "from", "", "Submit", "OK");
client.post_form(toBaseUrl(folder) + "createItem?", params, crumbFlag);
client.post_form(UrlUtils.toBaseUrl(folder) + "createItem?", params, crumbFlag);
}

/**
Expand All @@ -507,7 +507,7 @@ public String getJobXml(String jobName) throws IOException {
* @throws IOException in case of an error.
*/
public String getJobXml(FolderJob folder, String jobName) throws IOException {
return client.get(toJobBaseUrl(folder, jobName) + "/config.xml");
return client.get(UrlUtils.toJobBaseUrl(folder, jobName) + "/config.xml");
}

/**
Expand Down Expand Up @@ -577,11 +577,11 @@ public void updateView(String viewName, String viewXml, boolean crumbFlag) throw
}

public void updateView(FolderJob folder, String viewName, String viewXml) throws IOException {
client.post_xml(toBaseUrl(folder) + "view/" + EncodingUtils.encode(viewName) + "/config.xml", viewXml, true);
client.post_xml(UrlUtils.toBaseUrl(folder) + "view/" + EncodingUtils.encode(viewName) + "/config.xml", viewXml, true);
}

public void updateView(FolderJob folder, String viewName, String viewXml, boolean crumbFlag) throws IOException {
client.post_xml(toBaseUrl(folder) + "view/" + EncodingUtils.encode(viewName) + "/config.xml", viewXml, crumbFlag);
client.post_xml(UrlUtils.toBaseUrl(folder) + "view/" + EncodingUtils.encode(viewName) + "/config.xml", viewXml, crumbFlag);
}

/**
Expand Down Expand Up @@ -617,7 +617,7 @@ public void updateJob(String jobName, String jobXml, boolean crumbFlag) throws I
* @throws IOException in case of an error.
*/
public void updateJob(FolderJob folder, String jobName, String jobXml, boolean crumbFlag) throws IOException {
client.post_xml(toJobBaseUrl(folder, jobName) + "/config.xml", jobXml, crumbFlag);
client.post_xml(UrlUtils.toJobBaseUrl(folder, jobName) + "/config.xml", jobXml, crumbFlag);
}

/**
Expand Down Expand Up @@ -685,7 +685,7 @@ public void deleteJob(FolderJob folder, String jobName) throws IOException {
* @throws IOException in case of problems.
*/
public void deleteJob(FolderJob folder, String jobName, boolean crumbFlag) throws IOException {
client.post(toJobBaseUrl(folder, jobName) + "/doDelete", crumbFlag);
client.post(UrlUtils.toJobBaseUrl(folder, jobName) + "/doDelete", crumbFlag);
}

/**
Expand Down Expand Up @@ -878,80 +878,10 @@ public void renameJob(FolderJob folder, String oldJobName, String newJobName) th
*/
public void renameJob(FolderJob folder, String oldJobName, String newJobName, Boolean crumbFlag)
throws IOException {
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 The created base url.
*/
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}
* @param jobName the name of the job.
* @return converted base url.
*/
private String toJobBaseUrl(FolderJob folder, String jobName) {
String jobBaseUrl = toBaseUrl(folder) + "job/";

String[] jobNameParts = jobName.split("/");
for (int i = 0; i < jobNameParts.length; i++) {
jobBaseUrl += EncodingUtils.encode(jobNameParts[i]);

if (i != jobNameParts.length - 1) {
jobBaseUrl += "/";
}
}

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

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

/**
* Parses the provided job name for folders to get the full path for the job.
* @param jobName the fullName of the job.
* @return the path of the job including folders if present.
*/
private String parseFullName(String jobName)
{
if (!jobName.contains("/")) {
return jobName;
}

List<String> foldersAndJob = Arrays.asList(jobName.split("/"));

String foldersAndJobName = "";

for (int i = 0; i < foldersAndJob.size(); i++) {
foldersAndJobName += foldersAndJob.get(i);

if (i != foldersAndJob.size() -1) {
foldersAndJobName += "/job/";
}
}

return foldersAndJobName;
}
}
Loading