diff --git a/Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/IPhabricatorPluginLogger.java b/Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/IPhabricatorPluginLogger.java new file mode 100644 index 0000000..82ae374 --- /dev/null +++ b/Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/IPhabricatorPluginLogger.java @@ -0,0 +1,6 @@ +package uk.xlab.teamcity.phabricator; + +public interface IPhabricatorPluginLogger { + public void info(String message); + public void warn(String message, Exception e); +} diff --git a/Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/PhabricatorAgentLogger.java b/Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/PhabricatorAgentLogger.java new file mode 100644 index 0000000..fd624da --- /dev/null +++ b/Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/PhabricatorAgentLogger.java @@ -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); + } +} diff --git a/Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/PhabricatorPluginConfig.java b/Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/PhabricatorPluginConfig.java new file mode 100644 index 0000000..0462de1 --- /dev/null +++ b/Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/PhabricatorPluginConfig.java @@ -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 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 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)); + } + } + } +} diff --git a/Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/PhabricatorPluginLogger.java b/Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/PhabricatorPluginLogger.java deleted file mode 100644 index 7df96e2..0000000 --- a/Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/PhabricatorPluginLogger.java +++ /dev/null @@ -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); - } -} diff --git a/Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/PhabricatorServerLogger.java b/Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/PhabricatorServerLogger.java new file mode 100644 index 0000000..5a7e772 --- /dev/null +++ b/Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/PhabricatorServerLogger.java @@ -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); +// } + +} diff --git a/Teamcity-Phabricator-Plugin/phabricator-plugin-server/src/main/java/uk/xlab/teamcity/phabricator/BuildTracker.java b/Teamcity-Phabricator-Plugin/phabricator-plugin-server/src/main/java/uk/xlab/teamcity/phabricator/BuildTracker.java new file mode 100644 index 0000000..ecfbe1d --- /dev/null +++ b/Teamcity-Phabricator-Plugin/phabricator-plugin-server/src/main/java/uk/xlab/teamcity/phabricator/BuildTracker.java @@ -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 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 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"); + } + } + } +} diff --git a/Teamcity-Phabricator-Plugin/phabricator-plugin-server/src/main/java/uk/xlab/teamcity/phabricator/PhabricatorBuildServerAdapter.java b/Teamcity-Phabricator-Plugin/phabricator-plugin-server/src/main/java/uk/xlab/teamcity/phabricator/PhabricatorBuildServerAdapter.java new file mode 100644 index 0000000..8238f36 --- /dev/null +++ b/Teamcity-Phabricator-Plugin/phabricator-plugin-server/src/main/java/uk/xlab/teamcity/phabricator/PhabricatorBuildServerAdapter.java @@ -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, + @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(); + } +} diff --git a/Teamcity-Phabricator-Plugin/phabricator-plugin-server/src/main/java/uk/xlab/teamcity/phabricator/PhabricatorPluginBuildFeature.java b/Teamcity-Phabricator-Plugin/phabricator-plugin-server/src/main/java/uk/xlab/teamcity/phabricator/PhabricatorPluginBuildFeature.java index 58eceb7..484af0c 100644 --- a/Teamcity-Phabricator-Plugin/phabricator-plugin-server/src/main/java/uk/xlab/teamcity/phabricator/PhabricatorPluginBuildFeature.java +++ b/Teamcity-Phabricator-Plugin/phabricator-plugin-server/src/main/java/uk/xlab/teamcity/phabricator/PhabricatorPluginBuildFeature.java @@ -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 diff --git a/Teamcity-Phabricator-Plugin/phabricator-plugin-server/src/main/resources/META-INF/build-server-plugin-phabricator-plugin.xml b/Teamcity-Phabricator-Plugin/phabricator-plugin-server/src/main/resources/META-INF/build-server-plugin-phabricator-plugin.xml index df2dc83..a6345e4 100644 --- a/Teamcity-Phabricator-Plugin/phabricator-plugin-server/src/main/resources/META-INF/build-server-plugin-phabricator-plugin.xml +++ b/Teamcity-Phabricator-Plugin/phabricator-plugin-server/src/main/resources/META-INF/build-server-plugin-phabricator-plugin.xml @@ -5,5 +5,6 @@ default-autowire="constructor"> - + +