VersionPlace.java

  1. package emissary.place;

  2. import emissary.core.IBaseDataObject;
  3. import emissary.core.ResourceException;
  4. import emissary.util.GitRepositoryState;

  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;

  9. public class VersionPlace extends ServiceProviderPlace {

  10.     private static final String EMISSARY_VERSION = "EMISSARY_VERSION";
  11.     private static final String EMISSARY_VERSION_HASH = "EMISSARY_VERSION_HASH";
  12.     private boolean includeDate;
  13.     private boolean useAbbrevHash;
  14.     private String formattedVersion;
  15.     private String versionHash;
  16.     protected Pattern regexPatternForVersion;

  17.     /**
  18.      * Create the place from the specified config file or resource
  19.      *
  20.      * @param configInfo the config file or resource to use
  21.      * @param dir the name of the controlling directory to register with
  22.      * @param placeLoc string name of this place
  23.      */
  24.     public VersionPlace(String configInfo, String dir, String placeLoc) throws IOException {
  25.         super(configInfo, dir, placeLoc);
  26.         configurePlace();
  27.     }

  28.     /**
  29.      * Create the place from the specified config stream data
  30.      *
  31.      * @param configInfo the config file or resource to use
  32.      * @param dir the name of the controlling directory to register with
  33.      * @param placeLoc string name of this place
  34.      */
  35.     public VersionPlace(InputStream configInfo, String dir, String placeLoc) throws IOException {
  36.         super(configInfo, dir, placeLoc);
  37.         configurePlace();
  38.     }

  39.     /**
  40.      * Create the place from the specified config stream data
  41.      *
  42.      * @param configInfo the config file or resource to use
  43.      */
  44.     public VersionPlace(InputStream configInfo) throws IOException {
  45.         super(configInfo);
  46.         configurePlace();
  47.     }

  48.     /**
  49.      * Create with the default configuration
  50.      */
  51.     public VersionPlace() throws IOException {
  52.         super();
  53.         configurePlace();
  54.     }

  55.     private void configurePlace() {
  56.         GitRepositoryState gitRepositoryState = initGitRepositoryState();

  57.         includeDate = configG.findBooleanEntry("INCLUDE_DATE", true);
  58.         useAbbrevHash = configG.findBooleanEntry("USE_ABBREV_HASH", true);

  59.         String cfgRegex = configG.findStringEntry("VERSION_REGEX_FOR_NO_DATE", "^(\\d+\\.)?(\\d+\\.)?(\\d+)$");
  60.         regexPatternForVersion = Pattern.compile(cfgRegex);

  61.         formattedVersion = getVersion(gitRepositoryState);
  62.         versionHash = getVersionHash(gitRepositoryState);
  63.     }

  64.     @Override
  65.     public void process(IBaseDataObject payload) throws ResourceException {
  66.         payload.putParameter(EMISSARY_VERSION, formattedVersion);
  67.         payload.putParameter(EMISSARY_VERSION_HASH, versionHash);
  68.     }

  69.     GitRepositoryState initGitRepositoryState() {
  70.         return GitRepositoryState.getRepositoryState();
  71.     }

  72.     protected String getVersion(GitRepositoryState gitRepositoryState) {
  73.         String version = gitRepositoryState.getBuildVersion();
  74.         Matcher matcher = regexPatternForVersion.matcher(version);
  75.         // if a release version, return just the version, even if includeDate is true
  76.         if (matcher.matches()) {
  77.             return version;
  78.         }

  79.         if (includeDate) {
  80.             // version with date & time information
  81.             // changes format of date from 2024-09-23T10:41:18-0400, to 20240923104118
  82.             String buildTime = gitRepositoryState.getBuildTime();
  83.             int cutEndMark = buildTime.lastIndexOf(":") + 3;
  84.             String formattedDate = buildTime.substring(0, cutEndMark).replaceAll("\\D", "");
  85.             return version + "-" + formattedDate;
  86.         } else {
  87.             // adds just version
  88.             return version;
  89.         }
  90.     }

  91.     protected String getVersionHash(GitRepositoryState gitRepositoryState) {
  92.         if (useAbbrevHash) {
  93.             // first 7 chars of commit hash
  94.             return gitRepositoryState.getCommitIdAbbrev();
  95.         } else {
  96.             // full commit hash (default option)
  97.             return gitRepositoryState.getCommitId();
  98.         }
  99.     }

  100.     public void setIncludeDate(Boolean includeDate) {
  101.         this.includeDate = includeDate;
  102.     }

  103.     public void setAbbrevHash(Boolean useAbbrevHash) {
  104.         this.useAbbrevHash = useAbbrevHash;
  105.     }

  106.     public boolean isIncludeDate() {
  107.         return includeDate;
  108.     }

  109.     public boolean isUseAbbrevHash() {
  110.         return useAbbrevHash;
  111.     }
  112. }