/** If the command is currently running inside a build, return it. Otherwise null. */ protected Run optCurrentlyBuilding() throws CmdLineException { try { CLICommand c = CLICommand.getCurrent(); if (c == null) throw new IllegalStateException("Not executing a CLI command"); String[] envs = c.checkChannel().call(new GetCharacteristicEnvironmentVariables()); if (envs[0] == null || envs[1] == null) return null; Job j = Jenkins.getInstance().getItemByFullName(envs[0], Job.class); if (j == null) throw new CmdLineException("No such job: " + envs[0]); try { Run r = j.getBuildByNumber(Integer.parseInt(envs[1])); if (r == null) throw new CmdLineException("No such build #" + envs[1] + " in " + envs[0]); if (!r.isBuilding()) { throw new CmdLineException(r + " is not currently being built"); } return r; } catch (NumberFormatException e) { throw new CmdLineException("Invalid build number: " + envs[1]); } } catch (IOException e) { throw new CmdLineException("Failed to identify the build being executed", e); } catch (InterruptedException e) { throw new CmdLineException("Failed to identify the build being executed", e); } }
public Object revert(Object o) { String id = (String) o; int hash = id.lastIndexOf('#'); String jobName = id.substring(0, hash); String runNumber = id.substring(hash + 1); Job<?, ?> job = (Job<?, ?>) Hudson.getInstance().getItem(jobName); Run<?, ?> run = job.getBuildByNumber(Integer.parseInt(runNumber)); return run; }
public BallColor getIconColor() { Result result = null; AbstractBuild worseBuild = null; for (BuildState buildState : childrenBuildState) { Job project = (Job) Hudson.getInstance().getItem(buildState.getJobName()); AbstractBuild build = (AbstractBuild) project.getBuildByNumber(buildState.getLastBuildNumber()); if (build != null) { if (result == null) { result = build.getResult(); worseBuild = build; } else { if (build.getResult().isWorseThan(worseBuild.getResult())) { worseBuild = build; } } } } if (worseBuild != null) { return worseBuild.getIconColor(); } return null; }
// TODO: Break up this method and clean up URL handling. public void doJob(StaplerRequest request, StaplerResponse response) throws IOException, URISyntaxException, ServletException { requireGET(); MediaType type = AcceptUtil.matchMediaType(request, ACCEPTABLE); if (type == null) { throw HttpResponses.status(HttpServletResponse.SC_NOT_ACCEPTABLE); } String restOfPath = request.getRestOfPath(); if (restOfPath == null) { throw HttpResponses.notFound(); } // Remove leading '/' restOfPath = restOfPath.substring(1); String segments[] = restOfPath.split("/"); // URI patterns: // <job-name> // <job-name>/preview // <job-name>/run/<run-number> // <job-name>/run/<run-number>/preview // <job-name>/run/<run-number>/request // Find the job. String jobName = segments[0]; Job<?, ?> job = getJob(jobName); if (job == null) { throw HttpResponses.notFound(); } // Is it a run? // <job-name>/run/<run-number> if (segments.length >= 3 && PATH_RUN.equals(segments[1])) { String runNumber = segments[2]; int i; try { i = Integer.valueOf(Integer.parseInt(runNumber)); } catch (NumberFormatException e) { throw HttpResponses.notFound(); } Run<?, ?> run = job.getBuildByNumber(i); if (run == null) { throw HttpResponses.notFound(); } if (segments.length == 4) { // Is it a run preview? // <job-name>/run/<run-number>/preview if (PATH_PREVIEW.equals(segments[3])) { /* * See /hudson-oslc-auto/src/main/resources/hudson/model/Run/preview.jelly */ response.forward(run, "preview", request); return; } // Is it an AutomationRequest? // <job-name>/run/<run-number>/request if (PATH_REQUEST.equals(segments[3])) { AutomationRequest autoRequest = toAutomationRequest(request, job, run); marshal(autoRequest); } if ("buildStatus".equals(segments[3])) { throw HttpResponses.redirectViaContextPath(run.getUrl() + "/buildStatus"); } } else if (segments.length == 3) { // <job-name>/run/<run-number> if (MediaType.TEXT_HTML_TYPE.isCompatible(type)) { throw HttpResponses.redirectViaContextPath(run.getUrl()); } if (MarshallerConstants.MT_OSLC_COMPACT.isCompatible(type)) { handleCompact(job, run); } else { AutomationResult result = toAutomationResult(request, job, run); marshal(result); } } else { throw HttpResponses.notFound(); } } else { // Is it a job preview? // <job-name>/preview if (segments.length == 2 && PATH_PREVIEW.equals(segments[1])) { /* * See /hudson-oslc-auto/src/main/resources/hudson/model/Job/preview.jelly */ response.forward(job, "preview", request); return; } if (segments.length != 1) { throw HttpResponses.notFound(); } // Is it just a job name with no other segments? // <job-name> if (MediaType.TEXT_HTML_TYPE.isCompatible(type)) { throw HttpResponses.redirectViaContextPath(job.getUrl()); } if (MarshallerConstants.MT_OSLC_COMPACT.isCompatible(type)) { handleCompact(job); } else { AutomationPlan plan = toAutomationPlan(job); marshal(plan); } } }
@SuppressWarnings({"rawtypes"}) private BuildState createBuildState( BuildState parentBuildState, MultiJobProject multiJobProject, Job project) { int previousBuildNumber = 0; int lastBuildNumber = 0; int lastSuccessBuildNumber = 0; int lastFailureBuildNumber = 0; MultiJobBuild previousParentBuild = multiJobProject.getBuildByNumber(parentBuildState.getPreviousBuildNumber()); MultiJobBuild lastParentBuild = multiJobProject.getBuildByNumber(parentBuildState.getLastBuildNumber()); MultiJobBuild lastParentSuccessBuild = multiJobProject.getBuildByNumber(parentBuildState.getLastSuccessBuildNumber()); MultiJobBuild lastParentFailureBuild = multiJobProject.getBuildByNumber(parentBuildState.getLastFailureBuildNumber()); if (previousParentBuild != null) { List<SubBuild> subBuilds = previousParentBuild.getSubBuilds(); for (SubBuild subBuild : subBuilds) { if (subBuild.getJobName().equals(project.getName())) { previousBuildNumber = subBuild.getBuildNumber(); } } } if (lastParentBuild != null) { List<SubBuild> subBuilds = lastParentBuild.getSubBuilds(); for (SubBuild subBuild : subBuilds) { if (subBuild.getJobName().equals(project.getName())) { lastBuildNumber = subBuild.getBuildNumber(); } } } if (lastParentSuccessBuild != null) { List<SubBuild> subBuilds = lastParentSuccessBuild.getSubBuilds(); for (SubBuild subBuild : subBuilds) { if (subBuild.getJobName().equals(project.getName())) { AbstractBuild build = (AbstractBuild) project.getBuildByNumber(subBuild.getBuildNumber()); if (build != null && Result.SUCCESS.equals(build.getResult())) { lastSuccessBuildNumber = subBuild.getBuildNumber(); break; } else { lastParentSuccessBuild = multiJobProject.getBuildByNumber(parentBuildState.getPreviousBuildNumber()); } } } } if (lastParentFailureBuild != null) { List<SubBuild> subBuilds = lastParentFailureBuild.getSubBuilds(); for (SubBuild subBuild : subBuilds) { if (subBuild.getJobName().equals(project.getName())) { AbstractBuild build = (AbstractBuild) project.getBuildByNumber(subBuild.getBuildNumber()); if (build != null && Result.FAILURE.equals(((AbstractBuild) build).getResult())) { lastFailureBuildNumber = subBuild.getBuildNumber(); break; } else { lastParentFailureBuild = multiJobProject.getBuildByNumber(parentBuildState.getPreviousBuildNumber()); } } } } return new BuildState( project.getName(), previousBuildNumber, lastBuildNumber, lastSuccessBuildNumber, lastFailureBuildNumber); }
/** Gets the {@link Job} that this pointer points to, or null if such a job no longer exists. */ public Run getRun() { Job j = getJob(); if (j == null) return null; return j.getBuildByNumber(number); }