From 31e91a495132dd1e1d36ecf6566ec25d838909ab Mon Sep 17 00:00:00 2001 From: Sebastian Davids Date: Wed, 17 Apr 2024 17:14:58 +0200 Subject: [PATCH] feat: add author.email field Signed-off-by: Sebastian Davids --- doc/usage/Writing_Templates.md | 6 ++++ doc/usage/properties_file.md | 2 ++ src/main/java/org/doble/adr/model/Record.java | 10 ++++++ .../org/doble/commands/CommandConfig.java | 13 +++++++ .../java/org/doble/commands/CommandInit.java | 4 +++ .../java/org/doble/commands/CommandNew.java | 4 +++ .../java/org/doble/adr/CommandConfigTest.java | 12 +++++++ src/test/java/org/doble/adr/RecordTest.java | 34 ++++++++++++++++++- .../resources/template_with_author_email.md | 3 ++ 9 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 src/test/resources/template_with_author_email.md diff --git a/doc/usage/Writing_Templates.md b/doc/usage/Writing_Templates.md index 7021ea6..ebd5deb 100644 --- a/doc/usage/Writing_Templates.md +++ b/doc/usage/Writing_Templates.md @@ -72,6 +72,12 @@ Description: The initial author of the ADR; it is determined in the following or 1. the value of the environment variable `ADR_AUTHOR` 2. the value of the JVM system property "user.name" +### `author.email` + +Type: field + +Description: The email of the initial author of the ADR + ### `supersedes.id` Type: list diff --git a/doc/usage/properties_file.md b/doc/usage/properties_file.md index 3233165..29aeb32 100644 --- a/doc/usage/properties_file.md +++ b/doc/usage/properties_file.md @@ -16,3 +16,5 @@ This consists of a header with the creation date followed by property value pair `templateFile` Path to the template file used to create new ADRs. `docPath` Relative path to the location where ADRs are stored. + +`authorEmail` The email of the initial author of the ADR. diff --git a/src/main/java/org/doble/adr/model/Record.java b/src/main/java/org/doble/adr/model/Record.java index 49ce90c..f08b5c5 100644 --- a/src/main/java/org/doble/adr/model/Record.java +++ b/src/main/java/org/doble/adr/model/Record.java @@ -25,6 +25,7 @@ public class Record { private final String name; private final LocalDate date; private final String author; + private final String authorEmail; private final String status; private final DateTimeFormatter dateFormatter; @@ -45,6 +46,7 @@ private Record(Record.Builder builder) throws URISyntaxException { this.name = builder.name; this.date = builder.date; this.author = builder.author; + this.authorEmail = builder.authorEmail; this.status = builder.status; this.dateFormatter = builder.dateFormatter; @@ -131,6 +133,7 @@ public Path createPeristentRepresentation() throws ADRException { .map(line -> line.replaceAll("\\{\\{name\\}\\}", name)) .map(line -> line.replaceAll("\\{\\{status\\}\\}", status)) .map(line -> line.replaceAll("\\{\\{author\\}\\}", author)) + .map(line -> line.replaceAll("\\{\\{author.email\\}\\}", authorEmail)) .map(line -> line.replaceAll("\\{\\{date\\}\\}", dateFormatter.format(date))) .filter(line -> !(line.contains("{{{link.id}}}") && linkFragments.size() == 0)) // Remove lines // which will be @@ -285,6 +288,8 @@ public static class Builder { private String name; private String author = System.getProperty("user.name"); + private String authorEmail; + private LocalDate date = LocalDate.now(); private String status = "Proposed"; private DateTimeFormatter dateFormatter; @@ -335,6 +340,11 @@ public Builder author(String author) { return this; } + public Builder authorEmail(String authorEmail) { + this.authorEmail = authorEmail; + return this; + } + public Builder status(String status) { this.status = status; return this; diff --git a/src/main/java/org/doble/commands/CommandConfig.java b/src/main/java/org/doble/commands/CommandConfig.java index 43acf49..847c049 100644 --- a/src/main/java/org/doble/commands/CommandConfig.java +++ b/src/main/java/org/doble/commands/CommandConfig.java @@ -10,6 +10,8 @@ import picocli.CommandLine.Parameters; import picocli.CommandLine.ParentCommand; +import static java.util.Objects.requireNonNullElse; + /** * Subcommand to configure the properties * @@ -73,6 +75,17 @@ void author(@Parameters(paramLabel = "") String[] author) throws Excepti } + @Command(description = "Change the default email of the ADR author.") + void authorEmail(@Parameters(paramLabel = "") String authorEmail) throws Exception { + + ADRProperties properties = loadProperties(); + + authorEmail = requireNonNullElse(authorEmail, "").trim(); + + properties.setProperty("authorEmail", authorEmail); + properties.store(); + } + @Command(description = "Change the location where future ADRs are stored.") void docPath(@Parameters(paramLabel = "") String docPathName) throws Exception { diff --git a/src/main/java/org/doble/commands/CommandInit.java b/src/main/java/org/doble/commands/CommandInit.java index d462820..330ac6f 100644 --- a/src/main/java/org/doble/commands/CommandInit.java +++ b/src/main/java/org/doble/commands/CommandInit.java @@ -146,12 +146,16 @@ public Integer call() throws Exception { // If a template is specified and an initial template is specified create an // initial ADR using the specified initial template if (template != null && initialTemplate != null) { + // Set up the author's email + String authorEmail = properties.getProperty("authorEmail", "").trim(); + Record record = new Record.Builder(docsPath, dateFormatter) .template(initialTemplate) .id(1) .name("Record architecture decisions") .date(LocalDate.now()) .author(env.author) + .authorEmail(authorEmail) .build(); record.createPeristentRepresentation(); diff --git a/src/main/java/org/doble/commands/CommandNew.java b/src/main/java/org/doble/commands/CommandNew.java index b112693..01c61c5 100644 --- a/src/main/java/org/doble/commands/CommandNew.java +++ b/src/main/java/org/doble/commands/CommandNew.java @@ -124,12 +124,16 @@ public Integer call() throws Exception { // Create the ADR title from the arguments adrTitle = adrTitleParts.stream().collect(Collectors.joining(" ")); + // Set up the author's email + String authorEmail = properties.getProperty("authorEmail", "").trim(); + // Build the record Record record = new Record.Builder(docsPath, dateFormatter) .id(highestIndex() + 1) .name(adrTitle) .date(LocalDate.now()) .author(env.author) + .authorEmail(authorEmail) .template(templatePathName) .build(); diff --git a/src/test/java/org/doble/adr/CommandConfigTest.java b/src/test/java/org/doble/adr/CommandConfigTest.java index 0346aa9..c512803 100644 --- a/src/test/java/org/doble/adr/CommandConfigTest.java +++ b/src/test/java/org/doble/adr/CommandConfigTest.java @@ -93,6 +93,18 @@ void testConfigAuthorMulitpleName() throws Exception { assertEquals("William Shakespeare the Bard", properties.getProperty("author")); } + @Test + void testConfigAuthorEmail() throws Exception { + + int exitCode = ADR.run(TestUtilities.argify("config authorEmail andrew.l.doble@gmail.com"), env); + assertEquals(0, exitCode); + + ADRProperties properties = new ADRProperties(env); + properties.load(); + + assertEquals("andrew.l.doble@gmail.com", properties.getProperty("authorEmail")); + } + @Test void testDocPath() throws Exception { int exitCode = ADR.run(TestUtilities.argify("config docPath documents/ADRs"), env); diff --git a/src/test/java/org/doble/adr/RecordTest.java b/src/test/java/org/doble/adr/RecordTest.java index ca25ae2..0beadf7 100644 --- a/src/test/java/org/doble/adr/RecordTest.java +++ b/src/test/java/org/doble/adr/RecordTest.java @@ -352,5 +352,37 @@ void testUpdatingLinks() { fail("Not yet implmented"); //TODO implement this } - + + @Test + @Order(11) + public void testRecordConstructionWithGivenAuthorAndAuthorEmail() throws Exception { + String expectedContents = """ + # 68. This is a new record with given author and authorEmail + + Author: Andrew Doble """; + + // Build the record + Record record = new Record.Builder(docPath, dateFormatter) + .id(68) + .name("This is a new record with given author and authorEmail") + .author("Andrew Doble") + .authorEmail("andrew.l.doble@gmail.com") + .template("rsrc:template_with_author_email.md") + .build(); + + record.createPeristentRepresentation(); + + // Check if the ADR file has been created + assertTrue(Files.exists(fileSystem.getPath("/test/0068-this-is-a-new-record-with-given-author-and-authoremail.md"))); + + // Read in the file + Path adrFile = fileSystem.getPath("/test/0068-this-is-a-new-record-with-given-author-and-authoremail.md"); + + String actualContents; + try (Stream lines = Files.lines(adrFile)) { + actualContents = lines.collect(Collectors.joining("\n")); + } + + assertEquals(expectedContents, actualContents); + } } diff --git a/src/test/resources/template_with_author_email.md b/src/test/resources/template_with_author_email.md new file mode 100644 index 0000000..a17b495 --- /dev/null +++ b/src/test/resources/template_with_author_email.md @@ -0,0 +1,3 @@ +# {{id}}. {{name}} + +Author: {{author}} <{{author.email}}>