public InheritanceParametersDefinitionProperty(
      AbstractProject<?, ?> owner, ParametersDefinitionProperty other) {
    this(owner, other.getParameterDefinitions());
    // Then, we copy the scope
    if (other instanceof InheritanceParametersDefinitionProperty) {
      InheritanceParametersDefinitionProperty ipdp =
          (InheritanceParametersDefinitionProperty) other;

      ipdp.scopeLock.readLock().lock();
      this.scopeLock.writeLock().lock();
      try {
        for (String pName : ipdp.fullScope.keySet()) {
          List<ScopeEntry> oLst = ipdp.fullScope.get(pName);
          if (oLst == null) {
            continue;
          }
          LinkedList<ScopeEntry> newLst = new LinkedList<ScopeEntry>();
          for (ScopeEntry entry : oLst) {
            newLst.add(new ScopeEntry(entry.owner, entry.param));
          }
          this.fullScope.put(pName, newLst);
        }
      } finally {
        this.scopeLock.writeLock().unlock();
        ipdp.scopeLock.readLock().unlock();
      }
    } else {
      this.addScopedParameterDefinitions(
          (owner != null) ? owner.getName() : "", other.getParameterDefinitions());
    }
  }
コード例 #2
0
  public AbstractProject<?, ?> getParentProject() {
    AbstractProject<?, ?> context = null;
    List<AbstractProject> jobs = Jenkins.getInstance().getAllItems(AbstractProject.class);

    for (AbstractProject<?, ?> project : jobs) {
      if (!(project instanceof TopLevelItem)) continue;

      ParametersDefinitionProperty property =
          project.getProperty(ParametersDefinitionProperty.class);

      if (property != null) {
        List<ParameterDefinition> parameterDefinitions = property.getParameterDefinitions();

        if (parameterDefinitions != null) {
          for (ParameterDefinition pd : parameterDefinitions) {

            if (pd instanceof GitParameterDefinition
                && ((GitParameterDefinition) pd).compareTo(this) == 0) {

              context = project;
              break;
            }
          }
        }
      }
    }

    return context;
  }
コード例 #3
0
 private ArrayList<ParameterValue> getDefaultBuildParameters() {
   ArrayList<ParameterValue> values = new ArrayList<ParameterValue>();
   ParametersDefinitionProperty property = project.getProperty(ParametersDefinitionProperty.class);
   if (property != null) {
     for (ParameterDefinition pd : property.getParameterDefinitions()) {
       values.add(pd.getDefaultParameterValue());
     }
   }
   return values;
 }
コード例 #4
0
  /**
   * Actually build a project, passing in parameters where appropriate
   *
   * @param project
   * @return
   */
  protected final boolean performBuildProject(AbstractProject<?, ?> project) {
    if (!project.hasPermission(AbstractProject.BUILD)) {
      LOGGER.log(Level.WARNING, "Insufficient permission to build job '" + project.getName() + "'");
      return false;
    }

    if (action.equals(BuildAction.POLL_SCM)) {
      project.schedulePolling();
      return true;
    }

    // no user parameters provided, just build it
    if (param == null) {
      project.scheduleBuild(new Cause.UserIdCause());
      return true;
    }

    ParametersDefinitionProperty pp =
        (ParametersDefinitionProperty) project.getProperty(ParametersDefinitionProperty.class);

    // project does not except any parameters, just build it
    if (pp == null) {
      project.scheduleBuild(new Cause.UserIdCause());
      return true;
    }

    List<ParameterDefinition> parameterDefinitions = pp.getParameterDefinitions();
    List<ParameterValue> values = new ArrayList<ParameterValue>();

    for (ParameterDefinition paramDef : parameterDefinitions) {

      if (!(paramDef instanceof StringParameterDefinition)) {
        // TODO add support for other parameter types
        values.add(paramDef.getDefaultParameterValue());
        continue;
      }

      StringParameterDefinition stringParamDef = (StringParameterDefinition) paramDef;
      ParameterValue value;

      // Did user supply this parameter?
      if (param.containsKey(paramDef.getName())) {
        value = stringParamDef.createValue(param.get(stringParamDef.getName()));
      } else {
        // No, then use the default value
        value = stringParamDef.createValue(stringParamDef.getDefaultValue());
      }

      values.add(value);
    }

    Jenkins.getInstance().getQueue().schedule(pp.getOwner(), 1, new ParametersAction(values));
    return true;
  }
