Skip to content

Commit e7449a0

Browse files
committed
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.
1 parent be8dfaf commit e7449a0

File tree

5 files changed

+137
-17
lines changed

5 files changed

+137
-17
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ Where the arguments are used as follows:
3030
* `--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.
3131
* `--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.
3232
* `--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).
33-
* `--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).
34-
33+
* `--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).
34+
* `--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.
35+
* `--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.
36+
3537
Managing the Cache
3638
------------------
3739

@@ -47,5 +49,7 @@ In order to execute the unit tests properly (and build/install the program), you
4749
* `password = <password>` where `<password>` is the password for the specified user.
4850
* `project = <project>` where `<project>` is the identifier (key) of the JIRA project.
4951
* `version = <version>` where `<version>` is the version up to which the changelog should be generated.
52+
* `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.
53+
* `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.
5054

5155
Tests can be manually executed by running 'mvn test' from the base directory.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.switchtrue.jira.changelog</groupId>
66
<artifactId>jira-changelog-builder</artifactId>
7-
<version>1.05.3</version>
7+
<version>1.06</version>
88
<packaging>jar</packaging>
99

1010
<name>JIRAChangelogBuilder</name>

src/com/switchtrue/jira/changelog/Changelog.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
*/
1010
public class Changelog {
1111

12-
public static int FIX_VERSION_RESTICT_MODE_STARTS_WITH = 10;
13-
public static int FIX_VERSION_RESTICT_MODE_LESS_THAN = 20;_OR_EQUAL
12+
public static String FIX_VERSION_RESTICT_MODE_STARTS_WITH = "SW";
13+
public static String FIX_VERSION_RESTICT_MODE_LESS_THAN_OR_EQUAL = "LTE";
1414

1515
/**
1616
* Show usage of the application.
1717
*/
1818
public static void showUsage() {
19-
System.out.println("Usage:");k
19+
System.out.println("Usage:");
2020
System.out.println("java -jar jira-changelog-builder.jar <JIRA_URL> <JIRA_username> <JIRA_password> <JIRA_project_key> <version> <template_list> [<flags>]");
2121
System.out.println("<JIRA_URL>: The URL of the JIRA instance (e.g. https://somecompany.atlassian.net).");
2222
System.out.println("<JIRA_username>: The username used to log into JIRA.");
@@ -112,15 +112,15 @@ public static void main(String[] args) {
112112
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.");
113113
System.exit(2);
114114
}
115-
fixVersionRestrictMode = FIX_VERSION_RESTICT_MODE_STARTS_WITH
115+
fixVersionRestrictMode = FIX_VERSION_RESTICT_MODE_STARTS_WITH;
116116
fixVersionRestrictTerm = args[++currentArgument];
117117
Logger.log("--version-starts-with found. Only inlcude versions starting with " + fixVersionRestrictTerm + " in the Changelog.");
118118
} else if (args[currentArgument].equals("--version-less-than-or-equal")) {
119119
if (fixVersionRestrictMode != null) {
120120
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.");
121121
System.exit(2);
122122
}
123-
fixVersionRestrictMode = FIX_VERSION_RESTICT_MODE_LESS_THAN_OR_EQUAL
123+
fixVersionRestrictMode = FIX_VERSION_RESTICT_MODE_LESS_THAN_OR_EQUAL;
124124
fixVersionRestrictTerm = args[++currentArgument];
125125
Logger.log("--version-less-than-or-equal found. Only inlcude versions with a name less than or equal to " + fixVersionRestrictTerm + " in the Changelog.");
126126
}
@@ -152,7 +152,7 @@ public static void main(String[] args) {
152152
}
153153
}
154154

155-
JiraAPI jiraApi = new JiraAPI(jiraUsername, jiraPassword, jiraURL, jql, descriptionField);
155+
JiraAPI jiraApi = new JiraAPI(jiraUsername, jiraPassword, jiraURL, jql, descriptionField, fixVersionRestrictMode, fixVersionRestrictTerm);
156156

157157
if (objectCachePath != null) {
158158
VersionInfoCache cache = new VersionInfoCache(jiraProjectKey, objectCachePath);

src/com/switchtrue/jira/changelog/JiraAPI.java

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ public class JiraAPI {
3232
private final String username_, password_;
3333
private final URI jiraServerURI_;
3434
private String jql_;
35-
private String descriptionField;
35+
private String descriptionField_;
3636
private LinkedList<VersionInfo> versionList_;
3737
private VersionInfoCache cache_;
38+
private String fixVersionRestrictMode_;
39+
private String fixVersionRestrictTerm_;
40+
private Version buildVersion = null;
3841

3942
/**
4043
* JiraAPI Constructor that accepts the basic information require to
@@ -46,7 +49,7 @@ public class JiraAPI {
4649
* @param descriptionField The name of the field in JIRA to use as the
4750
* changelog description.
4851
*/
49-
public JiraAPI(String username, String password, String URL, String jql, String descriptionField) {
52+
public JiraAPI(String username, String password, String URL, String jql, String descriptionField, String fixVersionRestrictMode, String fixVersionRestrictTerm) {
5053
username_ = username;
5154
password_ = password;
5255

@@ -56,7 +59,9 @@ public JiraAPI(String username, String password, String URL, String jql, String
5659
jql_ = " and (" + jql + ")";
5760
}
5861

59-
this.descriptionField = descriptionField;
62+
descriptionField_ = descriptionField;
63+
fixVersionRestrictMode_ = fixVersionRestrictMode;
64+
fixVersionRestrictTerm_ = fixVersionRestrictTerm;
6065

6166
URI tempURI = null;
6267
try {
@@ -77,6 +82,43 @@ public void setVersionInfoCache(VersionInfoCache cache) {
7782
cache_ = cache;
7883
}
7984

85+
private boolean doesVersionNameMatchFilter(String versionName) {
86+
if (fixVersionRestrictMode_ == null) {
87+
return true;
88+
}
89+
90+
if (fixVersionRestrictMode_ == Changelog.FIX_VERSION_RESTICT_MODE_STARTS_WITH) {
91+
if (versionName.startsWith(fixVersionRestrictTerm_)) {
92+
Logger.log("Version '" + versionName + "' starts with '" + fixVersionRestrictTerm_ + "'.");
93+
return true;
94+
} else {
95+
Logger.log("Version '" + versionName + "' does not start with '" + fixVersionRestrictTerm_ + "'. Omitting from changelog.");
96+
return false;
97+
}
98+
}
99+
100+
if (fixVersionRestrictMode_ == Changelog.FIX_VERSION_RESTICT_MODE_LESS_THAN_OR_EQUAL) {
101+
int compare = versionName.compareTo(fixVersionRestrictTerm_);
102+
if (compare <= 0){
103+
Logger.log("Version '" + versionName + "' is less than or equal to '" + fixVersionRestrictTerm_ + "'.");
104+
return true;
105+
} else {
106+
Logger.log("Version '" + versionName + "' is not less than or equal to '" + fixVersionRestrictTerm_ + "'. Omitting from changelog.");
107+
return false;
108+
}
109+
}
110+
111+
return false;
112+
}
113+
114+
private boolean isVersionBeforeOrEqualToBuildReleaseDate(DateTime versionReleaseDate) {
115+
if (versionReleaseDate.isBefore(buildVersion.getReleaseDate()) || versionReleaseDate.isEqual(buildVersion.getReleaseDate())) {
116+
return true;
117+
}
118+
119+
return false;
120+
}
121+
80122
/**
81123
* Communicate with JIRA to find all versions prior to the version you are
82124
* currently building for each version found get a list of issues fixed in
@@ -104,7 +146,6 @@ public void fetchVersionDetails(String projectKey, String versionLabel) {
104146
// Get a list of versions for this project and identify the one were
105147
// currently trying to build.
106148
Logger.log("Determining if the version '" + versionLabel + "' exists in JIRA.");
107-
Version buildVersion = null;
108149
for (Version v : proj.getVersions()) {
109150
if (v.getName().equals(versionLabel)) {
110151
buildVersion = v;
@@ -131,8 +172,8 @@ public void fetchVersionDetails(String projectKey, String versionLabel) {
131172
if (versionReleaseDate == null) {
132173
versionReleaseDate = new DateTime();
133174
}
134-
if ((v.getName().equals(versionLabel) || versionReleaseDate.isBefore(buildVersion.getReleaseDate()) || versionReleaseDate.isEqual(buildVersion.getReleaseDate()))) {
135-
Logger.log("Version '" + v.getName() + "' was released before '" + versionLabel + "' - generating changelog.");
175+
if ( (v.getName().equals(versionLabel) || isVersionBeforeOrEqualToBuildReleaseDate(versionReleaseDate)) && doesVersionNameMatchFilter(v.getName()) ) {
176+
Logger.log("Adding '" + v.getName() + "' to changelog.");
136177
// Attempt to get the changelog from the cache. If it can't be found
137178
// or were trying to generate a changelog for the current version then
138179
// build/rebuild and cache.
@@ -157,7 +198,7 @@ public void fetchVersionDetails(String projectKey, String versionLabel) {
157198
String changelogDescription;
158199
String type = null;
159200
try {
160-
changelogDescription = i.getFieldByName(descriptionField).getValue().toString();
201+
changelogDescription = i.getFieldByName(descriptionField_).getValue().toString();
161202
} catch (NullPointerException npe) {
162203
// Changelog Description doesn't exist as a field for this
163204
// issue so just default to the summary.
@@ -179,7 +220,7 @@ public void fetchVersionDetails(String projectKey, String versionLabel) {
179220
String changelogDescription = null;
180221
String type = null;
181222
try {
182-
changelogDescription = i.getFieldByName(descriptionField).getValue().toString();
223+
changelogDescription = i.getFieldByName(descriptionField_).getValue().toString();
183224
} catch (NullPointerException npe) {
184225
// Changelog Description doesn't exist as a field for this
185226
// issue so just default to the summary.

test/com/switchtrue/jira/changelog/ChangelogTemplateTest.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,79 @@ public void testLineEndings() throws Exception {
165165
out3.close();
166166
assertTrue(true);
167167
}
168+
169+
/**
170+
* Black-box integration test for a complete run of the program limiting
171+
* the changelog to only version that start with a particular value.
172+
*
173+
* @throws Exception
174+
*/
175+
public void testVersionStartsWith() throws Exception {
176+
Properties properties = new Properties();
177+
properties.load(new FileInputStream("testing.properties"));
178+
System.out.println("startsWith");
179+
180+
String starts_with = properties.getProperty("versionstartswith");
181+
String file_name = "version_starts_with_" + starts_with + ".html";
182+
183+
String[] args = new String[14];
184+
args[0] = properties.getProperty("url");
185+
args[1] = properties.getProperty("username");
186+
args[2] = properties.getProperty("password");
187+
args[3] = properties.getProperty("project");
188+
args[4] = properties.getProperty("version");
189+
args[5] = "examples";
190+
args[6] = "html.mustache,plain-text.mustache";
191+
args[7] = "--debug";
192+
args[8] = "--changelog-file-name";
193+
args[9] = "test_output" + File.separator + file_name + ",test_output" + File.separator + "changelog.txt";
194+
args[10] = "--object-cache-path";
195+
args[11] = "cache";
196+
args[12] = "--version-starts-with";
197+
args[13] = starts_with;
198+
199+
// wrapper function has same effect as main, minus the System.exit call.
200+
Changelog.main(args);
201+
202+
File f = new File("test_output/" + file_name);
203+
assertTrue(f.exists());
204+
}
205+
206+
/**
207+
* Black-box integration test for a complete run of the program limiting
208+
* the changelog to only versions that have a name less than or equal to
209+
* a certain value.
210+
*
211+
* @throws Exception
212+
*/
213+
public void testVersionLessThanOrEqual() throws Exception {
214+
Properties properties = new Properties();
215+
properties.load(new FileInputStream("testing.properties"));
216+
System.out.println("lessThanOrEqual");
217+
218+
String less_than_equal = properties.getProperty("versionlessthanorequal");
219+
String file_name = "version_less_than_or_equal_" + less_than_equal + ".html";
220+
221+
String[] args = new String[14];
222+
args[0] = properties.getProperty("url");
223+
args[1] = properties.getProperty("username");
224+
args[2] = properties.getProperty("password");
225+
args[3] = properties.getProperty("project");
226+
args[4] = properties.getProperty("version");
227+
args[5] = "examples";
228+
args[6] = "html.mustache,plain-text.mustache";
229+
args[7] = "--debug";
230+
args[8] = "--changelog-file-name";
231+
args[9] = "test_output" + File.separator + file_name +",test_output" + File.separator + "changelog.txt";
232+
args[10] = "--object-cache-path";
233+
args[11] = "cache";
234+
args[12] = "--version-less-than-or-equal";
235+
args[13] = less_than_equal;
236+
237+
// wrapper function has same effect as main, minus the System.exit call.
238+
Changelog.main(args);
239+
240+
File f = new File("test_output/version_less_than_or_equal_" + less_than_equal + ".html");
241+
assertTrue(f.exists());
242+
}
168243
}

0 commit comments

Comments
 (0)