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 : Fix REST endpoints for adding tags #1042

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -257,21 +257,23 @@ public void deleteNotificationPublisher(final NotificationPublisher notification
}

/**
* @since 4.12.0
* @since 4.12.3
*/
@Override
public boolean bind(final NotificationRule notificationRule, final Collection<Tag> tags) {
public boolean bind(final NotificationRule notificationRule, final Collection<Tag> tags, final boolean keepExisting) {
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;
if (!keepExisting) {
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) {
Expand All @@ -290,4 +292,12 @@ public boolean bind(final NotificationRule notificationRule, final Collection<Ta
return modified;
});
}

/**
* @since 4.12.0
*/
@Override
public boolean bind(final NotificationRule notificationRule, final Collection<Tag> tags) {
return bind(notificationRule, tags, /* keepExisting */ false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -693,20 +693,22 @@ public long getAuditedCount(final Component component, final PolicyViolation.Typ
}

/**
* @since 4.12.0
* @since 4.12.3
*/
@Override
public boolean bind(final Policy policy, final Collection<Tag> tags) {
public boolean bind(final Policy policy, final Collection<Tag> tags, final boolean keepExisting) {
assertPersistent(policy, "policy must be persistent");
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;
if (!keepExisting) {
for (final Tag existingTag : policy.getTags()) {
if (!tags.contains(existingTag)) {
policy.getTags().remove(existingTag);
existingTag.getPolicies().remove(policy);
modified = true;
}
}
}

Expand All @@ -725,6 +727,14 @@ public boolean bind(final Policy policy, final Collection<Tag> tags) {
});
}

/**
* @since 4.12.0
*/
@Override
public boolean bind(final Policy policy, final Collection<Tag> tags) {
return bind(policy, tags, /* keepExisting */ false);
}

private void processViolationsFilters(Map<String, String> filters, Map<String, Object> params, List<String> filterCriteria) {
for (Map.Entry<String, String> filter : filters.entrySet()) {
switch (filter.getKey()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import java.io.IOException;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -70,6 +71,9 @@
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;

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

final class ProjectQueryManager extends QueryManager implements IQueryManager {

private static final Logger LOGGER = Logger.getLogger(ProjectQueryManager.class);
Expand Down Expand Up @@ -921,33 +925,52 @@ public List<ProjectProperty> getProjectProperties(final Project project) {
}

/**
* Binds the two objects together in a corresponding join table.
*
* @param project a Project object
* @param tags a List of Tag objects
* @since 4.12.3
*/
@Override
public void bind(Project project, List<Tag> tags) {
runInTransaction(() -> {
final Query<Tag> query = pm.newQuery(Tag.class, "projects.contains(:project)");
query.setParameters(project);
final List<Tag> currentProjectTags = executeAndCloseList(query);
public boolean bind(final Project project, final Collection<Tag> tags, final boolean keepExisting) {
assertPersistent(project, "project must be persistent");
assertPersistentAll(tags, "tags must be persistent");

for (final Tag tag : currentProjectTags) {
if (!tags.contains(tag)) {
tag.getProjects().remove(project);
return callInTransaction(() -> {
boolean modified = false;

if (!keepExisting) {
for (final Tag existingTag : project.getTags()) {
if (!tags.contains(existingTag)) {
project.getTags().remove(existingTag);
existingTag.getProjects().remove(project);
modified = true;
}
}
}
project.setTags(tags);
for (final Tag tag : tags) {
final List<Project> projects = tag.getProjects();
if (!projects.contains(project)) {
projects.add(project);
if (!project.getTags().contains(tag)) {
project.getTags().add(tag);

if (tag.getProjects() == null) {
tag.setProjects(new ArrayList<>(List.of(project)));
} else if (!tag.getProjects().contains(project)) {
tag.getProjects().add(project);
}

modified = true;
}
}
return modified;
});
}

/**
* Binds the two objects together in a corresponding join table.
* @param project a Project object
* @param tags a List of Tag objects
*/
@Override
public void bind(final Project project, final List<Tag> tags) {
bind(project, tags, /* keepExisting */ false);
}

/**
* Updates the last time a bom was imported.
*
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/dependencytrack/persistence/QueryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -1411,14 +1411,26 @@ public boolean isEnabled(final ConfigPropertyConstants configPropertyConstants)
return false;
}

public boolean bind(final Project project, final Collection<Tag> tags, final boolean keepExisting) {
return getProjectQueryManager().bind(project, tags, keepExisting);
}

public void bind(Project project, List<Tag> tags) {
getProjectQueryManager().bind(project, tags);
}

public boolean bind(final Policy policy, final Collection<Tag> tags, final boolean keepExisting) {
return getPolicyQueryManager().bind(policy, tags, keepExisting);
}

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, final boolean keepExisting) {
return getNotificationQueryManager().bind(notificationRule, tags, keepExisting);
}

public boolean bind(final NotificationRule notificationRule, final Collection<Tag> tags) {
return getNotificationQueryManager().bind(notificationRule, tags);
}
Expand Down
13 changes: 3 additions & 10 deletions src/main/java/org/dependencytrack/persistence/TagQueryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -383,14 +383,7 @@ public void tagProjects(final String tagName, final Collection<String> projectUu
final List<Project> projects = executeAndCloseList(projectsQuery);

for (final Project project : projects) {
if (project.getTags() == null || project.getTags().isEmpty()) {
project.setTags(List.of(tag));
continue;
}

if (!project.getTags().contains(tag)) {
project.getTags().add(tag);
}
bind(project, List.of(tag), /* keepExisting */ true);
}
});
}
Expand Down Expand Up @@ -487,7 +480,7 @@ public void tagPolicies(final String tagName, final Collection<String> policyUui
final List<Policy> policies = executeAndCloseList(policiesQuery);

for (final Policy policy : policies) {
bind(policy, List.of(tag));
bind(policy, List.of(tag), /* keepExisting */ true);
}
});
}
Expand Down Expand Up @@ -695,7 +688,7 @@ public void tagNotificationRules(final String tagName, final Collection<String>
final List<NotificationRule> notificationRules = executeAndCloseList(notificationRulesQuery);

for (final NotificationRule notificationRule : notificationRules) {
bind(notificationRule, List.of(tag));
bind(notificationRule, List.of(tag), /* keepExisting */ true);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -661,15 +661,26 @@ public void tagProjectsTest() {

qm.createTag("foo");

final var projectC = new Project();
projectC.setName("acme-app-c");
qm.persist(projectC);

qm.bind(projectC, List.of(qm.createTag("bar")));

final Response response = jersey.target(V1_TAG + "/foo/project")
.request()
.header(X_API_KEY, apiKey)
.post(Entity.json(List.of(projectA.getUuid(), projectB.getUuid())));
.post(Entity.json(List.of(projectA.getUuid(), projectB.getUuid(), projectC.getUuid())));
assertThat(response.getStatus()).isEqualTo(204);

qm.getPersistenceManager().evictAll();
assertThat(projectA.getTags()).satisfiesExactly(projectTag -> assertThat(projectTag.getName()).isEqualTo("foo"));
assertThat(projectB.getTags()).satisfiesExactly(projectTag -> assertThat(projectTag.getName()).isEqualTo("foo"));
assertThat(projectA.getTags()).satisfiesExactly(
projectTag -> assertThat(projectTag.getName()).isEqualTo("foo"));
assertThat(projectB.getTags()).satisfiesExactly(
projectTag -> assertThat(projectTag.getName()).isEqualTo("foo"));
assertThat(projectC.getTags()).satisfiesExactlyInAnyOrder(
projectTag -> assertThat(projectTag.getName()).isEqualTo("foo"),
projectTag -> assertThat(projectTag.getName()).isEqualTo("bar"));
}

@Test
Expand Down Expand Up @@ -1071,15 +1082,28 @@ public void tagPoliciesTest() {

qm.createTag("foo");

final var policyC = new Policy();
policyC.setName("policy-c");
policyC.setOperator(Policy.Operator.ALL);
policyC.setViolationState(Policy.ViolationState.INFO);
qm.persist(policyC);

qm.bind(policyC, List.of(qm.createTag("bar")));

final Response response = jersey.target(V1_TAG + "/foo/policy")
.request()
.header(X_API_KEY, apiKey)
.post(Entity.json(List.of(policyA.getUuid(), policyB.getUuid())));
.post(Entity.json(List.of(policyA.getUuid(), policyB.getUuid(), policyC.getUuid())));
assertThat(response.getStatus()).isEqualTo(204);

qm.getPersistenceManager().evictAll();
assertThat(policyA.getTags()).satisfiesExactly(policyTag -> assertThat(policyTag.getName()).isEqualTo("foo"));
assertThat(policyB.getTags()).satisfiesExactly(policyTag -> assertThat(policyTag.getName()).isEqualTo("foo"));
assertThat(policyA.getTags()).satisfiesExactly(
policyTag -> assertThat(policyTag.getName()).isEqualTo("foo"));
assertThat(policyB.getTags()).satisfiesExactly(
policyTag -> assertThat(policyTag.getName()).isEqualTo("foo"));
assertThat(policyC.getTags()).satisfiesExactlyInAnyOrder(
policyTag -> assertThat(policyTag.getName()).isEqualTo("foo"),
policyTag -> assertThat(policyTag.getName()).isEqualTo("bar"));
}

@Test
Expand Down Expand Up @@ -1536,15 +1560,27 @@ public void tagNotificationRulesTest() {

qm.createTag("foo");

final var notificationRuleC = new NotificationRule();
notificationRuleC.setName("rule-c");
notificationRuleC.setScope(NotificationScope.PORTFOLIO);
qm.persist(notificationRuleC);

qm.bind(notificationRuleC, List.of(qm.createTag("bar")));

final Response response = jersey.target(V1_TAG + "/foo/notificationRule")
.request()
.header(X_API_KEY, apiKey)
.post(Entity.json(List.of(notificationRuleA.getUuid(), notificationRuleB.getUuid())));
.post(Entity.json(List.of(notificationRuleA.getUuid(), notificationRuleB.getUuid(), notificationRuleC.getUuid())));
assertThat(response.getStatus()).isEqualTo(204);

qm.getPersistenceManager().evictAll();
assertThat(notificationRuleA.getTags()).satisfiesExactly(ruleTag -> assertThat(ruleTag.getName()).isEqualTo("foo"));
assertThat(notificationRuleB.getTags()).satisfiesExactly(ruleTag -> assertThat(ruleTag.getName()).isEqualTo("foo"));
assertThat(notificationRuleA.getTags()).satisfiesExactly(
ruleTag -> assertThat(ruleTag.getName()).isEqualTo("foo"));
assertThat(notificationRuleB.getTags()).satisfiesExactly(
ruleTag -> assertThat(ruleTag.getName()).isEqualTo("foo"));
assertThat(notificationRuleC.getTags()).satisfiesExactlyInAnyOrder(
ruleTag -> assertThat(ruleTag.getName()).isEqualTo("foo"),
ruleTag -> assertThat(ruleTag.getName()).isEqualTo("bar"));
}

@Test
Expand Down
Loading