From b2d8bff4f9c7ab335ceabf93079b012566cd06b6 Mon Sep 17 00:00:00 2001 From: Steven Cooney Date: Fri, 31 May 2019 14:44:51 +0100 Subject: [PATCH] Parse Phabricator Config Variables and Check all is Correct We are now parsing the phabractor variables and checking the ones we need are present. If there is no phabricator url then the process end likewise if there is a missing configuration variable the process ends too. --- .../phabricator-plugin-agent/pom.xml | 4 +- .../phabricator-plugin-common/pom.xml | 4 +- .../teamcity/phabricator/CommonUtils.java | 12 ++++ .../xlab/teamcity/phabricator/Constants.java | 12 +++- .../phabricator/PhabricatorPluginConfig.java | 68 ++++++++++++++++++- .../teamcity/phabricator/BuildTracker.java | 8 +++ 6 files changed, 102 insertions(+), 6 deletions(-) create mode 100644 Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/CommonUtils.java diff --git a/Teamcity-Phabricator-Plugin/phabricator-plugin-agent/pom.xml b/Teamcity-Phabricator-Plugin/phabricator-plugin-agent/pom.xml index 9707c2e..a87aecb 100644 --- a/Teamcity-Phabricator-Plugin/phabricator-plugin-agent/pom.xml +++ b/Teamcity-Phabricator-Plugin/phabricator-plugin-agent/pom.xml @@ -39,8 +39,8 @@ org.apache.maven.plugins maven-compiler-plugin - 1.6 - 1.6 + 1.8 + 1.8 diff --git a/Teamcity-Phabricator-Plugin/phabricator-plugin-common/pom.xml b/Teamcity-Phabricator-Plugin/phabricator-plugin-common/pom.xml index e384158..5b1ac97 100644 --- a/Teamcity-Phabricator-Plugin/phabricator-plugin-common/pom.xml +++ b/Teamcity-Phabricator-Plugin/phabricator-plugin-common/pom.xml @@ -24,8 +24,8 @@ org.apache.maven.plugins maven-compiler-plugin - 1.6 - 1.6 + 1.8 + 1.8 diff --git a/Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/CommonUtils.java b/Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/CommonUtils.java new file mode 100644 index 0000000..3e331a6 --- /dev/null +++ b/Teamcity-Phabricator-Plugin/phabricator-plugin-common/src/main/java/uk/xlab/teamcity/phabricator/CommonUtils.java @@ -0,0 +1,12 @@ +package uk.xlab.teamcity.phabricator; + +public final class CommonUtils { + + public static Boolean isNullOrEmpty(String str) { + return str == null || str.equals(""); + } + + public static Boolean isNull(Object obj) { + return obj == null; + } +} 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 98b41fa..aab23e8 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 @@ -1,8 +1,18 @@ package uk.xlab.teamcity.phabricator; public class Constants { - public static final String BUILD_FEATURE_TYPE = "phabricator-build-feature"; + public static final String PLUGIN_NAME = "phabricator"; public static final String PLUGIN_DISPLAY_NAME = "Phabricator Plugin"; + + // Build Feature + public static final String BUILD_FEATURE_TYPE = "phabricator-build-feature"; public static final String PHABRICATOR_URL_SETTING = "phabricator_url_setting"; + + // Build Config + public static final String BRANCH_NAME = "env.PHAB_BRANCH_NAME"; + public static final String BUILD_ID = "env.PHAB_BUILD_ID"; + public static final String DIFF_ID = "env.PHAB_DIFF_ID"; + public static final String HARBORMASTER_PHID = "env.PHAB_HARBORMASTER_TARGET_PHID"; + public static final String REVISION_ID = "env.PHAB_REVISION_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 76fbdf8..be8750b 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 @@ -1,6 +1,9 @@ package uk.xlab.teamcity.phabricator; +import java.net.MalformedURLException; +import java.net.URL; import java.util.Map; +import static uk.xlab.teamcity.phabricator.CommonUtils.*; /** * Holds all the parameters set on the build applied by the harbormaster trigger @@ -15,6 +18,16 @@ public class PhabricatorPluginConfig { private IPhabricatorPluginLogger logger; private Map params; + // Build Feature Variables + private URL phabricatorUrl; + + // Harbormaster Variables + private String branchName; + private String buildId; + private String diffId; + private String harbormasterPHID; + private String revisionId; + /** * Set the appropriate logger depending if the class is called from the SERVER * or AGENT @@ -35,9 +48,62 @@ public class PhabricatorPluginConfig { logger.info(String.format("Looking for parameters")); for (String param : params.keySet()) { - if (param != null) { + if (!isNullOrEmpty(param)) { logger.info(String.format("Found %s", param)); + + switch (param) { + case Constants.PHABRICATOR_URL_SETTING: + logger.info( + String.format("Found Phabrictor URL: %s", params.get(Constants.PHABRICATOR_URL_SETTING))); + try { + phabricatorUrl = parsePhabricatorURL(params.get(Constants.PHABRICATOR_URL_SETTING)); + } catch (MalformedURLException e) { + logger.warn(String.format("Failed to parse phabricator URL: %s", + params.get(Constants.PHABRICATOR_URL_SETTING)), e); + } + case Constants.BRANCH_NAME: + logger.info(String.format("Found branch name: %s", params.get(Constants.BRANCH_NAME))); + branchName = params.get(Constants.BRANCH_NAME); + case Constants.BUILD_ID: + logger.info(String.format("Found build id: %s", params.get(Constants.BUILD_ID))); + buildId = params.get(Constants.BUILD_ID); + case Constants.DIFF_ID: + logger.info(String.format("Found diff ID: %s", params.get(Constants.DIFF_ID))); + diffId = params.get(Constants.DIFF_ID); + case Constants.HARBORMASTER_PHID: + logger.info(String.format("Found harbormaster target PHID: %s", + params.get(Constants.HARBORMASTER_PHID))); + harbormasterPHID = params.get(Constants.HARBORMASTER_PHID); + case Constants.REVISION_ID: + logger.info(String.format("Found revision ID: %s", params.get(Constants.REVISION_ID))); + revisionId = params.get(Constants.REVISION_ID); + } } } } + + public boolean isPluginSetup() { + if (!isNull(phabricatorUrl) && !isNullOrEmpty(branchName) && !isNullOrEmpty(buildId) && !isNullOrEmpty(diffId) + && !isNullOrEmpty(harbormasterPHID) && !isNullOrEmpty(revisionId)) { + return true; + } + + return false; + } + + private URL parsePhabricatorURL(String input) throws MalformedURLException { + URL inputURL = new URL(input); + String scheme = inputURL.getProtocol(); + if (scheme == null) + scheme = "http"; + int port = inputURL.getPort(); + if (port == -1) { + if (scheme == "https") + port = 443; + else + port = 80; + } + return inputURL; + } + } 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 2700f6c..d7e297b 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 @@ -49,6 +49,14 @@ public class BuildTracker implements Runnable { // Setup plugin specific configuration // TODO: implement AppConfig as PluginConfig phabricatorConfig.setParameters(params); + + // Now we have set all the parameters we need to check if + // everything is present and correct for us to continue + if (!phabricatorConfig.isPluginSetup()) + { + logger.info("Plugin incorrectly configured"); + return; + } while (!build.isFinished()) { // Wait until the build finishes