1
0
Fork 0

Add Build Tracker which Listens for Build Result

Add a build tracker that is spun up when a bulid is created. The tracker will collect environment variables and wait for the build to finish before reporting the results.
This commit is contained in:
Steven Cooney 2019-05-31 12:18:06 +01:00
parent b083db56ac
commit 82ed9cc10b
9 changed files with 202 additions and 27 deletions

View file

@ -0,0 +1,6 @@
package uk.xlab.teamcity.phabricator;
public interface IPhabricatorPluginLogger {
public void info(String message);
public void warn(String message, Exception e);
}

View file

@ -0,0 +1,16 @@
package uk.xlab.teamcity.phabricator;
import jetbrains.buildServer.log.Loggers;
public final class PhabricatorAgentLogger implements IPhabricatorPluginLogger {
@Override
public void info(String message) {
Loggers.AGENT.info(String.format("Phabricator Plugin: %s", message));
}
@Override
public void warn(String message, Exception e) {
Loggers.AGENT.warn(String.format("Phabricator Plugin: %s", message), e);
}
}

View file

@ -0,0 +1,45 @@
package uk.xlab.teamcity.phabricator;
import java.util.Map;
/**
* Holds all the parameters set on the build applied by the
* harbormaster trigger from phabricator. It also has the
* methods used to communicate the build result back to
* harbormaster
*
* @author steven.cooney
*
*/
public class PhabricatorPluginConfig {
private IPhabricatorPluginLogger logger;
private Map<String, String> params;
/**
* Set the appropriate logger depending if the class is
* called from the SERVER or AGENT
*
* @param logger
*/
public void setLogger(PhabricatorServerLogger logger) {
this.logger = logger;
}
/**
* Take a copy of all build parameters which will then
* be parsed
*
* @param parameters
*/
public void setParameters(Map<String, String> parameters) {
params = parameters;
logger.info(String.format("Looking for parameters"));
for (String param : params.keySet()) {
if (param != null) {
logger.info(String.format("Found %s", param));
}
}
}
}

View file

@ -1,22 +0,0 @@
package uk.xlab.teamcity.phabricator;
import jetbrains.buildServer.log.Loggers;
public final class PhabricatorPluginLogger {
public void serverInfo(String message) {
Loggers.SERVER.info(String.format("Phabricator Plugin: %s", message));
}
public void serverWarn(String message, Exception e) {
Loggers.SERVER.warn(message, e);
}
public void agentInfo(String message) {
Loggers.AGENT.info(String.format("Phabricator Plugin: %s", message));
}
public void agentWarn(String message, Exception e) {
Loggers.AGENT.warn(String.format("Phabricator Plugin: %s", message), e);
}
}

View file

@ -0,0 +1,25 @@
package uk.xlab.teamcity.phabricator;
import jetbrains.buildServer.log.Loggers;
public final class PhabricatorServerLogger implements IPhabricatorPluginLogger {
@Override
public void info(String message) {
Loggers.SERVER.info(String.format("Phabricator Plugin: %s", message));
}
@Override
public void warn(String message, Exception e) {
Loggers.SERVER.warn(String.format("Phabricator Plugin: %s", message), e);
}
// public void agentInfo(String message) {
// Loggers.AGENT.info(String.format("Phabricator Plugin: %s", message));
// }
//
// public void agentWarn(String message, Exception e) {
// Loggers.AGENT.warn(String.format("Phabricator Plugin: %s", message), e);
// }
}

View file

@ -0,0 +1,64 @@
package uk.xlab.teamcity.phabricator;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import jetbrains.buildServer.serverSide.SBuildFeatureDescriptor;
import jetbrains.buildServer.serverSide.SRunningBuild;
/**
* Class that is run in a thread once a build has started. If the build does not
* have the phabricator build feature then the tread should come to an end
* otherwise wait for the build to finish and report back to phabricator
*
* @author steven.cooney
*
*/
public class BuildTracker implements Runnable {
private SRunningBuild build;
private PhabricatorServerLogger logger;
private PhabricatorPluginConfig phabricatorConfig;
public BuildTracker(SRunningBuild runningBuild, PhabricatorServerLogger phabLogger) {
build = runningBuild;
logger = phabLogger;
phabricatorConfig = new PhabricatorPluginConfig();
phabricatorConfig.setLogger(logger);
}
@Override
public void run() {
// Attempt to get the parameters set by the phabricator build feature. If non
// are set then the feature is not turned on.
Collection<SBuildFeatureDescriptor> phabricatorBuildFeatureParameters = build
.getBuildFeaturesOfType(Constants.BUILD_FEATURE_TYPE);
// Check if the build is part of a configuration which
// uses the phabricator build feature.
if (!phabricatorBuildFeatureParameters.isEmpty()) {
logger.info("Tracking build " + build.getBuildNumber());
// Gather together all the build and phabricator parameters
Map<String, String> params = new HashMap<>();
params.putAll(build.getBuildOwnParameters());
params.putAll(phabricatorBuildFeatureParameters.iterator().next().getParameters());
// Setup plugin specific configuration
// TODO: implement AppConfig as PluginConfig
phabricatorConfig.setParameters(params);
while (!build.isFinished()) {
// Wait until the build finishes
}
logger.info(String.format("Build %s finished: %s", build.getBuildNumber(), build.getBuildStatus()));
if (build.getStatusDescriptor().isSuccessful()) {
logger.info("Successful Build");
}
}
}
}

View file

@ -0,0 +1,40 @@
package uk.xlab.teamcity.phabricator;
import org.jetbrains.annotations.NotNull;
import jetbrains.buildServer.serverSide.BuildServerAdapter;
import jetbrains.buildServer.serverSide.BuildServerListener;
import jetbrains.buildServer.serverSide.SRunningBuild;
import jetbrains.buildServer.util.EventDispatcher;
/**
* Listen for builds been started and track their progress
* with BuildTracker
*
* @author steven.cooney
*
*/
public class PhabricatorBuildServerAdapter extends BuildServerAdapter {
private PhabricatorServerLogger logger;
public PhabricatorBuildServerAdapter(
@NotNull final EventDispatcher<BuildServerListener> buildServerListener,
@NotNull final PhabricatorServerLogger phabLogger
) {
buildServerListener.addListener(this);
logger = phabLogger;
logger.info("Build server adapter registered");
}
@Override
public void buildStarted(@NotNull SRunningBuild runningBuild) {
super.buildStarted(runningBuild);
// Do the work in a separate thread to avoid blocking
// other builds monitored by this adapter (this might be
// a tad overkill)
new Thread(new BuildTracker(runningBuild, logger)).start();
}
}

View file

@ -10,16 +10,16 @@ import jetbrains.buildServer.web.openapi.PluginDescriptor;
public class PhabricatorPluginBuildFeature extends BuildFeature {
private final String myEditUrl;
private PhabricatorPluginLogger logger;
private PhabricatorServerLogger logger;
public PhabricatorPluginBuildFeature(
@NotNull final PluginDescriptor pluginDescriptor,
@NotNull final PhabricatorPluginLogger logger
@NotNull final PhabricatorServerLogger phabLogger
) {
myEditUrl = pluginDescriptor.getPluginResourcesPath("phabricatorBuildFeature.jsp");
this.logger = logger;
logger = phabLogger;
this.logger.serverInfo("Build feature registered");
logger.info("Build feature registered");
}
@Override

View file

@ -5,5 +5,6 @@
default-autowire="constructor">
<bean class="uk.xlab.teamcity.phabricator.PhabricatorPluginBuildFeature" />
<bean class="uk.xlab.teamcity.phabricator.PhabricatorPluginLogger" />
<bean class="uk.xlab.teamcity.phabricator.PhabricatorServerLogger" />
<bean class="uk.xlab.teamcity.phabricator.PhabricatorBuildServerAdapter" />
</beans>