[JIRA] (JENKINS-58924) job.allBuilds points to wrong URL when called from a job inside a folderJob

0 views
Skip to first unread message

niclas.lindberg@nillsoft.com (JIRA)

unread,
Aug 14, 2019, 7:37:04 AM8/14/19
to jenkinsc...@googlegroups.com
Niclas Lindberg created an issue
 
Jenkins / Bug JENKINS-58924
job.allBuilds points to wrong URL when called from a job inside a folderJob
Issue Type: Bug Bug
Assignee: Karl-Heinz Marbaise
Components: java-client-api
Created: 2019-08-14 11:36
Priority: Critical Critical
Reporter: Niclas Lindberg

When a job is residing in a folderJob then the API url to the job in job.allBuilds() points to /job/SIMPLE_JOB_NAME/api/json which causes a 404. Instead the job.allBuilds() should point to /job/FOLDER_JOB_NAME/job/SIMPLE_JOB_NAME/api/json.

Code: JobWithDetails#127

Add Comment Add Comment
 
This message was sent by Atlassian Jira (v7.11.2#711002-sha1:fdc329d)

niclas.lindberg@nillsoft.com (JIRA)

unread,
Aug 14, 2019, 10:27:02 AM8/14/19
to jenkinsc...@googlegroups.com
Niclas Lindberg updated an issue
Change By: Niclas Lindberg
When a job is residing in a folderJob then the API url to the job in job.allBuilds() points to /job/SIMPLE_JOB_NAME/api/json which causes a 404. Instead the job.allBuilds() should point to /job/FOLDER_JOB_NAME/job/SIMPLE_JOB_NAME/api/json.

Code: JobWithDetails#127


Fix:

private String getJobPath() {
   String[] nameParts = this.getFullName().split("\\/");
   for(int i=0;i<nameParts.length;i++){
      nameParts[i] = EncodingUtils.encode(nameParts[i]);
   }
   return "/job/" + String.join("/job/",nameParts);
}

public List<Build> getAllBuilds() throws IOException {
   String path = getJobPath();
   try {

      List<Build> builds = client.get(path + "?tree=allBuilds[number[*],url[*],queueId[*]]", AllBuilds.class).getAllBuilds();

      if (builds == null) {
          return Collections.emptyList();
      } else {
         return transform(builds, new Function<Build, Build>() {
            @Override
            public Build apply(Build from) {
               return buildWithClient(from);
           }
        });
      }
   } catch (HttpResponseException e) {
              // TODO: Thinks about a better handling if the job does not exist?
         if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
            // TODO: Check this if this is necessary or a good idea?
           return null;
        }
        throw e;
   }
}
Reply all
Reply to author
Forward
0 new messages