private boolean updateExistingJob(AbstractProject<?, ?> project, String config) { boolean created; // Leverage XMLUnit to perform diffs Diff diff; try { String oldJob = project.getConfigFile().asString(); diff = XMLUnit.compareXML(oldJob, config); if (diff.similar()) { LOGGER.log(Level.FINE, String.format("Project %s is identical", project.getName())); return false; } } catch (Exception e) { // It's not a big deal if we can't diff, we'll just move on LOGGER.warning(e.getMessage()); } // TODO Perform comparison between old and new, and print to console // TODO Print out, for posterity, what the user might have changed, in the format of the DSL LOGGER.log(Level.FINE, String.format("Updating project %s as %s", project.getName(), config)); StreamSource streamSource = new StreamSource(new StringReader(config)); // TODO use real xmlReader try { project.updateByXml(streamSource); created = true; } catch (IOException ioex) { LOGGER.log(Level.WARNING, String.format("Error writing updated project to file."), ioex); created = false; } return created; }
private String lookupJob(String jobName) throws IOException { LOGGER.log(Level.FINE, String.format("Looking up Job %s", jobName)); String jobXml = ""; AbstractProject<?, ?> project = (AbstractProject<?, ?>) Jenkins.getInstance().getItemByFullName(jobName); if (project != null) { XmlFile xmlFile = project.getConfigFile(); jobXml = xmlFile.asString(); } else { LOGGER.log(Level.WARNING, String.format("No Job called %s could be found.", jobName)); throw new IOException(String.format("No Job called %s could be found.", jobName)); } LOGGER.log(Level.FINE, String.format("Looked up Job with config %s", jobXml)); return jobXml; }
@Override public void queueJob(String jobName) throws JobNameNotProvidedException { validateJobNameArg(jobName); AbstractProject<?, ?> project = (AbstractProject<?, ?>) Jenkins.getInstance().getItemByFullName(jobName); if (build != null && build instanceof Run) { Run run = (Run) build; LOGGER.log( Level.INFO, String.format("Scheduling build of %s from %s", jobName, run.getParent().getName())); project.scheduleBuild(new Cause.UpstreamCause(run)); } else { LOGGER.log(Level.INFO, String.format("Scheduling build of %s", jobName)); project.scheduleBuild(new Cause.UserCause()); } }