// TODO Tag projects as created by us, so that we can intelligently delete them and prevent
  // multiple jobs editing Projects
  private boolean createNewJob(String jobName, String config) {
    LOGGER.log(Level.FINE, String.format("Creating project as %s", config));
    boolean created;

    try {
      InputStream is =
          new ByteArrayInputStream(config.getBytes("UTF-8")); // TODO confirm that we're using UTF-8

      int i = jobName.lastIndexOf('/');
      Jenkins jenkins = Jenkins.getInstance();
      ModifiableTopLevelItemGroup ctx = jenkins;
      if (i > 0) {
        String contextName = jobName.substring(0, i);
        jobName = jobName.substring(i + 1);
        Item contextItem = jenkins.getItemByFullName(contextName);
        if (contextItem instanceof ModifiableItemGroup) {
          ctx = (ModifiableTopLevelItemGroup) contextItem;
        }
      }
      TopLevelItem item = ctx.createProjectFromXML(jobName, is);
      created = true;
    } catch (UnsupportedEncodingException ueex) {
      LOGGER.log(Level.WARNING, "Unsupported encoding used in config. Should be UTF-8.");
      created = false;
    } catch (IOException ioex) {
      LOGGER.log(
          Level.WARNING, String.format("Error writing config for new job %s.", jobName), ioex);
      created = false;
    }
    return created;
  }
 /**
  * Gets all group folders without applying the filter.
  *
  * @return all group folders
  */
 private Map<Integer, GroupFolderInfo> getUnfilteredFolders() {
   Map<Integer, GroupFolderInfo> folders = new TreeMap<Integer, GroupFolderInfo>();
   for (TopLevelItem item : itemGroup.getItems()) {
     // check if the item is a group folder
     GroupFolderInfo groupFolderInfo = GroupFolderInfo.createFromItem(item);
     if (groupFolderInfo != null) {
       folders.put(groupFolderInfo.getGroupId(), groupFolderInfo);
     }
   }
   return folders;
 }
 /**
  * Creates a group folder.
  *
  * <p>Does not check if a folder already exists for the GitLab group.
  *
  * @param group the group
  * @throws ItemNameCollisionException if an item name for a new folder already was in use
  * @throws IOException if saving to persistent storage failed
  */
 private void createFolder(GitLabGroupInfo group) throws ItemNameCollisionException, IOException {
   try {
     Folder folder = (Folder) itemGroup.createProject(folderDescriptor, group.getPath(), true);
     folder.addProperty(new GitLabFolderAuthorization(group.getId()));
   } catch (IllegalArgumentException e) {
     throw new ItemNameCollisionException(
         "Cannot create folder because an item with the name "
             + group.getPath()
             + " already exists");
   }
 }
Пример #4
0
  @Override
  public void cook(Recipe recipe, ImportReportList reportList) throws IOException {
    // expansion of this is deferred
    XStreamDOM actual = recipe.createImportOptions().apply(definition);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XStreamDOM.ConverterImpl c = new ConverterImpl();
    c.marshal(actual, new XppDriver().createWriter(baos), null);

    ModifiableTopLevelItemGroup g = Jenkins.getInstance();
    TopLevelItem j = g.getItem(name);
    InputStream is = new ByteArrayInputStream(baos.toByteArray());
    if (j == null) {
      j = g.createProjectFromXML(name, is);
    } else if (j instanceof AbstractItem) {
      Source source = new StreamSource(is);
      ((AbstractItem) j).updateByXml(source);
    } else {
      throw new IOException("Cannot update " + j + " in place");
    }
    reportList.add(new ImportReportImpl(j));
  }
  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;
  }