コード例 #5
0
  private static ParametersAction getDefaultParameters(AbstractProject<?, ?> project) {
    ParametersDefinitionProperty property = project.getProperty(ParametersDefinitionProperty.class);
    if (property == null) {
      return null;
    }

    List<ParameterValue> parameters = new ArrayList<ParameterValue>();
    for (ParameterDefinition pd : property.getParameterDefinitions()) {
      ParameterValue param = pd.getDefaultParameterValue();
      if (param != null) parameters.add(param);
    }

    return new ParametersAction(parameters);
  }
  public static InheritanceParametersDefinitionProperty createMerged(
      ParametersDefinitionProperty prior, ParametersDefinitionProperty latter) {
    // Determining which owner to use for the new merge.
    // It needs to be an InheritanceProject!
    InheritanceProject newOwner = null;
    ParametersDefinitionProperty[] pdps = {latter, prior};

    for (ParametersDefinitionProperty pdp : pdps) {
      if (pdp.getOwner() != null && pdp.getOwner() instanceof InheritanceProject) {
        newOwner = (InheritanceProject) pdp.getOwner();
        break;
      }
    }

    // Then, we merge their ParameterDefinitions based on their name
    HashMap<String, ParameterDefinition> unifyMap = new HashMap<String, ParameterDefinition>();
    for (int i = pdps.length - 1; i >= 0; i--) {
      ParametersDefinitionProperty pdp = pdps[i];
      for (ParameterDefinition pd : pdp.getParameterDefinitions()) {
        unifyMap.put(pd.getName(), pd);
      }
    }
    List<ParameterDefinition> unifyList = new LinkedList<ParameterDefinition>(unifyMap.values());

    // With that, we create a new IPDP
    InheritanceParametersDefinitionProperty out =
        new InheritanceParametersDefinitionProperty(newOwner, unifyList);

    // At the end, we merge the scoping informations
    for (int i = pdps.length - 1; i >= 0; i--) {
      ParametersDefinitionProperty pdp = pdps[i];

      if (pdp instanceof InheritanceParametersDefinitionProperty) {
        // We deal with an already inherited parameter; so we need to
        // copy the scope exactly.
        InheritanceParametersDefinitionProperty ipdp =
            (InheritanceParametersDefinitionProperty) pdp;
        for (ScopeEntry scope : ipdp.getAllScopedParameterDefinitions()) {
          out.addScopedParameterDefinitions(scope);
        }
      } else {
        // No inheritance means the scope is fully local
        String ownerName = (pdp.getOwner() != null) ? pdp.getOwner().getName() : "";
        out.addScopedParameterDefinitions(ownerName, pdp.getParameterDefinitions());
      }
    }

    return out;
  }
コード例 #7
0
  /*
   * Create the OSLC parameter definitions form the Hudson parameter definitions.
   */
  private void fillInParameters(
      StaplerRequest request, AutomationPlan plan, AbstractProject<?, ?> project)
      throws URISyntaxException {
    ParametersDefinitionProperty pp = project.getProperty(ParametersDefinitionProperty.class);
    if (pp == null) {
      return;
    }

    Property[] autoParams = new Property[pp.getParameterDefinitions().size()];
    int i = 0;
    for (ParameterDefinition def : pp.getParameterDefinitions()) {
      autoParams[i++] = toProperty(request, def);
    }

    plan.setParameterDefinitions(autoParams);
  }
コード例 #8
0
 @SuppressWarnings("unchecked")
 protected boolean matchesDefaultValue(Job job) {
   ParametersDefinitionProperty property =
       (ParametersDefinitionProperty) job.getProperty(ParametersDefinitionProperty.class);
   if (property != null) {
     List<ParameterDefinition> defs = property.getParameterDefinitions();
     for (ParameterDefinition def : defs) {
       boolean multiline = isValueMultiline(def);
       String svalue = getStringValue(def);
       boolean matches = matchesParameter(def.getName(), svalue, multiline, def.getDescription());
       if (matches) {
         return true;
       }
     }
   }
   return false;
 }
