Skip to content

Commit 70e7e43

Browse files
authored
Merge pull request #267 from amcginn/fullName
Add support for retrieving jobs by fullName (also return fullName on the Job)
2 parents 198f4be + 6913cac commit 70e7e43

File tree

5 files changed

+67
-7
lines changed

5 files changed

+67
-7
lines changed

jenkins-client/src/main/java/com/offbytwo/jenkins/JenkinsServer.java

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import java.io.IOException;
1010
import java.net.URI;
11+
import java.util.ArrayList;
12+
import java.util.Arrays;
1113
import java.util.List;
1214
import java.util.Map;
1315

@@ -260,7 +262,7 @@ public View getView(FolderJob folder, String name) throws IOException {
260262
* @throws IOException in case of an error.
261263
*/
262264
public JobWithDetails getJob(String jobName) throws IOException {
263-
return getJob(null, jobName);
265+
return getJob(null, parseFullName(jobName));
264266
}
265267

266268
/**
@@ -288,7 +290,7 @@ public JobWithDetails getJob(FolderJob folder, String jobName) throws IOExceptio
288290
}
289291

290292
public MavenJobWithDetails getMavenJob(String jobName) throws IOException {
291-
return getMavenJob(null, jobName);
293+
return getMavenJob(null, parseFullName(jobName));
292294
}
293295

294296
public MavenJobWithDetails getMavenJob(FolderJob folder, String jobName) throws IOException {
@@ -562,7 +564,6 @@ public PluginManager getPluginManager() throws IOException {
562564
/**
563565
* Update the xml description of an existing view
564566
*
565-
* @throws IOException in case of an error.
566567
* @param viewName name of the view.
567568
* @param viewXml the view configuration.
568569
* @throws IOException in case of an error.
@@ -903,7 +904,18 @@ private String toBaseUrl(FolderJob folder) {
903904
* @return converted base url.
904905
*/
905906
private String toJobBaseUrl(FolderJob folder, String jobName) {
906-
return toBaseUrl(folder) + "job/" + EncodingUtils.encode(jobName);
907+
String jobBaseUrl = toBaseUrl(folder) + "job/";
908+
909+
String[] jobNameParts = jobName.split("/");
910+
for (int i = 0; i < jobNameParts.length; i++) {
911+
jobBaseUrl += EncodingUtils.encode(jobNameParts[i]);
912+
913+
if (i != jobNameParts.length - 1) {
914+
jobBaseUrl += "/";
915+
}
916+
}
917+
918+
return jobBaseUrl;
907919
}
908920

909921
/**
@@ -917,4 +929,29 @@ private String toViewBaseUrl(FolderJob folder, String name) {
917929
return toBaseUrl(folder) + "view/" + EncodingUtils.encode(name);
918930
}
919931

932+
/**
933+
* Parses the provided job name for folders to get the full path for the job.
934+
* @param jobName the fullName of the job.
935+
* @return the path of the job including folders if present.
936+
*/
937+
private String parseFullName(String jobName)
938+
{
939+
if (!jobName.contains("/")) {
940+
return jobName;
941+
}
942+
943+
List<String> foldersAndJob = Arrays.asList(jobName.split("/"));
944+
945+
String foldersAndJobName = "";
946+
947+
for (int i = 0; i < foldersAndJob.size(); i++) {
948+
foldersAndJobName += foldersAndJob.get(i);
949+
950+
if (i != foldersAndJob.size() -1) {
951+
foldersAndJobName += "/job/";
952+
}
953+
}
954+
955+
return foldersAndJobName;
956+
}
920957
}

jenkins-client/src/main/java/com/offbytwo/jenkins/model/FolderJob.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public FolderJob() {
2020
public FolderJob(String name, String url) {
2121
super(name, url);
2222
}
23+
24+
public FolderJob(String name, String url, String fullName) {
25+
super(name, url, fullName);
26+
}
2327

2428
public String getDisplayName() {
2529
return displayName;

jenkins-client/src/main/java/com/offbytwo/jenkins/model/Job.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,23 @@ public class Job extends BaseModel {
2323

2424
private String name;
2525
private String url;
26+
private String fullName;
2627

2728
public Job() {
2829
}
29-
30+
3031
public Job(String name, String url) {
3132
this();
3233
this.name = name;
3334
this.url = url;
35+
this.fullName = null;
36+
}
37+
38+
public Job(String name, String url, String fullName) {
39+
this();
40+
this.name = name;
41+
this.url = url;
42+
this.fullName = fullName;
3443
}
3544

3645
public String getName() {
@@ -40,6 +49,10 @@ public String getName() {
4049
public String getUrl() {
4150
return url;
4251
}
52+
53+
public String getFullName() {
54+
return fullName;
55+
}
4356

4457
public JobWithDetails details() throws IOException {
4558
return client.get(url, JobWithDetails.class);
@@ -126,14 +139,16 @@ public boolean equals(Object o) {
126139
return false;
127140
if (url != null ? !url.equals(job.url) : job.url != null)
128141
return false;
142+
if (fullName != null ? !fullName.equals(job.fullName) : job.fullName != null)
143+
return false;
129144

130145
return true;
131146
}
132147

133148
@Override
134149
public int hashCode() {
135150
int result = name != null ? name.hashCode() : 0;
136-
result = 31 * result + (url != null ? url.hashCode() : 0);
151+
result = 31 * result + (url != null ? url.hashCode() : 0) + (fullName != null ? fullName.hashCode() : 0);
137152
return result;
138153
}
139154

jenkins-client/src/main/java/com/offbytwo/jenkins/model/JobWithDetails.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public class JobWithDetails extends Job {
5959
private List<Job> downstreamProjects;
6060

6161
private List<Job> upstreamProjects;
62-
62+
6363
public String getDescription() {
6464
return description;
6565
}

jenkins-client/src/main/java/com/offbytwo/jenkins/model/MavenJob.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ public MavenJob() {
1010
public MavenJob(String name, String url) {
1111
super(name, url);
1212
}
13+
14+
public MavenJob(String name, String url, String fullName) {
15+
super(name, url, fullName);
16+
}
1317

1418
public MavenJobWithDetails mavenDetails() throws IOException {
1519
return client.get(getUrl(), MavenJobWithDetails.class);

0 commit comments

Comments
 (0)