Skip to content

Commit

Permalink
Merge pull request #60 from ahartwellCpointe/message-suppression-rebase
Browse files Browse the repository at this point in the history
#59 Added the ability to suppress individual manual action notifications
  • Loading branch information
ewilkins-csi committed May 14, 2024
2 parents a0bf8f3 + 7a89cca commit 1df191b
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 28 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/maven.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ name: Java CI with Maven
on:
push:
branches: [ "dev" ]
pull_request:
pull_request_target:
branches: [ "dev" ]

jobs:
Expand All @@ -32,4 +32,4 @@ jobs:

# Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive
- name: Update dependency graph
uses: advanced-security/maven-dependency-submission-action@v3
uses: advanced-security/maven-dependency-submission-action@v4
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,23 @@ Allows the addition of local types within you current project.

**Default:** `${project.basedir}/src/main/resources/types.json`

### `suppressedMessages`
Allows you to define a list of manual action notifications to ignore. To suppress messages, add their message key
to the list of suppressed messages like in this example.
```
<configuration>
<suppressedMessages>
<message>example_message_key</message>
</suppressedMessages>
</configuration>
```
To get the key values of the messages produced by a build, run the fermenter-mda
plugin with the "fermenter.display.message.keys" set to true: ```mvn generate-sources -Dfermenter.display.message.keys=true```

**Required:** false

**Default:** none

