Skip to content

Commit 7ea44f8

Browse files
committed
Suppress build log messages in the common case of a non-trustworthy build (e.g., SCM trigger) where this does not matter (e.g., branch push) jenkinsci#180 (comment)
1 parent e5707b7 commit 7ea44f8

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/main/java/jenkins/scm/api/SCMSource.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
import hudson.scm.SCM;
4141
import hudson.util.AlternativeUiTextProvider;
4242
import hudson.util.LogTaskListener;
43+
import hudson.util.StreamTaskListener;
4344
import java.io.IOException;
45+
import java.io.StringWriter;
4446
import java.util.Collections;
4547
import java.util.HashSet;
4648
import java.util.LinkedHashSet;
@@ -974,15 +976,22 @@ public SCMRevision getTrustedRevision(@NonNull SCMRevision revision, @NonNull Ta
974976
@NonNull
975977
public final SCMRevision getTrustedRevisionForBuild(@NonNull SCMRevision revision, @NonNull TaskListener listener, @NonNull Run<?, ?> build)
976978
throws IOException, InterruptedException {
977-
if (ExtensionList.lookup(TrustworthyBuild.class).stream().anyMatch(tb -> tb.shouldBeTrusted(build, listener))) {
979+
// Cheaper to check TrustworthyBuild than to call some getTrustedRevision impls, so try that first,
980+
// but defer printing resulting messages if possible.
981+
StringWriter buffer = new StringWriter();
982+
TaskListener bufferedListener = new StreamTaskListener(buffer);
983+
if (ExtensionList.lookup(TrustworthyBuild.class).stream().anyMatch(tb -> tb.shouldBeTrusted(build, bufferedListener))) {
978984
LOGGER.fine(() -> build + " with " + build.getCauses() + " was considered trustworthy, so using " + revision + " as is");
985+
listener.getLogger().print(buffer.toString());
979986
return revision;
980987
} else {
981988
SCMRevision trustedRevision = getTrustedRevision(revision, listener);
982-
if (trustedRevision.equals(revision)) {
983-
LOGGER.fine(() -> revision + " was trusted anyway so it is irrelevant that " + build + " was not specifically considered trustworthy");
989+
if (trustedRevision.equals(revision)) { // common case
990+
LOGGER.fine(() -> revision + " was trusted anyway so it is irrelevant that " + build + " was not specifically considered trustworthy\n" + buffer);
984991
} else {
985992
LOGGER.fine(() -> build + " was not considered trustworthy, so replacing " + revision + " with " + trustedRevision);
993+
listener.getLogger().print(buffer.toString());
994+
listener.getLogger().println(build + " was not considered trustworthy, so replacing " + revision + " with " + trustedRevision);
986995
}
987996
return trustedRevision;
988997
}

0 commit comments

Comments
 (0)