/**
   * Creates a {@link hudson.model.StringParameterValue} and adds it to the provided list. If the
   * parameter with the same name already exists in the list it will be replaced by the new
   * parameter, but its description will be used, unless the parameter type is something else than a
   * StringParameterValue.
   *
   * @param parameters the list of existing parameters.
   * @param value the value.
   * @param escapeQuotes if quote characters should be escaped.
   */
  public void setOrCreateStringParameterValue(
      List<ParameterValue> parameters, String value, boolean escapeQuotes) {
    ParameterValue parameter = null;
    for (ParameterValue p : parameters) {
      if (p.getName().toUpperCase().equals(this.name())) {
        parameter = p;
        break;
      }
    }
    String description = null;
    if (parameter != null) {
      if (parameter instanceof StringParameterValue) {
        // Perhaps it is manually added to remind the user of what it is for.
        description = parameter.getDescription();
      }
      parameters.remove(parameter);
    }
    String stringValue;
    if (escapeQuotes) {
      stringValue = StringUtil.escapeQuotes(value);
    } else {
      stringValue = value;
    }
    if (stringValue == null) {
      stringValue = "";
    }

    parameter = new StringParameterValue(this.name(), stringValue, description);
    parameters.add(parameter);
  }
 /** {@inheritDoc} */
 @Override
 public boolean isSelectable(Run<?, ?> run, EnvVars env) {
   EnvVars otherEnv;
   try {
     otherEnv = run.getEnvironment(TaskListener.NULL);
   } catch (Exception ex) {
     return false;
   }
   if (!(run instanceof AbstractBuild)) {
     // Abstract#getEnvironment(TaskListener) put build parameters to
     // environments, but Run#getEnvironment(TaskListener) doesn't.
     // That means we can't retrieve build parameters from WorkflowRun
     // as it is a subclass of Run, not of AbstractBuild.
     // We need expand build parameters manually.
     // See JENKINS-26694 for details.
     for (ParametersAction pa : run.getActions(ParametersAction.class)) {
       // We have to extract parameters manually as ParametersAction#buildEnvVars
       // (overrides EnvironmentContributingAction#buildEnvVars)
       // is applicable only for AbstractBuild.
       for (ParameterValue pv : pa.getParameters()) {
         pv.buildEnvironment(run, otherEnv);
       }
     }
   }
   for (StringParameterValue spv : filters) {
     if (!spv.value.equals(otherEnv.get(spv.getName()))) {
       return false;
     }
   }
   return true;
 }
 @Override
 public void buildEnvVars(AbstractBuild<?, ?> build, EnvVars env) {
   List<ParameterValue> params = ((Promotion) build).getParameterValues();
   if (params != null) {
     for (ParameterValue value : params) {
       value.buildEnvVars(build, env);
     }
   }
 }
  /** Test for {@link ChoiceParameterDefinition#createValue(StaplerRequest, JSONObject)}. */
  @Test
  public void testCreateValueJSON() {
    final StaplerRequest req = mock(StaplerRequest.class);
    // final JSONObject jo = mock(JSONObject.class); // mockito cannot mock final classes
    final JSONObject jo = null;

    final StringParameterValue value =
        new StringParameterValue("value", (String) SCRIPT_STRINGS_RESULT[1]);

    when(req.bindJSON(StringParameterValue.class, jo)).thenReturn(value);
    final ParameterValue result = choiceParameterDefinition.createValue(req, jo);

    assertEquals(value, result);
    assertEquals(choiceParameterDefinition.getDescription(), result.getDescription());
  }
 private BuildData getBuildData(StringParameterValue pullRequestUrlParam) {
   for (Run<?, ?> build : project.getBuilds()) {
     ParametersAction paramsAction = build.getAction(ParametersAction.class);
     if (paramsAction != null) {
       for (ParameterValue param : paramsAction.getParameters()) {
         if (param.equals(pullRequestUrlParam)) {
           List<BuildData> buildDataList = build.getActions(BuildData.class);
           if (!buildDataList.isEmpty()) {
             return buildDataList.get(0);
           }
         }
       }
     }
   }
   return null;
 }
