Skip to content

Add parameters for generating more flexible changesets using the add goal #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 25, 2025
Merged
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
8 changes: 6 additions & 2 deletions .github/workflows/dependabot-changesets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
if: github.event.pull_request.user.login == 'dependabot[bot]' && github.repository == 'FortnoxAB/changesets-java'
steps:
- name: Dependabot metadata
id: metadata
id: dependabot-metadata
uses: dependabot/fetch-metadata@v2
with:
skip-verification: true
Expand All @@ -22,4 +22,8 @@ jobs:
run: |
echo "dependency-names: ${{ steps.dependabot-metadata.outputs.dependency-names }}"
echo "new-version: ${{ steps.dependabot-metadata.outputs.new-version }}"
echo "${{ toJSON(steps.metadata.outputs) }}"
echo "${{ toJSON(steps.dependabot-metadata.outputs) }}"

# Fake adding a changeset so we see that it runs with the correct parameters
echo "./mvnw se.fortnox.changesets:changesets-maven-plugin:add -DchangesetLevel=dependency -DchangesetContent=\"- ${{ steps.dependabot-metadata.outputs.dependency-names }}: ${{ steps.dependabot-metadata.outputs.new-version }}\""

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.slf4j.Logger;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
Expand Down Expand Up @@ -31,10 +32,14 @@ public ChangesetWriter(Path baseDir) {
}

public void writeChangeset(Changeset changeset) throws FileAlreadyExistsException {
writeChangeset(changeset.packageName(), changeset.level(), changeset.message());
writeChangeset(changeset.packageName(), changeset.level(), changeset.message(), changeset.file());
}

Path writeChangeset(String packageName, Level changeLevel, String message) throws FileAlreadyExistsException {
return writeChangeset(packageName, changeLevel, message, null);
}

