Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- Unreleased GitRef option. Allows the Unreleased link to compare to something else than HEAD.
- Optional different Range URL for `Unreleased` link

## [2.1.1] - 2020-02-21

### Fix
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,23 @@ public class InitMojo extends AbstractMojo {

@Parameter(property = "repositoryUrl")
protected URL repositoryUrl;


/**
* A custom range URL. If defined it will be used to generate diff links.
* Requires a ${start} and ${end} variables, matching the reference range.
* Overrides the repositoryUrl when creating diff links.
* <p>
* Example: {@code}https://gitrepos.com/prj/compare/${start}..${end}{@code}
*/
@Parameter(property = "rangeUrl")
protected String rangeUrl;

/**
* A custom range URL for the Unreleased link. Only used if {@link InitMojo#rangeUrl} is also supplied.
*/
@Parameter(property = "rangeUrlUnreleased")
protected String rangeUrlUnreleased;

@Parameter(property = "username")
protected String username;

Expand All @@ -72,6 +85,14 @@ public class InitMojo extends AbstractMojo {
@Parameter(defaultValue = "${settings}", readonly = true)
protected Settings settings;

/**
* By default, the Unreleased link compares the latest released version with HEAD.
* This optional parameter allows to compare to something else instead of HEAD.
* That can be useful e.g. if you use the git-flow branching concept and thus want to compare the latest version with the develop branch.
*/
@Parameter(defaultValue = TagUtils.DEFAULT_UNRELEASED_GITREF, readonly = true)
protected String unreleasedGitRef;

@Parameter(defaultValue = TagUtils.DEFAULT_TAG_FORMAT, readonly = true)
protected String tagFormat;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private void writeNewVersion(String currVersion, boolean refLink, BufferedWriter
* @throws IOException if an error occurs while writing.
*/
private void writeDiffLink(RepoServer repoServer, Range<String> tagRange, BufferedWriter bw) throws IOException {
DiffRefLink diffRefLink = new DiffRefLink(tagFormat, repoServer, tagRange);
DiffRefLink diffRefLink = new DiffRefLink(tagFormat, unreleasedGitRef, repoServer, tagRange);
bw.write(diffRefLink.toMarkdown());
bw.newLine();
}
Expand Down Expand Up @@ -213,8 +213,8 @@ private Path createTempChangelog() throws MojoFailureException {
* @param rangeUrl a range URL
* @return the repository server
*/
private RepoServer getRepoServer(String rangeUrl) {
return GitServerFactory.from(rangeUrl);
private RepoServer getRepoServer(String rangeUrl, String rangeUrlUnreleased) {
return GitServerFactory.from(rangeUrl, rangeUrlUnreleased);
}

/**
Expand Down Expand Up @@ -283,7 +283,7 @@ private void writeNewChangelog() throws MojoFailureException {
Path path = getChangelogPath();
String version = getAppVersion();
RepoServer origin = rangeUrl != null && !rangeUrl.isEmpty() ?
getRepoServer(rangeUrl) :
getRepoServer(rangeUrl, rangeUrlUnreleased) :
getOriginRepo(repositoryUrl);
writeNewChangelog(version, origin, path);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private void warnVersionsExceptTags(ChangelogValidator validator) {
logger.warn("Some versions in the changelog to not have a matching tag in the repository");
}
for (String version : versionsWithoutTags) {
String tag = toTag(tagFormat, version);
String tag = toTag(tagFormat, version, unreleasedGitRef);
getLog().warn("Missing tag: " + tag);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,16 @@ public URL diff(Range<String> range) {
String end = range.getEnd();
return diff(begin, end);
}

@Override
public URL diffUnreleased(Range<String> range) {
String begin = range.getBegin();
String end = range.getEnd();
return diffUnreleased(begin, end);
}

@Override
public URL diffUnreleased(String a, String b) {
return diff(a, b);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -35,14 +35,27 @@
* A custom Git server.
* <p>
* The diff URL requires two variables, {@code ${start}} and {@code ${end}}, that will represent a range. For example,
* a valid GitHub range URL is shown bellow:
* a valid GitHub range URL is shown below:
* <pre>
* https://github.com/me/myproject/compare/${left}..${right}
* https://github.com/me/myproject/compare/${start}..${end}
* </pre>
* This class optionally supports a different URL for the Unreleased link.
*/
public class CustomGitServer extends BaseGitServer implements RepoServer {

private String rangeUrl;
private String rangeUrlUnreleased;

/**
* Constructs a new custom Git server.
*
* @param rangeUrl the range URL with left and right variables
* @param rangeUrlUnreleased the range URL for Unreleased links with left and right variables
*/
public CustomGitServer(String rangeUrl, String rangeUrlUnreleased) {
this.rangeUrl = rangeUrl;
this.rangeUrlUnreleased = rangeUrlUnreleased;
}

/**
* Constructs a new custom Git server.
Expand All @@ -51,18 +64,27 @@ public class CustomGitServer extends BaseGitServer implements RepoServer {
*/
public CustomGitServer(String rangeUrl) {
this.rangeUrl = rangeUrl;
this.rangeUrlUnreleased = rangeUrl;
}

@Override
public URL diff(String a, String b) {
return diff(a, b, rangeUrl);
}

@Override
public URL diffUnreleased(String a, String b) {
return diff(a, b, rangeUrlUnreleased);
}

public URL diff(String a, String b, String customRangeUrl) {
try {
String url = rangeUrl;
String url = customRangeUrl;
url = StringUtils.replace(url, "start", a);
url = StringUtils.replace(url, "end", b);
return new URL(url);
} catch (MalformedURLException e) {
throw new GitServerException("Failed to create a diff link.", e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -35,7 +35,7 @@
* A factory for constructing Git Server representations.
*/
public class GitServerFactory {

public static final Logger logger = LoggerFactory.getLogger(GitServerFactory.class);

private static final String BITBUCKET_HOST_PREFIX = "bitbucket";
Expand All @@ -47,12 +47,17 @@ public class GitServerFactory {
* @param rangeUrl the range URL
* @return a repository server
*/
public static RepoServer from(String rangeUrl) {
public static RepoServer from(String rangeUrl, String rangeUrlUnreleased) {
if (rangeUrl == null) {
return null;
} else {
logger.info("Using range URL {}", rangeUrl);
return new CustomGitServer(rangeUrl);
if (rangeUrlUnreleased == null) {
logger.info("Using range URL {}", rangeUrl);
return new CustomGitServer(rangeUrl);
} else {
logger.info("Using range URL {} and unreleased range URL {}", rangeUrl, rangeUrlUnreleased);
return new CustomGitServer(rangeUrl, rangeUrlUnreleased);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,23 @@ public interface RepoServer {
* @throws GitServerException if the URL construction fails.
*/
URL diff(Range<String> range);

/**
* Returns an URL that compares two commits, one of them being the Unreleased version.
*
* @param a the first commit.
* @param b the second commit.
* @return an URL that compares two commits.
* @throws GitServerException if the URL construction fails.
*/
URL diffUnreleased(String a, String b);

/**
* Returns an URL that compares a range of commits, one of them being the Unreleased version.
*
* @param range the range of commits.
* @return an URL that compares a range of commits.
* @throws GitServerException if the URL construction fails.
*/
URL diffUnreleased(Range<String> range);
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class TagUtils {
private static final String VERSION_ID = "version";
public static final String DEFAULT_TAG_FORMAT = "v${" + VERSION_ID + "}";

private static final String HEAD = "HEAD";
public static final String DEFAULT_UNRELEASED_GITREF = "HEAD";
private static final String REFS_TAGS = "refs/tags/";

private static String getTagName(Ref tagRef) {
Expand Down Expand Up @@ -92,9 +92,9 @@ public static List<String> getTags(String url, String username, String password)
* @param version the version to be converted.
* @return a tag.
*/
public static String toTag(String tagFormat, String version) {
public static String toTag(String tagFormat, String version, String unreleasedGitRef) {
if (version.equals(UNRELEASED_VERSION)) {
return HEAD;
return unreleasedGitRef;
} else {
return StringUtils.replace(tagFormat, VERSION_ID, version);
}
Expand All @@ -107,9 +107,9 @@ public static String toTag(String tagFormat, String version) {
* @param versionRange the version range.
* @return a tag range.
*/
public static Range<String> toTagRange(String tagFormat, Range<String> versionRange) {
String b = toTag(tagFormat, versionRange.getBegin());
String e = toTag(tagFormat, versionRange.getEnd());
public static Range<String> toTagRange(String tagFormat, Range<String> versionRange, String unreleasedGitRef) {
String b = toTag(tagFormat, versionRange.getBegin(), unreleasedGitRef);
String e = toTag(tagFormat, versionRange.getEnd(), unreleasedGitRef);
return new Range<>(b, e);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@

import java.net.URL;

import static co.enear.maven.plugins.keepachangelog.InitMojo.UNRELEASED_VERSION;

/**
* A changelog version comparison.
*/
public class DiffRefLink implements Markdown {

private String tagFormat;
private String unreleasedGitRef;
private RepoServer repoServer;
private Range<String> versionRange;

Expand All @@ -50,8 +53,9 @@ public class DiffRefLink implements Markdown {
* @param repoServer the repository server.
* @param versionRange the version range.
*/
public DiffRefLink(String tagFormat, RepoServer repoServer, Range<String> versionRange) {
public DiffRefLink(String tagFormat, String unreleasedGitRef, RepoServer repoServer, Range<String> versionRange) {
this.tagFormat = tagFormat;
this.unreleasedGitRef = unreleasedGitRef;
this.repoServer = repoServer;
this.versionRange = versionRange;
}
Expand All @@ -64,8 +68,13 @@ public String toString() {
@Override
public String toMarkdown() {
String end = versionRange.getEnd();
Range<String> tagRange = TagUtils.toTagRange(tagFormat, versionRange);
URL diff = repoServer.diff(tagRange);
Range<String> tagRange = TagUtils.toTagRange(tagFormat, versionRange, unreleasedGitRef);
URL diff;
if (versionRange.contains(UNRELEASED_VERSION)) {
diff = repoServer.diffUnreleased(tagRange);
} else {
diff = repoServer.diff(tagRange);
}
RefLink refLink = new RefLink(end, diff);
return refLink.toMarkdown();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand Down Expand Up @@ -118,4 +118,8 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(begin, end);
}

public boolean contains(N object) {
return begin.equals(object) || end.equals(object);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,19 @@ public void should_GetEmptyList_WhenUrlIsNull() throws GitAPIException {

@Test
public void should_GetTag_WhenGivenReleasedVersion() {
assertEquals("v1.0.0", TagUtils.toTag("v${version}", "1.0.0"));
assertEquals("v1.0.0", TagUtils.toTag("v${version}", "1.0.0", HEAD));
}

@Test
public void should_GetHead_WhenGivenUnreleasedVersion() {
assertEquals(HEAD, TagUtils.toTag("", UNRELEASED_VERSION));
assertEquals(HEAD, TagUtils.toTag("", UNRELEASED_VERSION, HEAD));
}

@Test
public void should_GetTagRange_WhenGivenReleasedVersionRange() {
Range<String> range = new Range<String>("0.0.0", "3.0.0");

Range<String> actual = TagUtils.toTagRange("v${version}", range);
Range<String> actual = TagUtils.toTagRange("v${version}", range, HEAD);

assertEquals("v0.0.0", actual.getBegin());
assertEquals("v3.0.0", actual.getEnd());
Expand Down