Skip to content

Commit

Permalink
Both exact and "_0" if only one value
Browse files Browse the repository at this point in the history
 * If request, or header, expression matches only one value then both an exact variable and a variable with added _0 is now contributed. Users who expect only one value will probably expect the exact variable name. Users who expects several will probably want to always use the variableName_X varaibles.
  • Loading branch information
tomasbjerre committed Jul 13, 2017
1 parent 3d6e791 commit 4cc63cc
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 28 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ Changelog of Generic Webhook Plugin.
## Unreleased
### No issue

**Both exact and "_0" if only one value**

* If request, or header, expression matches only one value then both an exact variable and a variable with added _0 is now contributed. Users who expect only one value will probably expect the exact variable name. Users who expects several will probably want to always use the variableName_X varaibles.

[2ed09abb65eeef5](https://github.com/jenkinsci/generic-webhook-trigger-plugin/commit/2ed09abb65eeef5) Tomas Bjerre *2017-07-13 19:37:12*

**doc**


[6cd329ef01ad127](https://github.com/jenkinsci/generic-webhook-trigger-plugin/commit/6cd329ef01ad127) Tomas Bjerre *2017-07-13 18:11:30*
[3d6e791dfedabfe](https://github.com/jenkinsci/generic-webhook-trigger-plugin/commit/3d6e791dfedabfe) Tomas Bjerre *2017-07-13 18:15:51*


## 1.12 (2017-07-13 18:09:55)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ job('Generic Job Example') {
shell('''
echo $VARIABLE_FROM_POST
echo $VARIABLE_FROM_REQUEST
echo $VARIABLE_FROM_HEADER_0
echo $VARIABLE_FROM_HEADER
''')
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public class GenericWebhookEnvironmentContributor extends EnvironmentContributor
@SuppressWarnings("unchecked")
@Override
public void buildEnvironmentFor(
@Nonnull Run r, @Nonnull EnvVars envs, @Nonnull TaskListener listener)
@SuppressWarnings("rawtypes") @Nonnull Run r,
@Nonnull EnvVars envs,
@Nonnull TaskListener listener)
throws IOException, InterruptedException {
boolean shouldLog = shouldLog(r);
GenericCause cause = (GenericCause) r.getCause(GenericCause.class);
Expand Down Expand Up @@ -51,7 +53,7 @@ public void buildEnvironmentFor(
}
}

private boolean shouldLog(Run r) throws IOException {
private boolean shouldLog(@SuppressWarnings("rawtypes") Run r) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(r.getLogFile()))) {
String line;
while ((line = br.readLine()) != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public Map<String, String> getRequestHeaders(
String regexpFilter = configuredVariable.get().getRegexpFilter();
String filteredValue = filter(headerValue, regexpFilter);
found.put(headerName + "_" + i, filteredValue);
boolean firstAndOnlyValue = i == 0 && !headerEnumeration.hasMoreElements();
if (firstAndOnlyValue) {
//Users will probably expect this variable for parameters that are never a list
found.put(headerName, filteredValue);
}
i++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import static com.google.common.collect.Maps.newHashMap;
import static org.jenkinsci.plugins.gwt.FlattenerUtils.filter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand All @@ -29,24 +28,13 @@ public Map<String, String> getRequestParameters(
}
String regexpFilter = mappedRequestParameterOpt.get();
String[] values = incomingParameterMap.get(requestParamName);
if (values.length == 1) {
resolvedVariables.put(requestParamName, filter(values[0], regexpFilter));
} else {
List<String> foundFilteredValues = new ArrayList<>();
for (String valueCandidate : values) {
String filteredValue = filter(valueCandidate, regexpFilter);
if (!filteredValue.isEmpty()) {
foundFilteredValues.add(filteredValue);
}
}
if (foundFilteredValues.size() == 1) {
String filteredValue = foundFilteredValues.get(0);
for (int i = 0; i < values.length; i++) {
String filteredValue = filter(values[i], regexpFilter);
resolvedVariables.put(requestParamName + "_" + i, filteredValue);
boolean firstAndOnlyValue = i == 0 && values.length == 1;
if (firstAndOnlyValue) {
//Users will probably expect this variable for parameters that are never a list
resolvedVariables.put(requestParamName, filteredValue);
} else {
for (int i = 0; i < foundFilteredValues.size(); i++) {
String filteredValue = foundFilteredValues.get(i);
resolvedVariables.put(requestParamName + "_" + i, filteredValue);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ public void testGenericRequestParameters() throws Exception {
.getVariables();

assertThat(variables) //
.containsEntry("someparam", "some value") //
.containsEntry("someparam_0", "some value") //
.containsEntry("anotherparam_1", "eee") //
.containsEntry("anotherparam_0", "ee") //
.hasSize(3);
.hasSize(4);
}

private Enumeration<String> enumeration(final String... string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ public void testJSONPathGetAllVariable() throws Exception {
.containsEntry("reqp1_0", "a") //
.containsEntry("reqp1_1", "b") //
.containsEntry("reqp2", "just one") //
.hasSize(6);
.containsEntry("reqp2_0", "just one") //
.hasSize(7);
}

@Test
Expand Down Expand Up @@ -151,11 +152,13 @@ public void testGenericRequestParameters() throws Exception {
.getVariables();

assertThat(variables) //
.containsEntry("reqp1", "123456") //
.containsEntry("reqp1_0", "123456") //
.containsEntry("reqp1_1", "") //
.containsEntry("reqp3", "justone") //
.containsEntry("reqp3_0", "justone") //
.containsEntry("reqp4_0", "just one") //
.containsEntry("reqp4_1", "just one again") //
.hasSize(4);
.hasSize(6);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ public void testGenericRequestParameters() throws Exception {
.getVariables();

assertThat(variables) //
.containsEntry("reqp1", "123456") //
.containsEntry("reqp1_0", "123456") //
.containsEntry("reqp1_1", "") //
.containsEntry("reqp3", "justone") //
.containsEntry("reqp3_0", "justone") //
.containsEntry("reqp4_0", "just one") //
.containsEntry("reqp4_1", "just one again") //
.hasSize(4);
.hasSize(6);
}
}

0 comments on commit 4cc63cc

Please sign in to comment.