1
0
Fork 0

Tidy up and add error option to logging

Formatted the files so far getting rid of theose pesky tabs. Also added the error logging options to the logging wrapper classes.
This commit is contained in:
Steven Cooney 2019-05-31 12:22:55 +01:00
parent 82ed9cc10b
commit 5f25b065aa
8 changed files with 144 additions and 146 deletions

View file

@ -1,8 +1,8 @@
package uk.xlab.teamcity.phabricator; package uk.xlab.teamcity.phabricator;
public class Constants { public class Constants {
public static final String BUILD_FEATURE_TYPE = "phabricator-build-feature"; public static final String BUILD_FEATURE_TYPE = "phabricator-build-feature";
public static final String PLUGIN_NAME = "phabricator"; public static final String PLUGIN_NAME = "phabricator";
public static final String PLUGIN_DISPLAY_NAME = "Phabricator Plugin"; public static final String PLUGIN_DISPLAY_NAME = "Phabricator Plugin";
public static final String PHABRICATOR_URL_SETTING = "phabricator_url_setting"; public static final String PHABRICATOR_URL_SETTING = "phabricator_url_setting";
} }

View file

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

View file

@ -4,13 +4,18 @@ import jetbrains.buildServer.log.Loggers;
public final class PhabricatorAgentLogger implements IPhabricatorPluginLogger { public final class PhabricatorAgentLogger implements IPhabricatorPluginLogger {
@Override @Override
public void info(String message) { public void info(String message) {
Loggers.AGENT.info(String.format("Phabricator Plugin: %s", message)); Loggers.AGENT.info(String.format("Phabricator Plugin: %s", message));
} }
@Override @Override
public void warn(String message, Exception e) { public void warn(String message, Exception e) {
Loggers.AGENT.warn(String.format("Phabricator Plugin: %s", message), e); Loggers.AGENT.warn(String.format("Phabricator Plugin: %s", message), e);
} }
@Override
public void error(String message, Exception e) {
Loggers.AGENT.error(String.format("Phabricator Plugin: %s", message), e);
}
} }

View file

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

View file

@ -4,22 +4,18 @@ import jetbrains.buildServer.log.Loggers;
public final class PhabricatorServerLogger implements IPhabricatorPluginLogger { public final class PhabricatorServerLogger implements IPhabricatorPluginLogger {
@Override @Override
public void info(String message) { public void info(String message) {
Loggers.SERVER.info(String.format("Phabricator Plugin: %s", message)); Loggers.SERVER.info(String.format("Phabricator Plugin: %s", message));
} }
@Override @Override
public void warn(String message, Exception e) { public void warn(String message, Exception e) {
Loggers.SERVER.warn(String.format("Phabricator Plugin: %s", message), e); Loggers.SERVER.warn(String.format("Phabricator Plugin: %s", message), e);
} }
// public void agentInfo(String message) { @Override
// Loggers.AGENT.info(String.format("Phabricator Plugin: %s", message)); public void error(String message, Exception e) {
// } Loggers.SERVER.error(String.format("Phabricator Plugin: %s", message), e);
// }
// public void agentWarn(String message, Exception e) {
// Loggers.AGENT.warn(String.format("Phabricator Plugin: %s", message), e);
// }
} }

View file