コード例 #9
0
  /*
   * Determine the Hudson parameter values from the OSLC parameter instances
   * in the AutomationRequest
   */
  private List<ParameterValue> getParameterValues(
      AbstractProject<?, ?> project, ParameterInstance[] parameters) {
    ParametersDefinitionProperty pp = project.getProperty(ParametersDefinitionProperty.class);
    if (pp == null) {
      LOG.log(Level.FINE, "Job does not take parameters: " + project.getName());
      throw HttpResponses.status(
          HttpServletResponse.SC_BAD_REQUEST); // This build is not parameterized.
    }

    HashMap<String, String> inputMap = new HashMap<String, String>();
    for (ParameterInstance param : parameters) {
      inputMap.put(param.getName(), param.getValue());
    }

    List<ParameterValue> values = new ArrayList<ParameterValue>();
    for (ParameterDefinition def : pp.getParameterDefinitions()) {
      String inputValue = inputMap.get(def.getName());
      if (inputValue == null) {
        ParameterValue defaultValue = def.getDefaultParameterValue();
        if (defaultValue == null) {
          LOG.log(
              Level.FINE, "Missing parameter " + def.getName() + " for job " + project.getName());
          throw HttpResponses.status(HttpServletResponse.SC_BAD_REQUEST);
        }

        values.add(defaultValue);
      } else {
        if (def instanceof SimpleParameterDefinition) {
          SimpleParameterDefinition simple = (SimpleParameterDefinition) def;
          values.add(simple.createValue(inputValue));
        } else {
          LOG.log(
              Level.WARNING,
              "Unsupported parameter type with name "
                  + def.getName()
                  + " for project "
                  + project.getName());
          throw HttpResponses.status(HttpServletResponse.SC_NOT_IMPLEMENTED);
        }
      }
    }

    return values;
  }
コード例 #10
0
 /**
  * Method for getting the ParameterValue instance from ParameterDefinition or ParamterAction.
  *
  * @param paramDefProp ParametersDefinitionProperty
  * @param parameterName Name of the Parameter.
  * @param paramAction ParametersAction
  * @param req StaplerRequest
  * @param jo JSONObject
  * @return ParameterValue instance of subclass of ParameterValue
  */
 public ParameterValue getParameterValue(
     ParametersDefinitionProperty paramDefProp,
     String parameterName,
     ParametersAction paramAction,
     StaplerRequest req,
     JSONObject jo) {
   ParameterDefinition paramDef;
   // this is normal case when user try to rebuild a parameterized job.
   if (paramDefProp != null) {
     paramDef = paramDefProp.getParameterDefinition(parameterName);
     if (paramDef != null) {
       // The copy artifact plugin throws an exception when using createValue(req, jo)
       // If the parameter comes from the copy artifact plugin, then use the single argument
       // createValue
       if (jo.toString().contains("BuildSelector")
           || jo.toString().contains("WorkspaceSelector")) {
         SimpleParameterDefinition parameterDefinition =
             (SimpleParameterDefinition) paramDefProp.getParameterDefinition(parameterName);
         return parameterDefinition.createValue(jo.getString("value"));
       }
       return paramDef.createValue(req, jo);
     }
   }
   /*
    * when user try to rebuild a build that was invoked by
    * parameterized trigger plugin in that case ParameterDefinition
    * is null for that parametername that is paased by parameterize
    * trigger plugin,so for handling that scenario, we need to
    * create an instance of that specific ParameterValue with
    * passed parameter value by form.
    *
    * In contrast to all other parameterActions, ListSubversionTagsParameterValue uses "tag" instead of "value"
    */
   if (jo.containsKey("value")) {
     return cloneParameter(paramAction.getParameter(parameterName), jo.getString("value"));
   } else {
     return cloneParameter(paramAction.getParameter(parameterName), jo.getString("tag"));
   }
 }
