diff --git a/cmd/server/app/history_purge_test.go b/cmd/server/app/history_purge_test.go index d0990adde3..11b40a2fc6 100644 --- a/cmd/server/app/history_purge_test.go +++ b/cmd/server/app/history_purge_test.go @@ -41,9 +41,12 @@ func TestRecordSize(t *testing.T) { db.ListEvaluationHistoryStaleRecordsRow{ ID: uuid.Nil, EvaluationTime: time.Now(), - EntityType: int32(1), - EntityID: uuid.Nil, - RuleID: uuid.Nil, + EntityType: db.NullEntities{ + Entities: db.EntitiesRepository, + Valid: true, + }, + EntityID: uuid.Nil, + RuleID: uuid.Nil, }, ) @@ -76,8 +79,11 @@ func TestPurgeLoop(t *testing.T) { EvaluationTime: time.Now(), ID: uuid1, RuleID: ruleID1, - EntityType: int32(1), - EntityID: entityID1, + EntityType: db.NullEntities{ + Entities: db.EntitiesRepository, + Valid: true, + }, + EntityID: entityID1, }, ), withTransactionStuff(), @@ -104,8 +110,11 @@ func TestPurgeLoop(t *testing.T) { EvaluationTime: time.Now(), ID: uuid1, RuleID: ruleID1, - EntityType: int32(1), - EntityID: entityID1, + EntityType: db.NullEntities{ + Entities: db.EntitiesRepository, + Valid: true, + }, + EntityID: entityID1, }, ), ), @@ -126,22 +135,31 @@ func TestPurgeLoop(t *testing.T) { EvaluationTime: time.Now(), ID: uuid1, RuleID: ruleID1, - EntityType: int32(1), - EntityID: entityID1, + EntityType: db.NullEntities{ + Entities: db.EntitiesRepository, + Valid: true, + }, + EntityID: entityID1, }, db.ListEvaluationHistoryStaleRecordsRow{ EvaluationTime: time.Now(), ID: uuid2, RuleID: ruleID2, - EntityType: int32(1), - EntityID: entityID2, + EntityType: db.NullEntities{ + Entities: db.EntitiesRepository, + Valid: true, + }, + EntityID: entityID2, }, db.ListEvaluationHistoryStaleRecordsRow{ EvaluationTime: time.Now(), ID: uuid3, RuleID: ruleID3, - EntityType: int32(1), - EntityID: entityID3, + EntityType: db.NullEntities{ + Entities: db.EntitiesRepository, + Valid: true, + }, + EntityID: entityID3, }, ), withTransactionStuff(), @@ -201,8 +219,11 @@ func TestPurgeLoop(t *testing.T) { EvaluationTime: time.Now(), ID: uuid1, RuleID: ruleID1, - EntityType: int32(1), - EntityID: entityID1, + EntityType: db.NullEntities{ + Entities: db.EntitiesRepository, + Valid: true, + }, + EntityID: entityID1, }, ), withTransactionStuff(), @@ -434,14 +455,17 @@ var ( ruleID3 = uuid.MustParse("00000000-0000-0000-0000-000000000333") evaluatedAt1 = time.Now() evaluatedAt2 = evaluatedAt1.Add(-1 * time.Hour) - entityType = int32(1) + entityType = db.NullEntities{ + Entities: db.EntitiesRepository, + Valid: true, + } ) //nolint:unparam func makeHistoryRow( id uuid.UUID, evaluatedAt time.Time, - entityType int32, + entityType db.NullEntities, entityID uuid.UUID, ruleID uuid.UUID, ) db.ListEvaluationHistoryStaleRecordsRow { diff --git a/database/migrations/000102_remove_entities_tables.down.sql b/database/migrations/000102_remove_entities_tables.down.sql new file mode 100644 index 0000000000..e69de29bb2 diff --git a/database/migrations/000102_remove_entities_tables.up.sql b/database/migrations/000102_remove_entities_tables.up.sql new file mode 100644 index 0000000000..0aafe5df04 --- /dev/null +++ b/database/migrations/000102_remove_entities_tables.up.sql @@ -0,0 +1,91 @@ +-- Copyright 2024 Stacklok, Inc +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. + +BEGIN; + +-- remove the repositories, artifacts and pull_request tables + +DROP TABLE IF EXISTS repositories; +DROP TABLE IF EXISTS artifacts; +DROP TABLE IF EXISTS pull_requests; + +CREATE VIEW repositories AS +SELECT + ei.id, + ei.project_id, + pr.name AS provider, + ei.provider_id, + (prop_owner.value->>'text')::TEXT AS repo_owner, + (prop_name.value->>'text')::TEXT AS repo_name, + (prop_repo_id.value->>'number')::BIGINT AS repo_id, + (prop_is_private.value->>'boolean')::BOOLEAN AS is_private, + (prop_is_fork.value->>'boolean')::BOOLEAN AS is_fork, + (prop_webhook_id.value->>'number')::BIGINT AS webhook_id, + (prop_webhook_url.value->>'text')::TEXT AS webhook_url, + (prop_deploy_url.value->>'text')::TEXT AS deploy_url, + (prop_clone_url.value->>'text')::TEXT AS clone_url, + (prop_default_branch.value->>'text')::TEXT AS default_branch, + (prop_license.value->>'text')::TEXT AS license, + ei.created_at +FROM + entity_instances ei + JOIN providers pr ON ei.provider_id = pr.id + LEFT JOIN properties prop_owner ON ei.id = prop_owner.entity_id AND prop_owner.key = 'repo_owner' + LEFT JOIN properties prop_name ON ei.id = prop_name.entity_id AND prop_name.key = 'repo_name' + LEFT JOIN properties prop_repo_id ON ei.id = prop_repo_id.entity_id AND prop_repo_id.key = 'repo_id' + LEFT JOIN properties prop_is_private ON ei.id = prop_is_private.entity_id AND prop_is_private.key = 'is_private' + LEFT JOIN properties prop_is_fork ON ei.id = prop_is_fork.entity_id AND prop_is_fork.key = 'is_fork' + LEFT JOIN properties prop_webhook_id ON ei.id = prop_webhook_id.entity_id AND prop_webhook_id.key = 'webhook_id' + LEFT JOIN properties prop_webhook_url ON ei.id = prop_webhook_url.entity_id AND prop_webhook_url.key = 'webhook_url' + LEFT JOIN properties prop_deploy_url ON ei.id = prop_deploy_url.entity_id AND prop_deploy_url.key = 'deploy_url' + LEFT JOIN properties prop_clone_url ON ei.id = prop_clone_url.entity_id AND prop_clone_url.key = 'clone_url' + LEFT JOIN properties prop_default_branch ON ei.id = prop_default_branch.entity_id AND prop_default_branch.key = 'default_branch' + LEFT JOIN properties prop_license ON ei.id = prop_license.entity_id AND prop_license.key = 'license' +WHERE + ei.entity_type = 'repository'; + +CREATE VIEW artifacts AS +SELECT + ei.id, + ei.project_id, + pr.name AS provider_name, + ei.provider_id, + ei.originated_from AS repository_id, + (prop_artifact_name.value->>'text')::TEXT AS artifact_name, + (prop_artifact_type.value->>'text')::TEXT AS artifact_type, + (prop_artifact_visibility.value->>'text')::TEXT AS artifact_visibility, + ei.created_at +FROM + entity_instances ei + JOIN providers pr ON ei.provider_id = pr.id + LEFT JOIN properties prop_artifact_name ON ei.id = prop_artifact_name.entity_id AND prop_artifact_name.key = 'artifact_name' + LEFT JOIN properties prop_artifact_type ON ei.id = prop_artifact_type.entity_id AND prop_artifact_type.key = 'artifact_type' + LEFT JOIN properties prop_artifact_visibility ON ei.id = prop_artifact_visibility.entity_id AND prop_artifact_visibility.key = 'artifact_visibility' +WHERE + ei.entity_type = 'artifact'; + +CREATE VIEW pull_requests AS +SELECT + ei.id, + ei.originated_from AS repository_id, + (prop_pr_number.value->>'number')::BIGINT AS pr_number, + ei.created_at +FROM + entity_instances ei + LEFT JOIN properties prop_pr_number ON ei.id = prop_pr_number.entity_id AND prop_pr_number.key = 'pr_number' +WHERE + ei.entity_type = 'pull_request'; + + +COMMIT; \ No newline at end of file diff --git a/database/mock/store.go b/database/mock/store.go index 72dbbb4b89..50dac44e3d 100644 --- a/database/mock/store.go +++ b/database/mock/store.go @@ -296,36 +296,6 @@ func (mr *MockStoreMockRecorder) CreateProvider(arg0, arg1 any) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateProvider", reflect.TypeOf((*MockStore)(nil).CreateProvider), arg0, arg1) } -// CreatePullRequest mocks base method. -func (m *MockStore) CreatePullRequest(arg0 context.Context, arg1 db.CreatePullRequestParams) (db.PullRequest, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "CreatePullRequest", arg0, arg1) - ret0, _ := ret[0].(db.PullRequest) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// CreatePullRequest indicates an expected call of CreatePullRequest. -func (mr *MockStoreMockRecorder) CreatePullRequest(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreatePullRequest", reflect.TypeOf((*MockStore)(nil).CreatePullRequest), arg0, arg1) -} - -// CreateRepository mocks base method. -func (m *MockStore) CreateRepository(arg0 context.Context, arg1 db.CreateRepositoryParams) (db.Repository, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "CreateRepository", arg0, arg1) - ret0, _ := ret[0].(db.Repository) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// CreateRepository indicates an expected call of CreateRepository. -func (mr *MockStoreMockRecorder) CreateRepository(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateRepository", reflect.TypeOf((*MockStore)(nil).CreateRepository), arg0, arg1) -} - // CreateRuleType mocks base method. func (m *MockStore) CreateRuleType(arg0 context.Context, arg1 db.CreateRuleTypeParams) (db.RuleType, error) { m.ctrl.T.Helper() @@ -415,20 +385,6 @@ func (mr *MockStoreMockRecorder) DeleteAllPropertiesForEntity(arg0, arg1 any) *g return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteAllPropertiesForEntity", reflect.TypeOf((*MockStore)(nil).DeleteAllPropertiesForEntity), arg0, arg1) } -// DeleteArtifact mocks base method. -func (m *MockStore) DeleteArtifact(arg0 context.Context, arg1 uuid.UUID) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "DeleteArtifact", arg0, arg1) - ret0, _ := ret[0].(error) - return ret0 -} - -// DeleteArtifact indicates an expected call of DeleteArtifact. -func (mr *MockStoreMockRecorder) DeleteArtifact(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteArtifact", reflect.TypeOf((*MockStore)(nil).DeleteArtifact), arg0, arg1) -} - // DeleteEntity mocks base method. func (m *MockStore) DeleteEntity(arg0 context.Context, arg1 db.DeleteEntityParams) error { m.ctrl.T.Helper() @@ -601,34 +557,6 @@ func (mr *MockStoreMockRecorder) DeleteProvider(arg0, arg1 any) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteProvider", reflect.TypeOf((*MockStore)(nil).DeleteProvider), arg0, arg1) } -// DeletePullRequest mocks base method. -func (m *MockStore) DeletePullRequest(arg0 context.Context, arg1 db.DeletePullRequestParams) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "DeletePullRequest", arg0, arg1) - ret0, _ := ret[0].(error) - return ret0 -} - -// DeletePullRequest indicates an expected call of DeletePullRequest. -func (mr *MockStoreMockRecorder) DeletePullRequest(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeletePullRequest", reflect.TypeOf((*MockStore)(nil).DeletePullRequest), arg0, arg1) -} - -// DeleteRepository mocks base method. -func (m *MockStore) DeleteRepository(arg0 context.Context, arg1 uuid.UUID) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "DeleteRepository", arg0, arg1) - ret0, _ := ret[0].(error) - return ret0 -} - -// DeleteRepository indicates an expected call of DeleteRepository. -func (mr *MockStoreMockRecorder) DeleteRepository(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteRepository", reflect.TypeOf((*MockStore)(nil).DeleteRepository), arg0, arg1) -} - // DeleteRuleType mocks base method. func (m *MockStore) DeleteRuleType(arg0 context.Context, arg1 uuid.UUID) error { m.ctrl.T.Helper() @@ -2235,20 +2163,6 @@ func (mr *MockStoreMockRecorder) UpdateProvider(arg0, arg1 any) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateProvider", reflect.TypeOf((*MockStore)(nil).UpdateProvider), arg0, arg1) } -// UpdateReminderLastSentForRepositories mocks base method. -func (m *MockStore) UpdateReminderLastSentForRepositories(arg0 context.Context, arg1 []uuid.UUID) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "UpdateReminderLastSentForRepositories", arg0, arg1) - ret0, _ := ret[0].(error) - return ret0 -} - -// UpdateReminderLastSentForRepositories indicates an expected call of UpdateReminderLastSentForRepositories. -func (mr *MockStoreMockRecorder) UpdateReminderLastSentForRepositories(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateReminderLastSentForRepositories", reflect.TypeOf((*MockStore)(nil).UpdateReminderLastSentForRepositories), arg0, arg1) -} - // UpdateRuleType mocks base method. func (m *MockStore) UpdateRuleType(arg0 context.Context, arg1 db.UpdateRuleTypeParams) (db.RuleType, error) { m.ctrl.T.Helper() @@ -2294,21 +2208,6 @@ func (mr *MockStoreMockRecorder) UpsertAccessToken(arg0, arg1 any) *gomock.Call return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpsertAccessToken", reflect.TypeOf((*MockStore)(nil).UpsertAccessToken), arg0, arg1) } -// UpsertArtifact mocks base method. -func (m *MockStore) UpsertArtifact(arg0 context.Context, arg1 db.UpsertArtifactParams) (db.Artifact, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "UpsertArtifact", arg0, arg1) - ret0, _ := ret[0].(db.Artifact) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// UpsertArtifact indicates an expected call of UpsertArtifact. -func (mr *MockStoreMockRecorder) UpsertArtifact(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpsertArtifact", reflect.TypeOf((*MockStore)(nil).UpsertArtifact), arg0, arg1) -} - // UpsertBundle mocks base method. func (m *MockStore) UpsertBundle(arg0 context.Context, arg1 db.UpsertBundleParams) error { m.ctrl.T.Helper() @@ -2397,21 +2296,6 @@ func (mr *MockStoreMockRecorder) UpsertPropertyValueV1(arg0, arg1 any) *gomock.C return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpsertPropertyValueV1", reflect.TypeOf((*MockStore)(nil).UpsertPropertyValueV1), arg0, arg1) } -// UpsertPullRequest mocks base method. -func (m *MockStore) UpsertPullRequest(arg0 context.Context, arg1 db.UpsertPullRequestParams) (db.PullRequest, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "UpsertPullRequest", arg0, arg1) - ret0, _ := ret[0].(db.PullRequest) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// UpsertPullRequest indicates an expected call of UpsertPullRequest. -func (mr *MockStoreMockRecorder) UpsertPullRequest(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpsertPullRequest", reflect.TypeOf((*MockStore)(nil).UpsertPullRequest), arg0, arg1) -} - // UpsertRuleInstance mocks base method. func (m *MockStore) UpsertRuleInstance(arg0 context.Context, arg1 db.UpsertRuleInstanceParams) (uuid.UUID, error) { m.ctrl.T.Helper() diff --git a/database/query/artifacts.sql b/database/query/artifacts.sql index d29cd0642f..3ec075f37b 100644 --- a/database/query/artifacts.sql +++ b/database/query/artifacts.sql @@ -1,20 +1,3 @@ --- name: UpsertArtifact :one -INSERT INTO artifacts ( - repository_id, - artifact_name, - artifact_type, - artifact_visibility, - project_id, - provider_id, - provider_name -) VALUES ($1, $2, $3, $4, sqlc.arg(project_id), sqlc.arg(provider_id), sqlc.arg(provider_name)) -ON CONFLICT (project_id, LOWER(artifact_name)) -DO UPDATE SET - artifact_type = $3, - artifact_visibility = $4 -WHERE artifacts.repository_id = $1 AND artifacts.artifact_name = $2 -RETURNING *; - -- name: GetArtifactByID :one SELECT * FROM artifacts WHERE artifacts.id = $1 AND artifacts.project_id = $2; @@ -27,8 +10,4 @@ AND artifacts.repository_id = $1 AND artifacts.project_id = $2; -- name: ListArtifactsByRepoID :many SELECT * FROM artifacts WHERE repository_id = $1 -ORDER BY id; - --- name: DeleteArtifact :exec -DELETE FROM artifacts -WHERE id = $1; \ No newline at end of file +ORDER BY id; \ No newline at end of file diff --git a/database/query/eval_history.sql b/database/query/eval_history.sql index 68879e5f1f..8c9275f40f 100644 --- a/database/query/eval_history.sql +++ b/database/query/eval_history.sql @@ -99,21 +99,9 @@ INSERT INTO alert_events( SELECT s.id::uuid AS evaluation_id, s.evaluation_time as evaluated_at, ere.entity_type, - -- entity id - CAST( - CASE - WHEN ere.repository_id IS NOT NULL THEN r.id - WHEN ere.pull_request_id IS NOT NULL THEN pr.id - WHEN ere.artifact_id IS NOT NULL THEN a.id - END AS uuid - ) AS entity_id, - ere.entity_instance_id as new_entity_id, + ere.entity_instance_id as entity_id, -- raw fields for entity names - r.repo_owner, - r.repo_name, - pr.pr_number, - a.artifact_name, - j.id as project_id, + ei.name as entity_name, -- rule type, name, and profile rt.name AS rule_type, ri.name AS rule_name, @@ -133,9 +121,7 @@ FROM evaluation_statuses s JOIN rule_instances ri ON ere.rule_id = ri.id JOIN rule_type rt ON ri.rule_type_id = rt.id JOIN profiles p ON ri.profile_id = p.id - LEFT JOIN repositories r ON ere.repository_id = r.id - LEFT JOIN pull_requests pr ON ere.pull_request_id = pr.id - LEFT JOIN artifacts a ON ere.artifact_id = a.id + LEFT JOIN entity_instances ei ON ei.id = ere.entity_instance_id LEFT JOIN remediation_events re ON re.evaluation_id = s.id LEFT JOIN alert_events ae ON ae.evaluation_id = s.id LEFT JOIN projects j ON r.project_id = j.id @@ -144,22 +130,10 @@ WHERE s.id = sqlc.arg(evaluation_id) AND j.id = sqlc.arg(project_id); -- name: ListEvaluationHistory :many SELECT s.id::uuid AS evaluation_id, s.evaluation_time as evaluated_at, - ere.entity_type, - -- entity id - CAST( - CASE - WHEN ere.repository_id IS NOT NULL THEN r.id - WHEN ere.pull_request_id IS NOT NULL THEN pr.id - WHEN ere.artifact_id IS NOT NULL THEN a.id - END AS uuid - ) AS entity_id, - ere.entity_instance_id as new_entity_id, + ei.entity_type, + ere.entity_instance_id as entity_id, -- raw fields for entity names - r.repo_owner, - r.repo_name, - pr.pr_number, - a.artifact_name, - j.id as project_id, + ei.name as entity_name, -- rule type, name, and profile rt.name AS rule_type, ri.name AS rule_name, @@ -179,9 +153,7 @@ SELECT s.id::uuid AS evaluation_id, JOIN rule_instances ri ON ere.rule_id = ri.id JOIN rule_type rt ON ri.rule_type_id = rt.id JOIN profiles p ON ri.profile_id = p.id - LEFT JOIN repositories r ON ere.repository_id = r.id - LEFT JOIN pull_requests pr ON ere.pull_request_id = pr.id - LEFT JOIN artifacts a ON ere.artifact_id = a.id + JOIN entity_instances ei ON ei.id = ere.entity_instance_id LEFT JOIN remediation_events re ON re.evaluation_id = s.id LEFT JOIN alert_events ae ON ae.evaluation_id = s.id LEFT JOIN projects j ON r.project_id = j.id @@ -219,25 +191,11 @@ SELECT s.id::uuid AS evaluation_id, SELECT s.evaluation_time, s.id, ere.rule_id, - -- entity type - CAST( - CASE - WHEN ere.repository_id IS NOT NULL THEN 1 - WHEN ere.pull_request_id IS NOT NULL THEN 2 - WHEN ere.artifact_id IS NOT NULL THEN 3 - END AS integer - ) AS entity_type, - -- entity id - CAST( - CASE - WHEN ere.repository_id IS NOT NULL THEN ere.repository_id - WHEN ere.pull_request_id IS NOT NULL THEN ere.pull_request_id - WHEN ere.artifact_id IS NOT NULL THEN ere.artifact_id - END AS uuid - ) AS entity_id, - ere.entity_instance_id as new_entity_id + ei.entity_type, + ere.entity_instance_id as entity_id FROM evaluation_statuses s JOIN evaluation_rule_entities ere ON s.rule_entity_id = ere.id + LEFT JOIN entity_instances AS ei ON ei.id = ere.entity_instance_id LEFT JOIN latest_evaluation_statuses l ON l.rule_entity_id = s.rule_entity_id AND l.evaluation_history_id = s.id diff --git a/database/query/profile_status.sql b/database/query/profile_status.sql index 5075f96cc1..d11297c3b3 100644 --- a/database/query/profile_status.sql +++ b/database/query/profile_status.sql @@ -66,38 +66,29 @@ SELECT ad.alert_metadata, ad.alert_last_updated, ed.id AS rule_evaluation_id, - ere.repository_id, - ere.entity_type, + ei.id AS entity_id, + ei.entity_type, ri.name AS rule_name, - repo.repo_name, - repo.repo_owner, - repo.provider, + ei.name AS entity_name, + ei.provider_id, rt.name AS rule_type_name, rt.severity_value as rule_type_severity_value, rt.id AS rule_type_id, rt.guidance as rule_type_guidance, rt.display_name as rule_type_display_name, - -- TODO: store entity ID directly in evaluation_rule_entities - CASE - WHEN ere.entity_type = 'artifact'::entities THEN ere.artifact_id - WHEN ere.entity_type = 'repository'::entities THEN ere.repository_id - WHEN ere.entity_type = 'pull_request'::entities THEN ere.pull_request_id - END::uuid as entity_id, rt.release_phase as rule_type_release_phase FROM latest_evaluation_statuses les INNER JOIN evaluation_rule_entities ere ON ere.id = les.rule_entity_id + INNER JOIN entity_instances ei ON ei.id = ere.entity_instance_id INNER JOIN eval_details ed ON ed.id = les.evaluation_history_id INNER JOIN remediation_details rd ON rd.evaluation_id = les.evaluation_history_id INNER JOIN alert_details ad ON ad.evaluation_id = les.evaluation_history_id INNER JOIN rule_instances AS ri ON ri.id = ere.rule_id INNER JOIN rule_type rt ON rt.id = ri.rule_type_id - LEFT JOIN repositories repo ON repo.id = ere.repository_id WHERE les.profile_id = $1 AND ( CASE - WHEN sqlc.narg(entity_type)::entities = 'repository' AND ere.repository_id = sqlc.narg(entity_id)::UUID THEN true - WHEN sqlc.narg(entity_type)::entities = 'artifact' AND ere.artifact_id = sqlc.narg(entity_id)::UUID THEN true - WHEN sqlc.narg(entity_type)::entities = 'pull_request' AND ere.pull_request_id = sqlc.narg(entity_id)::UUID THEN true + WHEN ei.entity_id = sqlc.narg(entity_id)::UUID THEN true WHEN sqlc.narg(entity_id)::UUID IS NULL THEN true ELSE false END diff --git a/database/query/pull_requests.sql b/database/query/pull_requests.sql index 89a871608d..ba9d7554cd 100644 --- a/database/query/pull_requests.sql +++ b/database/query/pull_requests.sql @@ -1,28 +1,7 @@ --- name: CreatePullRequest :one -INSERT INTO pull_requests ( - repository_id, - pr_number -) VALUES ($1, $2) RETURNING *; - --- name: UpsertPullRequest :one -INSERT INTO pull_requests ( - repository_id, - pr_number -) VALUES ($1, $2) -ON CONFLICT (repository_id, pr_number) -DO UPDATE SET - updated_at = NOW() -WHERE pull_requests.repository_id = $1 AND pull_requests.pr_number = $2 -RETURNING *; - -- name: GetPullRequest :one SELECT * FROM pull_requests WHERE repository_id = $1 AND pr_number = $2; -- name: GetPullRequestByID :one SELECT * FROM pull_requests -WHERE id = $1; - --- name: DeletePullRequest :exec -DELETE FROM pull_requests -WHERE repository_id = $1 AND pr_number = $2; \ No newline at end of file +WHERE id = $1; \ No newline at end of file diff --git a/database/query/repositories.sql b/database/query/repositories.sql index 5ac412f6fb..fbe89a68bb 100644 --- a/database/query/repositories.sql +++ b/database/query/repositories.sql @@ -1,21 +1,3 @@ --- name: CreateRepository :one -INSERT INTO repositories ( - provider, - project_id, - repo_owner, - repo_name, - repo_id, - is_private, - is_fork, - webhook_id, - webhook_url, - deploy_url, - clone_url, - default_branch, - license, - provider_id - ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, sqlc.arg(default_branch), sqlc.arg(license), sqlc.arg(provider_id)) RETURNING *; - -- name: GetRepositoryByRepoID :one SELECT * FROM repositories WHERE repo_id = $1; @@ -52,11 +34,6 @@ WHERE id > $1 ORDER BY id LIMIT sqlc.arg('limit')::bigint; --- name: UpdateReminderLastSentForRepositories :exec -UPDATE repositories -SET reminder_last_sent = NOW() -WHERE id = ANY (sqlc.arg('repository_ids')::uuid[]); - -- name: RepositoryExistsAfterID :one SELECT EXISTS ( SELECT 1 @@ -64,10 +41,6 @@ SELECT EXISTS ( WHERE id > $1) AS exists; --- name: DeleteRepository :exec -DELETE FROM repositories -WHERE id = $1; - -- name: CountRepositories :one SELECT COUNT(*) FROM repositories; diff --git a/internal/db/artifacts.sql.go b/internal/db/artifacts.sql.go index 4c01cc72fd..fbb542b217 100644 --- a/internal/db/artifacts.sql.go +++ b/internal/db/artifacts.sql.go @@ -11,18 +11,8 @@ import ( "github.com/google/uuid" ) -const deleteArtifact = `-- name: DeleteArtifact :exec -DELETE FROM artifacts -WHERE id = $1 -` - -func (q *Queries) DeleteArtifact(ctx context.Context, id uuid.UUID) error { - _, err := q.db.ExecContext(ctx, deleteArtifact, id) - return err -} - const getArtifactByID = `-- name: GetArtifactByID :one -SELECT id, repository_id, artifact_name, artifact_type, artifact_visibility, created_at, updated_at, project_id, provider_id, provider_name FROM artifacts +SELECT id, project_id, provider_name, provider_id, repository_id, artifact_name, artifact_type, artifact_visibility, created_at FROM artifacts WHERE artifacts.id = $1 AND artifacts.project_id = $2 ` @@ -36,21 +26,20 @@ func (q *Queries) GetArtifactByID(ctx context.Context, arg GetArtifactByIDParams var i Artifact err := row.Scan( &i.ID, + &i.ProjectID, + &i.ProviderName, + &i.ProviderID, &i.RepositoryID, &i.ArtifactName, &i.ArtifactType, &i.ArtifactVisibility, &i.CreatedAt, - &i.UpdatedAt, - &i.ProjectID, - &i.ProviderID, - &i.ProviderName, ) return i, err } const getArtifactByName = `-- name: GetArtifactByName :one -SELECT id, repository_id, artifact_name, artifact_type, artifact_visibility, created_at, updated_at, project_id, provider_id, provider_name FROM artifacts +SELECT id, project_id, provider_name, provider_id, repository_id, artifact_name, artifact_type, artifact_visibility, created_at FROM artifacts WHERE lower(artifacts.artifact_name) = lower($3) AND artifacts.repository_id = $1 AND artifacts.project_id = $2 ` @@ -66,21 +55,20 @@ func (q *Queries) GetArtifactByName(ctx context.Context, arg GetArtifactByNamePa var i Artifact err := row.Scan( &i.ID, + &i.ProjectID, + &i.ProviderName, + &i.ProviderID, &i.RepositoryID, &i.ArtifactName, &i.ArtifactType, &i.ArtifactVisibility, &i.CreatedAt, - &i.UpdatedAt, - &i.ProjectID, - &i.ProviderID, - &i.ProviderName, ) return i, err } const listArtifactsByRepoID = `-- name: ListArtifactsByRepoID :many -SELECT id, repository_id, artifact_name, artifact_type, artifact_visibility, created_at, updated_at, project_id, provider_id, provider_name FROM artifacts +SELECT id, project_id, provider_name, provider_id, repository_id, artifact_name, artifact_type, artifact_visibility, created_at FROM artifacts WHERE repository_id = $1 ORDER BY id ` @@ -96,15 +84,14 @@ func (q *Queries) ListArtifactsByRepoID(ctx context.Context, repositoryID uuid.N var i Artifact if err := rows.Scan( &i.ID, + &i.ProjectID, + &i.ProviderName, + &i.ProviderID, &i.RepositoryID, &i.ArtifactName, &i.ArtifactType, &i.ArtifactVisibility, &i.CreatedAt, - &i.UpdatedAt, - &i.ProjectID, - &i.ProviderID, - &i.ProviderName, ); err != nil { return nil, err } @@ -118,57 +105,3 @@ func (q *Queries) ListArtifactsByRepoID(ctx context.Context, repositoryID uuid.N } return items, nil } - -const upsertArtifact = `-- name: UpsertArtifact :one -INSERT INTO artifacts ( - repository_id, - artifact_name, - artifact_type, - artifact_visibility, - project_id, - provider_id, - provider_name -) VALUES ($1, $2, $3, $4, $5, $6, $7) -ON CONFLICT (project_id, LOWER(artifact_name)) -DO UPDATE SET - artifact_type = $3, - artifact_visibility = $4 -WHERE artifacts.repository_id = $1 AND artifacts.artifact_name = $2 -RETURNING id, repository_id, artifact_name, artifact_type, artifact_visibility, created_at, updated_at, project_id, provider_id, provider_name -` - -type UpsertArtifactParams struct { - RepositoryID uuid.NullUUID `json:"repository_id"` - ArtifactName string `json:"artifact_name"` - ArtifactType string `json:"artifact_type"` - ArtifactVisibility string `json:"artifact_visibility"` - ProjectID uuid.UUID `json:"project_id"` - ProviderID uuid.UUID `json:"provider_id"` - ProviderName string `json:"provider_name"` -} - -func (q *Queries) UpsertArtifact(ctx context.Context, arg UpsertArtifactParams) (Artifact, error) { - row := q.db.QueryRowContext(ctx, upsertArtifact, - arg.RepositoryID, - arg.ArtifactName, - arg.ArtifactType, - arg.ArtifactVisibility, - arg.ProjectID, - arg.ProviderID, - arg.ProviderName, - ) - var i Artifact - err := row.Scan( - &i.ID, - &i.RepositoryID, - &i.ArtifactName, - &i.ArtifactType, - &i.ArtifactVisibility, - &i.CreatedAt, - &i.UpdatedAt, - &i.ProjectID, - &i.ProviderID, - &i.ProviderName, - ) - return i, err -} diff --git a/internal/db/eval_history.sql.go b/internal/db/eval_history.sql.go index 71c4a58641..87b2e8ab3f 100644 --- a/internal/db/eval_history.sql.go +++ b/internal/db/eval_history.sql.go @@ -43,21 +43,9 @@ const getEvaluationHistory = `-- name: GetEvaluationHistory :one SELECT s.id::uuid AS evaluation_id, s.evaluation_time as evaluated_at, ere.entity_type, - -- entity id - CAST( - CASE - WHEN ere.repository_id IS NOT NULL THEN r.id - WHEN ere.pull_request_id IS NOT NULL THEN pr.id - WHEN ere.artifact_id IS NOT NULL THEN a.id - END AS uuid - ) AS entity_id, - ere.entity_instance_id as new_entity_id, + ere.entity_instance_id as entity_id, -- raw fields for entity names - r.repo_owner, - r.repo_name, - pr.pr_number, - a.artifact_name, - j.id as project_id, + ei.name as entity_name, -- rule type, name, and profile rt.name AS rule_type, ri.name AS rule_name, @@ -77,9 +65,7 @@ FROM evaluation_statuses s JOIN rule_instances ri ON ere.rule_id = ri.id JOIN rule_type rt ON ri.rule_type_id = rt.id JOIN profiles p ON ri.profile_id = p.id - LEFT JOIN repositories r ON ere.repository_id = r.id - LEFT JOIN pull_requests pr ON ere.pull_request_id = pr.id - LEFT JOIN artifacts a ON ere.artifact_id = a.id + LEFT JOIN entity_instances ei ON ei.id = ere.entity_instance_id LEFT JOIN remediation_events re ON re.evaluation_id = s.id LEFT JOIN alert_events ae ON ae.evaluation_id = s.id LEFT JOIN projects j ON r.project_id = j.id @@ -96,12 +82,7 @@ type GetEvaluationHistoryRow struct { EvaluatedAt time.Time `json:"evaluated_at"` EntityType Entities `json:"entity_type"` EntityID uuid.UUID `json:"entity_id"` - NewEntityID uuid.UUID `json:"new_entity_id"` - RepoOwner sql.NullString `json:"repo_owner"` - RepoName sql.NullString `json:"repo_name"` - PrNumber sql.NullInt64 `json:"pr_number"` - ArtifactName sql.NullString `json:"artifact_name"` - ProjectID uuid.NullUUID `json:"project_id"` + EntityName sql.NullString `json:"entity_name"` RuleType string `json:"rule_type"` RuleName string `json:"rule_name"` RuleSeverity Severity `json:"rule_severity"` @@ -122,12 +103,7 @@ func (q *Queries) GetEvaluationHistory(ctx context.Context, arg GetEvaluationHis &i.EvaluatedAt, &i.EntityType, &i.EntityID, - &i.NewEntityID, - &i.RepoOwner, - &i.RepoName, - &i.PrNumber, - &i.ArtifactName, - &i.ProjectID, + &i.EntityName, &i.RuleType, &i.RuleName, &i.RuleSeverity, @@ -336,22 +312,10 @@ func (q *Queries) InsertRemediationEvent(ctx context.Context, arg InsertRemediat const listEvaluationHistory = `-- name: ListEvaluationHistory :many SELECT s.id::uuid AS evaluation_id, s.evaluation_time as evaluated_at, - ere.entity_type, - -- entity id - CAST( - CASE - WHEN ere.repository_id IS NOT NULL THEN r.id - WHEN ere.pull_request_id IS NOT NULL THEN pr.id - WHEN ere.artifact_id IS NOT NULL THEN a.id - END AS uuid - ) AS entity_id, - ere.entity_instance_id as new_entity_id, + ei.entity_type, + ere.entity_instance_id as entity_id, -- raw fields for entity names - r.repo_owner, - r.repo_name, - pr.pr_number, - a.artifact_name, - j.id as project_id, + ei.name as entity_name, -- rule type, name, and profile rt.name AS rule_type, ri.name AS rule_name, @@ -371,9 +335,7 @@ SELECT s.id::uuid AS evaluation_id, JOIN rule_instances ri ON ere.rule_id = ri.id JOIN rule_type rt ON ri.rule_type_id = rt.id JOIN profiles p ON ri.profile_id = p.id - LEFT JOIN repositories r ON ere.repository_id = r.id - LEFT JOIN pull_requests pr ON ere.pull_request_id = pr.id - LEFT JOIN artifacts a ON ere.artifact_id = a.id + JOIN entity_instances ei ON ei.id = ere.entity_instance_id LEFT JOIN remediation_events re ON re.evaluation_id = s.id LEFT JOIN alert_events ae ON ae.evaluation_id = s.id LEFT JOIN projects j ON r.project_id = j.id @@ -434,12 +396,7 @@ type ListEvaluationHistoryRow struct { EvaluatedAt time.Time `json:"evaluated_at"` EntityType Entities `json:"entity_type"` EntityID uuid.UUID `json:"entity_id"` - NewEntityID uuid.UUID `json:"new_entity_id"` - RepoOwner sql.NullString `json:"repo_owner"` - RepoName sql.NullString `json:"repo_name"` - PrNumber sql.NullInt64 `json:"pr_number"` - ArtifactName sql.NullString `json:"artifact_name"` - ProjectID uuid.NullUUID `json:"project_id"` + EntityName string `json:"entity_name"` RuleType string `json:"rule_type"` RuleName string `json:"rule_name"` RuleSeverity Severity `json:"rule_severity"` @@ -485,12 +442,7 @@ func (q *Queries) ListEvaluationHistory(ctx context.Context, arg ListEvaluationH &i.EvaluatedAt, &i.EntityType, &i.EntityID, - &i.NewEntityID, - &i.RepoOwner, - &i.RepoName, - &i.PrNumber, - &i.ArtifactName, - &i.ProjectID, + &i.EntityName, &i.RuleType, &i.RuleName, &i.RuleSeverity, @@ -519,25 +471,11 @@ const listEvaluationHistoryStaleRecords = `-- name: ListEvaluationHistoryStaleRe SELECT s.evaluation_time, s.id, ere.rule_id, - -- entity type - CAST( - CASE - WHEN ere.repository_id IS NOT NULL THEN 1 - WHEN ere.pull_request_id IS NOT NULL THEN 2 - WHEN ere.artifact_id IS NOT NULL THEN 3 - END AS integer - ) AS entity_type, - -- entity id - CAST( - CASE - WHEN ere.repository_id IS NOT NULL THEN ere.repository_id - WHEN ere.pull_request_id IS NOT NULL THEN ere.pull_request_id - WHEN ere.artifact_id IS NOT NULL THEN ere.artifact_id - END AS uuid - ) AS entity_id, - ere.entity_instance_id as new_entity_id + ei.entity_type, + ere.entity_instance_id as entity_id FROM evaluation_statuses s JOIN evaluation_rule_entities ere ON s.rule_entity_id = ere.id + LEFT JOIN entity_instances AS ei ON ei.id = ere.entity_instance_id LEFT JOIN latest_evaluation_statuses l ON l.rule_entity_id = s.rule_entity_id AND l.evaluation_history_id = s.id @@ -555,12 +493,11 @@ type ListEvaluationHistoryStaleRecordsParams struct { } type ListEvaluationHistoryStaleRecordsRow struct { - EvaluationTime time.Time `json:"evaluation_time"` - ID uuid.UUID `json:"id"` - RuleID uuid.UUID `json:"rule_id"` - EntityType int32 `json:"entity_type"` - EntityID uuid.UUID `json:"entity_id"` - NewEntityID uuid.UUID `json:"new_entity_id"` + EvaluationTime time.Time `json:"evaluation_time"` + ID uuid.UUID `json:"id"` + RuleID uuid.UUID `json:"rule_id"` + EntityType NullEntities `json:"entity_type"` + EntityID uuid.UUID `json:"entity_id"` } func (q *Queries) ListEvaluationHistoryStaleRecords(ctx context.Context, arg ListEvaluationHistoryStaleRecordsParams) ([]ListEvaluationHistoryStaleRecordsRow, error) { @@ -578,7 +515,6 @@ func (q *Queries) ListEvaluationHistoryStaleRecords(ctx context.Context, arg Lis &i.RuleID, &i.EntityType, &i.EntityID, - &i.NewEntityID, ); err != nil { return nil, err } diff --git a/internal/db/eval_history_test.go b/internal/db/eval_history_test.go index 42b1d5dfd7..f0b8a83c7e 100644 --- a/internal/db/eval_history_test.go +++ b/internal/db/eval_history_test.go @@ -70,8 +70,6 @@ func TestListEvaluationHistoryFilters(t *testing.T) { require.Equal(t, es1, row.EvaluationID) require.Equal(t, EntitiesRepository, row.EntityType) require.Equal(t, repo1.ID, row.EntityID) - require.Equal(t, repo1.RepoOwner, row.RepoOwner.String) - require.Equal(t, repo1.RepoName, row.RepoName.String) }, }, @@ -94,8 +92,6 @@ func TestListEvaluationHistoryFilters(t *testing.T) { require.Equal(t, es1, row.EvaluationID) require.Equal(t, EntitiesRepository, row.EntityType) require.Equal(t, repo1.ID, row.EntityID) - require.Equal(t, repo1.RepoOwner, row.RepoOwner.String) - require.Equal(t, repo1.RepoName, row.RepoName.String) }, }, { @@ -148,8 +144,6 @@ func TestListEvaluationHistoryFilters(t *testing.T) { require.Equal(t, es1, row.EvaluationID) require.Equal(t, EntitiesRepository, row.EntityType) require.Equal(t, repo1.ID, row.EntityID) - require.Equal(t, repo1.RepoOwner, row.RepoOwner.String) - require.Equal(t, repo1.RepoName, row.RepoName.String) }, }, { @@ -170,8 +164,6 @@ func TestListEvaluationHistoryFilters(t *testing.T) { require.Equal(t, es1, row.EvaluationID) require.Equal(t, EntitiesRepository, row.EntityType) require.Equal(t, repo1.ID, row.EntityID) - require.Equal(t, repo1.RepoOwner, row.RepoOwner.String) - require.Equal(t, repo1.RepoName, row.RepoName.String) }, }, { @@ -210,8 +202,6 @@ func TestListEvaluationHistoryFilters(t *testing.T) { require.Equal(t, es1, row.EvaluationID) require.Equal(t, EntitiesRepository, row.EntityType) require.Equal(t, repo1.ID, row.EntityID) - require.Equal(t, repo1.RepoOwner, row.RepoOwner.String) - require.Equal(t, repo1.RepoName, row.RepoName.String) }, }, { @@ -264,8 +254,6 @@ func TestListEvaluationHistoryFilters(t *testing.T) { require.Equal(t, es1, row.EvaluationID) require.Equal(t, EntitiesRepository, row.EntityType) require.Equal(t, repo1.ID, row.EntityID) - require.Equal(t, repo1.RepoOwner, row.RepoOwner.String) - require.Equal(t, repo1.RepoName, row.RepoName.String) }, }, { @@ -286,8 +274,6 @@ func TestListEvaluationHistoryFilters(t *testing.T) { require.Equal(t, es1, row.EvaluationID) require.Equal(t, EntitiesRepository, row.EntityType) require.Equal(t, repo1.ID, row.EntityID) - require.Equal(t, repo1.RepoOwner, row.RepoOwner.String) - require.Equal(t, repo1.RepoName, row.RepoName.String) }, }, { @@ -326,8 +312,6 @@ func TestListEvaluationHistoryFilters(t *testing.T) { require.Equal(t, es1, row.EvaluationID) require.Equal(t, EntitiesRepository, row.EntityType) require.Equal(t, repo1.ID, row.EntityID) - require.Equal(t, repo1.RepoOwner, row.RepoOwner.String) - require.Equal(t, repo1.RepoName, row.RepoName.String) }, }, { @@ -380,8 +364,6 @@ func TestListEvaluationHistoryFilters(t *testing.T) { require.Equal(t, es1, row.EvaluationID) require.Equal(t, EntitiesRepository, row.EntityType) require.Equal(t, repo1.ID, row.EntityID) - require.Equal(t, repo1.RepoOwner, row.RepoOwner.String) - require.Equal(t, repo1.RepoName, row.RepoName.String) }, }, { @@ -402,8 +384,6 @@ func TestListEvaluationHistoryFilters(t *testing.T) { require.Equal(t, es1, row.EvaluationID) require.Equal(t, EntitiesRepository, row.EntityType) require.Equal(t, repo1.ID, row.EntityID) - require.Equal(t, repo1.RepoOwner, row.RepoOwner.String) - require.Equal(t, repo1.RepoName, row.RepoName.String) }, }, { @@ -464,8 +444,6 @@ func TestListEvaluationHistoryFilters(t *testing.T) { require.Equal(t, es1, row.EvaluationID) require.Equal(t, EntitiesRepository, row.EntityType) require.Equal(t, repo1.ID, row.EntityID) - require.Equal(t, repo1.RepoOwner, row.RepoOwner.String) - require.Equal(t, repo1.RepoName, row.RepoName.String) }, }, { @@ -489,8 +467,6 @@ func TestListEvaluationHistoryFilters(t *testing.T) { require.Equal(t, es1, row.EvaluationID) require.Equal(t, EntitiesRepository, row.EntityType) require.Equal(t, repo1.ID, row.EntityID) - require.Equal(t, repo1.RepoOwner, row.RepoOwner.String) - require.Equal(t, repo1.RepoName, row.RepoName.String) }, }, { @@ -601,8 +577,6 @@ func TestListEvaluationHistoryPagination(t *testing.T) { require.Equal(t, ess[9], row.EvaluationID) require.Equal(t, EntitiesRepository, row.EntityType) require.Equal(t, repos[9].ID, row.EntityID) - require.Equal(t, repos[9].RepoOwner, row.RepoOwner.String) - require.Equal(t, repos[9].RepoName, row.RepoName.String) }, }, { @@ -646,8 +620,6 @@ func TestListEvaluationHistoryPagination(t *testing.T) { require.Equal(t, ess[0], row.EvaluationID) require.Equal(t, EntitiesRepository, row.EntityType) require.Equal(t, repos[0].ID, row.EntityID) - require.Equal(t, repos[0].RepoOwner, row.RepoOwner.String) - require.Equal(t, repos[0].RepoName, row.RepoName.String) }, }, { diff --git a/internal/db/models.go b/internal/db/models.go index e40f2822c1..506ad9adc1 100644 --- a/internal/db/models.go +++ b/internal/db/models.go @@ -478,15 +478,14 @@ type AlertEvent struct { type Artifact struct { ID uuid.UUID `json:"id"` + ProjectID uuid.UUID `json:"project_id"` + ProviderName string `json:"provider_name"` + ProviderID uuid.UUID `json:"provider_id"` RepositoryID uuid.NullUUID `json:"repository_id"` ArtifactName string `json:"artifact_name"` ArtifactType string `json:"artifact_type"` ArtifactVisibility string `json:"artifact_visibility"` CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` - ProjectID uuid.UUID `json:"project_id"` - ProviderID uuid.UUID `json:"provider_id"` - ProviderName string `json:"provider_name"` } type Bundle struct { @@ -674,11 +673,10 @@ type ProviderGithubAppInstallation struct { } type PullRequest struct { - ID uuid.UUID `json:"id"` - RepositoryID uuid.UUID `json:"repository_id"` - PrNumber int64 `json:"pr_number"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` + ID uuid.UUID `json:"id"` + RepositoryID uuid.NullUUID `json:"repository_id"` + PrNumber int64 `json:"pr_number"` + CreatedAt time.Time `json:"created_at"` } type RemediationEvent struct { @@ -691,24 +689,22 @@ type RemediationEvent struct { } type Repository struct { - ID uuid.UUID `json:"id"` - Provider string `json:"provider"` - ProjectID uuid.UUID `json:"project_id"` - RepoOwner string `json:"repo_owner"` - RepoName string `json:"repo_name"` - RepoID int64 `json:"repo_id"` - IsPrivate bool `json:"is_private"` - IsFork bool `json:"is_fork"` - WebhookID sql.NullInt64 `json:"webhook_id"` - WebhookUrl string `json:"webhook_url"` - DeployUrl string `json:"deploy_url"` - CloneUrl string `json:"clone_url"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` - DefaultBranch sql.NullString `json:"default_branch"` - License sql.NullString `json:"license"` - ProviderID uuid.UUID `json:"provider_id"` - ReminderLastSent sql.NullTime `json:"reminder_last_sent"` + ID uuid.UUID `json:"id"` + ProjectID uuid.UUID `json:"project_id"` + Provider string `json:"provider"` + ProviderID uuid.UUID `json:"provider_id"` + RepoOwner string `json:"repo_owner"` + RepoName string `json:"repo_name"` + RepoID int64 `json:"repo_id"` + IsPrivate bool `json:"is_private"` + IsFork bool `json:"is_fork"` + WebhookID int64 `json:"webhook_id"` + WebhookUrl string `json:"webhook_url"` + DeployUrl string `json:"deploy_url"` + CloneUrl string `json:"clone_url"` + DefaultBranch string `json:"default_branch"` + License string `json:"license"` + CreatedAt time.Time `json:"created_at"` } type RuleInstance struct { diff --git a/internal/db/profile_status.sql.go b/internal/db/profile_status.sql.go index f4d08e2714..2f573d1239 100644 --- a/internal/db/profile_status.sql.go +++ b/internal/db/profile_status.sql.go @@ -198,48 +198,38 @@ SELECT ad.alert_metadata, ad.alert_last_updated, ed.id AS rule_evaluation_id, - ere.repository_id, - ere.entity_type, + ei.id AS entity_id, + ei.entity_type, ri.name AS rule_name, - repo.repo_name, - repo.repo_owner, - repo.provider, + ei.name AS entity_name, + ei.provider_id, rt.name AS rule_type_name, rt.severity_value as rule_type_severity_value, rt.id AS rule_type_id, rt.guidance as rule_type_guidance, rt.display_name as rule_type_display_name, - -- TODO: store entity ID directly in evaluation_rule_entities - CASE - WHEN ere.entity_type = 'artifact'::entities THEN ere.artifact_id - WHEN ere.entity_type = 'repository'::entities THEN ere.repository_id - WHEN ere.entity_type = 'pull_request'::entities THEN ere.pull_request_id - END::uuid as entity_id, rt.release_phase as rule_type_release_phase FROM latest_evaluation_statuses les INNER JOIN evaluation_rule_entities ere ON ere.id = les.rule_entity_id + INNER JOIN entity_instances ei ON ei.id = ere.entity_instance_id INNER JOIN eval_details ed ON ed.id = les.evaluation_history_id INNER JOIN remediation_details rd ON rd.evaluation_id = les.evaluation_history_id INNER JOIN alert_details ad ON ad.evaluation_id = les.evaluation_history_id INNER JOIN rule_instances AS ri ON ri.id = ere.rule_id INNER JOIN rule_type rt ON rt.id = ri.rule_type_id - LEFT JOIN repositories repo ON repo.id = ere.repository_id WHERE les.profile_id = $1 AND ( CASE - WHEN $2::entities = 'repository' AND ere.repository_id = $3::UUID THEN true - WHEN $2::entities = 'artifact' AND ere.artifact_id = $3::UUID THEN true - WHEN $2::entities = 'pull_request' AND ere.pull_request_id = $3::UUID THEN true - WHEN $3::UUID IS NULL THEN true + WHEN ei.entity_id = $2::UUID THEN true + WHEN $2::UUID IS NULL THEN true ELSE false END - ) AND (rt.name = $4 OR $4 IS NULL) - AND (lower(ri.name) = lower($5) OR $5 IS NULL) + ) AND (rt.name = $3 OR $3 IS NULL) + AND (lower(ri.name) = lower($4) OR $4 IS NULL) ` type ListRuleEvaluationsByProfileIdParams struct { ProfileID uuid.UUID `json:"profile_id"` - EntityType NullEntities `json:"entity_type"` EntityID uuid.NullUUID `json:"entity_id"` RuleTypeName sql.NullString `json:"rule_type_name"` RuleName sql.NullString `json:"rule_name"` @@ -258,25 +248,22 @@ type ListRuleEvaluationsByProfileIdRow struct { AlertMetadata json.RawMessage `json:"alert_metadata"` AlertLastUpdated time.Time `json:"alert_last_updated"` RuleEvaluationID uuid.UUID `json:"rule_evaluation_id"` - RepositoryID uuid.NullUUID `json:"repository_id"` + EntityID uuid.UUID `json:"entity_id"` EntityType Entities `json:"entity_type"` RuleName string `json:"rule_name"` - RepoName sql.NullString `json:"repo_name"` - RepoOwner sql.NullString `json:"repo_owner"` - Provider sql.NullString `json:"provider"` + EntityName string `json:"entity_name"` + ProviderID uuid.UUID `json:"provider_id"` RuleTypeName string `json:"rule_type_name"` RuleTypeSeverityValue Severity `json:"rule_type_severity_value"` RuleTypeID uuid.UUID `json:"rule_type_id"` RuleTypeGuidance string `json:"rule_type_guidance"` RuleTypeDisplayName string `json:"rule_type_display_name"` - EntityID uuid.UUID `json:"entity_id"` RuleTypeReleasePhase ReleaseStatus `json:"rule_type_release_phase"` } func (q *Queries) ListRuleEvaluationsByProfileId(ctx context.Context, arg ListRuleEvaluationsByProfileIdParams) ([]ListRuleEvaluationsByProfileIdRow, error) { rows, err := q.db.QueryContext(ctx, listRuleEvaluationsByProfileId, arg.ProfileID, - arg.EntityType, arg.EntityID, arg.RuleTypeName, arg.RuleName, @@ -301,18 +288,16 @@ func (q *Queries) ListRuleEvaluationsByProfileId(ctx context.Context, arg ListRu &i.AlertMetadata, &i.AlertLastUpdated, &i.RuleEvaluationID, - &i.RepositoryID, + &i.EntityID, &i.EntityType, &i.RuleName, - &i.RepoName, - &i.RepoOwner, - &i.Provider, + &i.EntityName, + &i.ProviderID, &i.RuleTypeName, &i.RuleTypeSeverityValue, &i.RuleTypeID, &i.RuleTypeGuidance, &i.RuleTypeDisplayName, - &i.EntityID, &i.RuleTypeReleasePhase, ); err != nil { return nil, err diff --git a/internal/db/profiles_test.go b/internal/db/profiles_test.go index fbe8dcd5f2..ecda022ab1 100644 --- a/internal/db/profiles_test.go +++ b/internal/db/profiles_test.go @@ -3652,11 +3652,6 @@ func verifyRow( require.Equal(t, rt.ID, row.RuleTypeID) require.Equal(t, rt.Name, row.RuleTypeName) - - require.Equal(t, randomEntities.repo.RepoName, row.RepoName.String) - require.Equal(t, randomEntities.repo.RepoOwner, row.RepoOwner.String) - - require.Equal(t, randomEntities.prov.Name, row.Provider.String) } func TestListRuleEvaluations(t *testing.T) { diff --git a/internal/db/pull_requests.sql.go b/internal/db/pull_requests.sql.go index bb3887720a..30da54c2b8 100644 --- a/internal/db/pull_requests.sql.go +++ b/internal/db/pull_requests.sql.go @@ -11,54 +11,14 @@ import ( "github.com/google/uuid" ) -const createPullRequest = `-- name: CreatePullRequest :one -INSERT INTO pull_requests ( - repository_id, - pr_number -) VALUES ($1, $2) RETURNING id, repository_id, pr_number, created_at, updated_at -` - -type CreatePullRequestParams struct { - RepositoryID uuid.UUID `json:"repository_id"` - PrNumber int64 `json:"pr_number"` -} - -func (q *Queries) CreatePullRequest(ctx context.Context, arg CreatePullRequestParams) (PullRequest, error) { - row := q.db.QueryRowContext(ctx, createPullRequest, arg.RepositoryID, arg.PrNumber) - var i PullRequest - err := row.Scan( - &i.ID, - &i.RepositoryID, - &i.PrNumber, - &i.CreatedAt, - &i.UpdatedAt, - ) - return i, err -} - -const deletePullRequest = `-- name: DeletePullRequest :exec -DELETE FROM pull_requests -WHERE repository_id = $1 AND pr_number = $2 -` - -type DeletePullRequestParams struct { - RepositoryID uuid.UUID `json:"repository_id"` - PrNumber int64 `json:"pr_number"` -} - -func (q *Queries) DeletePullRequest(ctx context.Context, arg DeletePullRequestParams) error { - _, err := q.db.ExecContext(ctx, deletePullRequest, arg.RepositoryID, arg.PrNumber) - return err -} - const getPullRequest = `-- name: GetPullRequest :one -SELECT id, repository_id, pr_number, created_at, updated_at FROM pull_requests +SELECT id, repository_id, pr_number, created_at FROM pull_requests WHERE repository_id = $1 AND pr_number = $2 ` type GetPullRequestParams struct { - RepositoryID uuid.UUID `json:"repository_id"` - PrNumber int64 `json:"pr_number"` + RepositoryID uuid.NullUUID `json:"repository_id"` + PrNumber int64 `json:"pr_number"` } func (q *Queries) GetPullRequest(ctx context.Context, arg GetPullRequestParams) (PullRequest, error) { @@ -69,13 +29,12 @@ func (q *Queries) GetPullRequest(ctx context.Context, arg GetPullRequestParams) &i.RepositoryID, &i.PrNumber, &i.CreatedAt, - &i.UpdatedAt, ) return i, err } const getPullRequestByID = `-- name: GetPullRequestByID :one -SELECT id, repository_id, pr_number, created_at, updated_at FROM pull_requests +SELECT id, repository_id, pr_number, created_at FROM pull_requests WHERE id = $1 ` @@ -87,37 +46,6 @@ func (q *Queries) GetPullRequestByID(ctx context.Context, id uuid.UUID) (PullReq &i.RepositoryID, &i.PrNumber, &i.CreatedAt, - &i.UpdatedAt, - ) - return i, err -} - -const upsertPullRequest = `-- name: UpsertPullRequest :one -INSERT INTO pull_requests ( - repository_id, - pr_number -) VALUES ($1, $2) -ON CONFLICT (repository_id, pr_number) -DO UPDATE SET - updated_at = NOW() -WHERE pull_requests.repository_id = $1 AND pull_requests.pr_number = $2 -RETURNING id, repository_id, pr_number, created_at, updated_at -` - -type UpsertPullRequestParams struct { - RepositoryID uuid.UUID `json:"repository_id"` - PrNumber int64 `json:"pr_number"` -} - -func (q *Queries) UpsertPullRequest(ctx context.Context, arg UpsertPullRequestParams) (PullRequest, error) { - row := q.db.QueryRowContext(ctx, upsertPullRequest, arg.RepositoryID, arg.PrNumber) - var i PullRequest - err := row.Scan( - &i.ID, - &i.RepositoryID, - &i.PrNumber, - &i.CreatedAt, - &i.UpdatedAt, ) return i, err } diff --git a/internal/db/querier.go b/internal/db/querier.go index 1d234b5009..3d88746886 100644 --- a/internal/db/querier.go +++ b/internal/db/querier.go @@ -35,8 +35,6 @@ type Querier interface { CreateProject(ctx context.Context, arg CreateProjectParams) (Project, error) CreateProjectWithID(ctx context.Context, arg CreateProjectWithIDParams) (Project, error) CreateProvider(ctx context.Context, arg CreateProviderParams) (Provider, error) - CreatePullRequest(ctx context.Context, arg CreatePullRequestParams) (PullRequest, error) - CreateRepository(ctx context.Context, arg CreateRepositoryParams) (Repository, error) CreateRuleType(ctx context.Context, arg CreateRuleTypeParams) (RuleType, error) CreateSelector(ctx context.Context, arg CreateSelectorParams) (ProfileSelector, error) CreateSessionState(ctx context.Context, arg CreateSessionStateParams) (SessionStore, error) @@ -44,7 +42,6 @@ type Querier interface { CreateSubscription(ctx context.Context, arg CreateSubscriptionParams) (Subscription, error) CreateUser(ctx context.Context, identitySubject string) (User, error) DeleteAllPropertiesForEntity(ctx context.Context, entityID uuid.UUID) error - DeleteArtifact(ctx context.Context, id uuid.UUID) error // DeleteEntity removes an entity from the entity_instances table for a project. DeleteEntity(ctx context.Context, arg DeleteEntityParams) error // DeleteEntityByName removes an entity from the entity_instances table for a project. @@ -62,8 +59,6 @@ type Querier interface { DeleteProject(ctx context.Context, id uuid.UUID) ([]DeleteProjectRow, error) DeleteProperty(ctx context.Context, arg DeletePropertyParams) error DeleteProvider(ctx context.Context, arg DeleteProviderParams) error - DeletePullRequest(ctx context.Context, arg DeletePullRequestParams) error - DeleteRepository(ctx context.Context, id uuid.UUID) error DeleteRuleType(ctx context.Context, id uuid.UUID) error DeleteSelector(ctx context.Context, id uuid.UUID) error DeleteSelectorsByProfileID(ctx context.Context, profileID uuid.UUID) error @@ -243,11 +238,9 @@ type Querier interface { UpdateProfile(ctx context.Context, arg UpdateProfileParams) (Profile, error) UpdateProjectMeta(ctx context.Context, arg UpdateProjectMetaParams) (Project, error) UpdateProvider(ctx context.Context, arg UpdateProviderParams) error - UpdateReminderLastSentForRepositories(ctx context.Context, repositoryIds []uuid.UUID) error UpdateRuleType(ctx context.Context, arg UpdateRuleTypeParams) (RuleType, error) UpdateSelector(ctx context.Context, arg UpdateSelectorParams) (ProfileSelector, error) UpsertAccessToken(ctx context.Context, arg UpsertAccessTokenParams) (ProviderAccessToken, error) - UpsertArtifact(ctx context.Context, arg UpsertArtifactParams) (Artifact, error) // Copyright 2024 Stacklok, Inc // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -267,7 +260,6 @@ type Querier interface { UpsertLatestEvaluationStatus(ctx context.Context, arg UpsertLatestEvaluationStatusParams) error UpsertProfileForEntity(ctx context.Context, arg UpsertProfileForEntityParams) (EntityProfile, error) UpsertProperty(ctx context.Context, arg UpsertPropertyParams) (Property, error) - UpsertPullRequest(ctx context.Context, arg UpsertPullRequestParams) (PullRequest, error) // Copyright 2024 Stacklok, Inc // // Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/internal/db/repositories.sql.go b/internal/db/repositories.sql.go index 902aa56447..78a33aa0ed 100644 --- a/internal/db/repositories.sql.go +++ b/internal/db/repositories.sql.go @@ -10,7 +10,6 @@ import ( "database/sql" "github.com/google/uuid" - "github.com/lib/pq" ) const countRepositories = `-- name: CountRepositories :one @@ -24,102 +23,15 @@ func (q *Queries) CountRepositories(ctx context.Context) (int64, error) { return count, err } -const createRepository = `-- name: CreateRepository :one -INSERT INTO repositories ( - provider, - project_id, - repo_owner, - repo_name, - repo_id, - is_private, - is_fork, - webhook_id, - webhook_url, - deploy_url, - clone_url, - default_branch, - license, - provider_id - ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14) RETURNING id, provider, project_id, repo_owner, repo_name, repo_id, is_private, is_fork, webhook_id, webhook_url, deploy_url, clone_url, created_at, updated_at, default_branch, license, provider_id, reminder_last_sent -` - -type CreateRepositoryParams struct { - Provider string `json:"provider"` - ProjectID uuid.UUID `json:"project_id"` - RepoOwner string `json:"repo_owner"` - RepoName string `json:"repo_name"` - RepoID int64 `json:"repo_id"` - IsPrivate bool `json:"is_private"` - IsFork bool `json:"is_fork"` - WebhookID sql.NullInt64 `json:"webhook_id"` - WebhookUrl string `json:"webhook_url"` - DeployUrl string `json:"deploy_url"` - CloneUrl string `json:"clone_url"` - DefaultBranch sql.NullString `json:"default_branch"` - License sql.NullString `json:"license"` - ProviderID uuid.UUID `json:"provider_id"` -} - -func (q *Queries) CreateRepository(ctx context.Context, arg CreateRepositoryParams) (Repository, error) { - row := q.db.QueryRowContext(ctx, createRepository, - arg.Provider, - arg.ProjectID, - arg.RepoOwner, - arg.RepoName, - arg.RepoID, - arg.IsPrivate, - arg.IsFork, - arg.WebhookID, - arg.WebhookUrl, - arg.DeployUrl, - arg.CloneUrl, - arg.DefaultBranch, - arg.License, - arg.ProviderID, - ) - var i Repository - err := row.Scan( - &i.ID, - &i.Provider, - &i.ProjectID, - &i.RepoOwner, - &i.RepoName, - &i.RepoID, - &i.IsPrivate, - &i.IsFork, - &i.WebhookID, - &i.WebhookUrl, - &i.DeployUrl, - &i.CloneUrl, - &i.CreatedAt, - &i.UpdatedAt, - &i.DefaultBranch, - &i.License, - &i.ProviderID, - &i.ReminderLastSent, - ) - return i, err -} - -const deleteRepository = `-- name: DeleteRepository :exec -DELETE FROM repositories -WHERE id = $1 -` - -func (q *Queries) DeleteRepository(ctx context.Context, id uuid.UUID) error { - _, err := q.db.ExecContext(ctx, deleteRepository, id) - return err -} - const getProviderWebhooks = `-- name: GetProviderWebhooks :many SELECT repo_owner, repo_name, webhook_id FROM repositories WHERE webhook_id IS NOT NULL AND provider_id = $1 ` type GetProviderWebhooksRow struct { - RepoOwner string `json:"repo_owner"` - RepoName string `json:"repo_name"` - WebhookID sql.NullInt64 `json:"webhook_id"` + RepoOwner string `json:"repo_owner"` + RepoName string `json:"repo_name"` + WebhookID int64 `json:"webhook_id"` } // get a list of repos with webhooks belonging to a provider @@ -184,7 +96,7 @@ func (q *Queries) GetRepoPathFromPullRequestID(ctx context.Context, id uuid.UUID } const getRepositoryByID = `-- name: GetRepositoryByID :one -SELECT id, provider, project_id, repo_owner, repo_name, repo_id, is_private, is_fork, webhook_id, webhook_url, deploy_url, clone_url, created_at, updated_at, default_branch, license, provider_id, reminder_last_sent FROM repositories WHERE id = $1 +SELECT id, project_id, provider, provider_id, repo_owner, repo_name, repo_id, is_private, is_fork, webhook_id, webhook_url, deploy_url, clone_url, default_branch, license, created_at FROM repositories WHERE id = $1 ` // avoid using this, where possible use GetRepositoryByIDAndProject instead @@ -193,8 +105,9 @@ func (q *Queries) GetRepositoryByID(ctx context.Context, id uuid.UUID) (Reposito var i Repository err := row.Scan( &i.ID, - &i.Provider, &i.ProjectID, + &i.Provider, + &i.ProviderID, &i.RepoOwner, &i.RepoName, &i.RepoID, @@ -204,18 +117,15 @@ func (q *Queries) GetRepositoryByID(ctx context.Context, id uuid.UUID) (Reposito &i.WebhookUrl, &i.DeployUrl, &i.CloneUrl, - &i.CreatedAt, - &i.UpdatedAt, &i.DefaultBranch, &i.License, - &i.ProviderID, - &i.ReminderLastSent, + &i.CreatedAt, ) return i, err } const getRepositoryByIDAndProject = `-- name: GetRepositoryByIDAndProject :one -SELECT id, provider, project_id, repo_owner, repo_name, repo_id, is_private, is_fork, webhook_id, webhook_url, deploy_url, clone_url, created_at, updated_at, default_branch, license, provider_id, reminder_last_sent FROM repositories WHERE id = $1 AND project_id = $2 +SELECT id, project_id, provider, provider_id, repo_owner, repo_name, repo_id, is_private, is_fork, webhook_id, webhook_url, deploy_url, clone_url, default_branch, license, created_at FROM repositories WHERE id = $1 AND project_id = $2 ` type GetRepositoryByIDAndProjectParams struct { @@ -228,8 +138,9 @@ func (q *Queries) GetRepositoryByIDAndProject(ctx context.Context, arg GetReposi var i Repository err := row.Scan( &i.ID, - &i.Provider, &i.ProjectID, + &i.Provider, + &i.ProviderID, &i.RepoOwner, &i.RepoName, &i.RepoID, @@ -239,18 +150,15 @@ func (q *Queries) GetRepositoryByIDAndProject(ctx context.Context, arg GetReposi &i.WebhookUrl, &i.DeployUrl, &i.CloneUrl, - &i.CreatedAt, - &i.UpdatedAt, &i.DefaultBranch, &i.License, - &i.ProviderID, - &i.ReminderLastSent, + &i.CreatedAt, ) return i, err } const getRepositoryByRepoID = `-- name: GetRepositoryByRepoID :one -SELECT id, provider, project_id, repo_owner, repo_name, repo_id, is_private, is_fork, webhook_id, webhook_url, deploy_url, clone_url, created_at, updated_at, default_branch, license, provider_id, reminder_last_sent FROM repositories WHERE repo_id = $1 +SELECT id, project_id, provider, provider_id, repo_owner, repo_name, repo_id, is_private, is_fork, webhook_id, webhook_url, deploy_url, clone_url, default_branch, license, created_at FROM repositories WHERE repo_id = $1 ` func (q *Queries) GetRepositoryByRepoID(ctx context.Context, repoID int64) (Repository, error) { @@ -258,8 +166,9 @@ func (q *Queries) GetRepositoryByRepoID(ctx context.Context, repoID int64) (Repo var i Repository err := row.Scan( &i.ID, - &i.Provider, &i.ProjectID, + &i.Provider, + &i.ProviderID, &i.RepoOwner, &i.RepoName, &i.RepoID, @@ -269,18 +178,15 @@ func (q *Queries) GetRepositoryByRepoID(ctx context.Context, repoID int64) (Repo &i.WebhookUrl, &i.DeployUrl, &i.CloneUrl, - &i.CreatedAt, - &i.UpdatedAt, &i.DefaultBranch, &i.License, - &i.ProviderID, - &i.ReminderLastSent, + &i.CreatedAt, ) return i, err } const getRepositoryByRepoName = `-- name: GetRepositoryByRepoName :one -SELECT id, provider, project_id, repo_owner, repo_name, repo_id, is_private, is_fork, webhook_id, webhook_url, deploy_url, clone_url, created_at, updated_at, default_branch, license, provider_id, reminder_last_sent FROM repositories +SELECT id, project_id, provider, provider_id, repo_owner, repo_name, repo_id, is_private, is_fork, webhook_id, webhook_url, deploy_url, clone_url, default_branch, license, created_at FROM repositories WHERE repo_owner = $1 AND repo_name = $2 AND project_id = $3 AND (lower(provider) = lower($4::text) OR $4::text IS NULL) ` @@ -302,8 +208,9 @@ func (q *Queries) GetRepositoryByRepoName(ctx context.Context, arg GetRepository var i Repository err := row.Scan( &i.ID, - &i.Provider, &i.ProjectID, + &i.Provider, + &i.ProviderID, &i.RepoOwner, &i.RepoName, &i.RepoID, @@ -313,18 +220,15 @@ func (q *Queries) GetRepositoryByRepoName(ctx context.Context, arg GetRepository &i.WebhookUrl, &i.DeployUrl, &i.CloneUrl, - &i.CreatedAt, - &i.UpdatedAt, &i.DefaultBranch, &i.License, - &i.ProviderID, - &i.ReminderLastSent, + &i.CreatedAt, ) return i, err } const listRegisteredRepositoriesByProjectIDAndProvider = `-- name: ListRegisteredRepositoriesByProjectIDAndProvider :many -SELECT id, provider, project_id, repo_owner, repo_name, repo_id, is_private, is_fork, webhook_id, webhook_url, deploy_url, clone_url, created_at, updated_at, default_branch, license, provider_id, reminder_last_sent FROM repositories +SELECT id, project_id, provider, provider_id, repo_owner, repo_name, repo_id, is_private, is_fork, webhook_id, webhook_url, deploy_url, clone_url, default_branch, license, created_at FROM repositories WHERE project_id = $1 AND webhook_id IS NOT NULL AND (lower(provider) = lower($2::text) OR $2::text IS NULL) ORDER BY repo_name @@ -346,8 +250,9 @@ func (q *Queries) ListRegisteredRepositoriesByProjectIDAndProvider(ctx context.C var i Repository if err := rows.Scan( &i.ID, - &i.Provider, &i.ProjectID, + &i.Provider, + &i.ProviderID, &i.RepoOwner, &i.RepoName, &i.RepoID, @@ -357,12 +262,9 @@ func (q *Queries) ListRegisteredRepositoriesByProjectIDAndProvider(ctx context.C &i.WebhookUrl, &i.DeployUrl, &i.CloneUrl, - &i.CreatedAt, - &i.UpdatedAt, &i.DefaultBranch, &i.License, - &i.ProviderID, - &i.ReminderLastSent, + &i.CreatedAt, ); err != nil { return nil, err } @@ -378,7 +280,7 @@ func (q *Queries) ListRegisteredRepositoriesByProjectIDAndProvider(ctx context.C } const listRepositoriesAfterID = `-- name: ListRepositoriesAfterID :many -SELECT id, provider, project_id, repo_owner, repo_name, repo_id, is_private, is_fork, webhook_id, webhook_url, deploy_url, clone_url, created_at, updated_at, default_branch, license, provider_id, reminder_last_sent +SELECT id, project_id, provider, provider_id, repo_owner, repo_name, repo_id, is_private, is_fork, webhook_id, webhook_url, deploy_url, clone_url, default_branch, license, created_at FROM repositories WHERE id > $1 ORDER BY id @@ -401,8 +303,9 @@ func (q *Queries) ListRepositoriesAfterID(ctx context.Context, arg ListRepositor var i Repository if err := rows.Scan( &i.ID, - &i.Provider, &i.ProjectID, + &i.Provider, + &i.ProviderID, &i.RepoOwner, &i.RepoName, &i.RepoID, @@ -412,12 +315,9 @@ func (q *Queries) ListRepositoriesAfterID(ctx context.Context, arg ListRepositor &i.WebhookUrl, &i.DeployUrl, &i.CloneUrl, - &i.CreatedAt, - &i.UpdatedAt, &i.DefaultBranch, &i.License, - &i.ProviderID, - &i.ReminderLastSent, + &i.CreatedAt, ); err != nil { return nil, err } @@ -433,7 +333,7 @@ func (q *Queries) ListRepositoriesAfterID(ctx context.Context, arg ListRepositor } const listRepositoriesByProjectID = `-- name: ListRepositoriesByProjectID :many -SELECT id, provider, project_id, repo_owner, repo_name, repo_id, is_private, is_fork, webhook_id, webhook_url, deploy_url, clone_url, created_at, updated_at, default_branch, license, provider_id, reminder_last_sent FROM repositories +SELECT id, project_id, provider, provider_id, repo_owner, repo_name, repo_id, is_private, is_fork, webhook_id, webhook_url, deploy_url, clone_url, default_branch, license, created_at FROM repositories WHERE project_id = $1 AND (repo_id >= $2 OR $2 IS NULL) AND lower(provider) = lower(COALESCE($3, provider)::text) @@ -464,8 +364,9 @@ func (q *Queries) ListRepositoriesByProjectID(ctx context.Context, arg ListRepos var i Repository if err := rows.Scan( &i.ID, - &i.Provider, &i.ProjectID, + &i.Provider, + &i.ProviderID, &i.RepoOwner, &i.RepoName, &i.RepoID, @@ -475,12 +376,9 @@ func (q *Queries) ListRepositoriesByProjectID(ctx context.Context, arg ListRepos &i.WebhookUrl, &i.DeployUrl, &i.CloneUrl, - &i.CreatedAt, - &i.UpdatedAt, &i.DefaultBranch, &i.License, - &i.ProviderID, - &i.ReminderLastSent, + &i.CreatedAt, ); err != nil { return nil, err } @@ -509,14 +407,3 @@ func (q *Queries) RepositoryExistsAfterID(ctx context.Context, id uuid.UUID) (bo err := row.Scan(&exists) return exists, err } - -const updateReminderLastSentForRepositories = `-- name: UpdateReminderLastSentForRepositories :exec -UPDATE repositories -SET reminder_last_sent = NOW() -WHERE id = ANY ($1::uuid[]) -` - -func (q *Queries) UpdateReminderLastSentForRepositories(ctx context.Context, repositoryIds []uuid.UUID) error { - _, err := q.db.ExecContext(ctx, updateReminderLastSentForRepositories, pq.Array(repositoryIds)) - return err -} diff --git a/internal/db/store.go b/internal/db/store.go index 987bfd828b..4ccdce5f2b 100644 --- a/internal/db/store.go +++ b/internal/db/store.go @@ -116,7 +116,6 @@ func (q *Queries) GetRuleEvaluationByProfileIdAndRuleType( ) (*ListRuleEvaluationsByProfileIdRow, error) { params := ListRuleEvaluationsByProfileIdParams{ ProfileID: profileID, - EntityType: entityType, EntityID: entityID, RuleName: ruleName, RuleTypeName: ruleTypeName,