Path writeChangeset(String packageName, Level changeLevel, String message, File file) throws FileAlreadyExistsException {
final String fileContent;
if(message == null) {
fileContent = """
Expand Down Expand Up @@ -62,6 +67,26 @@ Path writeChangeset(String packageName, Level changeLevel, String message) throw
}
}


Path changesetFile;
if (file != null) {
// TODO Add tests
changesetFile = file.toPath();
} else {
changesetFile = generateChangesetFilename(changesetsDir);
}

try {
LOG.info("Writing changeset to {}", changesetFile);
Files.writeString(changesetFile, fileContent, StandardOpenOption.CREATE_NEW);
} catch (IOException e) {
LOG.error("Failed to create new changeset", e);
Copy link
Preview

Copilot AI Jun 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider propagating the IOException (or wrapping it in a RuntimeException) to ensure that failures in writing the changeset file are not silently ignored.

Suggested change
LOG.error("Failed to create new changeset", e);
LOG.error("Failed to create new changeset", e);
throw e;

Copilot uses AI. Check for mistakes.

}

return changesetFile;
}

private Path generateChangesetFilename(Path changesetsDir) throws FileAlreadyExistsException {
String newFileName = this.nameGenerator.humanId() + ".md";
Path changesetFile = changesetsDir.resolve(newFileName);

Expand All @@ -80,14 +105,6 @@ Path writeChangeset(String packageName, Level changeLevel, String message) throw
throw new FileAlreadyExistsException(string, null, "Failed to generate a unique name after %s attempts".formatted(attempt));
}
}

try {
LOG.info("Writing changeset to {}", changesetFile);
Files.writeString(changesetFile, fileContent, StandardOpenOption.CREATE_NEW);
} catch (IOException e) {
LOG.error("Failed to create new changeset", e);
}

return changesetFile;
}
}
2 changes: 2 additions & 0 deletions changesets-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@
<settingsFile>src/it/settings.xml</settingsFile>
<addTestClassPath>true</addTestClassPath>
<showErrors>true</showErrors>
<!-- Skip these when skipping tests -->
<skipInvocation>${skipTests}</skipInvocation>
<goals>
<goal>clean</goal>
<goal>test-compile</goal>
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
invoker.goals=${project.groupId}:${project.artifactId}:${project.version}:add -DchangesetContent='thecontent'
# changesetFilename is used to get predictable names for the changesets, so testing is easier

invoker.goals.1=${project.groupId}:${project.artifactId}:${project.version}:add -DchangesetContent='Patch change' -DchangesetLevel=Patch -DchangesetFilename=patch.md
invoker.goals.2=${project.groupId}:${project.artifactId}:${project.version}:add -DchangesetContent='Minor change' -DchangesetLevel=MINOR -DchangesetFilename=minor.md
invoker.goals.3=${project.groupId}:${project.artifactId}:${project.version}:add -DchangesetContent='Major change' -DchangesetLevel=major -DchangesetFilename=major.md

# TODO Enable this goal to test the dependency change, once that PR is merged
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolve this TODO when #95 is merged

# invoker.goals.4=${project.groupId}:${project.artifactId}:${project.version}:add -DchangesetContent='Dependency change' -DchangesetLevel=Dependency -DchangesetFilename=dependency.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
// Verify that exactly one markdown file was added to the .changeset folder and has the expected content
def firstChangeset = new File(basedir, ".changeset")
def changesetFiles = new File(basedir, ".changeset")
.listFiles()
.findAll { it.name ==~ /.*\.md/ }
.first()
assert changesetFiles.size() == 3

assert firstChangeset.text.equals("---\n" +

assert new File(basedir, ".changeset/patch.md").text.equals("---\n" +
"\"add-blank-changelog\": patch\n" +
"---\n" +
"\n" +
"thecontent");
"Patch change");

assert new File(basedir, ".changeset/minor.md").text.equals("---\n" +
"\"add-blank-changelog\": minor\n" +
"---\n" +
"\n" +
"Minor change");

assert new File(basedir, ".changeset/major.md").text.equals("---\n" +
"\"add-blank-changelog\": major\n" +
"---\n" +
"\n" +
"Major change");

// See invoker.properties for the dependency change details
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolve this when #95 is merged

/*
assert new File(basedir, ".changeset/dependency.md").text.equals("---\n" +
"\"add-blank-changelog\": dependency\n" +
"---\n" +
"\n" +
"Dependency change");
*/
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,60 @@
import se.fortnox.changesets.ChangesetWriter;
import se.fortnox.changesets.Level;

import java.io.File;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;

import static se.fortnox.changesets.ChangesetWriter.CHANGESET_DIR;

@Mojo(name = "add", defaultPhase = LifecyclePhase.INITIALIZE, aggregator = true)
public class AddMojo extends AbstractMojo {
@Parameter(defaultValue = "${project}", readonly = true, required = true)
private org.apache.maven.project.MavenProject project;

/**
* Default content of the changeset.
*/
@Parameter(property = "changesetContent")
String changesetContent;

/**
* Level of the changeset, e.g. patch, minor, major.
*/
@Parameter(property = "changesetLevel", defaultValue = "patch")
String changesetLevel;

/**
* Create the changeset with a specific filename.
* By default, a filename will be automatically generated.
*/
@Parameter(property = "changesetFilename")
String changesetFilename;

@Override
public void execute() {
Path baseDir = project.getBasedir().toPath();

ChangesetWriter changesetWriter = new ChangesetWriter(baseDir);

Level level;
try {
level = Level.valueOf(this.changesetLevel.toUpperCase());
} catch (IllegalArgumentException e) {
List<String> validLevels = Arrays.stream(Level.values()).map(Enum::name).toList();
getLog().error("Invalid changeset level: %s. Valid values are: %s".formatted(this.changesetLevel, validLevels));
return;
}

File changesetFile = null;
if (changesetFilename != null && !changesetFilename.isBlank()) {
changesetFile = baseDir.resolve(CHANGESET_DIR).resolve(changesetFilename).toFile();
}

try {
var changeset = new Changeset("%s".formatted(project.getArtifactId()), Level.PATCH, changesetContent, null);
var changeset = new Changeset(project.getArtifactId(), level, changesetContent, changesetFile);
changesetWriter.writeChangeset(changeset);
} catch (FileAlreadyExistsException e) {
throw new RuntimeException(e);
Expand Down
Loading