Skip to content

Commit

Permalink
fix: contributed variables repeated in build log (refs #283)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Nov 25, 2023
1 parent 093e815 commit 573e39f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 35 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
<jenkins.version>2.361.4</jenkins.version>
<findbugs.failOnError>false</findbugs.failOnError>
<maven.javadoc.skip>true</maven.javadoc.skip>
<violations.version>1.50.8</violations.version>
<changelog>1.97.1</changelog>
<violations.version>1.50.9</violations.version>
<changelog>1.100.6</changelog>
</properties>

<artifactId>generic-webhook-trigger</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
./mvnw versions:update-properties
./mvnw hpi:run \
-Djava.util.logging.config.file=logging.properties \
-Djenkins.version=2.361.4 \
-Djenkins.version=2.426.1 \
-Denforcer.skip=true \
-Dhudson.model.ParametersAction.keepUndefinedParameters=true
12 changes: 12 additions & 0 deletions src/main/java/org/jenkinsci/plugins/gwt/GenericCause.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public class GenericCause extends Cause {
private final String postContent;
private final boolean printContributedVariables;
private final boolean printPostContent;
/** Used at runtime to only print once. */
private boolean postContentPrinted;

private final String cause;

public GenericCause(
Expand All @@ -25,6 +28,7 @@ public GenericCause(
this.resolvedVariables = resolvedVariables;
this.printContributedVariables = printContributedVariables;
this.printPostContent = printPostContent;
this.postContentPrinted = false;
if (!isNullOrEmpty(cause)) {
this.cause = cause;
} else {
Expand All @@ -40,6 +44,14 @@ public boolean isPrintPostContent() {
return printPostContent;
}

public void setPostContentPrinted(boolean postContentPrinted) {
this.postContentPrinted = postContentPrinted;
}

public boolean isPostContentPrinted() {
return postContentPrinted;
}

public Map<String, String> getResolvedVariables() {
return resolvedVariables;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package org.jenkinsci.plugins.gwt;

import com.google.common.base.Charsets;
import hudson.EnvVars;
import hudson.Extension;
import hudson.model.EnvironmentContributor;
import hudson.model.Run;
import hudson.model.TaskListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import javax.annotation.Nonnull;

Expand All @@ -27,38 +24,29 @@ public void buildEnvironmentFor(
final GenericCause cause = (GenericCause) r.getCause(GenericCause.class);
if (cause != null) {
final boolean shouldLog =
(cause.isPrintContributedVariables() || cause.isPrintPostContent()) && notLogged(r);
if (shouldLog && cause.isPrintPostContent()) {
listener.getLogger().println(CONTRIBUTING_VARIABLES);
listener.getLogger().println(" Received:\n\n" + cause.getPostContent() + "\n\n");
}
final Map<String, String> resolvedVariables = cause.getResolvedVariables();
if (shouldLog && cause.isPrintContributedVariables()) {
listener.getLogger().println("Contributing variables:\n");
}
for (final String variable : resolvedVariables.keySet()) {
final String resolved = cause.getResolvedVariables().get(variable);
if (shouldLog && cause.isPrintContributedVariables()) {
listener.getLogger().println(" " + variable + " = " + resolved);
(cause.isPrintContributedVariables() || cause.isPrintPostContent())
&& !cause.isPostContentPrinted();
if (shouldLog) {
cause.setPostContentPrinted(true);
if (cause.isPrintPostContent()) {
listener.getLogger().println(CONTRIBUTING_VARIABLES);
listener.getLogger().println(" Received:\n\n" + cause.getPostContent() + "\n\n");
}
envs.override(variable, resolved);
}
if (shouldLog && cause.isPrintContributedVariables()) {
listener.getLogger().println("\n");
}
}
}

private boolean notLogged(@SuppressWarnings("rawtypes") final Run r) throws IOException {
try (BufferedReader br =
new BufferedReader(new InputStreamReader(r.getLogInputStream(), Charsets.UTF_8))) {
String line;
while ((line = br.readLine()) != null) {
if (line.contains(CONTRIBUTING_VARIABLES)) {
return false;
final Map<String, String> resolvedVariables = cause.getResolvedVariables();
if (cause.isPrintContributedVariables()) {
listener.getLogger().println("Contributing variables:\n");
}
for (final String variable : resolvedVariables.keySet()) {
final String resolved = cause.getResolvedVariables().get(variable);
if (cause.isPrintContributedVariables()) {
listener.getLogger().println(" " + variable + " = " + resolved);
}
envs.override(variable, resolved);
}
if (cause.isPrintContributedVariables()) {
listener.getLogger().println("\n");
}
}
}
return true;
}
}

0 comments on commit 573e39f

Please sign in to comment.