Skip to content

Commit

Permalink
Merge pull request #40 from jglick/Run.logFile-JENKINS-54128
Browse files Browse the repository at this point in the history
[JENKINS-54128] Avoid calling Run.getLogFile
  • Loading branch information
ikedam committed Jun 15, 2019
2 parents 91b25d9 + 7136052 commit 8f48cbe
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 14 deletions.
57 changes: 49 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>3.2</version>
<version>3.43</version>
<relativePath />
</parent>

<name>Groovy Postbuild</name>
<groupId>org.jvnet.hudson.plugins</groupId>
<artifactId>groovy-postbuild</artifactId>
<version>2.5-SNAPSHOT</version>
<packaging>hpi</packaging>
<url>http://wiki.jenkins-ci.org/display/JENKINS/Groovy+Postbuild+Plugin</url>
<url>https://wiki.jenkins.io/display/JENKINS/Groovy+Postbuild+Plugin</url>

<developers>
<developer>
Expand All @@ -27,17 +28,17 @@
<licenses>
<license>
<name>The MIT license</name>
<url>http://www.opensource.org/licenses/mit-license.php</url>
<url>https://opensource.org/licenses/MIT</url>
<distribution>repo</distribution>
</license>
</licenses>

<properties>
<jenkins.version>2.60.3</jenkins.version>
<jenkins.version>2.121.1</jenkins.version>
<java.level>8</java.level>
<badge-plugin.version>1.2</badge-plugin.version>
<workflow-step-api-plugin.version>2.14</workflow-step-api-plugin.version>
<workflow-cps-plugin.version>2.45</workflow-cps-plugin.version>
<workflow-step-api-plugin.version>2.20</workflow-step-api-plugin.version>
<workflow-cps-plugin.version>2.69</workflow-cps-plugin.version>
</properties>

<scm>
Expand All @@ -50,7 +51,7 @@
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>http://repo.jenkins-ci.org/public/</url>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>

Expand Down Expand Up @@ -87,7 +88,13 @@
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>2.10</version>
<version>2.32</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-basic-steps</artifactId>
<version>2.15</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -97,4 +104,38 @@
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<version>${workflow-step-api-plugin.version}</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-api</artifactId>
<version>2.33</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-support</artifactId>
<version>3.3</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>scm-api</artifactId>
<version>2.2.6</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>script-security</artifactId>
<version>1.60</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import groovy.lang.Binding;
import hudson.AbortException;
import hudson.EnvVars;
import hudson.Functions;
import hudson.Launcher;
import hudson.matrix.MatrixAggregatable;
import hudson.matrix.MatrixAggregator;
Expand Down Expand Up @@ -254,7 +255,7 @@ public void buildScriptFailed(Exception e) {

@Whitelisted
public boolean logContains(String regexp) {
return contains(build.getLogFile(), build.getCharset(), regexp);
return getLogMatcher(regexp) != null;
}


Expand All @@ -263,6 +264,7 @@ public boolean contains(File f, String regexp) {
return contains(f, Charset.defaultCharset(), regexp);
}

@Deprecated
// not @Whitelisted unless we know what file that is
public boolean contains(File f, Charset charset, String regexp) {
Matcher matcher = getMatcher(f, charset, regexp);
Expand All @@ -271,18 +273,23 @@ public boolean contains(File f, Charset charset, String regexp) {

@Whitelisted
public Matcher getLogMatcher(String regexp) {
return getMatcher(build.getLogFile(), build.getCharset(), regexp);
try (Reader r = build.getLogReader()) {
return getMatcher(r, regexp);
} catch (IOException e) {
Functions.printStackTrace(e, listener.error("Groovy Postbuild: logContains(\"" + regexp + "\") failed."));
buildScriptFailed(e);
return null;
}
}

@Deprecated
public Matcher getMatcher(File f, String regexp) {
return getMatcher(f, Charset.defaultCharset(), regexp);
}

public Matcher getMatcher(File f, Charset charset, String regexp) {
LOGGER.fine("Searching for '" + regexp + "' in '" + f + "'.");
public Matcher getMatcher(Reader r, String regexp) {
Matcher matcher = null;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f), charset))) {
try (BufferedReader reader = new BufferedReader(r)) {
Pattern pattern = compilePattern(regexp);
// Assume default encoding and text files
String line;
Expand All @@ -294,10 +301,23 @@ public Matcher getMatcher(File f, Charset charset, String regexp) {
}
}
} catch (IOException e) {
e.printStackTrace(listener.error("Groovy Postbuild: getMatcher(\"" + f + "\", \"" + regexp + "\") failed."));
Functions.printStackTrace(e, listener.error("Groovy Postbuild: getMatcher(, \"" + regexp + "\") failed."));
buildScriptFailed(e);
}
return matcher;

}

@Deprecated
public Matcher getMatcher(File f, Charset charset, String regexp) {
LOGGER.fine("Searching for '" + regexp + "' in '" + f + "'.");
try (InputStream is = new FileInputStream(f); Reader r = new InputStreamReader(is, charset)) {
return getMatcher(r, regexp);
} catch (IOException e) {
Functions.printStackTrace(e, listener.error("Groovy Postbuild: getMatcher(\"" + f + "\", \"" + regexp + "\") failed."));
buildScriptFailed(e);
}
return null;
}

private Pattern compilePattern(String regexp) throws AbortException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,18 @@
import org.jvnet.hudson.test.JenkinsRule;

import com.jenkinsci.plugins.badge.action.BadgeAction;
import java.util.Collections;
import java.util.logging.Level;
import org.jvnet.hudson.test.LoggerRule;

public class WorkflowTest {

@Rule
public JenkinsRule r = new JenkinsRule();

@Rule
public LoggerRule logging = new LoggerRule();

@Issue("JENKINS-26918")
@Test
public void usingManager() throws Exception {
Expand All @@ -48,4 +54,18 @@ public void usingManager() throws Exception {
assertEquals("stuff is broken", b.getAction(BadgeAction.class).getText());
}

@Issue("JENKINS-54128")
@Test
public void logContains() throws Exception {
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"echo '1st message'\n" +
"echo '2nd message'\n" +
"sleep 1\n" + // to flush output (inspecting Run.log from the build itself is unreliable; use a TaskListenerDecorator instead)
"echo(/found first message? ${manager.logContains(/1st message/)} second? ${manager.logContains(/2nd message/)} third? ${manager.logContains(/3rd message/)} /); ", true));
logging.record(WorkflowRun.class, Level.WARNING).capture(100);
r.assertLogContains("found first message? true second? true third? false", r.assertBuildStatusSuccess(p.scheduleBuild2(0)));
assertEquals(Collections.emptyList(), logging.getRecords());
}

}

0 comments on commit 8f48cbe

Please sign in to comment.