Skip to content

Commit

Permalink
Migrate project active flag to date type inactiveSince (#1611)
Browse files Browse the repository at this point in the history
  • Loading branch information
sahibamittal authored Jan 3, 2025
1 parent 45ba817 commit 1ef2870
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ public class Project implements Serializable {
@Column(name = "LAST_RISKSCORE")
private Double lastInheritedRiskScore;

@Column(name = "ACTIVE")
private boolean active;
@Column(name = "INACTIVE_SINCE")
private Date inactiveSince;

public long getId() {
return id;
Expand Down Expand Up @@ -241,11 +241,15 @@ public void setLastInheritedRiskScore(Double lastInheritedRiskScore) {
}

public boolean isActive() {
return active;
return inactiveSince == null;
}

public void setActive(boolean active) {
this.active = active;
public Date getInactiveSince() {
return inactiveSince;
}

public void setInactiveSince(Date inactiveSince) {
this.inactiveSince = inactiveSince;
}

public Collection<Project> getChildren() {
Expand Down
5 changes: 2 additions & 3 deletions commons-persistence/src/main/resources/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,7 @@ BEGIN
FROM (SELECT DISTINCT ON ("PM"."PROJECT_ID") *
FROM "PROJECTMETRICS" AS "PM"
INNER JOIN "PROJECT" AS "P" ON "P"."ID" = "PM"."PROJECT_ID"
WHERE "P"."ACTIVE" = TRUE -- Only consider active projects
OR "P"."ACTIVE" IS NULL -- ACTIVE is nullable, assume TRUE per default
WHERE "P"."INACTIVE_SINCE" IS NULL -- Only consider active projects
ORDER BY "PM"."PROJECT_ID", "PM"."LAST_OCCURRENCE" DESC) AS "LATEST_PROJECT_METRICS"
INTO
"v_projects",
Expand Down Expand Up @@ -1532,7 +1531,6 @@ ALTER TABLE public."PORTFOLIOMETRICS" ALTER COLUMN "ID" ADD GENERATED BY DEFAULT

CREATE TABLE public."PROJECT" (
"ID" bigint NOT NULL,
"ACTIVE" boolean DEFAULT true NOT NULL,
"CLASSIFIER" character varying(255),
"CPE" character varying(255),
"DESCRIPTION" character varying(255),
Expand All @@ -1553,6 +1551,7 @@ CREATE TABLE public."PROJECT" (
"MANUFACTURER" text,
"AUTHORS" text,
"IS_LATEST" boolean DEFAULT false NOT NULL,
"INACTIVE_SINCE" timestamp with time zone,
CONSTRAINT "PROJECT_CLASSIFIER_check" CHECK ((("CLASSIFIER" IS NULL) OR (("CLASSIFIER")::text = ANY (ARRAY['APPLICATION'::text, 'CONTAINER'::text, 'DEVICE'::text, 'FILE'::text, 'FIRMWARE'::text, 'FRAMEWORK'::text, 'LIBRARY'::text, 'OPERATING_SYSTEM'::text]))))
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.junit.jupiter.params.provider.CsvSource;
import org.mockito.Mockito;

import java.util.Date;
import java.util.List;
import java.util.UUID;

Expand Down Expand Up @@ -147,7 +148,7 @@ void testResolveRulesWithValidMatchingProjectLimitRule() throws Exception {
NotificationGroup.NEW_VULNERABILITY, publisherId);
// Creates a project which will later be matched on
final UUID projectUuid = UUID.randomUUID();
final Long projectId = createProject("Test Project", "1.0", true, projectUuid);
final Long projectId = createProject("Test Project", "1.0", null, projectUuid);
addProjectToRule(projectId, ruleId);
// Creates a new notification
final var notification = Notification.newBuilder()
Expand Down Expand Up @@ -185,7 +186,7 @@ void testResolveRulesWithValidNonMatchingProjectLimitRule() throws Exception {
NotificationGroup.NEW_VULNERABILITY, publisherId);
// Creates a project which will later be matched on
final UUID projectUuid = UUID.randomUUID();
final Long projectId = createProject("Test Project", "1.0", true, projectUuid);
final Long projectId = createProject("Test Project", "1.0", null, projectUuid);
addProjectToRule(projectId, ruleId);
// Creates a new notification
final var notification = Notification.newBuilder()
Expand Down Expand Up @@ -221,7 +222,7 @@ void testResolveRulesWithValidNonMatchingRule() throws Exception {
NotificationGroup.PROJECT_AUDIT_CHANGE, publisherId);
// Creates a project which will later be matched on
final UUID projectUuid = UUID.randomUUID();
final Long projectId = createProject("Test Project", "1.0", true, projectUuid);
final Long projectId = createProject("Test Project", "1.0", null, projectUuid);
addProjectToRule(projectId, ruleId);
// Creates a new notification
final var notification = Notification.newBuilder()
Expand Down Expand Up @@ -298,10 +299,10 @@ void testResolveRulesWithDisabledRule() throws Exception {
@TestTransaction
void testResolveRulesLimitedToProjectForNewVulnerabilityNotification() throws Exception {
final UUID projectUuidA = UUID.randomUUID();
final Long projectIdA = createProject("Project A", "1.0", true, projectUuidA);
final Long projectIdA = createProject("Project A", "1.0", null, projectUuidA);

final UUID projectUuidB = UUID.randomUUID();
createProject("Project B", "2.0", true, projectUuidB);
createProject("Project B", "2.0", null, projectUuidB);

final Long publisherId = createConsolePublisher();
final Long ruleId = createRule("Limit To Test Rule",
Expand Down Expand Up @@ -345,10 +346,10 @@ void testResolveRulesLimitedToProjectForNewVulnerabilityNotification() throws Ex
@TestTransaction
void testResolveRulesLimitedToProjectForNewVulnerableDependencyNotification() throws Exception {
final UUID projectUuidA = UUID.randomUUID();
final Long projectIdA = createProject("Project A", "1.0", true, projectUuidA);
final Long projectIdA = createProject("Project A", "1.0", null, projectUuidA);

final UUID projectUuidB = UUID.randomUUID();
createProject("Project B", "2.0", true, projectUuidB);
createProject("Project B", "2.0", null, projectUuidB);

final Long publisherId = createConsolePublisher();
final Long ruleId = createRule("Limit To Test Rule",
Expand Down Expand Up @@ -388,10 +389,10 @@ void testResolveRulesLimitedToProjectForNewVulnerableDependencyNotification() th
@TestTransaction
void testResolveRulesLimitedToProjectForBomConsumedOrProcessedNotification() throws Exception {
final UUID projectUuidA = UUID.randomUUID();
final Long projectIdA = createProject("Project A", "1.0", true, projectUuidA);
final Long projectIdA = createProject("Project A", "1.0", null, projectUuidA);

final UUID projectUuidB = UUID.randomUUID();
createProject("Project B", "2.0", true, projectUuidB);
createProject("Project B", "2.0", null, projectUuidB);

final Long publisherId = createConsolePublisher();
final Long ruleId = createRule("Limit To Test Rule",
Expand Down Expand Up @@ -427,10 +428,10 @@ void testResolveRulesLimitedToProjectForBomConsumedOrProcessedNotification() thr
@TestTransaction
void testResolveRulesLimitedToProjectForVexConsumedOrProcessedNotification() throws Exception {
final UUID projectUuidA = UUID.randomUUID();
final Long projectIdA = createProject("Project A", "1.0", true, projectUuidA);
final Long projectIdA = createProject("Project A", "1.0", null, projectUuidA);

final UUID projectUuidB = UUID.randomUUID();
createProject("Project B", "2.0", true, projectUuidB);
createProject("Project B", "2.0", null, projectUuidB);

final Long publisherId = createConsolePublisher();
final Long ruleId = createRule("Limit To Test Rule",
Expand Down Expand Up @@ -466,10 +467,10 @@ void testResolveRulesLimitedToProjectForVexConsumedOrProcessedNotification() thr
@TestTransaction
void testResolveRulesLimitedToProjectForPolicyViolationNotification() throws Exception {
final UUID projectUuidA = UUID.randomUUID();
final Long projectIdA = createProject("Project A", "1.0", true, projectUuidA);
final Long projectIdA = createProject("Project A", "1.0", null, projectUuidA);

final UUID projectUuidB = UUID.randomUUID();
createProject("Project B", "2.0", true, projectUuidB);
createProject("Project B", "2.0", null, projectUuidB);

final Long publisherId = createConsolePublisher();
final Long ruleId = createRule("Limit To Test Rule",
Expand Down Expand Up @@ -505,10 +506,10 @@ void testResolveRulesLimitedToProjectForPolicyViolationNotification() throws Exc
@TestTransaction
void testResolveRulesLimitedToProjectForVulnerabilityAnalysisDecisionChangeNotification() throws Exception {
final UUID projectUuidA = UUID.randomUUID();
final Long projectIdA = createProject("Project A", "1.0", true, projectUuidA);
final Long projectIdA = createProject("Project A", "1.0", null, projectUuidA);

final UUID projectUuidB = UUID.randomUUID();
createProject("Project B", "2.0", true, projectUuidB);
createProject("Project B", "2.0", null, projectUuidB);

final Long publisherId = createConsolePublisher();
final Long ruleId = createRule("Limit To Test Rule",
Expand Down Expand Up @@ -544,10 +545,10 @@ void testResolveRulesLimitedToProjectForVulnerabilityAnalysisDecisionChangeNotif
@TestTransaction
void testResolveRulesLimitedToProjectForPolicyViolationAnalysisDecisionChangeNotification() throws Exception {
final UUID projectUuidA = UUID.randomUUID();
final Long projectIdA = createProject("Project A", "1.0", true, projectUuidA);
final Long projectIdA = createProject("Project A", "1.0", null, projectUuidA);

final UUID projectUuidB = UUID.randomUUID();
createProject("Project B", "2.0", true, projectUuidB);
createProject("Project B", "2.0", null, projectUuidB);

final Long publisherId = createConsolePublisher();
final Long ruleId = createRule("Limit To Test Rule",
Expand Down Expand Up @@ -583,10 +584,10 @@ void testResolveRulesLimitedToProjectForPolicyViolationAnalysisDecisionChangeNot
@TestTransaction
void testResolveRulesLimitedToProjectForBomProcessingFailedNotification() throws Exception {
final UUID projectUuidA = UUID.randomUUID();
final Long projectIdA = createProject("Project A", "1.0", true, projectUuidA);
final Long projectIdA = createProject("Project A", "1.0", null, projectUuidA);

final UUID projectUuidB = UUID.randomUUID();
createProject("Project B", "2.0", true, projectUuidB);
createProject("Project B", "2.0", null, projectUuidB);

final Long publisherId = createConsolePublisher();
final Long ruleId = createRule("Limit To Test Rule",
Expand Down Expand Up @@ -622,10 +623,10 @@ void testResolveRulesLimitedToProjectForBomProcessingFailedNotification() throws
@TestTransaction
void testResolveRulesLimitedToProjectForBomValidationFailedNotification() throws Exception {
final UUID projectUuidA = UUID.randomUUID();
final Long projectIdA = createProject("Project A", "1.0", true, projectUuidA);
final Long projectIdA = createProject("Project A", "1.0", null, projectUuidA);

final UUID projectUuidB = UUID.randomUUID();
createProject("Project B", "2.0", true, projectUuidB);
createProject("Project B", "2.0", null, projectUuidB);

final Long publisherId = createConsolePublisher();
final Long ruleId = createRule("Limit To Test Rule",
Expand Down Expand Up @@ -668,15 +669,15 @@ void testResolveRulesWithAffectedChild() throws Exception {
setNotifyChildren(ruleId, true);
// Creates a project which will later be matched on
final UUID grandParentUuid = UUID.randomUUID();
final Long grandParentProjectId = createProject("Test Project Grandparent", "1.0", true, grandParentUuid);
final Long grandParentProjectId = createProject("Test Project Grandparent", "1.0", null, grandParentUuid);
final UUID parentUuid = UUID.randomUUID();
final Long parentProjectId = createProject("Test Project Parent", "1.0", true, parentUuid);
final Long parentProjectId = createProject("Test Project Parent", "1.0", null, parentUuid);
setProjectParent(parentProjectId, grandParentProjectId);
final UUID childUuid = UUID.randomUUID();
final Long childProjectId = createProject("Test Project Child", "1.0", true, childUuid);
final Long childProjectId = createProject("Test Project Child", "1.0", null, childUuid);
setProjectParent(childProjectId, parentProjectId);
final UUID grandChildUuid = UUID.randomUUID();
final Long grandChildProjectId = createProject("Test Project Grandchild", "1.0", true, grandChildUuid);
final Long grandChildProjectId = createProject("Test Project Grandchild", "1.0", null, grandChildUuid);
setProjectParent(grandChildProjectId, childProjectId);
addProjectToRule(grandParentProjectId, ruleId);
// Creates a new notification
Expand Down Expand Up @@ -716,15 +717,15 @@ void testResolveRulesWithAffectedChildAndNotifyChildrenDisabled() throws Excepti
setNotifyChildren(ruleId, false);
// Creates a project which will later be matched on
final UUID grandParentUuid = UUID.randomUUID();
final Long grandParentProjectId = createProject("Test Project Grandparent", "1.0", true, grandParentUuid);
final Long grandParentProjectId = createProject("Test Project Grandparent", "1.0", null, grandParentUuid);
final UUID parentUuid = UUID.randomUUID();
final Long parentProjectId = createProject("Test Project Parent", "1.0", true, parentUuid);
final Long parentProjectId = createProject("Test Project Parent", "1.0", null, parentUuid);
setProjectParent(parentProjectId, grandParentProjectId);
final UUID childUuid = UUID.randomUUID();
final Long childProjectId = createProject("Test Project Child", "1.0", true, childUuid);
final Long childProjectId = createProject("Test Project Child", "1.0", null, childUuid);
setProjectParent(childProjectId, parentProjectId);
final UUID grandChildUuid = UUID.randomUUID();
final Long grandChildProjectId = createProject("Test Project Grandchild", "1.0", true, grandChildUuid);
final Long grandChildProjectId = createProject("Test Project Grandchild", "1.0", null, grandChildUuid);
setProjectParent(grandChildProjectId, childProjectId);
addProjectToRule(grandParentProjectId, ruleId);
// Creates a new notification
Expand Down Expand Up @@ -762,15 +763,15 @@ void testResolveRulesWithAffectedChildAndInactiveChild() throws Exception {
setNotifyChildren(ruleId, true);
// Creates a project which will later be matched on
final UUID grandParentUuid = UUID.randomUUID();
final Long grandParentProjectId = createProject("Test Project Grandparent", "1.0", true, grandParentUuid);
final Long grandParentProjectId = createProject("Test Project Grandparent", "1.0", null, grandParentUuid);
final UUID parentUuid = UUID.randomUUID();
final Long parentProjectId = createProject("Test Project Parent", "1.0", true, parentUuid);
final Long parentProjectId = createProject("Test Project Parent", "1.0", null, parentUuid);
setProjectParent(parentProjectId, grandParentProjectId);
final UUID childUuid = UUID.randomUUID();
final Long childProjectId = createProject("Test Project Child", "1.0", true, childUuid);
final Long childProjectId = createProject("Test Project Child", "1.0", null, childUuid);
setProjectParent(childProjectId, parentProjectId);
final UUID grandChildUuid = UUID.randomUUID();
final Long grandChildProjectId = createProject("Test Project Grandchild", "1.0", false, grandChildUuid);
final Long grandChildProjectId = createProject("Test Project Grandchild", "1.0", new Date(), grandChildUuid);
setProjectParent(grandChildProjectId, childProjectId);
addProjectToRule(grandParentProjectId, ruleId);
// Creates a new notification
Expand Down Expand Up @@ -807,7 +808,7 @@ void testInformWithValidMatchingRule() throws Exception {
NotificationGroup.NEW_VULNERABILITY, publisherId);
// Creates a project which will later be matched on
final UUID projectUuid = UUID.randomUUID();
createProject("Test Project", "1.0", true, projectUuid);
createProject("Test Project", "1.0", null, projectUuid);
// Creates a new notification
final var notification = Notification.newBuilder()
.setScope(SCOPE_PORTFOLIO)
Expand Down Expand Up @@ -860,7 +861,7 @@ void testResolveRulesUserCreatedNotification() throws Exception {
@TestTransaction
void testResolveRulesLimitedToProjectTag() throws Exception {
final UUID projectUuidA = UUID.randomUUID();
createProject("Project A", "1.0", true, projectUuidA);
createProject("Project A", "1.0", null, projectUuidA);

final Long tagId = createTag("test-tag");

Expand Down Expand Up @@ -899,7 +900,7 @@ void testResolveRulesWithValidNonMatchingTagLimitRule() throws Exception {
NotificationGroup.NEW_VULNERABILITY, publisherId);

final UUID projectUuid = UUID.randomUUID();
createProject("Test Project", "1.0", true, projectUuid);
createProject("Test Project", "1.0", null, projectUuid);

final Long tagId = createTag("test-tag");
addTagToRule(tagId, ruleId);
Expand Down Expand Up @@ -964,15 +965,15 @@ private void disableRule(final Long ruleId) {
.executeUpdate();
}

private Long createProject(final String name, final String version, final Boolean active, final UUID uuid) {
private Long createProject(final String name, final String version, final Date inactiveSince, final UUID uuid) {
return (Long) entityManager.createNativeQuery("""
INSERT INTO "PROJECT" ("NAME", "VERSION", "ACTIVE", "UUID") VALUES
(:name, :version, :active, :uuid)
INSERT INTO "PROJECT" ("NAME", "VERSION", "INACTIVE_SINCE", "UUID") VALUES
(:name, :version, :inactiveSince, :uuid)
RETURNING "ID";
""")
.setParameter("name", name)
.setParameter("version", version)
.setParameter("active", active != null ? active.booleanValue() : null)
.setParameter("inactiveSince", inactiveSince)
.setParameter("uuid", uuid)
.getSingleResult();
}
Expand Down

0 comments on commit 1ef2870

Please sign in to comment.