@ -17,48 +17,48 @@ import jetbrains.buildServer.serverSide.SRunningBuild;
*/ */
public class BuildTracker implements Runnable { public class BuildTracker implements Runnable {
private SRunningBuild build; private SRunningBuild build;
private PhabricatorServerLogger logger; private PhabricatorServerLogger logger;
private PhabricatorPluginConfig phabricatorConfig; private PhabricatorPluginConfig phabricatorConfig;
public BuildTracker(SRunningBuild runningBuild, PhabricatorServerLogger phabLogger) { public BuildTracker(SRunningBuild runningBuild, PhabricatorServerLogger phabLogger) {
build = runningBuild; build = runningBuild;
logger = phabLogger; logger = phabLogger;
phabricatorConfig = new PhabricatorPluginConfig(); phabricatorConfig = new PhabricatorPluginConfig();
phabricatorConfig.setLogger(logger); phabricatorConfig.setLogger(logger);
} }
@Override @Override
public void run() { public void run() {
// Attempt to get the parameters set by the phabricator build feature. If non // Attempt to get the parameters set by the phabricator build feature. If non
// are set then the feature is not turned on. // are set then the feature is not turned on.
Collection<SBuildFeatureDescriptor> phabricatorBuildFeatureParameters = build Collection<SBuildFeatureDescriptor> phabricatorBuildFeatureParameters = build
.getBuildFeaturesOfType(Constants.BUILD_FEATURE_TYPE); .getBuildFeaturesOfType(Constants.BUILD_FEATURE_TYPE);
// Check if the build is part of a configuration which // Check if the build is part of a configuration which
// uses the phabricator build feature. // uses the phabricator build feature.
if (!phabricatorBuildFeatureParameters.isEmpty()) { if (!phabricatorBuildFeatureParameters.isEmpty()) {
logger.info("Tracking build " + build.getBuildNumber()); logger.info("Tracking build " + build.getBuildNumber());
// Gather together all the build and phabricator parameters // Gather together all the build and phabricator parameters
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.putAll(build.getBuildOwnParameters()); params.putAll(build.getBuildOwnParameters());
params.putAll(phabricatorBuildFeatureParameters.iterator().next().getParameters()); params.putAll(phabricatorBuildFeatureParameters.iterator().next().getParameters());
// Setup plugin specific configuration // Setup plugin specific configuration
// TODO: implement AppConfig as PluginConfig // TODO: implement AppConfig as PluginConfig
phabricatorConfig.setParameters(params); phabricatorConfig.setParameters(params);
while (!build.isFinished()) { while (!build.isFinished()) {
// Wait until the build finishes // Wait until the build finishes
} }
logger.info(String.format("Build %s finished: %s", build.getBuildNumber(), build.getBuildStatus())); logger.info(String.format("Build %s finished: %s", build.getBuildNumber(), build.getBuildStatus()));
if (build.getStatusDescriptor().isSuccessful()) { if (build.getStatusDescriptor().isSuccessful()) {
logger.info("Successful Build"); logger.info("Successful Build");
} }
} }
} }
} }

View file

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

View file

@ -9,49 +9,47 @@ import jetbrains.buildServer.web.openapi.PluginDescriptor;
public class PhabricatorPluginBuildFeature extends BuildFeature { public class PhabricatorPluginBuildFeature extends BuildFeature {
private final String myEditUrl; private final String myEditUrl;
private PhabricatorServerLogger logger; private PhabricatorServerLogger logger;
public PhabricatorPluginBuildFeature( public PhabricatorPluginBuildFeature(@NotNull final PluginDescriptor pluginDescriptor,
@NotNull final PluginDescriptor pluginDescriptor, @NotNull final PhabricatorServerLogger phabLogger) {
@NotNull final PhabricatorServerLogger phabLogger myEditUrl = pluginDescriptor.getPluginResourcesPath("phabricatorBuildFeature.jsp");
) { logger = phabLogger;
myEditUrl = pluginDescriptor.getPluginResourcesPath("phabricatorBuildFeature.jsp");
logger = phabLogger;
logger.info("Build feature registered"); logger.info("Build feature registered");
} }
@Override @Override
public String getType() { public String getType() {
return Constants.BUILD_FEATURE_TYPE; return Constants.BUILD_FEATURE_TYPE;
} }
@Override @Override
public String getDisplayName() { public String getDisplayName() {
return Constants.PLUGIN_DISPLAY_NAME; return Constants.PLUGIN_DISPLAY_NAME;
} }
@Override @Override
public String getEditParametersUrl() { public String getEditParametersUrl() {
return myEditUrl; return myEditUrl;
} }
@Override @Override
public boolean isMultipleFeaturesPerBuildTypeAllowed() { public boolean isMultipleFeaturesPerBuildTypeAllowed() {
return false; return false;
} }
/** /**
* Populates the Parameters Description on the build features page * Populates the Parameters Description on the build features page
*/ */
@NotNull @NotNull
@Override @Override
public String describeParameters(@NotNull final Map<String, String> params) { public String describeParameters(@NotNull final Map<String, String> params) {
String url = ""; String url = "";
for(String key : params.keySet()){ for (String key : params.keySet()) {
if(key.equals(Constants.PHABRICATOR_URL_SETTING)){ if (key.equals(Constants.PHABRICATOR_URL_SETTING)) {
url = params.get(key); url = params.get(key);
} }
} }