Skip to content

Commit

Permalink
Merge pull request #93 from shipkit/sf
Browse files Browse the repository at this point in the history
Improved flexibility for tag and main dev branch conventions
  • Loading branch information
shestee authored Mar 21, 2021
2 parents ad8e0fc + 0d0faa3 commit a7dabe5
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 30 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ You can read more about all [default env variables](https://docs.github.com/en/f
Note that you can use *any* CI system, not necessarily Github Actions.
Just refer to the documentation of your CI system to learn what are its default env variables.

### Tag name convention

By default the plugin assumes "v" prefix notation for tags, for example: "v1.0.0".
To use a different tag notation, such as "release-1.0.0" or "1.0.0" use `releaseTag` property on the tasks.
See reference examples.

## Customers / sample projects

- https://github.com/shipkit/shipkit-demo (great example/reference project)
Expand Down Expand Up @@ -225,7 +231,10 @@ Complete task configuration
revision = "HEAD"
//The release version, default as below
version = project.version
version = project.version
//Release tag, by default it is "v" + project.version
releaseTag = "v" + project.version
//Repository to look for tickets, *no default*
repository = "mockito/mockito"
Expand Down Expand Up @@ -281,7 +290,10 @@ Complete task configuration
githubToken = System.getenv("GITHUB_TOKEN") // using env var to avoid checked-in secrets
//SHA of the revision from which release is created; *no default*
newTagRevision = System.getenv("GITHUB_SHA") // using an env var automatically exported by Github Actions
newTagRevision = System.getenv("GITHUB_SHA") // using an env var automatically exported by Github Actions
//Release tag, by default it is "v" + project.version
releaseTag = "v" + project.version
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,25 @@ class ChangelogPluginIntegTest extends BaseSpecification {
id 'org.shipkit.shipkit-changelog'
}
tasks.named("generateChangelog") {
version = "1.2.3"
tasks.named("generateChangelog") {
githubToken = "secret"
repository = "mockito/mockito"
repository = "org/repo"
date = "2022-01-01" // for reproducible assertion
}
"""

expect: "run in dry-run mode to smoke test the configuration"
runner("generateChangelog", "-m").build()
when:
runner("generateChangelog").build()

then:
//since this is an edge case (no previous versions/tags) we're ok with oversimplified output with bad links
file("build/changelog.md").text == """<sup><sup>*Changelog generated by [Shipkit Changelog Gradle Plugin](https://github.com/shipkit/shipkit-changelog)*</sup></sup>
#### 1.2.3
- 2022-01-01 - [0 commit(s)](https://github.com/org/repo/compare/@[email protected]) by
- No notable improvements. No pull requests (issues) were referenced from commits."""
}

def "complete task configuration"() {
Expand Down Expand Up @@ -56,13 +67,16 @@ class ChangelogPluginIntegTest extends BaseSpecification {
revision = "HEAD"
//The release version, default as below
version = project.version
version = project.version
//Release tag, by default it is "v" + project.version
releaseTag = "v" + project.version
//Token that enables querying Github, safe to check-in because it is read-only, *no default*
githubToken = "a0a4c0f41c200f7c653323014d6a72a127764e17"
//Repository to look for tickets, *no default*
repository = "mockito/mockito"
repository = "org/repo"
}
"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class GithubReleasePluginIntegTest extends BaseSpecification {
//SHA of the revision from which release is created; *no default*
newTagRevision = "ff2fb22b3bb2fb08164c126c0e2055d57dee441b"
//Release tag, by default it is "v" + project.version
releaseTag = "v" + project.version
//Github token used for posting to Github API, *no default*
githubToken = "secret"
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/shipkit/changelog/ChangelogFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ChangelogFormat {
* Builds the changelog String based on the input parameters.
*/
public static String formatChangelog(Collection<String> contributors, Collection<Ticket> tickets, int commitCount,
final String version, final String previousRev,
final String releaseTag, final String version, final String previousRev,
final String githubRepoUrl, final String date) {
String template = "@header@\n" +
"\n" +
Expand All @@ -31,7 +31,7 @@ public static String formatChangelog(Collection<String> contributors, Collection
put("commitCount", "" + commitCount);
put("repoUrl", githubRepoUrl);
put("previousRev", previousRev);
put("newRev", "v" + version);
put("newRev", releaseTag);
put("contributors", String.join(", ", contributors));
put("improvements", formatImprovements(tickets));
}};
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/shipkit/changelog/ChangelogPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public void apply(Project project) {
t.setGithubUrl("https://github.com");
t.setWorkingDir(project.getProjectDir());
t.setVersion("" + project.getVersion());
t.setReleaseTag("v" + project.getVersion());
t.getOutputs().upToDateWhen(Specs.satisfyNone()); //depends on state of Git repo, Github, etc.
});
}
Expand Down
49 changes: 35 additions & 14 deletions src/main/java/org/shipkit/changelog/GenerateChangelogTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class GenerateChangelogTask extends DefaultTask {
private String repository;
private String previousRevision;
private String version;
private String releaseTag;
private String revision;
private String date;

Expand Down Expand Up @@ -79,6 +80,22 @@ public void setVersion(String version) {
this.version = version;
}

/**
* Release tag, for example "v1.2.3".
* It is used to construct a GitHub link to a diff between previous revision and the new release tag.
*/
@Input
public String getReleaseTag() {
return releaseTag;
}

/**
* See {@link #getReleaseTag()}
*/
public void setReleaseTag(String releaseTag) {
this.releaseTag = releaseTag;
}

/**
* Target revision for changelog generation.
* The changelog is generated between {@link #getPreviousRevision()} and {@code #getRevision()}.
Expand Down Expand Up @@ -171,25 +188,29 @@ public void setGithubToken(String githubToken) {
ProcessRunner runner = new ProcessRunner(workingDir);
GitLogProvider logProvider = new GitLogProvider(runner);

String previousRevision = this.previousRevision != null? this.previousRevision : "master";
LOG.lifecycle("Finding commits between {}..{} in dir: {}", previousRevision, revision, workingDir);
Collection<GitCommit> commits = new GitCommitProvider(logProvider).getCommits(previousRevision, revision);

LOG.lifecycle("Collecting ticket ids from {} commits.", commits.size());
List<String> ticketIds = new LinkedList<>();
Collection<GitCommit> commits = new LinkedList<>();
Collection<Ticket> improvements = new LinkedList<>();
Set<String> contributors = new TreeSet<>();
for (GitCommit c : commits) {
ticketIds.addAll(c.getTickets());
contributors.add(c.getAuthor());
}

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

GithubTicketFetcher fetcher = new GithubTicketFetcher(githubApiUrl, repository, githubToken);
Collection<Ticket> improvements = fetcher.fetchTickets(ticketIds);
LOG.lifecycle("Collecting ticket ids from {} commits.", commits.size());
List<String> ticketIds = new LinkedList<>();
for (GitCommit c : commits) {
ticketIds.addAll(c.getTickets());
contributors.add(c.getAuthor());
}

LOG.lifecycle("Fetching ticket info from {}/{} based on {} ids {}", githubApiUrl, repository, ticketIds.size(), ticketIds);

GithubTicketFetcher fetcher = new GithubTicketFetcher(githubApiUrl, repository, githubToken);
improvements = fetcher.fetchTickets(ticketIds);
}

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

LOG.lifecycle("Saving changelog to file: {}", outputFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class GithubReleasePlugin implements Plugin<Project> {
public void apply(Project project) {
project.getTasks().register("githubRelease", GithubReleaseTask.class, t -> {
t.setGithubApiUrl("https://api.github.com");
String tagName = "v" + project.getVersion();
String tagName = "v" + project.getVersion();//
t.setReleaseTag(tagName);
t.setReleaseName(tagName);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,18 @@ public void setReleaseName(String releaseName) {
this.releaseName = releaseName;
}

/**
* Release tag, for example "v1.2.3".
* One of the parameters of the GitHub API call that creates GitHub release and the Git tag.
*/
@Input
public String getReleaseTag() {
return releaseTag;
}

/**
* See {@link #getReleaseTag()}
*/
public void setReleaseTag(String releaseTag) {
this.releaseTag = releaseTag;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ChangelogFormatTest extends Specification {
new Ticket(12, "improved feature", "https://github.com/myorg/myrepo/issues/12"),
]
when:
def changelog = ChangelogFormat.formatChangelog(['mockitoguy', 'john'], improvements, 5,
def changelog = ChangelogFormat.formatChangelog(['mockitoguy', 'john'], improvements, 5, "v1.0.0",
"1.0.0", "v0.0.9", "https://github.com/myorg/myrepo",
"2020-01-01")

Expand All @@ -25,15 +25,15 @@ class ChangelogFormatTest extends Specification {

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

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

0 comments on commit a7dabe5

Please sign in to comment.