Ejemplo n.º 1
0
 public static JobIngredient fromJob(Job j, String description) {
   XStreamDOM dom = XStreamDOM.from(j.getConfigFile().getXStream(), j);
   return new JobIngredient(j.getName(), description, dom);
 }
  protected int run() throws Exception {
    Hudson h = Hudson.getInstance();

    if (!h.isTeamManagementEnabled()) {
      stderr.println("Team management is not enabled");
      return -1;
    }

    TeamManager teamManager = h.getTeamManager();

    if (!teamManager.isCurrentUserSysAdmin()) {
      stderr.println("User not authorized to create team");
      return -1;
    }

    Team fromTeam;
    try {
      fromTeam = teamManager.findTeam(from);
    } catch (TeamNotFoundException e) {
      stderr.println("From team " + from + " not found");
      return -1;
    }

    if (nodes != null
        && !("move".equalsIgnoreCase(nodes)
            || "visible".equalsIgnoreCase(nodes)
            || "ignore".equalsIgnoreCase(nodes))) {
      stderr.println("nodes must be one of move, visible or ignore");
    }

    if (views != null
        && !("move".equalsIgnoreCase(views)
            || "visible".equalsIgnoreCase(views)
            || "ignore".equalsIgnoreCase(views))) {
      stderr.println("views must be one of move, visible or ignore");
    }

    Team toTeam = null;
    try {
      toTeam = teamManager.createTeam(to);
    } catch (IOException ex) {
      stderr.println(ex.getMessage());
      return -1;
    } catch (TeamAlreadyExistsException ex) {
      stderr.println("To team " + to + " already exists");
      return -1;
    }

    Set<String> jobNames = fromTeam.getJobNames();
    for (String jobName : jobNames) {
      String unqualifiedName = teamManager.getUnqualifiedJobName(jobName);
      TopLevelItem item = h.getItem(jobName);
      if (item instanceof Job) {
        Job job = (Job) item;
        XmlFile file = job.getConfigFile();
        InputStream in;
        try {
          in = fixConfigFile(file, from, to, email);
        } catch (Failure ex) {
          stderr.println("Error reading config.xml for job " + jobName);
          stderr.println(ex.getMessage());
          return -1;
        }
        h.createProjectFromXML(unqualifiedName, to, in);
      }
    }

    if (nodes != null) {
      if ("move".equalsIgnoreCase(nodes)) {
        for (String nodeName : fromTeam.getNodeNames()) {
          teamManager.moveNode(fromTeam, toTeam, nodeName);
        }
      } else if ("visible".equalsIgnoreCase(nodes)) {
        for (TeamNode teamNode : fromTeam.getNodes()) {
          teamManager.addNodeVisibility(teamNode, toTeam.getName());
          // Assume target team wants to be able to use the node.
          teamManager.setNodeEnabled(teamNode.getId(), toTeam, true);
        }
      }
    }

    if (views != null) {
      if ("move".equalsIgnoreCase(views)) {
        for (String viewName : fromTeam.getViewNames()) {
          teamManager.moveView(fromTeam, toTeam, viewName);
        }
      } else if ("visible".equalsIgnoreCase(nodes)) {
        for (TeamView teamView : fromTeam.getViews()) {
          teamManager.addViewVisibility(teamView, toTeam.getName());
        }
      }
    }

    return 0;
  }