/*
   * 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;
  }
예제 #2
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"));
   }
 }