diff --git a/Teamcity-Phabricator-Plugin/phabricator-plugin-agent/src/main/java/uk/xlab/teamcity/phabricator/AgentBuildExtension.java b/Teamcity-Phabricator-Plugin/phabricator-plugin-agent/src/main/java/uk/xlab/teamcity/phabricator/AgentBuildExtension.java index 36af74a..8a2a89c 100644 --- a/Teamcity-Phabricator-Plugin/phabricator-plugin-agent/src/main/java/uk/xlab/teamcity/phabricator/AgentBuildExtension.java +++ b/Teamcity-Phabricator-Plugin/phabricator-plugin-agent/src/main/java/uk/xlab/teamcity/phabricator/AgentBuildExtension.java @@ -12,6 +12,7 @@ import jetbrains.buildServer.agent.AgentLifeCycleListener; import jetbrains.buildServer.agent.AgentRunningBuild; import jetbrains.buildServer.agent.BuildProgressLogger; import jetbrains.buildServer.util.EventDispatcher; +import uk.xlab.teamcity.phabricator.logging.PhabricatorAgentLogger; public class AgentBuildExtension extends AgentLifeCycleAdapter { diff --git a/Teamcity-Phabricator-Plugin/phabricator-plugin-agent/src/main/resources/META-INF/build-agent-plugin-phabricator-plugin.xml b/Teamcity-Phabricator-Plugin/phabricator-plugin-agent/src/main/resources/META-INF/build-agent-plugin-phabricator-plugin.xml index 8acecf4..d62c377 100644 --- a/Teamcity-Phabricator-Plugin/phabricator-plugin-agent/src/main/resources/META-INF/build-agent-plugin-phabricator-plugin.xml +++ b/Teamcity-Phabricator-Plugin/phabricator-plugin-agent/src/main/resources/META-INF/build-agent-plugin-phabricator-plugin.xml @@ -6,5 +6,5 @@ default-autowire="constructor"> - + diff --git a/Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/Command.java b/Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/Command.java new file mode 100644 index 0000000..c4945a3 --- /dev/null +++ b/Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/Command.java @@ -0,0 +1,47 @@ +package uk.xlab.teamcity.phabricator; + +import java.io.File; + +import static uk.xlab.teamcity.phabricator.CommonUtils.*; + +public class Command { + + private final File workingDir; + private String[] fullCommand; + private ProcessBuilder processBuilder; + private Process process; + + public Command(final String[] executableAndArguments, final String workingDirectory) { + workingDir = isNullOrEmpty(workingDirectory) ? null : new File(workingDirectory); + fullCommand = executableAndArguments; + + // ProcessBuilder requires the full command to be an array to prepend the + // executable to the front; + processBuilder = new ProcessBuilder(executableAndArguments); + processBuilder.directory(workingDir); + processBuilder.inheritIO(); + } + + public int executeAndWait() throws Exception { + // May well fail and throw an exception but we will handle that in the agent + // plugin. + process = processBuilder.start(); + return process.waitFor(); + } + + @Override + public String toString() { + if (fullCommand.length < 1) { + return "Command not setup"; + } + + String printableCommand = String.join(" ", fullCommand); + + // The workingDir might not have been explicitly set + if (processBuilder.directory() == null) { + return String.format("Command: %s", printableCommand); + } + + return String.format("WorkingDir: %s - Command: %s", processBuilder.directory().toString(), printableCommand); + } +} diff --git a/Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/CommandBuilder.java b/Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/CommandBuilder.java new file mode 100644 index 0000000..17c828d --- /dev/null +++ b/Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/CommandBuilder.java @@ -0,0 +1,82 @@ +package uk.xlab.teamcity.phabricator; + +import static uk.xlab.teamcity.phabricator.CommonUtils.*; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class CommandBuilder { + + private String command = null; + private String action = null; + private String workingDir = null; + private List args = new ArrayList(); + + public CommandBuilder setWorkingDir(String workingDir) { + if (isNullOrEmpty(workingDir)) + throw new IllegalArgumentException("Need to provide valid working directory"); + else + this.workingDir = workingDir; + return this; + } + + public CommandBuilder setCommand(String cmd) { + if (isNullOrEmpty(cmd)) + throw new IllegalArgumentException("Need to provide a valid command"); + else + this.command = cmd; + return this; + } + + public CommandBuilder setAction(String action) { + if (isNullOrEmpty(action)) + throw new IllegalArgumentException("Need to provide a valid action"); + else + this.action = action; + return this; + } + + public CommandBuilder setArg(String arg) { + if (isNullOrEmpty(arg)) + throw new IllegalArgumentException("Need to provide a valid argument"); + else + this.args.add(arg); + return this; + } + + public CommandBuilder setFlagWithValueEquals(String key, String value) { + this.args.add(String.format("%s=%s", formatFlag(key), value)); + return this; + } + + public Command build() { + if (isNullOrEmpty(this.command)) { + throw new IllegalArgumentException("Must provide a command"); + } + + else { + this.args.add(0, this.command); + } + + if (!isNullOrEmpty(this.action)) { + this.args.add(1, this.action); + } + + return new Command(args.toArray(new String[args.size()]), this.workingDir); + } + + private static String formatFlag(String flag) { + Pattern withFlag = Pattern.compile("^\\-\\-[a-zA-Z0-9-]+$"); + Pattern singleWord = Pattern.compile("^\\w$"); + Matcher m = withFlag.matcher(flag.trim()); + Matcher m1 = singleWord.matcher(flag.trim()); + if (m.matches()) { + return flag.trim(); + } else if (m1.matches()) { + return String.format("--%s", flag); + } else { + throw new IllegalArgumentException(String.format("%s is not a valid flag", flag)); + } + } +} diff --git a/Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/Constants.java b/Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/Constants.java index 06b8565..971c53b 100644 --- a/Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/Constants.java +++ b/Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/Constants.java @@ -9,6 +9,7 @@ public class Constants { public static final String BUILD_FEATURE_TYPE = "phabricator-build-feature"; public static final String PHABRICATOR_URL_SETTING = "plugin.phabricatorUrl"; public static final String PHABRICATOR_CONDUIT_TOKEN_SETTING = "plugin.conduitToken"; + public static final String PHABRICATOR_ARCANIST_PATH_SETTING = "plugin.pathToArc"; // Build Config public static final String BUILD_ID = "phabricator.BUILD_ID"; 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 index 9ec6cfb..e80db73 100644 --- 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 @@ -3,6 +3,9 @@ package uk.xlab.teamcity.phabricator; import java.net.MalformedURLException; import java.net.URL; import java.util.Map; + +import uk.xlab.teamcity.phabricator.logging.PhabricatorPluginLogger; + import static uk.xlab.teamcity.phabricator.CommonUtils.*; /** @@ -21,6 +24,7 @@ public class PhabricatorPluginConfig { // Build Feature Variables private URL phabricatorUrl; private String conduitToken; + private String pathToArcanist; // Harbormaster Variables private String buildId; @@ -66,6 +70,10 @@ public class PhabricatorPluginConfig { logger.info("Found Phabricator Conduit Token"); conduitToken = params.get(Constants.PHABRICATOR_CONDUIT_TOKEN_SETTING); break; + case Constants.PHABRICATOR_ARCANIST_PATH_SETTING: + logger.info(String.format("Found Phabricator Arcanist Path: %s", params.get(Constants.PHABRICATOR_ARCANIST_PATH_SETTING))); + pathToArcanist = params.get(Constants.PHABRICATOR_ARCANIST_PATH_SETTING); + break; case Constants.BUILD_ID: logger.info(String.format("Found build id: %s", params.get(Constants.BUILD_ID))); buildId = params.get(Constants.BUILD_ID); @@ -92,7 +100,7 @@ public class PhabricatorPluginConfig { } public boolean isPluginSetup() { - if (!isNull(phabricatorUrl) && !isNullOrEmpty(buildId) && !isNullOrEmpty(diffId) + if (!isNull(phabricatorUrl) && !isNull(pathToArcanist) && !isNullOrEmpty(buildId) && !isNullOrEmpty(diffId) && !isNullOrEmpty(harbormasterPHID) && !isNullOrEmpty(revisionId)) { return true; } 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/logging/PhabricatorAgentLogger.java similarity index 90% rename from Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/PhabricatorAgentLogger.java rename to Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/logging/PhabricatorAgentLogger.java index 8e54cb1..d735376 100644 --- 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/logging/PhabricatorAgentLogger.java @@ -1,4 +1,4 @@ -package uk.xlab.teamcity.phabricator; +package uk.xlab.teamcity.phabricator.logging; import jetbrains.buildServer.log.Loggers; 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/logging/PhabricatorPluginLogger.java similarity index 77% rename from Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/PhabricatorPluginLogger.java rename to Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/logging/PhabricatorPluginLogger.java index c7f33eb..52724f3 100644 --- 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/logging/PhabricatorPluginLogger.java @@ -1,4 +1,4 @@ -package uk.xlab.teamcity.phabricator; +package uk.xlab.teamcity.phabricator.logging; public interface PhabricatorPluginLogger { 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/logging/PhabricatorServerLogger.java similarity index 90% rename from Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/PhabricatorServerLogger.java rename to Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/logging/PhabricatorServerLogger.java index c2421e2..a229542 100644 --- 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/logging/PhabricatorServerLogger.java @@ -1,4 +1,4 @@ -package uk.xlab.teamcity.phabricator; +package uk.xlab.teamcity.phabricator.logging; import jetbrains.buildServer.log.Loggers; 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 index c6f1507..b69fe12 100644 --- 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 @@ -10,6 +10,7 @@ import java.util.Map; import jetbrains.buildServer.messages.Status; import jetbrains.buildServer.serverSide.SBuildFeatureDescriptor; import jetbrains.buildServer.serverSide.SRunningBuild; +import uk.xlab.teamcity.phabricator.logging.PhabricatorServerLogger; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; 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 index 2116172..c8896cd 100644 --- 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 @@ -6,6 +6,7 @@ import jetbrains.buildServer.serverSide.BuildServerAdapter; import jetbrains.buildServer.serverSide.BuildServerListener; import jetbrains.buildServer.serverSide.SRunningBuild; import jetbrains.buildServer.util.EventDispatcher; +import uk.xlab.teamcity.phabricator.logging.PhabricatorServerLogger; /** * Listen for builds been started and track their progress with BuildTracker 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 402bb6f..2884302 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 @@ -6,6 +6,7 @@ import org.jetbrains.annotations.NotNull; import jetbrains.buildServer.serverSide.BuildFeature; import jetbrains.buildServer.web.openapi.PluginDescriptor; +import uk.xlab.teamcity.phabricator.logging.PhabricatorServerLogger; public class PhabricatorPluginBuildFeature extends BuildFeature { 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 037ccd7..179b4e7 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,7 +5,7 @@ default-autowire="constructor"> - + diff --git a/Teamcity-Phabricator-Plugin/phabricator-plugin-server/src/main/resources/buildServerResources/phabricatorBuildFeature.jsp b/Teamcity-Phabricator-Plugin/phabricator-plugin-server/src/main/resources/buildServerResources/phabricatorBuildFeature.jsp index e98c329..a95e7ed 100644 --- a/Teamcity-Phabricator-Plugin/phabricator-plugin-server/src/main/resources/buildServerResources/phabricatorBuildFeature.jsp +++ b/Teamcity-Phabricator-Plugin/phabricator-plugin-server/src/main/resources/buildServerResources/phabricatorBuildFeature.jsp @@ -5,7 +5,9 @@ + Report build status in real-time to your Phabricator instance. Phabricator URL: Conduit Token: +Path To Arcanist: