Skip to content
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

Port : Support tagging of notification rules #918

Merged
merged 3 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 18 additions & 4 deletions src/main/java/org/dependencytrack/model/NotificationRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import org.apache.commons.collections4.CollectionUtils;
import org.dependencytrack.notification.NotificationGroup;
import org.dependencytrack.notification.NotificationScope;
Expand All @@ -40,10 +44,6 @@
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.Unique;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -116,6 +116,12 @@ public class NotificationRule implements Serializable {
@Order(extensions = @Extension(vendorName = "datanucleus", key = "list-ordering", value = "name ASC, version ASC"))
private List<Project> projects;

@Persistent(table = "NOTIFICATIONRULE_TAGS", defaultFetchGroup = "true", mappedBy = "notificationRules")
@Join(column = "NOTIFICATIONRULE_ID")
@Element(column = "TAG_ID")
@Order(extensions = @Extension(vendorName = "datanucleus", key = "list-ordering", value = "name ASC"))
private List<Tag> tags;

@Persistent(table = "NOTIFICATIONRULE_TEAMS", defaultFetchGroup = "true")
@Join(column = "NOTIFICATIONRULE_ID")
@Element(column = "TEAM_ID")
Expand Down Expand Up @@ -215,6 +221,14 @@ public void setProjects(List<Project> projects) {
this.projects = projects;
}

public List<Tag> getTags() {
return tags;
}

public void setTags(final List<Tag> tags) {
this.tags = tags;
}

public List<Team> getTeams() {
return teams;
}
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/dependencytrack/model/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@
@Order(extensions = @Extension(vendorName = "datanucleus", key = "list-ordering", value = "vulnId ASC"))
private List<Vulnerability> vulnerabilities;

@Persistent
@JsonIgnore
@Order(extensions = @Extension(vendorName = "datanucleus", key = "list-ordering", value = "name ASC"))
private List<NotificationRule> notificationRules;

Check notice on line 91 in src/main/java/org/dependencytrack/model/Tag.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main/java/org/dependencytrack/model/Tag.java#L91

Fields should be declared at the top of the class, before any method declarations, constructors, initializers or inner classes.

public long getId() {
return id;
}
Expand Down Expand Up @@ -125,6 +130,14 @@
this.vulnerabilities = vulnerabilities;
}

public List<NotificationRule> getNotificationRules() {
return notificationRules;
}

public void setNotificationRules(final List<NotificationRule> notificationRules) {
this.notificationRules = notificationRules;
}

@Override
public boolean equals(Object object) {
if (object instanceof Tag) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@
import org.dependencytrack.model.NotificationPublisher;
import org.dependencytrack.model.NotificationRule;
import org.dependencytrack.model.Project;
import org.dependencytrack.model.Tag;
import org.dependencytrack.notification.NotificationScope;
import org.dependencytrack.notification.publisher.PublisherClass;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static org.dependencytrack.util.PersistenceUtil.assertPersistent;
import static org.dependencytrack.util.PersistenceUtil.assertPersistentAll;

public class NotificationQueryManager extends QueryManager implements IQueryManager {


Expand Down Expand Up @@ -61,15 +67,17 @@ public class NotificationQueryManager extends QueryManager implements IQueryMana
* @return a new NotificationRule
*/
public NotificationRule createNotificationRule(String name, NotificationScope scope, NotificationLevel level, NotificationPublisher publisher) {
final NotificationRule rule = new NotificationRule();
rule.setName(name);
rule.setScope(scope);
rule.setNotificationLevel(level);
rule.setPublisher(publisher);
rule.setEnabled(true);
rule.setNotifyChildren(true);
rule.setLogSuccessfulPublish(false);
return persist(rule);
return callInTransaction(() -> {
final NotificationRule rule = new NotificationRule();
rule.setName(name);
rule.setScope(scope);
rule.setNotificationLevel(level);
rule.setPublisher(publisher);
rule.setEnabled(true);
rule.setNotifyChildren(true);
rule.setLogSuccessfulPublish(false);
return persist(rule);
});
}

/**
Expand All @@ -78,15 +86,18 @@ public NotificationRule createNotificationRule(String name, NotificationScope sc
* @return a NotificationRule
*/
public NotificationRule updateNotificationRule(NotificationRule transientRule) {
final NotificationRule rule = getObjectByUuid(NotificationRule.class, transientRule.getUuid());
rule.setName(transientRule.getName());
rule.setEnabled(transientRule.isEnabled());
rule.setNotifyChildren(transientRule.isNotifyChildren());
rule.setLogSuccessfulPublish(transientRule.isLogSuccessfulPublish());
rule.setNotificationLevel(transientRule.getNotificationLevel());
rule.setPublisherConfig(transientRule.getPublisherConfig());
rule.setNotifyOn(transientRule.getNotifyOn());
return persist(rule);
return callInTransaction(() -> {
final NotificationRule rule = getObjectByUuid(NotificationRule.class, transientRule.getUuid());
rule.setName(transientRule.getName());
rule.setEnabled(transientRule.isEnabled());
rule.setNotifyChildren(transientRule.isNotifyChildren());
rule.setLogSuccessfulPublish(transientRule.isLogSuccessfulPublish());
rule.setNotificationLevel(transientRule.getNotificationLevel());
rule.setPublisherConfig(transientRule.getPublisherConfig());
rule.setNotifyOn(transientRule.getNotifyOn());
bind(rule, resolveTags(transientRule.getTags()));
return persist(rule);
});
}

/**
Expand Down Expand Up @@ -244,4 +255,39 @@ public void deleteNotificationPublisher(final NotificationPublisher notification
query.deletePersistentAll(notificationPublisher.getUuid());
delete(notificationPublisher);
}

/**
* @since 4.12.0
*/
@Override
public boolean bind(final NotificationRule notificationRule, final Collection<Tag> tags) {
assertPersistent(notificationRule, "notificationRule must be persistent");
assertPersistentAll(tags, "tags must be persistent");

return callInTransaction(() -> {
boolean modified = false;

for (final Tag existingTag : notificationRule.getTags()) {
if (!tags.contains(existingTag)) {
notificationRule.getTags().remove(existingTag);
existingTag.getNotificationRules().remove(notificationRule);
modified = true;
}
}
for (final Tag tag : tags) {
if (!notificationRule.getTags().contains(tag)) {
notificationRule.getTags().add(tag);

if (tag.getNotificationRules() == null) {
tag.setNotificationRules(new ArrayList<>(List.of(notificationRule)));
} else if (!tag.getNotificationRules().contains(notificationRule)) {
tag.getNotificationRules().add(notificationRule);
}

modified = true;
}
}
return modified;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,15 @@ public boolean bind(final Policy policy, final Collection<Tag> tags) {
assertPersistentAll(tags, "tags must be persistent");
return callInTransaction(() -> {
boolean modified = false;

for (final Tag existingTag : policy.getTags()) {
if (!tags.contains(existingTag)) {
policy.getTags().remove(existingTag);
existingTag.getPolicies().remove(policy);
modified = true;
}
}

for (final Tag tag : tags) {
if (!policy.getTags().contains(tag)) {
policy.getTags().add(tag);
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/dependencytrack/persistence/QueryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,10 @@ public Tag createTag(final String name) {
return getTagQueryManager().createTag(name);
}

public List<Tag> createTags(final List<String> names) {
return getTagQueryManager().createTags(names);
}

public Project createProject(String name, String description, String version, List<Tag> tags, Project parent, PackageURL purl, boolean active, boolean commitIndex) {
return getProjectQueryManager().createProject(name, description, version, tags, parent, purl, active, commitIndex);
}
Expand Down Expand Up @@ -1418,6 +1422,10 @@ public boolean bind(final Policy policy, final Collection<Tag> tags) {
return getPolicyQueryManager().bind(policy, tags);
}

public boolean bind(final NotificationRule notificationRule, final Collection<Tag> tags) {
return getNotificationQueryManager().bind(notificationRule, tags);
}

public boolean hasAccessManagementPermission(final Object principal) {
if (principal instanceof final UserPrincipal userPrincipal) {
return hasAccessManagementPermission(userPrincipal);
Expand Down Expand Up @@ -1472,6 +1480,18 @@ public PaginatedResult getTagsForPolicy(String policyUuid) {
return getTagQueryManager().getTagsForPolicy(policyUuid);
}

public List<TagQueryManager.TaggedNotificationRuleRow> getTaggedNotificationRules(final String tagName) {
return getTagQueryManager().getTaggedNotificationRules(tagName);
}

public void tagNotificationRules(final String tagName, final Collection<String> notificationRuleUuids) {
getTagQueryManager().tagNotificationRules(tagName, notificationRuleUuids);
}

public void untagNotificationRules(final String tagName, final Collection<String> notificationRuleUuids) {
getTagQueryManager().untagNotificationRules(tagName, notificationRuleUuids);
}

/**
* Fetch multiple objects from the data store by their ID.
*
Expand Down
Loading
Loading