Skip to content

Commit

Permalink
feat: add author.email field
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Davids <sdavids@gmx.de>
  • Loading branch information
sdavids committed Apr 17, 2024
1 parent ccb3bc9 commit 31e91a4
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 1 deletion.
6 changes: 6 additions & 0 deletions doc/usage/Writing_Templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions doc/usage/properties_file.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
10 changes: 10 additions & 0 deletions src/main/java/org/doble/adr/model/Record.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/doble/commands/CommandConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import picocli.CommandLine.Parameters;
import picocli.CommandLine.ParentCommand;

import static java.util.Objects.requireNonNullElse;

/**
* Subcommand to configure the properties
*
Expand Down Expand Up @@ -73,6 +75,17 @@ void author(@Parameters(paramLabel = "<author>") String[] author) throws Excepti

}

@Command(description = "Change the default email of the ADR author.")
void authorEmail(@Parameters(paramLabel = "<authorEmail>") 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 = "<docPath>") String docPathName) throws Exception {

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/doble/commands/CommandInit.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/doble/commands/CommandNew.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
12 changes: 12 additions & 0 deletions src/test/java/org/doble/adr/CommandConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
34 changes: 33 additions & 1 deletion src/test/java/org/doble/adr/RecordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <andrew.l.doble@gmail.com>""";

// 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<String> lines = Files.lines(adrFile)) {
actualContents = lines.collect(Collectors.joining("\n"));
}

assertEquals(expectedContents, actualContents);
}
}
3 changes: 3 additions & 0 deletions src/test/resources/template_with_author_email.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# {{id}}. {{name}}

Author: {{author}} <{{author.email}}>

0 comments on commit 31e91a4

Please sign in to comment.