/** Gets the {@link ParameterDefinition} of the given name, if any. */
  public ParameterDefinition getParameterDefinition(String name) {
    if (parameterDefinitions == null) {
      return null;
    }

    for (ParameterDefinition pd : parameterDefinitions) if (pd.getName().equals(name)) return pd;

    return null;
  }
  /*
   * 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;
  }
예제 #3
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;
  }
  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;
  }
 @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;
 }
 public void addScopedParameterDefinitions(String owner, Collection<ParameterDefinition> pds) {
   for (ParameterDefinition pd : pds) {
     String pdName = pd.getName();
     this.scopeLock.writeLock().lock();
     try {
       List<ScopeEntry> lst = fullScope.get(pdName);
       if (lst == null) {
         lst = new LinkedList<ScopeEntry>();
         lst.add(new ScopeEntry(owner, pd));
         fullScope.put(pdName, lst);
       } else {
         lst.add(new ScopeEntry(owner, pd));
       }
     } finally {
       this.scopeLock.writeLock().unlock();
     }
   }
 }
  /*
   * Convert an individual Hudson parameter definition to an OSLC Property.
   */
  private Property toProperty(StaplerRequest request, ParameterDefinition def)
      throws URISyntaxException {
    Property prop = new Property();
    prop.setName(def.getName());
    prop.setDescription(def.getDescription());

    if (def instanceof BooleanParameterDefinition) {
      prop.setValueType(new URI(XSDDatatype.XSDboolean.getURI()));
    } else if (def instanceof StringParameterDefinition
        || def instanceof PasswordParameterDefinition) {
      prop.setValueType(new URI(XSDDatatype.XSDstring.getURI()));
    } else if (def instanceof ChoiceParameterDefinition) {
      prop.setValueType(new URI(XSDDatatype.XSDstring.getURI()));
      ChoiceParameterDefinition choices = (ChoiceParameterDefinition) def;
      prop.setAllowedValuesCollection(choices.getChoices());
    }
    // TODO: Other types?

    ParameterValue defaultValue = def.getDefaultParameterValue();
    if (defaultValue == null) {
      prop.setOccurs(Occurs.ExactlyOne);
    } else {
      prop.setOccurs(Occurs.ZeroOrOne);
      if (!defaultValue.isSensitive()) {
        if (defaultValue instanceof BooleanParameterValue) {
          BooleanParameterValue bool = (BooleanParameterValue) defaultValue;
          prop.setDefaultValue(bool.value);
        } else if (defaultValue instanceof StringParameterValue) {
          StringParameterValue str = (StringParameterValue) defaultValue;
          prop.setDefaultValue(str.value);
        }
        // TODO: Other types?
      }
    }

    return prop;
  }