From e7449a0f9ed88eff0ebcf83e631f1f57fbe89aae Mon Sep 17 00:00:00 2001 From: Mike Leonard Date: Tue, 9 Dec 2014 22:09:22 +1100 Subject: [PATCH] Implementation of --version-starts-with and --version-less-than-or-equal flags to further restrict which version are included in the changeling. See README.md for full instructions on use. --- README.md | 8 +- pom.xml | 2 +- .../switchtrue/jira/changelog/Changelog.java | 12 +-- .../switchtrue/jira/changelog/JiraAPI.java | 57 ++++++++++++-- .../jira/changelog/ChangelogTemplateTest.java | 75 +++++++++++++++++++ 5 files changed, 137 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index e257a65..1e14eb8 100644 --- a/README.md +++ b/README.md @@ -30,8 +30,10 @@ Where the arguments are used as follows: * `--debug`: Print debug/logging information to standard out. This will also force errors to go to the standard out and exit with code 0 rather than 1. * `--changelog-description-field "field_name"`: The name of the field in JIRA you wish to use as the changelog description field. If you do not use this, it will default to the summary field. * `--changelog-file-name /some/path/filename`: A CSV list of paths on disk to the files you wish to output the file changelogs to. If you do not use this, the file changelog will be written to changelog#.txt in the working directory by default (where # is the changelog file number). -* `--eol-style (NATIVE|CRLF|LF)`: The type of line endings you wish the changelog files to use. Valid values are NATIVE (system line endings), CRLF (Windows line endings) or LF (UNIX line endings). If you do not use this, the changelogs will use the default system line endings (NATIVE). - + * `--eol-style (NATIVE|CRLF|LF)`: The type of line endings you wish the changelog files to use. Valid values are NATIVE (system line endings), CRLF (Windows line endings) or LF (UNIX line endings). If you do not use this, the changelogs will use the default system line endings (NATIVE). + * `--version-starts-with "Version name prefix"`: Only display versions in the changelog that have a name starting with 'Version name prefix'. This cannot be used with --version-less-than-or-equal. This is useful for restricting what goes in the changelog if you are producing different version side-by-side. For example, if 'Version name prefix' is "ACME_1.3" then "ACME_1.3.1", "ACME_1.3.5" and "ACME_1.3.8" would all match whilst "ACME_1.1.8" and "ACME_1.4.9" would not. + * `--version-less-than-or-equal "Version name"`: Only display versions in the changelog that have a name less than or equal to 'Version name'. This cannot be used with --version-starts-with. This uses a Java string comparison (compareTo). This is useful for restricting what goes in the changelog if you are producing different version side-by-side. For example if 'Version name' is "ACME_1.3.8" then "ACME_1.3.1", "ACME_1.3.5" and "ACME_1.1.8" would all match whilst "ACME_1.3.9" and "ACME_1.4.9" would not. + Managing the Cache ------------------ @@ -47,5 +49,7 @@ In order to execute the unit tests properly (and build/install the program), you * `password = ` where `` is the password for the specified user. * `project = ` where `` is the identifier (key) of the JIRA project. * `version = ` where `` is the version up to which the changelog should be generated. + * `versionstartswith = ` where `` is a substring (prefix) of a version for which the write will match on to decide which version to include. For example, if `` is "ACME_1.3" then "ACME_1.3.1", "ACME_1.3.5" and "ACME_1.3.8" would all match whilst "ACME_1.1.8" and "ACME_1.4.9" would not. + * `versionlessthanorequal = ` where `` is a version name that you want to compare as a string and only include versions with a name less than or equal to this. For example if `` is "ACME_1.3.8" then "ACME_1.3.1", "ACME_1.3.5" and "ACME_1.1.8" would all match whilst "ACME_1.3.9" and "ACME_1.4.9" would not. Tests can be manually executed by running 'mvn test' from the base directory. diff --git a/pom.xml b/pom.xml index 0b7850e..3387749 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.switchtrue.jira.changelog jira-changelog-builder - 1.05.3 + 1.06 jar JIRAChangelogBuilder diff --git a/src/com/switchtrue/jira/changelog/Changelog.java b/src/com/switchtrue/jira/changelog/Changelog.java index 3efe49b..14336b7 100644 --- a/src/com/switchtrue/jira/changelog/Changelog.java +++ b/src/com/switchtrue/jira/changelog/Changelog.java @@ -9,14 +9,14 @@ */ public class Changelog { - public static int FIX_VERSION_RESTICT_MODE_STARTS_WITH = 10; - public static int FIX_VERSION_RESTICT_MODE_LESS_THAN = 20;_OR_EQUAL + public static String FIX_VERSION_RESTICT_MODE_STARTS_WITH = "SW"; + public static String FIX_VERSION_RESTICT_MODE_LESS_THAN_OR_EQUAL = "LTE"; /** * Show usage of the application. */ public static void showUsage() { - System.out.println("Usage:");k + System.out.println("Usage:"); System.out.println("java -jar jira-changelog-builder.jar []"); System.out.println(": The URL of the JIRA instance (e.g. https://somecompany.atlassian.net)."); System.out.println(": The username used to log into JIRA."); @@ -112,7 +112,7 @@ public static void main(String[] args) { Logger.err("You cannot use both --version-starts-with and --version-less-than-or-equal at the same time or supply either of them more than once."); System.exit(2); } - fixVersionRestrictMode = FIX_VERSION_RESTICT_MODE_STARTS_WITH + fixVersionRestrictMode = FIX_VERSION_RESTICT_MODE_STARTS_WITH; fixVersionRestrictTerm = args[++currentArgument]; Logger.log("--version-starts-with found. Only inlcude versions starting with " + fixVersionRestrictTerm + " in the Changelog."); } else if (args[currentArgument].equals("--version-less-than-or-equal")) { @@ -120,7 +120,7 @@ public static void main(String[] args) { Logger.err("You cannot use both --version-starts-with and --version-less-than-or-equal at the same time or supply either of them more than once."); System.exit(2); } - fixVersionRestrictMode = FIX_VERSION_RESTICT_MODE_LESS_THAN_OR_EQUAL + fixVersionRestrictMode = FIX_VERSION_RESTICT_MODE_LESS_THAN_OR_EQUAL; fixVersionRestrictTerm = args[++currentArgument]; Logger.log("--version-less-than-or-equal found. Only inlcude versions with a name less than or equal to " + fixVersionRestrictTerm + " in the Changelog."); } @@ -152,7 +152,7 @@ public static void main(String[] args) { } } - JiraAPI jiraApi = new JiraAPI(jiraUsername, jiraPassword, jiraURL, jql, descriptionField); + JiraAPI jiraApi = new JiraAPI(jiraUsername, jiraPassword, jiraURL, jql, descriptionField, fixVersionRestrictMode, fixVersionRestrictTerm); if (objectCachePath != null) { VersionInfoCache cache = new VersionInfoCache(jiraProjectKey, objectCachePath); diff --git a/src/com/switchtrue/jira/changelog/JiraAPI.java b/src/com/switchtrue/jira/changelog/JiraAPI.java index 649e306..d641f23 100644 --- a/src/com/switchtrue/jira/changelog/JiraAPI.java +++ b/src/com/switchtrue/jira/changelog/JiraAPI.java @@ -32,9 +32,12 @@ public class JiraAPI { private final String username_, password_; private final URI jiraServerURI_; private String jql_; - private String descriptionField; + private String descriptionField_; private LinkedList versionList_; private VersionInfoCache cache_; + private String fixVersionRestrictMode_; + private String fixVersionRestrictTerm_; + private Version buildVersion = null; /** * JiraAPI Constructor that accepts the basic information require to @@ -46,7 +49,7 @@ public class JiraAPI { * @param descriptionField The name of the field in JIRA to use as the * changelog description. */ - public JiraAPI(String username, String password, String URL, String jql, String descriptionField) { + public JiraAPI(String username, String password, String URL, String jql, String descriptionField, String fixVersionRestrictMode, String fixVersionRestrictTerm) { username_ = username; password_ = password; @@ -56,7 +59,9 @@ public JiraAPI(String username, String password, String URL, String jql, String jql_ = " and (" + jql + ")"; } - this.descriptionField = descriptionField; + descriptionField_ = descriptionField; + fixVersionRestrictMode_ = fixVersionRestrictMode; + fixVersionRestrictTerm_ = fixVersionRestrictTerm; URI tempURI = null; try { @@ -77,6 +82,43 @@ public void setVersionInfoCache(VersionInfoCache cache) { cache_ = cache; } + private boolean doesVersionNameMatchFilter(String versionName) { + if (fixVersionRestrictMode_ == null) { + return true; + } + + if (fixVersionRestrictMode_ == Changelog.FIX_VERSION_RESTICT_MODE_STARTS_WITH) { + if (versionName.startsWith(fixVersionRestrictTerm_)) { + Logger.log("Version '" + versionName + "' starts with '" + fixVersionRestrictTerm_ + "'."); + return true; + } else { + Logger.log("Version '" + versionName + "' does not start with '" + fixVersionRestrictTerm_ + "'. Omitting from changelog."); + return false; + } + } + + if (fixVersionRestrictMode_ == Changelog.FIX_VERSION_RESTICT_MODE_LESS_THAN_OR_EQUAL) { + int compare = versionName.compareTo(fixVersionRestrictTerm_); + if (compare <= 0){ + Logger.log("Version '" + versionName + "' is less than or equal to '" + fixVersionRestrictTerm_ + "'."); + return true; + } else { + Logger.log("Version '" + versionName + "' is not less than or equal to '" + fixVersionRestrictTerm_ + "'. Omitting from changelog."); + return false; + } + } + + return false; + } + + private boolean isVersionBeforeOrEqualToBuildReleaseDate(DateTime versionReleaseDate) { + if (versionReleaseDate.isBefore(buildVersion.getReleaseDate()) || versionReleaseDate.isEqual(buildVersion.getReleaseDate())) { + return true; + } + + return false; + } + /** * Communicate with JIRA to find all versions prior to the version you are * currently building for each version found get a list of issues fixed in @@ -104,7 +146,6 @@ public void fetchVersionDetails(String projectKey, String versionLabel) { // Get a list of versions for this project and identify the one were // currently trying to build. Logger.log("Determining if the version '" + versionLabel + "' exists in JIRA."); - Version buildVersion = null; for (Version v : proj.getVersions()) { if (v.getName().equals(versionLabel)) { buildVersion = v; @@ -131,8 +172,8 @@ public void fetchVersionDetails(String projectKey, String versionLabel) { if (versionReleaseDate == null) { versionReleaseDate = new DateTime(); } - if ((v.getName().equals(versionLabel) || versionReleaseDate.isBefore(buildVersion.getReleaseDate()) || versionReleaseDate.isEqual(buildVersion.getReleaseDate()))) { - Logger.log("Version '" + v.getName() + "' was released before '" + versionLabel + "' - generating changelog."); + if ( (v.getName().equals(versionLabel) || isVersionBeforeOrEqualToBuildReleaseDate(versionReleaseDate)) && doesVersionNameMatchFilter(v.getName()) ) { + Logger.log("Adding '" + v.getName() + "' to changelog."); // Attempt to get the changelog from the cache. If it can't be found // or were trying to generate a changelog for the current version then // build/rebuild and cache. @@ -157,7 +198,7 @@ public void fetchVersionDetails(String projectKey, String versionLabel) { String changelogDescription; String type = null; try { - changelogDescription = i.getFieldByName(descriptionField).getValue().toString(); + changelogDescription = i.getFieldByName(descriptionField_).getValue().toString(); } catch (NullPointerException npe) { // Changelog Description doesn't exist as a field for this // issue so just default to the summary. @@ -179,7 +220,7 @@ public void fetchVersionDetails(String projectKey, String versionLabel) { String changelogDescription = null; String type = null; try { - changelogDescription = i.getFieldByName(descriptionField).getValue().toString(); + changelogDescription = i.getFieldByName(descriptionField_).getValue().toString(); } catch (NullPointerException npe) { // Changelog Description doesn't exist as a field for this // issue so just default to the summary. diff --git a/test/com/switchtrue/jira/changelog/ChangelogTemplateTest.java b/test/com/switchtrue/jira/changelog/ChangelogTemplateTest.java index a22368b..6876c87 100644 --- a/test/com/switchtrue/jira/changelog/ChangelogTemplateTest.java +++ b/test/com/switchtrue/jira/changelog/ChangelogTemplateTest.java @@ -165,4 +165,79 @@ public void testLineEndings() throws Exception { out3.close(); assertTrue(true); } + + /** + * Black-box integration test for a complete run of the program limiting + * the changelog to only version that start with a particular value. + * + * @throws Exception + */ + public void testVersionStartsWith() throws Exception { + Properties properties = new Properties(); + properties.load(new FileInputStream("testing.properties")); + System.out.println("startsWith"); + + String starts_with = properties.getProperty("versionstartswith"); + String file_name = "version_starts_with_" + starts_with + ".html"; + + String[] args = new String[14]; + args[0] = properties.getProperty("url"); + args[1] = properties.getProperty("username"); + args[2] = properties.getProperty("password"); + args[3] = properties.getProperty("project"); + args[4] = properties.getProperty("version"); + args[5] = "examples"; + args[6] = "html.mustache,plain-text.mustache"; + args[7] = "--debug"; + args[8] = "--changelog-file-name"; + args[9] = "test_output" + File.separator + file_name + ",test_output" + File.separator + "changelog.txt"; + args[10] = "--object-cache-path"; + args[11] = "cache"; + args[12] = "--version-starts-with"; + args[13] = starts_with; + + // wrapper function has same effect as main, minus the System.exit call. + Changelog.main(args); + + File f = new File("test_output/" + file_name); + assertTrue(f.exists()); + } + + /** + * Black-box integration test for a complete run of the program limiting + * the changelog to only versions that have a name less than or equal to + * a certain value. + * + * @throws Exception + */ + public void testVersionLessThanOrEqual() throws Exception { + Properties properties = new Properties(); + properties.load(new FileInputStream("testing.properties")); + System.out.println("lessThanOrEqual"); + + String less_than_equal = properties.getProperty("versionlessthanorequal"); + String file_name = "version_less_than_or_equal_" + less_than_equal + ".html"; + + String[] args = new String[14]; + args[0] = properties.getProperty("url"); + args[1] = properties.getProperty("username"); + args[2] = properties.getProperty("password"); + args[3] = properties.getProperty("project"); + args[4] = properties.getProperty("version"); + args[5] = "examples"; + args[6] = "html.mustache,plain-text.mustache"; + args[7] = "--debug"; + args[8] = "--changelog-file-name"; + args[9] = "test_output" + File.separator + file_name +",test_output" + File.separator + "changelog.txt"; + args[10] = "--object-cache-path"; + args[11] = "cache"; + args[12] = "--version-less-than-or-equal"; + args[13] = less_than_equal; + + // wrapper function has same effect as main, minus the System.exit call. + Changelog.main(args); + + File f = new File("test_output/version_less_than_or_equal_" + less_than_equal + ".html"); + assertTrue(f.exists()); + } } \ No newline at end of file