
fluximpl.GitTriggerImpl Maven / Gradle / Ivy
The newest version!
package fluximpl;
import flux.*;
import flux.dev.NotTriggeredException;
import fluximpl.audittrail.AuditTrailConstants;
import fluximpl.xml.five_zero_converter.ElementNames;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.*;
/**
* Implementation for GitTrigger. Uses "polling" based commit consumption from Git.
*
* @author [email protected]
*/
public class GitTriggerImpl extends TriggerImpl implements GitTrigger {
public static final String TRIGGER_VARIABLE = "GIT_TRIGGER";
public static final String GIT_CHANGESET = "GIT_CHANGESET";
/**
* Git CLI Command that returns the latest commit.
*/
public static final String EXEC_LS_REMOTE = "git ls-remote ";
public GitTriggerImpl() {
super(new FlowChartImpl(), "Git Trigger");
}
public GitTriggerImpl(FlowChartImpl flowChart, String s) {
super(flowChart, s);
}
public Set getHiddenVariableNames() {
// The returned raw type is a Set of String.
@SuppressWarnings("unchecked")
Set set = super.getHiddenVariableNames();
set.add(TRIGGER_VARIABLE);
return set;
}
public String getRepositoryUrl() {
return getVariable().getRepositoryUrl();
}
@Override
public void setRepositoryUrl(String url) {
GitTriggerVariable var = getVariable();
var.setRepositoryUrl(url);
setVariable(var);
}
public String getPath() {
return getVariable().getPath();
}
@Override
public void setPath(String path) {
GitTriggerVariable var = getVariable();
var.setPath(path);
setVariable(var);
}
public String getPollingDelay() {
return getVariable().getPollingDelay();
}
@Override
public void setPollingDelay(String timeExpression) {
GitTriggerVariable var = getVariable();
var.setPollingDelay(timeExpression);
setVariable(var);
}
@Override
public Date getNextPollingDate() {
try {
Factory fluxFactory = Factory.makeInstance();
EngineHelper engineHelper = fluxFactory.makeEngineHelper();
return engineHelper.applyTimeExpression(getPollingDelay(), null, null);
} // try
catch (EngineException e) {
// Should not occur.
} // catch
return null;
}
@Override
public Object execute(FlowContext flowContext) throws Exception {
GitTriggerResult result = new GitTriggerResult();
String command;
if (!StringUtil.isNullOrEmpty(getPath())) {
command = getPath() + EXEC_LS_REMOTE + getRepositoryUrl();
} else {
command = EXEC_LS_REMOTE + getRepositoryUrl();
}
String lastSeenChangeSet = (String) flowContext.getFlowChart().getVariableManager().get(GIT_CHANGESET);
if (StringUtil.isNullOrEmpty(lastSeenChangeSet)) {
Process process = Runtime.getRuntime().exec(command);
byte[] buf = new byte[4096];
InputStream in = process.getInputStream();
int exitCode = -1;
try {
exitCode = process.waitFor();
} catch (InterruptedException e) {
// ignore
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
for (int bytesRead = in.read(buf); bytesRead != -1; bytesRead = in.read(buf)) {
outputStream.write(buf, 0, bytesRead);
}
lastSeenChangeSet = outputStream.toString();
//split by newline character
String[] lastSeenChangeSets = lastSeenChangeSet.split("\\r?\\n");
lastSeenChangeSet = getHead(lastSeenChangeSets);
if (exitCode == 0 && !StringUtil.isNullOrEmpty(lastSeenChangeSet))
flowContext.getFlowChart().getVariableManager().put(GIT_CHANGESET, lastSeenChangeSet);
try {
outputStream.close();
} catch (Exception e) {
// ignore
}
throw new NotTriggeredException();
} else {
Process process = Runtime.getRuntime().exec(command);
byte[] buf = new byte[4096];
InputStream in = process.getInputStream();
int exitCode = -1;
try {
exitCode = process.waitFor();
} catch (InterruptedException e) {
// ignore
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
for (int bytesRead = in.read(buf); bytesRead != -1; bytesRead = in.read(buf)) {
outputStream.write(buf, 0, bytesRead);
}
String latestChangeSet = outputStream.toString();
//split by newline character
String[] latestChangeSets = latestChangeSet.split("\\r?\\n");
latestChangeSet = getHead(latestChangeSets);
try {
outputStream.close();
} catch (Exception e) {
// ignore
}
if (exitCode == 0 && !StringUtil.isNullOrEmpty(latestChangeSet)) {
flowContext.getFlowChart().getVariableManager().put(GIT_CHANGESET, latestChangeSet);
}
if (lastSeenChangeSet.equalsIgnoreCase(latestChangeSet)) {
throw new NotTriggeredException();
} else {
result.commitId = latestChangeSet;
flowContext.sendToAuditTrail("flux.audittrail.server.ExecutingActionEvent", getDetails(result, flowContext.getActionName()));
return result;
}
}
}
private String getHead(String[] changeSets) {
Map changeSetMap = new HashMap();
for (String changeSet : changeSets) {
String[] csEntry = changeSet.split("\\t");
changeSetMap.put(csEntry[1], csEntry[0]);
}
return changeSetMap.get("HEAD");
}
public Object expedite(FlowContext flowContext) throws Exception, NotTriggeredException {
flowContext.getLogger().info("Expedite called for " + flowContext.getActionName());
return null;
}
/**
* This delegation is required for pre 7.11 trigger expedite implementation.
*/
public Object a(FlowContext flowContext) throws Exception, NotTriggeredException {
return expedite(flowContext);
}
@Override
public void verify() throws EngineException {
if (StringUtil.isNullOrEmpty(getRepositoryUrl())) {
throw new EngineException("Expected \"Repository URL\" to be non-null or non-empty, but it was null or empty.");
}
}
@Override
public Class getResultInfo() {
return GitTriggerResult.class;
}
public String getDetails(Object result, String actionName) {
GitTriggerResult mtr = (GitTriggerResult) result;
StringBuffer sb = new StringBuffer();
String name = AuditTrailConstants.RESULT;
String value = mtr.commitId;
if (!StringUtil.isNullOrEmpty(value)) {
sb.append("<" + ElementNames.PROPERTY + ">");
sb.append("<" + ElementNames.NAME + ">");
sb.append("");
sb.append("" + ElementNames.NAME + ">");
sb.append("<" + ElementNames.VALUE + ">");
sb.append("");
sb.append("" + ElementNames.VALUE + ">");
sb.append("" + ElementNames.PROPERTY + ">");
}
return ElementNames.open(ElementNames.EVENT_MESSAGE_ENTRY)
+ ElementNames.open(ElementNames.EVENT_DESCRIPTION) + "Exiting " + actionName + " \"" + getName() + "\""
+ ElementNames.close(ElementNames.EVENT_DESCRIPTION)
+ ElementNames.open(ElementNames.MESSAGE_DETAIL)
+ ElementNames.open(ElementNames.DETAIL) + sb.toString()
+ ElementNames.close(ElementNames.DETAIL)
+ ElementNames.close(ElementNames.MESSAGE_DETAIL)
+ ElementNames.close(ElementNames.EVENT_MESSAGE_ENTRY);
}
private GitTriggerVariable getVariable() {
if (!getVariableManager().contains(TRIGGER_VARIABLE)) {
getVariableManager().put(TRIGGER_VARIABLE, new GitTriggerVariable());
}
return (GitTriggerVariable) getVariableManager().get(TRIGGER_VARIABLE);
}
private void setVariable(GitTriggerVariable variable) {
getVariableManager().put(TRIGGER_VARIABLE, variable);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy