Skip to content

Commit

Permalink
Implementation of --version-starts-with and --version-less-than-or-equal
Browse files Browse the repository at this point in the history
flags to further restrict which version are included in the changeling.
See README.md for full instructions on use.
  • Loading branch information
switchtrue committed Dec 9, 2014
1 parent be8dfaf commit e7449a0
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 17 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------------

Expand All @@ -47,5 +49,7 @@ In order to execute the unit tests properly (and build/install the program), you
* `password = <password>` where `<password>` is the password for the specified user.
* `project = <project>` where `<project>` is the identifier (key) of the JIRA project.
* `version = <version>` where `<version>` is the version up to which the changelog should be generated.
* `versionstartswith = <starts_with>` where `<starts_with>` is a substring (prefix) of a version for which the write will match on to decide which version to include. For example, if `<starts_with>` 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 = <less_than_or_equal>` where `<less_than_or_equal>` 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 `<less_than_or_equal>` 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.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.switchtrue.jira.changelog</groupId>
<artifactId>jira-changelog-builder</artifactId>
<version>1.05.3</version>
<version>1.06</version>
<packaging>jar</packaging>

<name>JIRAChangelogBuilder</name>
Expand Down
12 changes: 6 additions & 6 deletions src/com/switchtrue/jira/changelog/Changelog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <JIRA_URL> <JIRA_username> <JIRA_password> <JIRA_project_key> <version> <template_list> [<flags>]");
System.out.println("<JIRA_URL>: The URL of the JIRA instance (e.g. https://somecompany.atlassian.net).");
System.out.println("<JIRA_username>: The username used to log into JIRA.");
Expand Down Expand Up @@ -112,15 +112,15 @@ 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")) {
if (fixVersionRestrictMode != null) {
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.");
}
Expand Down Expand Up @@ -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);
Expand Down
57 changes: 49 additions & 8 deletions src/com/switchtrue/jira/changelog/JiraAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<VersionInfo> versionList_;
private VersionInfoCache cache_;
private String fixVersionRestrictMode_;
private String fixVersionRestrictTerm_;
private Version buildVersion = null;

/**
* JiraAPI Constructor that accepts the basic information require to
Expand All @@ -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;

Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down
75 changes: 75 additions & 0 deletions test/com/switchtrue/jira/changelog/ChangelogTemplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit e7449a0

Please sign in to comment.