Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-54128] Deprecated calls to Run.getLogFile #42

Merged
merged 1 commit into from
Mar 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,8 @@
import hudson.model.Run;
import hudson.model.TaskListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
Expand All @@ -41,13 +35,6 @@ public final class LogRegExMacro extends DataBoundTokenMacro {
@Parameter(required=true)
public String replacement = null;

/**
* Charset to be used in order to read logs from the file.
* @since TODO
*/
@Parameter(required = false)
public String charset = null;

@Override
public boolean acceptsMacroName(String macroName) {
return MACRO_NAME.equals(macroName);
Expand All @@ -65,40 +52,26 @@ public String evaluate(AbstractBuild<?, ?> context, TaskListener listener, Strin

@Override
public String evaluate(Run<?, ?> run, FilePath workspace, TaskListener listener, String macroName) throws MacroEvaluationException, IOException, InterruptedException {
return readLogFile(run.getLogFile());
return readLogFile(run);
}

public String readLogFile(File file) throws IOException {
private String readLogFile(Run<?, ?> run) throws IOException {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't changing the visibility a bit risky?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit risky, yes. I am fine with it though

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine then, thanks!

if (regex == null) {
return "";
}

// Prepare patterns and encodings
String line;
Pattern pattern = Pattern.compile(regex);
Charset logCharset = Charset.defaultCharset();
if (charset != null) {
try {
logCharset = Charset.forName(charset);
} catch (IllegalCharsetNameException ex) {
throw new IOException("Charset " + charset + " is illegal", ex);
} catch (UnsupportedCharsetException ex) {
throw new IOException("Charset " + charset + " is not supported", ex);
}
}

BufferedReader reader = new BufferedReader(
new InputStreamReader(new FileInputStream(file), logCharset));
try {
try (BufferedReader reader = new BufferedReader(run.getLogReader())) {
while ((line = reader.readLine()) != null) {
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
// Match only the top-most line
return getTranslatedDescription(matcher);
}
}
} finally {
reader.close();
}

return "";
Expand Down