@Override
  public void createOrUpdateView(String path, String config, boolean ignoreExisting) {
    validateUpdateArgs(path, config);
    String viewBaseName = FilenameUtils.getName(path);
    Jenkins.checkGoodName(viewBaseName);
    try {
      InputStream inputStream = new ByteArrayInputStream(config.getBytes("UTF-8"));

      ItemGroup parent = lookupStrategy.getParent(build.getProject(), path);
      if (parent instanceof ViewGroup) {
        View view = ((ViewGroup) parent).getView(viewBaseName);
        if (view == null) {
          if (parent instanceof Jenkins) {
            ((Jenkins) parent).addView(createViewFromXML(viewBaseName, inputStream));
          } else if (parent instanceof Folder) {
            ((Folder) parent).addView(createViewFromXML(viewBaseName, inputStream));
          } else {
            LOGGER.log(Level.WARNING, format("Could not create view within %s", parent.getClass()));
          }
        } else if (!ignoreExisting) {
          view.updateByXml(new StreamSource(inputStream));
        }
      } else if (parent == null) {
        throw new DslException(format(Messages.CreateView_UnknownParent(), path));
      } else {
        LOGGER.log(Level.WARNING, format("Could not create view within %s", parent.getClass()));
      }
    } catch (UnsupportedEncodingException e) {
      LOGGER.log(Level.WARNING, "Unsupported encoding used in config. Should be UTF-8.");
    } catch (IOException e) {
      e.printStackTrace();
      LOGGER.log(Level.WARNING, format("Error writing config for new view %s.", path), e);
    }
  }
  @Override
  public void queueJob(String path) throws NameNotProvidedException {
    validateNameArg(path);

    BuildableItem project = lookupStrategy.getItem(build.getParent(), path, BuildableItem.class);

    LOGGER.log(
        Level.INFO, format("Scheduling build of %s from %s", path, build.getParent().getName()));
    project.scheduleBuild(new Cause.UpstreamCause((Run) build));
  }
Esempio n. 3
0
 @Override
 public UserInformation getUserInformation(String country, String number) {
   try {
     LookupStrategy strategy = getStrategy(country);
     if (strategy != null) {
       UserInformation info = strategy.lookup(number);
       info.setCellphone(number);
       return info;
     }
   } catch (Exception e) {
     log.warn(
         "Error occured when looking up user information country["
             + country
             + "] number["
             + number
             + "]",
         e);
   }
   return null;
 }
  private String lookupJob(String path) throws IOException {
    LOGGER.log(Level.FINE, format("Looking up item %s", path));

    AbstractItem item = lookupStrategy.getItem(build.getProject(), path, AbstractItem.class);
    if (item != null) {
      XmlFile xmlFile = item.getConfigFile();
      String jobXml = xmlFile.asString();
      LOGGER.log(Level.FINE, format("Looked up item with config %s", jobXml));
      return jobXml;
    } else {
      LOGGER.log(Level.WARNING, format("No item called %s could be found.", path));
      throw new IOException(format("No item called %s could be found.", path));
    }
  }
 @Override
 public void renameJobMatching(final String previousNames, String destination) throws IOException {
   final ItemGroup context = lookupStrategy.getContext(build.getProject());
   Collection<Job> items = Jenkins.getInstance().getAllItems(Job.class);
   Collection<Job> matchingJobs =
       Collections2.filter(
           items,
           new Predicate<Job>() {
             @Override
             public boolean apply(Job topLevelItem) {
               return topLevelItem.getRelativeNameFrom(context).matches(previousNames);
             }
           });
   if (matchingJobs.size() == 1) {
     renameJob(matchingJobs.iterator().next(), destination);
   } else if (matchingJobs.size() > 1) {
     throw new DslException(format(Messages.RenameJobMatching_MultipleJobsFound(), matchingJobs));
   }
 }
  @Override
  public boolean createOrUpdateConfig(String path, String config, boolean ignoreExisting)
      throws NameNotProvidedException, ConfigurationMissingException {

    LOGGER.log(Level.INFO, format("createOrUpdateConfig for %s", path));
    boolean created = false;

    validateUpdateArgs(path, config);

    AbstractItem item = lookupStrategy.getItem(build.getProject(), path, AbstractItem.class);
    String jobName = FilenameUtils.getName(path);
    Jenkins.checkGoodName(jobName);

    if (item == null) {
      created = createNewItem(path, config);
    } else if (!ignoreExisting) {
      created = updateExistingItem(item, config);
    }
    return created;
  }
  private void renameJob(Job from, String to) throws IOException {
    LOGGER.info(format("Renaming job %s to %s", from.getFullName(), to));

    ItemGroup fromParent = from.getParent();
    ItemGroup toParent = lookupStrategy.getParent(build.getProject(), to);
    if (fromParent != toParent) {
      LOGGER.info(
          format("Moving Job %s to folder %s", fromParent.getFullName(), toParent.getFullName()));
      if (toParent instanceof DirectlyModifiableTopLevelItemGroup) {
        DirectlyModifiableTopLevelItemGroup itemGroup =
            (DirectlyModifiableTopLevelItemGroup) toParent;
        move(from, itemGroup);
      } else {
        throw new DslException(
            format(
                Messages.RenameJobMatching_DestinationNotFolder(),
                from.getFullName(),
                toParent.getFullName()));
      }
    }
    from.renameTo(FilenameUtils.getName(to));
  }
  private boolean createNewItem(String path, String config) {
    LOGGER.log(Level.FINE, format("Creating item as %s", config));
    boolean created = false;

    try {
      InputStream is = new ByteArrayInputStream(config.getBytes("UTF-8"));

      ItemGroup parent = lookupStrategy.getParent(build.getProject(), path);
      String itemName = FilenameUtils.getName(path);
      if (parent instanceof ModifiableTopLevelItemGroup) {
        ((ModifiableTopLevelItemGroup) parent).createProjectFromXML(itemName, is);
        created = true;
      } else if (parent == null) {
        throw new DslException(format(Messages.CreateItem_UnknownParent(), path));
      } else {
        LOGGER.log(Level.WARNING, format("Could not create item within %s", parent.getClass()));
      }
    } catch (UnsupportedEncodingException e) {
      LOGGER.log(Level.WARNING, "Unsupported encoding used in config. Should be UTF-8.");
    } catch (IOException e) {
      LOGGER.log(Level.WARNING, format("Error writing config for new item %s.", path), e);
    }
    return created;
  }