Ejemplo n.º 6
0
 private void populateWithParameters(Run build, String externalizableId) {
   ParametersAction action = build.getAction(ParametersAction.class);
   if (action != null) {
     List<ParameterValue> parameters = action.getParameters();
     if (parameters != null) {
       for (ParameterValue parameter : parameters) {
         if (parameter instanceof StringParameterValue) {
           put(
               "parameter-" + parameter.getName(),
               ((StringParameterValue) parameter).value,
               externalizableId);
         }
       }
     }
   }
 }
 @SuppressWarnings("unchecked")
 public boolean matchesRun(Run run) {
   ParametersAction action = run.getAction(ParametersAction.class);
   if (action == null) {
     return false;
   }
   // look for one parameter value that matches our criteria
   for (ParameterValue value : action.getParameters()) {
     String sval = getStringValue(value);
     if (matchesParameter(value.getName(), sval, false, null)) {
       return true;
     }
   }
   // no parameters matched the criteria
   return false;
 }
  public void test() {
    // Test default value
    ArbitraryParameterDefinition testDefinition =
        new ArbitraryParameterDefinition(
            "param_name", "FOo1=BARVALUE1\nfoo2=BAR2", "This is the description.");
    ArbitraryParameterValue testDefaultValue = testDefinition.getDefaultParameterValue();
    EnvVars testVars = new EnvVars(new HashMap<String, String>());
    testDefaultValue.buildEnvVars(null, testVars);
    assertEquals("BARVALUE1", testVars.get("FOo1"));
    assertEquals("BAR2", testVars.get("foo2"));

    // Test custom value
    ParameterValue testCustomValue =
        testDefinition.createValue("param=val val val\nA_Param='vel vel vel'\n");
    testCustomValue.buildEnvVars(null, testVars);
    assertEquals("val val val", testVars.get("param"));
    assertEquals("\'vel vel vel\'", testVars.get("A_Param"));
  }
  /**
   * Creates a {@link hudson.model.ParameterValue} and adds it to the provided list. If the
   * parameter with the same name already exists in the list it will be replaced by the new
   * parameter, but its description will be used, unless the parameter type is something else than a
   * StringParameterValue.
   *
   * @param parameters the list of existing parameters.
   * @param value the value.
   * @param escapeQuotes if quote characters should be escaped.
   * @param clazz the class which extends {@link hudson.model.ParameterValue}.
   */
  private void setOrCreateParameterValue(
      List<ParameterValue> parameters,
      String value,
      boolean escapeQuotes,
      Class<? extends StringParameterValue> clazz) {
    ParameterValue parameter = null;
    for (ParameterValue p : parameters) {
      if (p.getName().toUpperCase().equals(this.name())) {
        parameter = p;
        break;
      }
    }
    String description = null;
    if (parameter != null) {
      if (parameter instanceof StringParameterValue) {
        // Perhaps it is manually added to remind the user of what it is for.
        description = parameter.getDescription();
      }
      parameters.remove(parameter);
    }
    String stringValue;
    if (escapeQuotes) {
      stringValue = StringUtil.escapeQuotes(value);
    } else {
      stringValue = value;
    }
    if (stringValue == null) {
      stringValue = "";
    }

    Class<?>[] types = {String.class, String.class, String.class};
    Object[] args = {this.name(), stringValue, description};
    Constructor<? extends StringParameterValue> constructor;
    try {
      constructor = clazz.getConstructor(types);
      parameter = constructor.newInstance(args);
      parameters.add(parameter);
    } catch (Exception ex) {
      parameter = null;
    }
  }
Ejemplo n.º 10
0
  /*
   * 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;
  }
 private static ParametersAction mergeParameters(ParametersAction base, ParametersAction overlay) {
   LinkedHashMap<String, ParameterValue> params = new LinkedHashMap<String, ParameterValue>();
   for (ParameterValue param : base.getParameters()) params.put(param.getName(), param);
   for (ParameterValue param : overlay.getParameters()) params.put(param.getName(), param);
   return new ParametersAction(params.values().toArray(new ParameterValue[params.size()]));
 }
Ejemplo n.º 12
0
 /**
  * Method for replacing the old parametervalue with new parameter value
  *
  * @param oldValue ParameterValue
  * @param newValue The value that is submitted by user using form.
  * @return ParameterValue
  */
 private ParameterValue cloneParameter(ParameterValue oldValue, String newValue) {
   if (oldValue instanceof StringParameterValue) {
     return new StringParameterValue(oldValue.getName(), newValue, oldValue.getDescription());
   } else if (oldValue instanceof BooleanParameterValue) {
     return new BooleanParameterValue(
         oldValue.getName(), Boolean.valueOf(newValue), oldValue.getDescription());
   } else if (oldValue instanceof RunParameterValue) {
     return new RunParameterValue(oldValue.getName(), newValue, oldValue.getDescription());
   } else if (oldValue instanceof PasswordParameterValue) {
     return new PasswordParameterValue(oldValue.getName(), newValue, oldValue.getDescription());
   } else if (oldValue.getClass().getName().equals(SVN_TAG_PARAM_CLASS)) {
     /** getClass().getName() to avoid dependency on svn plugin. */
     return new StringParameterValue(oldValue.getName(), newValue, oldValue.getDescription());
   }
   throw new IllegalArgumentException("Unrecognized parameter type: " + oldValue.getClass());
 }