コード例 #11
0
    public ListBoxModel doFillValueItems(
        @AncestorInPath AbstractProject<?, ?> project, @QueryParameter String param)
        throws IOException, InterruptedException {
      ListBoxModel items = new ListBoxModel();

      scm = getProjectSCM(project);
      if (scm == null) {
        items.add("!No Git repository configured in SCM configuration");
        return items;
      }
      ParametersDefinitionProperty prop = project.getProperty(ParametersDefinitionProperty.class);
      if (prop != null) {
        ParameterDefinition def = prop.getParameterDefinition(param);
        if (def instanceof GitParameterDefinition) {
          GitParameterDefinition paramDef = (GitParameterDefinition) def;
          Map<String, String> paramList = paramDef.generateContents(project, scm);

          for (Map.Entry<String, String> entry : paramList.entrySet()) {
            items.add(entry.getValue(), entry.getKey());
          }
        }
      }
      return items;
    }
 public void addScopedParameterDefinitions(ParametersDefinitionProperty pdp) {
   String owner = (pdp.getOwner() != null) ? pdp.getOwner().getName() : "";
   this.addScopedParameterDefinitions(owner, pdp.getParameterDefinitions());
 }
  /**
   * Returns a list of Subversion dirs to be displayed in {@code
   * ListSubversionTagsParameterDefinition/index.jelly}.
   *
   * <p>This method plainly reuses settings that must have been preivously defined when configuring
   * the Subversion SCM.
   *
   * <p>This method never returns {@code null}. In case an error happens, the returned list contains
   * an error message surrounded by &lt; and &gt;.
   */
  public List<String> getTags() {
    AbstractProject context = null;
    List<AbstractProject> jobs = Hudson.getInstance().getItems(AbstractProject.class);

    // which project is this parameter bound to? (I should take time to move
    // this code to Hudson core one day)
    for (AbstractProject project : jobs) {
      ParametersDefinitionProperty property =
          (ParametersDefinitionProperty) project.getProperty(ParametersDefinitionProperty.class);
      if (property != null) {
        List<ParameterDefinition> parameterDefinitions = property.getParameterDefinitions();
        if (parameterDefinitions != null) {
          for (ParameterDefinition pd : parameterDefinitions) {
            if (pd instanceof ListSubversionTagsParameterDefinition
                && ((ListSubversionTagsParameterDefinition) pd).compareTo(this) == 0) {
              context = project;
              break;
            }
          }
        }
      }
    }

    SimpleSVNDirEntryHandler dirEntryHandler = new SimpleSVNDirEntryHandler(tagsFilter);
    List<String> dirs = new ArrayList<String>();

    try {
      ISVNAuthenticationProvider authProvider =
          getDescriptor().createAuthenticationProvider(context);
      ISVNAuthenticationManager authManager =
          SubversionSCM.createSvnAuthenticationManager(authProvider);
      SVNURL repoURL = SVNURL.parseURIDecoded(getTagsDir());

      SVNRepository repo = SVNRepositoryFactory.create(repoURL);
      repo.setAuthenticationManager(authManager);
      SVNLogClient logClient = new SVNLogClient(authManager, null);

      if (isSVNRepositoryProjectRoot(repo)) {
        dirs = this.getSVNRootRepoDirectories(logClient, repoURL);
      } else {
        logClient.doList(
            repoURL, SVNRevision.HEAD, SVNRevision.HEAD, false, false, dirEntryHandler);
        dirs = dirEntryHandler.getDirs(isReverseByDate(), isReverseByName());
      }
    } catch (SVNException e) {
      // logs are not translated (IMO, this is a bad idea to translate logs)
      LOGGER.log(
          Level.SEVERE,
          "An SVN exception occurred while listing the directory entries at " + getTagsDir(),
          e);
      return new ArrayList() {
        {
          add(
              "&lt;"
                  + ResourceBundleHolder.get(ListSubversionTagsParameterDefinition.class)
                      .format("SVNException")
                  + "&gt;");
        }
      };
    }

    // SVNKit's doList() method returns also the parent dir, so we need to remove it
    if (dirs != null) {
      removeParentDir(dirs);
    } else {
      LOGGER.log(
          Level.INFO,
          "No directory entries were found for the following SVN repository: {0}",
          getTagsDir());
      return new ArrayList() {
        {
          add(
              "&lt;"
                  + ResourceBundleHolder.get(ListSubversionTagsParameterDefinition.class)
                      .format("NoDirectoryEntriesFound")
                  + "&gt;");
        }
      };
    }

    // Conform list to the maxTags option.
    Integer max = (isInt(this.maxTags) ? Integer.parseInt(this.maxTags) : null);
    if ((max != null) && (dirs.size() > max)) {
      dirs = dirs.subList(0, max);
    }

    return dirs;
  }