Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Merge pull request #16 from ikedam/feature/JENKINS-29715_ControlWithA…
…ction [JENKINS-29715] Control rescheduling with action
- Loading branch information
Showing
7 changed files
with
888 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/main/java/com/chikli/hudson/plugin/naginator/NaginatorPublisherScheduleAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package com.chikli.hudson.plugin.naginator; | ||
|
||
import hudson.matrix.MatrixRun; | ||
import hudson.model.Result; | ||
import hudson.model.Run; | ||
import hudson.model.TaskListener; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import javax.annotation.CheckForNull; | ||
import javax.annotation.Nonnull; | ||
|
||
/** | ||
* Used from {@link NaginatorPublisher} to mark a build to be reshceduled. | ||
*/ | ||
public class NaginatorPublisherScheduleAction extends NaginatorScheduleAction { | ||
private static final Logger LOGGER = Logger.getLogger(NaginatorPublisherScheduleAction.class.getName()); | ||
|
||
private final String regexpForRerun; | ||
private final boolean rerunIfUnstable; | ||
private final boolean checkRegexp; | ||
|
||
public NaginatorPublisherScheduleAction(NaginatorPublisher publisher) { | ||
super(publisher.getMaxSchedule(), publisher.getDelay(), publisher.isRerunMatrixPart()); | ||
this.regexpForRerun = publisher.getRegexpForRerun(); | ||
this.rerunIfUnstable = publisher.isRerunIfUnstable(); | ||
this.checkRegexp = publisher.isCheckRegexp(); | ||
} | ||
|
||
@CheckForNull | ||
public String getRegexpForRerun() { | ||
return regexpForRerun; | ||
} | ||
|
||
public boolean isRerunIfUnstable() { | ||
return rerunIfUnstable; | ||
} | ||
|
||
public boolean isCheckRegexp() { | ||
return checkRegexp; | ||
} | ||
|
||
@Override | ||
public boolean shouldSchedule(@Nonnull Run<?, ?> run, @Nonnull TaskListener listener, int retryCount) { | ||
if ((run.getResult() == Result.SUCCESS) || (run.getResult() == Result.ABORTED)) { | ||
return false; | ||
} | ||
|
||
// If we're not set to rerun if unstable, and the build's unstable, return true. | ||
if ((!isRerunIfUnstable()) && (run.getResult() == Result.UNSTABLE)) { | ||
return false; | ||
} | ||
|
||
// If we're supposed to check for a regular expression in the build output before | ||
// scheduling a new build, do so. | ||
if (isCheckRegexp()) { | ||
LOGGER.log(Level.FINEST, "Got checkRegexp == true"); | ||
|
||
String regexpForRerun = getRegexpForRerun(); | ||
if ((regexpForRerun != null) && (!regexpForRerun.equals(""))) { | ||
LOGGER.log(Level.FINEST, "regexpForRerun - {0}", regexpForRerun); | ||
|
||
try { | ||
// If parseLog returns false, we didn't find the regular expression, | ||
// so return true. | ||
if (!parseLog(run.getLogFile(), regexpForRerun)) { | ||
LOGGER.log(Level.FINEST, "regexp not in logfile"); | ||
return false; | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(listener | ||
.error("error while parsing logs for naginator - forcing rebuild.")); | ||
} | ||
} | ||
} | ||
|
||
return super.shouldSchedule(run, listener, retryCount); | ||
} | ||
|
||
@Override | ||
public boolean shouldScheduleForMatrixRun(@Nonnull MatrixRun run, @Nonnull TaskListener listener) { | ||
if ((run.getResult() == Result.SUCCESS) || (run.getResult() == Result.ABORTED)) { | ||
return false; | ||
} | ||
if ((!isRerunIfUnstable()) && (run.getResult() == Result.UNSTABLE)) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
private boolean parseLog(File logFile, @Nonnull String regexp) throws IOException { | ||
// TODO annotate `logFile` with `@Nonnull` | ||
// after upgrading the target Jenkins to 1.568 or later. | ||
|
||
// Assume default encoding and text files | ||
String line; | ||
Pattern pattern = Pattern.compile(regexp); | ||
BufferedReader reader = null; | ||
try { | ||
reader = new BufferedReader(new FileReader(logFile)); | ||
while ((line = reader.readLine()) != null) { | ||
Matcher matcher = pattern.matcher(line); | ||
if (matcher.find()) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} finally { | ||
if (reader != null) { | ||
reader.close(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.