## Creating and specifying a `profile`
Profiles represent a collection of targets that will be used to generate source in a given execution of the
`fermenter-mda` plugin. Targets will be discussed in more detail in the next section, but in short they control how a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ public class GenerateSourcesMojo extends AbstractMojo {
@Parameter
private String basePackage;

@Parameter
private List<String> suppressedMessages;

/**
* Captures the target programming language in which source code artifacts will be generated. This
* configuration drives the automatic configuration of {@link #mainSourceRoot}, {@link #generatedSourceRoot},
Expand Down Expand Up @@ -194,7 +197,7 @@ public void execute() throws MojoExecutionException {

// store notifications in the target directory between plugin invocations so they can be output
// at the end of the build:
notificationService.recordNotifications(getProject());
notificationService.recordNotifications(getProject(), suppressedMessages);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class NotificationService extends AbstractMavenLifecycleParticipant {
private MavenSession session;

private boolean hideManualActions = false;
private boolean displayMessageKeys = false;

private static ObjectMapper objectMapper = new ObjectMapper();

Expand All @@ -71,9 +72,13 @@ public NotificationService(MavenSession session) {
public void afterSessionEnd(MavenSession session) {
this.session = session;
String rawHideManualActions = session.getUserProperties().getProperty("fermenter.hide.manual.actions");
String displayMessageKeys = session.getUserProperties().getProperty("fermenter.display.message.keys");
if (rawHideManualActions != null) {
this.hideManualActions = Boolean.parseBoolean(rawHideManualActions);
}
if(displayMessageKeys != null) {
this.displayMessageKeys = Boolean.parseBoolean(displayMessageKeys);
}

try {
displayNotifications();
Expand All @@ -100,7 +105,7 @@ static void addNotificationToPassedMap(String targetFile, Notification notificat
* Writes any encountered notifications to a file for use later. These are written to the path defined by
* NOTIFICATION_DIRECTORY_PATH.
*/
public void recordNotifications(MavenProject project) {
public void recordNotifications(MavenProject project, List<String> suppressedMessages) {
int manualActionCount = 0;
File projectParentFile = new File(project.getBasedir(), NOTIFICATION_DIRECTORY_PATH);
Map<String, Map<String, Notification>> collectorNotifications = NotificationCollector.getNotifications();
Expand All @@ -109,19 +114,21 @@ public void recordNotifications(MavenProject project) {
Map<String, Notification> notificationsForFile = entry.getValue();
int i = 0;
for (Map.Entry<String, Notification> subMapEntry : notificationsForFile.entrySet()) {
File notificationParentFile = getNotificationParentFile(projectParentFile, subMapEntry.getValue());
File outputFile = new File(notificationParentFile, FilenameUtils.getName(fileName + "-" + UUID.randomUUID().toString() + "." + JSON));
try {
Notification notification = subMapEntry.getValue();
FileUtils.forceMkdir(notificationParentFile);
FileUtils.write(outputFile, getNotificationJson(notification), Charset.defaultCharset());
if (notification instanceof VelocityNotification) {
VelocityNotification velocityNotification = (VelocityNotification) notification;
velocityNotification.writeExternalVelocityContextProperties(project.getBasedir());
Notification notification = subMapEntry.getValue();
if(suppressedMessages == null || !suppressedMessages.contains(subMapEntry.getValue().getKey())) {
File notificationParentFile = getNotificationParentFile(projectParentFile, subMapEntry.getValue());
File outputFile = new File(notificationParentFile, FilenameUtils.getName(fileName + "-" + UUID.randomUUID().toString() + "." + JSON));
try {
FileUtils.forceMkdir(notificationParentFile);
FileUtils.write(outputFile, getNotificationJson(notification), Charset.defaultCharset());
if (notification instanceof VelocityNotification) {
VelocityNotification velocityNotification = (VelocityNotification) notification;
velocityNotification.writeExternalVelocityContextProperties(project.getBasedir());
}
manualActionCount++;
} catch (IOException e) {
throw new GenerationException("Could not write manual action notification to disk!", e);
}
manualActionCount++;
} catch (IOException e) {
throw new GenerationException("Could not write manual action notification to disk!", e);
}
}

Expand Down Expand Up @@ -182,14 +189,20 @@ private void emitNotifications() throws IOException {
} else {
for (Notification notification : entry.getValue()) {
denoteNewManualAction(i);
logger.warn(notification.getNotificationAsString());

if(displayMessageKeys) {
logger.warn("Message Key: " + key + "\n" + notification.getNotificationAsString());
} else {
logger.warn(notification.getNotificationAsString());
}
}
}

i++;
}

logger.warn("To disable these messages, please use -Dfermenter.hide.manual.actions=true");
logger.warn("To disable specific messages, please add their message keys to the suppressedMessages list. See the Fermenter docs for more info: https://github.com/TechnologyBrewery/fermenter");

}
}
Expand Down Expand Up @@ -262,11 +275,15 @@ private void loadGroupVelocityContextProperties(Notification notification, Group
}
}

private static void outputGroupNotification(Map.Entry<String, List<Notification>> entry, Properties groupProperties, String groupName) throws IOException {
private void outputGroupNotification(Map.Entry<String, List<Notification>> entry, Properties groupProperties, String groupName) throws IOException {
String templateName = "templates/notifications/group-" + groupName + ".vm";
Set<String> groupItems = new HashSet<>();
for (Notification notification : entry.getValue()) {
groupItems.add(notification.getNotificationAsString());
if(displayMessageKeys) {
logger.warn("Message Key: " + notification.getKey() + "\n" + notification.getNotificationAsString());
} else {
logger.warn(notification.getNotificationAsString());
}
}

VelocityNotification groupNotification = new VelocityNotification(groupName, groupName, groupItems, templateName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ File getGroupVelocityContenctPropertiesFile(File parentFile) {
*/
@Override
public String getNotificationAsString() {
String notificationString;
String notificationString = "";
if (StringUtils.isBlank(velocityTemplate)) {
throw new GenerationException("Template location MUST be provided!");
}
Expand All @@ -159,7 +159,7 @@ public String getNotificationAsString() {
try (Writer writer = new StringWriter()) {
template.merge(context, writer);

notificationString = writer.toString();
notificationString += writer.toString();

} catch (IOException e) {
throw new GenerationException("Could not process notification of manual action!", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.technologybrewery.fermenter.mda.generator;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.project.MavenProject;
import org.technologybrewery.fermenter.mda.notification.Notification;
import org.technologybrewery.fermenter.mda.notification.NotificationCollector;
import org.technologybrewery.fermenter.mda.notification.NotificationService;
import org.technologybrewery.fermenter.mda.notification.VelocityNotification;

import javax.inject.Inject;
import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class TestSuppressedNotificationGenerator extends TestGenerator {

private List<String> suppressedMessages;

private NotificationService notificationService;

public TestSuppressedNotificationGenerator(List<String> messages, MavenSession session) {
super(1, false);
suppressedMessages = messages;
notificationService = new NotificationService(session);
}

@Override
public void generate(GenerationContext context) {
MavenProject project = new MavenProject();
project.setFile(new File("src/test/resources/plugin-testing-harness-pom-files/", "suppress-messages"));
Notification notification = new VelocityNotification("test-message-id", new HashSet<>(), "templates/notifications/sample.notification.vm");
NotificationCollector.addNotification("test-message-id", notification);
notificationService.recordNotifications(project, suppressedMessages);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.junit.Assert;
import org.technologybrewery.fermenter.mda.GenerateSourcesMojo;
import org.technologybrewery.fermenter.mda.MojoTestCaseWrapper;
import org.technologybrewery.fermenter.mda.generator.AbstractGenerator;
import org.technologybrewery.fermenter.mda.generator.GenerationContext;
import org.technologybrewery.fermenter.mda.generator.TestGenerator;
import org.technologybrewery.fermenter.mda.generator.TestMultipleNotificationGenerator;
import org.technologybrewery.fermenter.mda.generator.TestSpecificNotificationGenerator;
import org.technologybrewery.fermenter.mda.generator.*;
import org.technologybrewery.fermenter.mda.reporting.StatisticsService;

import java.io.File;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -29,18 +27,18 @@ public class CreateNotificationSteps {

public static final String APPLE_RECORDS = "apple records";
private List<TestGenerator> generators;
private NotificationService notificationService;
private StatisticsService statisticsService;
private VelocityEngine engine;
private MojoTestCaseWrapper testCase;
protected File mavenProjectBaseDir;
protected GenerateSourcesMojo generateSourcesMojo;

@Before("@createNotifications")
public void setup() throws Exception {
generators = new ArrayList<>();
testCase = new MojoTestCaseWrapper();
testCase.configurePluginTestHarness();
MavenSession session = testCase.newMavenSession();
notificationService = new NotificationService(session);
statisticsService = new StatisticsService(session);

engine = new VelocityEngine();
Expand Down Expand Up @@ -78,6 +76,13 @@ public void a_configuration_that_triggers_a_notification_for_manual_action_with_
generators.add(generator);
}

@Given("^a notification key to suppress$")
public void a_notification_key_to_suppress() throws Throwable {
MavenSession session = testCase.newMavenSession();
TestSuppressedNotificationGenerator generator = new TestSuppressedNotificationGenerator(List.of("test-message-id"), session);
generators.add(generator);
}

@When("^the MDA plugin runs$")
public void the_MDA_plugin_runs() throws Throwable {
GenerationContext context = new GenerationContext();
Expand Down Expand Up @@ -123,6 +128,13 @@ public void the_resulting_message_contains_the_and_programmatic_value(String exp

}

@Then("^the notification indicated by the key is not shown")
public void the_notification_indicated_by_the_key_is_not_shown() throws Throwable {
Map<String, Map<String, Notification>> notificationsByFilenameMap = getNotificationByFilename();
notificationsByFilenameMap.values();
Assert.assertTrue(notificationsByFilenameMap.isEmpty());
}

private static Map<String, Map<String, Notification>> getNotificationByFilename() {
Map<String, Map<String, Notification>> notificationsByFilenameMap = NotificationCollector.getNotifications();
Assert.assertNotNull("Notifications map was not found!", notificationsByFilenameMap);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<name>Model-Driven Architecture Code Generator::Generate Java Project with Default Configurations Test</name>
<groupId>org.technologybrewery.fermenter</groupId>
<artifactId>java-default-config</artifactId>
<version>1.0.0</version>

<build>
<plugins>
<plugin>
<groupId>org.technologybrewery.fermenter</groupId>
<artifactId>fermenter-mda</artifactId>
<version>${project.version}</version>
<configuration>
<basePackage>org.technologybrewery.fermenter</basePackage>
<profile>foo</profile>
<suppressedMessages>
<message>test-message-id</message>
</suppressedMessages>
</configuration>
<executions>
<execution>
<id>test-generate</id>
<phase>validate</phase>
<goals>
<goal>generate-sources</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ Feature: Support the ability to create notifications that can be output at the e
| girl | lennon |
| we-can-work-it-out | lennon, mccartney |

Scenario: Display unique IDs for notifications
Given a notification key to suppress
When the MDA plugin runs
Then the notification indicated by the key is not shown

@manual
Scenario Outline: when multiple notification with the same key are read across modules, only one is emitted
Given a configuration that triggers multiple notifications with the "<key>" in different modules
Expand Down

0 comments on commit 1df191b

Please sign in to comment.