Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d79cd94
commit ed9a90b
Showing
6 changed files
with
312 additions
and
26 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
66 changes: 66 additions & 0 deletions
66
src/main/java/org/jenkinsci/lib/xtrigger/AbstractTriggerByFullContext.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,66 @@ | ||
package org.jenkinsci.lib.xtrigger; | ||
|
||
import antlr.ANTLRException; | ||
import hudson.model.BuildableItem; | ||
import hudson.model.Node; | ||
|
||
|
||
/** | ||
* @author Gregory Boissinot | ||
*/ | ||
public abstract class AbstractTriggerByFullContext<C extends XTriggerContext> extends AbstractTrigger { | ||
|
||
private transient C context; | ||
|
||
/** | ||
* Builds a trigger object | ||
* Calls an implementation trigger | ||
* | ||
* @param cronTabSpec the scheduler value | ||
* @throws ANTLRException | ||
*/ | ||
public AbstractTriggerByFullContext(String cronTabSpec) throws ANTLRException { | ||
super(cronTabSpec); | ||
} | ||
|
||
/** | ||
* Can be overridden if needed | ||
*/ | ||
@Override | ||
protected void start(Node pollingNode, BuildableItem project, boolean newInstance, XTriggerLog log) throws XTriggerException { | ||
context = getContext(pollingNode, log); | ||
} | ||
|
||
@Override | ||
protected boolean checkIfModified(Node pollingNode, XTriggerLog log) throws XTriggerException { | ||
|
||
C newContext = getContext(pollingNode, log); | ||
|
||
if (offlineSlaveOnStartup) { | ||
log.info("Slave(s) were offline at startup or at previous poll."); | ||
log.info("Recording environment context and waiting for next schedule to check if there are modifications."); | ||
offlineSlaveOnStartup = false; | ||
setNewContext(newContext); | ||
return false; | ||
} | ||
|
||
boolean changed = checkIfModified(context, newContext, log); | ||
setNewContext(newContext); | ||
return changed; | ||
} | ||
|
||
|
||
private void setNewContext(C context) { | ||
this.context = context; | ||
} | ||
|
||
protected abstract C getContext(Node pollingNode, XTriggerLog log) throws XTriggerException; | ||
|
||
/** | ||
* Checks if there are modifications in the environment between last poll | ||
* | ||
* @return true if there are modifications | ||
*/ | ||
protected abstract boolean checkIfModified(C oldContext, C newContext, XTriggerLog log) throws XTriggerException; | ||
|
||
} |
55 changes: 53 additions & 2 deletions
55
src/main/java/org/jenkinsci/lib/xtrigger/XTriggerCause.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 |
---|---|---|
@@ -1,27 +1,78 @@ | ||
package org.jenkinsci.lib.xtrigger; | ||
|
||
import hudson.model.AbstractBuild; | ||
import hudson.model.Cause; | ||
import hudson.model.Hudson; | ||
import hudson.remoting.Callable; | ||
import org.apache.commons.io.FileUtils; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* @author Gregory Boissinot | ||
*/ | ||
public class XTriggerCause extends Cause { | ||
|
||
private static Logger LOGGER = Logger.getLogger(XTriggerCause.class.getName()); | ||
|
||
private String triggerName; | ||
|
||
private String causeFrom; | ||
|
||
protected XTriggerCause(String triggerName, String causeFrom) { | ||
private boolean logEnabled; | ||
|
||
protected XTriggerCause(String triggerName, String causeFrom, boolean logEnabled) { | ||
this.triggerName = triggerName; | ||
this.causeFrom = causeFrom; | ||
this.logEnabled = logEnabled; | ||
} | ||
|
||
@Override | ||
public void onAddedTo(final AbstractBuild build) { | ||
final XTriggerCauseAction causeAction = build.getAction(XTriggerCauseAction.class); | ||
if (causeAction != null) { | ||
try { | ||
Hudson.getInstance().getRootPath().act(new Callable<Void, XTriggerException>() { | ||
@Override | ||
public Void call() throws XTriggerException { | ||
causeAction.setBuild(build); | ||
File triggerLogFile = causeAction.getLogFile(); | ||
String logContent = causeAction.getLogMessage(); | ||
try { | ||
FileUtils.writeStringToFile(triggerLogFile, logContent); | ||
} catch (IOException ioe) { | ||
throw new XTriggerException(ioe); | ||
} | ||
return null; | ||
} | ||
}); | ||
} catch (IOException ioe) { | ||
LOGGER.log(Level.SEVERE, "Problem to attach cause object to build object.", ioe); | ||
} catch (InterruptedException ie) { | ||
LOGGER.log(Level.SEVERE, "Problem to attach cause object to build object.", ie); | ||
} catch (XTriggerException xe) { | ||
LOGGER.log(Level.SEVERE, "Problem to attach cause object to build object.", xe); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public String getShortDescription() { | ||
if (causeFrom == null) { | ||
return "[" + triggerName + "]"; | ||
} else { | ||
} else if (!logEnabled) { | ||
return String.format("[%s] %s", triggerName, causeFrom); | ||
} else { | ||
return String.format("[%s] %s (%s)", triggerName, causeFrom, "<a href=\"triggerCauseAction\">log</a>"); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public String getTriggerName() { | ||
return triggerName; | ||
} | ||
|
||
} |
Oops, something went wrong.