Skip to content

Commit 0d0faa3

Browse files
committed
Improved flexibility
- tag convention is now configurable (previously the plugin assumed "v" prefix) - the default development branch name can be anything, "master", "main", etc. Previously, it was hardcoded to "master".
1 parent ad8e0fc commit 0d0faa3

File tree

9 files changed

+88
-30
lines changed

9 files changed

+88
-30
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ You can read more about all [default env variables](https://docs.github.com/en/f
126126
Note that you can use *any* CI system, not necessarily Github Actions.
127127
Just refer to the documentation of your CI system to learn what are its default env variables.
128128

129+
### Tag name convention
130+
131+
By default the plugin assumes "v" prefix notation for tags, for example: "v1.0.0".
132+
To use a different tag notation, such as "release-1.0.0" or "1.0.0" use `releaseTag` property on the tasks.
133+
See reference examples.
134+
129135
## Customers / sample projects
130136

131137
- https://github.com/shipkit/shipkit-demo (great example/reference project)
@@ -225,7 +231,10 @@ Complete task configuration
225231
revision = "HEAD"
226232
227233
//The release version, default as below
228-
version = project.version
234+
version = project.version
235+
236+
//Release tag, by default it is "v" + project.version
237+
releaseTag = "v" + project.version
229238
230239
//Repository to look for tickets, *no default*
231240
repository = "mockito/mockito"
@@ -281,7 +290,10 @@ Complete task configuration
281290
githubToken = System.getenv("GITHUB_TOKEN") // using env var to avoid checked-in secrets
282291
283292
//SHA of the revision from which release is created; *no default*
284-
newTagRevision = System.getenv("GITHUB_SHA") // using an env var automatically exported by Github Actions
293+
newTagRevision = System.getenv("GITHUB_SHA") // using an env var automatically exported by Github Actions
294+
295+
//Release tag, by default it is "v" + project.version
296+
releaseTag = "v" + project.version
285297
}
286298
```
287299

src/integTest/groovy/org/shipkit/changelog/ChangelogPluginIntegTest.groovy

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,25 @@ class ChangelogPluginIntegTest extends BaseSpecification {
1717
id 'org.shipkit.shipkit-changelog'
1818
}
1919
20-
tasks.named("generateChangelog") {
20+
version = "1.2.3"
21+
22+
tasks.named("generateChangelog") {
2123
githubToken = "secret"
22-
repository = "mockito/mockito"
24+
repository = "org/repo"
25+
date = "2022-01-01" // for reproducible assertion
2326
}
2427
"""
2528

26-
expect: "run in dry-run mode to smoke test the configuration"
27-
runner("generateChangelog", "-m").build()
29+
when:
30+
runner("generateChangelog").build()
31+
32+
then:
33+
//since this is an edge case (no previous versions/tags) we're ok with oversimplified output with bad links
34+
file("build/changelog.md").text == """<sup><sup>*Changelog generated by [Shipkit Changelog Gradle Plugin](https://github.com/shipkit/shipkit-changelog)*</sup></sup>
35+
36+
#### 1.2.3
37+
- 2022-01-01 - [0 commit(s)](https://github.com/org/repo/compare/@previousRev@...v1.2.3) by
38+
- No notable improvements. No pull requests (issues) were referenced from commits."""
2839
}
2940

3041
def "complete task configuration"() {
@@ -56,13 +67,16 @@ class ChangelogPluginIntegTest extends BaseSpecification {
5667
revision = "HEAD"
5768
5869
//The release version, default as below
59-
version = project.version
60-
70+
version = project.version
71+
72+
//Release tag, by default it is "v" + project.version
73+
releaseTag = "v" + project.version
74+
6175
//Token that enables querying Github, safe to check-in because it is read-only, *no default*
6276
githubToken = "a0a4c0f41c200f7c653323014d6a72a127764e17"
6377
6478
//Repository to look for tickets, *no default*
65-
repository = "mockito/mockito"
79+
repository = "org/repo"
6680
}
6781
"""
6882

src/integTest/groovy/org/shipkit/gh/release/GithubReleasePluginIntegTest.groovy

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ class GithubReleasePluginIntegTest extends BaseSpecification {
5959
//SHA of the revision from which release is created; *no default*
6060
newTagRevision = "ff2fb22b3bb2fb08164c126c0e2055d57dee441b"
6161
62+
//Release tag, by default it is "v" + project.version
63+
releaseTag = "v" + project.version
64+
6265
//Github token used for posting to Github API, *no default*
6366
githubToken = "secret"
6467
}

src/main/java/org/shipkit/changelog/ChangelogFormat.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class ChangelogFormat {
1616
* Builds the changelog String based on the input parameters.
1717
*/
1818
public static String formatChangelog(Collection<String> contributors, Collection<Ticket> tickets, int commitCount,
19-
final String version, final String previousRev,
19+
final String releaseTag, final String version, final String previousRev,
2020
final String githubRepoUrl, final String date) {
2121
String template = "@header@\n" +
2222
"\n" +
@@ -31,7 +31,7 @@ public static String formatChangelog(Collection<String> contributors, Collection
3131
put("commitCount", "" + commitCount);
3232
put("repoUrl", githubRepoUrl);
3333
put("previousRev", previousRev);
34-
put("newRev", "v" + version);
34+
put("newRev", releaseTag);
3535
put("contributors", String.join(", ", contributors));
3636
put("improvements", formatImprovements(tickets));
3737
}};

src/main/java/org/shipkit/changelog/ChangelogPlugin.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public void apply(Project project) {
2121
t.setGithubUrl("https://github.com");
2222
t.setWorkingDir(project.getProjectDir());
2323
t.setVersion("" + project.getVersion());
24+
t.setReleaseTag("v" + project.getVersion());
2425
t.getOutputs().upToDateWhen(Specs.satisfyNone()); //depends on state of Git repo, Github, etc.
2526
});
2627
}

src/main/java/org/shipkit/changelog/GenerateChangelogTask.java

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class GenerateChangelogTask extends DefaultTask {
2525
private String repository;
2626
private String previousRevision;
2727
private String version;
28+
private String releaseTag;
2829
private String revision;
2930
private String date;
3031

@@ -79,6 +80,22 @@ public void setVersion(String version) {
7980
this.version = version;
8081
}
8182

83+
/**
84+
* Release tag, for example "v1.2.3".
85+
* It is used to construct a GitHub link to a diff between previous revision and the new release tag.
86+
*/
87+
@Input
88+
public String getReleaseTag() {
89+
return releaseTag;
90+
}
91+
92+
/**
93+
* See {@link #getReleaseTag()}
94+
*/
95+
public void setReleaseTag(String releaseTag) {
96+
this.releaseTag = releaseTag;
97+
}
98+
8299
/**
83100
* Target revision for changelog generation.
84101
* The changelog is generated between {@link #getPreviousRevision()} and {@code #getRevision()}.
@@ -171,25 +188,29 @@ public void setGithubToken(String githubToken) {
171188
ProcessRunner runner = new ProcessRunner(workingDir);
172189
GitLogProvider logProvider = new GitLogProvider(runner);
173190

174-
String previousRevision = this.previousRevision != null? this.previousRevision : "master";
175-
LOG.lifecycle("Finding commits between {}..{} in dir: {}", previousRevision, revision, workingDir);
176-
Collection<GitCommit> commits = new GitCommitProvider(logProvider).getCommits(previousRevision, revision);
177-
178-
LOG.lifecycle("Collecting ticket ids from {} commits.", commits.size());
179-
List<String> ticketIds = new LinkedList<>();
191+
Collection<GitCommit> commits = new LinkedList<>();
192+
Collection<Ticket> improvements = new LinkedList<>();
180193
Set<String> contributors = new TreeSet<>();
181-
for (GitCommit c : commits) {
182-
ticketIds.addAll(c.getTickets());
183-
contributors.add(c.getAuthor());
184-
}
185194

186-
LOG.lifecycle("Fetching ticket info from {}/{} based on {} ids {}", githubApiUrl, repository, ticketIds.size(), ticketIds);
195+
if (previousRevision != null) {
196+
LOG.lifecycle("Finding commits between {}..{} in dir: {}", previousRevision, revision, workingDir);
197+
commits = new GitCommitProvider(logProvider).getCommits(previousRevision, revision);
187198

188-
GithubTicketFetcher fetcher = new GithubTicketFetcher(githubApiUrl, repository, githubToken);
189-
Collection<Ticket> improvements = fetcher.fetchTickets(ticketIds);
199+
LOG.lifecycle("Collecting ticket ids from {} commits.", commits.size());
200+
List<String> ticketIds = new LinkedList<>();
201+
for (GitCommit c : commits) {
202+
ticketIds.addAll(c.getTickets());
203+
contributors.add(c.getAuthor());
204+
}
205+
206+
LOG.lifecycle("Fetching ticket info from {}/{} based on {} ids {}", githubApiUrl, repository, ticketIds.size(), ticketIds);
207+
208+
GithubTicketFetcher fetcher = new GithubTicketFetcher(githubApiUrl, repository, githubToken);
209+
improvements = fetcher.fetchTickets(ticketIds);
210+
}
190211

191212
LOG.lifecycle("Generating changelog based on {} tickets from Github", improvements.size());
192-
String changelog = ChangelogFormat.formatChangelog(contributors, improvements, commits.size(), version,
213+
String changelog = ChangelogFormat.formatChangelog(contributors, improvements, commits.size(), releaseTag, version,
193214
previousRevision, githubUrl + "/" + repository, date);
194215

195216
LOG.lifecycle("Saving changelog to file: {}", outputFile);

src/main/java/org/shipkit/github/release/GithubReleasePlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class GithubReleasePlugin implements Plugin<Project> {
1111
public void apply(Project project) {
1212
project.getTasks().register("githubRelease", GithubReleaseTask.class, t -> {
1313
t.setGithubApiUrl("https://api.github.com");
14-
String tagName = "v" + project.getVersion();
14+
String tagName = "v" + project.getVersion();//
1515
t.setReleaseTag(tagName);
1616
t.setReleaseName(tagName);
1717
});

src/main/java/org/shipkit/github/release/GithubReleaseTask.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,18 @@ public void setReleaseName(String releaseName) {
5454
this.releaseName = releaseName;
5555
}
5656

57+
/**
58+
* Release tag, for example "v1.2.3".
59+
* One of the parameters of the GitHub API call that creates GitHub release and the Git tag.
60+
*/
5761
@Input
5862
public String getReleaseTag() {
5963
return releaseTag;
6064
}
6165

66+
/**
67+
* See {@link #getReleaseTag()}
68+
*/
6269
public void setReleaseTag(String releaseTag) {
6370
this.releaseTag = releaseTag;
6471
}

src/test/groovy/org/shipkit/changelog/ChangelogFormatTest.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class ChangelogFormatTest extends Specification {
1010
new Ticket(12, "improved feature", "https://github.com/myorg/myrepo/issues/12"),
1111
]
1212
when:
13-
def changelog = ChangelogFormat.formatChangelog(['mockitoguy', 'john'], improvements, 5,
13+
def changelog = ChangelogFormat.formatChangelog(['mockitoguy', 'john'], improvements, 5, "v1.0.0",
1414
"1.0.0", "v0.0.9", "https://github.com/myorg/myrepo",
1515
"2020-01-01")
1616

@@ -25,15 +25,15 @@ class ChangelogFormatTest extends Specification {
2525

2626
def "no improvements"() {
2727
when:
28-
def changelog = ChangelogFormat.formatChangelog(['mockitoguy'], [], 2,
29-
"2.0.0", "v1.5.5", "https://github.com/myorg/myrepo",
28+
def changelog = ChangelogFormat.formatChangelog(['mockitoguy'], [], 2, "2.0.0",
29+
"2.0.0", "1.5.5", "https://github.com/myorg/myrepo",
3030
"2020-01-01")
3131

3232
then:
3333
changelog == """<sup><sup>*Changelog generated by [Shipkit Changelog Gradle Plugin](https://github.com/shipkit/shipkit-changelog)*</sup></sup>
3434
3535
#### 2.0.0
36-
- 2020-01-01 - [2 commit(s)](https://github.com/myorg/myrepo/compare/v1.5.5...v2.0.0) by mockitoguy
36+
- 2020-01-01 - [2 commit(s)](https://github.com/myorg/myrepo/compare/1.5.5...2.0.0) by mockitoguy
3737
- No notable improvements. No pull requests (issues) were referenced from commits."""
3838
}
3939
}

0 commit comments

Comments
 (0)