From 17699388829dcadd14e6ee9889f58ab10dd23b64 Mon Sep 17 00:00:00 2001 From: Juan Antonio Osorio Date: Mon, 26 Aug 2024 16:00:10 +0300 Subject: [PATCH 1/2] Remove per-entity tables Signed-off-by: Juan Antonio Osorio --- .../000101_remove_entities_tables.down.sql | 0 .../000101_remove_entities_tables.up.sql | 23 + database/mock/store.go | 371 ------------- database/query/artifacts.sql | 34 -- database/query/eval_history.sql | 60 +- database/query/profile_status.sql | 21 +- database/query/pull_requests.sql | 28 - database/query/repositories.sql | 88 --- internal/db/artifacts.sql.go | 174 ------ internal/db/eval_history.sql.go | 100 +--- internal/db/models.go | 42 -- internal/db/profile_status.sql.go | 45 +- internal/db/pull_requests.sql.go | 123 ----- internal/db/querier.go | 28 - internal/db/repositories.sql.go | 522 ------------------ internal/db/store.go | 1 - 16 files changed, 71 insertions(+), 1589 deletions(-) create mode 100644 database/migrations/000101_remove_entities_tables.down.sql create mode 100644 database/migrations/000101_remove_entities_tables.up.sql delete mode 100644 database/query/artifacts.sql delete mode 100644 database/query/pull_requests.sql delete mode 100644 database/query/repositories.sql delete mode 100644 internal/db/artifacts.sql.go delete mode 100644 internal/db/pull_requests.sql.go delete mode 100644 internal/db/repositories.sql.go diff --git a/database/migrations/000101_remove_entities_tables.down.sql b/database/migrations/000101_remove_entities_tables.down.sql new file mode 100644 index 0000000000..e69de29bb2 diff --git a/database/migrations/000101_remove_entities_tables.up.sql b/database/migrations/000101_remove_entities_tables.up.sql new file mode 100644 index 0000000000..df7d94f6a6 --- /dev/null +++ b/database/migrations/000101_remove_entities_tables.up.sql @@ -0,0 +1,23 @@ +-- 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; + +COMMIT; \ No newline at end of file diff --git a/database/mock/store.go b/database/mock/store.go index 72dbbb4b89..9d314956c2 100644 --- a/database/mock/store.go +++ b/database/mock/store.go @@ -131,21 +131,6 @@ func (mr *MockStoreMockRecorder) CountProfilesByName(arg0, arg1 any) *gomock.Cal return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CountProfilesByName", reflect.TypeOf((*MockStore)(nil).CountProfilesByName), arg0, arg1) } -// CountRepositories mocks base method. -func (m *MockStore) CountRepositories(arg0 context.Context) (int64, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "CountRepositories", arg0) - ret0, _ := ret[0].(int64) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// CountRepositories indicates an expected call of CountRepositories. -func (mr *MockStoreMockRecorder) CountRepositories(arg0 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CountRepositories", reflect.TypeOf((*MockStore)(nil).CountRepositories), arg0) -} - // CountUsers mocks base method. func (m *MockStore) CountUsers(arg0 context.Context) (int64, error) { m.ctrl.T.Helper() @@ -296,36 +281,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 +370,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 +542,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() @@ -834,36 +747,6 @@ func (mr *MockStoreMockRecorder) GetAllPropertyValuesV1(arg0, arg1 any) *gomock. return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAllPropertyValuesV1", reflect.TypeOf((*MockStore)(nil).GetAllPropertyValuesV1), arg0, arg1) } -// GetArtifactByID mocks base method. -func (m *MockStore) GetArtifactByID(arg0 context.Context, arg1 db.GetArtifactByIDParams) (db.Artifact, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetArtifactByID", arg0, arg1) - ret0, _ := ret[0].(db.Artifact) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// GetArtifactByID indicates an expected call of GetArtifactByID. -func (mr *MockStoreMockRecorder) GetArtifactByID(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetArtifactByID", reflect.TypeOf((*MockStore)(nil).GetArtifactByID), arg0, arg1) -} - -// GetArtifactByName mocks base method. -func (m *MockStore) GetArtifactByName(arg0 context.Context, arg1 db.GetArtifactByNameParams) (db.Artifact, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetArtifactByName", arg0, arg1) - ret0, _ := ret[0].(db.Artifact) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// GetArtifactByName indicates an expected call of GetArtifactByName. -func (mr *MockStoreMockRecorder) GetArtifactByName(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetArtifactByName", reflect.TypeOf((*MockStore)(nil).GetArtifactByName), arg0, arg1) -} - // GetBundle mocks base method. func (m *MockStore) GetBundle(arg0 context.Context, arg1 db.GetBundleParams) (db.Bundle, error) { m.ctrl.T.Helper() @@ -1359,51 +1242,6 @@ func (mr *MockStoreMockRecorder) GetProviderByName(arg0, arg1 any) *gomock.Call return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetProviderByName", reflect.TypeOf((*MockStore)(nil).GetProviderByName), arg0, arg1) } -// GetProviderWebhooks mocks base method. -func (m *MockStore) GetProviderWebhooks(arg0 context.Context, arg1 uuid.UUID) ([]db.GetProviderWebhooksRow, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetProviderWebhooks", arg0, arg1) - ret0, _ := ret[0].([]db.GetProviderWebhooksRow) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// GetProviderWebhooks indicates an expected call of GetProviderWebhooks. -func (mr *MockStoreMockRecorder) GetProviderWebhooks(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetProviderWebhooks", reflect.TypeOf((*MockStore)(nil).GetProviderWebhooks), arg0, arg1) -} - -// GetPullRequest mocks base method. -func (m *MockStore) GetPullRequest(arg0 context.Context, arg1 db.GetPullRequestParams) (db.PullRequest, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetPullRequest", arg0, arg1) - ret0, _ := ret[0].(db.PullRequest) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// GetPullRequest indicates an expected call of GetPullRequest. -func (mr *MockStoreMockRecorder) GetPullRequest(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetPullRequest", reflect.TypeOf((*MockStore)(nil).GetPullRequest), arg0, arg1) -} - -// GetPullRequestByID mocks base method. -func (m *MockStore) GetPullRequestByID(arg0 context.Context, arg1 uuid.UUID) (db.PullRequest, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetPullRequestByID", arg0, arg1) - ret0, _ := ret[0].(db.PullRequest) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// GetPullRequestByID indicates an expected call of GetPullRequestByID. -func (mr *MockStoreMockRecorder) GetPullRequestByID(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetPullRequestByID", reflect.TypeOf((*MockStore)(nil).GetPullRequestByID), arg0, arg1) -} - // GetQuerierWithTransaction mocks base method. func (m *MockStore) GetQuerierWithTransaction(arg0 *sql.Tx) db.ExtendQuerier { m.ctrl.T.Helper() @@ -1418,96 +1256,6 @@ func (mr *MockStoreMockRecorder) GetQuerierWithTransaction(arg0 any) *gomock.Cal return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetQuerierWithTransaction", reflect.TypeOf((*MockStore)(nil).GetQuerierWithTransaction), arg0) } -// GetRepoPathFromArtifactID mocks base method. -func (m *MockStore) GetRepoPathFromArtifactID(arg0 context.Context, arg1 uuid.UUID) (db.GetRepoPathFromArtifactIDRow, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetRepoPathFromArtifactID", arg0, arg1) - ret0, _ := ret[0].(db.GetRepoPathFromArtifactIDRow) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// GetRepoPathFromArtifactID indicates an expected call of GetRepoPathFromArtifactID. -func (mr *MockStoreMockRecorder) GetRepoPathFromArtifactID(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetRepoPathFromArtifactID", reflect.TypeOf((*MockStore)(nil).GetRepoPathFromArtifactID), arg0, arg1) -} - -// GetRepoPathFromPullRequestID mocks base method. -func (m *MockStore) GetRepoPathFromPullRequestID(arg0 context.Context, arg1 uuid.UUID) (db.GetRepoPathFromPullRequestIDRow, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetRepoPathFromPullRequestID", arg0, arg1) - ret0, _ := ret[0].(db.GetRepoPathFromPullRequestIDRow) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// GetRepoPathFromPullRequestID indicates an expected call of GetRepoPathFromPullRequestID. -func (mr *MockStoreMockRecorder) GetRepoPathFromPullRequestID(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetRepoPathFromPullRequestID", reflect.TypeOf((*MockStore)(nil).GetRepoPathFromPullRequestID), arg0, arg1) -} - -// GetRepositoryByID mocks base method. -func (m *MockStore) GetRepositoryByID(arg0 context.Context, arg1 uuid.UUID) (db.Repository, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetRepositoryByID", arg0, arg1) - ret0, _ := ret[0].(db.Repository) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// GetRepositoryByID indicates an expected call of GetRepositoryByID. -func (mr *MockStoreMockRecorder) GetRepositoryByID(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetRepositoryByID", reflect.TypeOf((*MockStore)(nil).GetRepositoryByID), arg0, arg1) -} - -// GetRepositoryByIDAndProject mocks base method. -func (m *MockStore) GetRepositoryByIDAndProject(arg0 context.Context, arg1 db.GetRepositoryByIDAndProjectParams) (db.Repository, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetRepositoryByIDAndProject", arg0, arg1) - ret0, _ := ret[0].(db.Repository) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// GetRepositoryByIDAndProject indicates an expected call of GetRepositoryByIDAndProject. -func (mr *MockStoreMockRecorder) GetRepositoryByIDAndProject(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetRepositoryByIDAndProject", reflect.TypeOf((*MockStore)(nil).GetRepositoryByIDAndProject), arg0, arg1) -} - -// GetRepositoryByRepoID mocks base method. -func (m *MockStore) GetRepositoryByRepoID(arg0 context.Context, arg1 int64) (db.Repository, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetRepositoryByRepoID", arg0, arg1) - ret0, _ := ret[0].(db.Repository) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// GetRepositoryByRepoID indicates an expected call of GetRepositoryByRepoID. -func (mr *MockStoreMockRecorder) GetRepositoryByRepoID(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetRepositoryByRepoID", reflect.TypeOf((*MockStore)(nil).GetRepositoryByRepoID), arg0, arg1) -} - -// GetRepositoryByRepoName mocks base method. -func (m *MockStore) GetRepositoryByRepoName(arg0 context.Context, arg1 db.GetRepositoryByRepoNameParams) (db.Repository, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetRepositoryByRepoName", arg0, arg1) - ret0, _ := ret[0].(db.Repository) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// GetRepositoryByRepoName indicates an expected call of GetRepositoryByRepoName. -func (mr *MockStoreMockRecorder) GetRepositoryByRepoName(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetRepositoryByRepoName", reflect.TypeOf((*MockStore)(nil).GetRepositoryByRepoName), arg0, arg1) -} - // GetRuleEvaluationByProfileIdAndRuleType mocks base method. func (m *MockStore) GetRuleEvaluationByProfileIdAndRuleType(arg0 context.Context, arg1 uuid.UUID, arg2 db.NullEntities, arg3 sql.NullString, arg4 uuid.NullUUID, arg5 sql.NullString) (*db.ListRuleEvaluationsByProfileIdRow, error) { m.ctrl.T.Helper() @@ -1806,21 +1554,6 @@ func (mr *MockStoreMockRecorder) InsertRemediationEvent(arg0, arg1 any) *gomock. return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InsertRemediationEvent", reflect.TypeOf((*MockStore)(nil).InsertRemediationEvent), arg0, arg1) } -// ListArtifactsByRepoID mocks base method. -func (m *MockStore) ListArtifactsByRepoID(arg0 context.Context, arg1 uuid.NullUUID) ([]db.Artifact, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ListArtifactsByRepoID", arg0, arg1) - ret0, _ := ret[0].([]db.Artifact) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// ListArtifactsByRepoID indicates an expected call of ListArtifactsByRepoID. -func (mr *MockStoreMockRecorder) ListArtifactsByRepoID(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListArtifactsByRepoID", reflect.TypeOf((*MockStore)(nil).ListArtifactsByRepoID), arg0, arg1) -} - // ListEvaluationHistory mocks base method. func (m *MockStore) ListEvaluationHistory(arg0 context.Context, arg1 db.ListEvaluationHistoryParams) ([]db.ListEvaluationHistoryRow, error) { m.ctrl.T.Helper() @@ -1956,51 +1689,6 @@ func (mr *MockStoreMockRecorder) ListProvidersByProjectIDPaginated(arg0, arg1 an return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListProvidersByProjectIDPaginated", reflect.TypeOf((*MockStore)(nil).ListProvidersByProjectIDPaginated), arg0, arg1) } -// ListRegisteredRepositoriesByProjectIDAndProvider mocks base method. -func (m *MockStore) ListRegisteredRepositoriesByProjectIDAndProvider(arg0 context.Context, arg1 db.ListRegisteredRepositoriesByProjectIDAndProviderParams) ([]db.Repository, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ListRegisteredRepositoriesByProjectIDAndProvider", arg0, arg1) - ret0, _ := ret[0].([]db.Repository) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// ListRegisteredRepositoriesByProjectIDAndProvider indicates an expected call of ListRegisteredRepositoriesByProjectIDAndProvider. -func (mr *MockStoreMockRecorder) ListRegisteredRepositoriesByProjectIDAndProvider(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListRegisteredRepositoriesByProjectIDAndProvider", reflect.TypeOf((*MockStore)(nil).ListRegisteredRepositoriesByProjectIDAndProvider), arg0, arg1) -} - -// ListRepositoriesAfterID mocks base method. -func (m *MockStore) ListRepositoriesAfterID(arg0 context.Context, arg1 db.ListRepositoriesAfterIDParams) ([]db.Repository, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ListRepositoriesAfterID", arg0, arg1) - ret0, _ := ret[0].([]db.Repository) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// ListRepositoriesAfterID indicates an expected call of ListRepositoriesAfterID. -func (mr *MockStoreMockRecorder) ListRepositoriesAfterID(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListRepositoriesAfterID", reflect.TypeOf((*MockStore)(nil).ListRepositoriesAfterID), arg0, arg1) -} - -// ListRepositoriesByProjectID mocks base method. -func (m *MockStore) ListRepositoriesByProjectID(arg0 context.Context, arg1 db.ListRepositoriesByProjectIDParams) ([]db.Repository, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ListRepositoriesByProjectID", arg0, arg1) - ret0, _ := ret[0].([]db.Repository) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// ListRepositoriesByProjectID indicates an expected call of ListRepositoriesByProjectID. -func (mr *MockStoreMockRecorder) ListRepositoriesByProjectID(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListRepositoriesByProjectID", reflect.TypeOf((*MockStore)(nil).ListRepositoriesByProjectID), arg0, arg1) -} - // ListRuleEvaluationsByProfileId mocks base method. func (m *MockStore) ListRuleEvaluationsByProfileId(arg0 context.Context, arg1 db.ListRuleEvaluationsByProfileIdParams) ([]db.ListRuleEvaluationsByProfileIdRow, error) { m.ctrl.T.Helper() @@ -2105,21 +1793,6 @@ func (mr *MockStoreMockRecorder) ReleaseLock(arg0, arg1 any) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReleaseLock", reflect.TypeOf((*MockStore)(nil).ReleaseLock), arg0, arg1) } -// RepositoryExistsAfterID mocks base method. -func (m *MockStore) RepositoryExistsAfterID(arg0 context.Context, arg1 uuid.UUID) (bool, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "RepositoryExistsAfterID", arg0, arg1) - ret0, _ := ret[0].(bool) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// RepositoryExistsAfterID indicates an expected call of RepositoryExistsAfterID. -func (mr *MockStoreMockRecorder) RepositoryExistsAfterID(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RepositoryExistsAfterID", reflect.TypeOf((*MockStore)(nil).RepositoryExistsAfterID), arg0, arg1) -} - // Rollback mocks base method. func (m *MockStore) Rollback(arg0 *sql.Tx) error { m.ctrl.T.Helper() @@ -2235,20 +1908,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 +1953,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 +2041,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 deleted file mode 100644 index d29cd0642f..0000000000 --- a/database/query/artifacts.sql +++ /dev/null @@ -1,34 +0,0 @@ --- 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; - --- name: GetArtifactByName :one -SELECT * FROM artifacts -WHERE lower(artifacts.artifact_name) = lower(sqlc.arg(artifact_name)) -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 diff --git a/database/query/eval_history.sql b/database/query/eval_history.sql index 68879e5f1f..3c33b6da7a 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 @@ -145,21 +131,9 @@ WHERE s.id = sqlc.arg(evaluation_id) AND j.id = sqlc.arg(project_id); 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, @@ -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 + 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 @@ -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 deleted file mode 100644 index 89a871608d..0000000000 --- a/database/query/pull_requests.sql +++ /dev/null @@ -1,28 +0,0 @@ --- 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 diff --git a/database/query/repositories.sql b/database/query/repositories.sql deleted file mode 100644 index 5ac412f6fb..0000000000 --- a/database/query/repositories.sql +++ /dev/null @@ -1,88 +0,0 @@ --- 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; - --- name: GetRepositoryByRepoName :one -SELECT * FROM repositories - WHERE repo_owner = $1 AND repo_name = $2 AND project_id = $3 - AND (lower(provider) = lower(sqlc.narg('provider')::text) OR sqlc.narg('provider')::text IS NULL); - --- avoid using this, where possible use GetRepositoryByIDAndProject instead --- name: GetRepositoryByID :one -SELECT * FROM repositories WHERE id = $1; - --- name: GetRepositoryByIDAndProject :one -SELECT * FROM repositories WHERE id = $1 AND project_id = $2; - --- name: ListRepositoriesByProjectID :many -SELECT * FROM repositories -WHERE project_id = $1 - AND (repo_id >= sqlc.narg('repo_id') OR sqlc.narg('repo_id') IS NULL) - AND lower(provider) = lower(COALESCE(sqlc.narg('provider'), provider)::text) -ORDER BY project_id, provider, repo_id -LIMIT sqlc.narg('limit')::bigint; - --- name: ListRegisteredRepositoriesByProjectIDAndProvider :many -SELECT * FROM repositories -WHERE project_id = $1 AND webhook_id IS NOT NULL - AND (lower(provider) = lower(sqlc.narg('provider')::text) OR sqlc.narg('provider')::text IS NULL) -ORDER BY repo_name; - --- name: ListRepositoriesAfterID :many -SELECT * -FROM repositories -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 - FROM repositories - WHERE id > $1) -AS exists; - --- name: DeleteRepository :exec -DELETE FROM repositories -WHERE id = $1; - --- name: CountRepositories :one -SELECT COUNT(*) FROM repositories; - --- get a list of repos with webhooks belonging to a provider --- is used for webhook cleanup during provider deletion --- name: GetProviderWebhooks :many -SELECT repo_owner, repo_name, webhook_id FROM repositories -WHERE webhook_id IS NOT NULL AND provider_id = $1; - --- name: GetRepoPathFromArtifactID :one -SELECT r.repo_owner AS owner , r.repo_name AS name FROM repositories AS r -JOIN artifacts AS a ON a.repository_id = r.id -WHERE a.id = $1; - --- name: GetRepoPathFromPullRequestID :one -SELECT r.repo_owner AS owner , r.repo_name AS name FROM repositories AS r -JOIN pull_requests AS p ON p.repository_id = r.id -WHERE p.id = $1; \ No newline at end of file diff --git a/internal/db/artifacts.sql.go b/internal/db/artifacts.sql.go deleted file mode 100644 index 4c01cc72fd..0000000000 --- a/internal/db/artifacts.sql.go +++ /dev/null @@ -1,174 +0,0 @@ -// Code generated by sqlc. DO NOT EDIT. -// versions: -// sqlc v1.27.0 -// source: artifacts.sql - -package db - -import ( - "context" - - "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 -WHERE artifacts.id = $1 AND artifacts.project_id = $2 -` - -type GetArtifactByIDParams struct { - ID uuid.UUID `json:"id"` - ProjectID uuid.UUID `json:"project_id"` -} - -func (q *Queries) GetArtifactByID(ctx context.Context, arg GetArtifactByIDParams) (Artifact, error) { - row := q.db.QueryRowContext(ctx, getArtifactByID, arg.ID, arg.ProjectID) - 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 -} - -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 -WHERE lower(artifacts.artifact_name) = lower($3) -AND artifacts.repository_id = $1 AND artifacts.project_id = $2 -` - -type GetArtifactByNameParams struct { - RepositoryID uuid.NullUUID `json:"repository_id"` - ProjectID uuid.UUID `json:"project_id"` - ArtifactName string `json:"artifact_name"` -} - -func (q *Queries) GetArtifactByName(ctx context.Context, arg GetArtifactByNameParams) (Artifact, error) { - row := q.db.QueryRowContext(ctx, getArtifactByName, arg.RepositoryID, arg.ProjectID, arg.ArtifactName) - 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 -} - -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 -WHERE repository_id = $1 -ORDER BY id -` - -func (q *Queries) ListArtifactsByRepoID(ctx context.Context, repositoryID uuid.NullUUID) ([]Artifact, error) { - rows, err := q.db.QueryContext(ctx, listArtifactsByRepoID, repositoryID) - if err != nil { - return nil, err - } - defer rows.Close() - items := []Artifact{} - for rows.Next() { - var i Artifact - if err := rows.Scan( - &i.ID, - &i.RepositoryID, - &i.ArtifactName, - &i.ArtifactType, - &i.ArtifactVisibility, - &i.CreatedAt, - &i.UpdatedAt, - &i.ProjectID, - &i.ProviderID, - &i.ProviderName, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Close(); err != nil { - return nil, err - } - if err := rows.Err(); err != nil { - return nil, err - } - 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..9d94b0b9c6 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, @@ -337,21 +313,9 @@ 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, + 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 + 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 @@ -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 sql.NullString `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/models.go b/internal/db/models.go index e40f2822c1..3a68950b5d 100644 --- a/internal/db/models.go +++ b/internal/db/models.go @@ -476,19 +476,6 @@ type AlertEvent struct { CreatedAt time.Time `json:"created_at"` } -type Artifact struct { - ID uuid.UUID `json:"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 { ID uuid.UUID `json:"id"` Namespace string `json:"namespace"` @@ -673,14 +660,6 @@ type ProviderGithubAppInstallation struct { IsOrg bool `json:"is_org"` } -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"` -} - type RemediationEvent struct { ID uuid.UUID `json:"id"` EvaluationID uuid.UUID `json:"evaluation_id"` @@ -690,27 +669,6 @@ type RemediationEvent struct { CreatedAt time.Time `json:"created_at"` } -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"` -} - type RuleInstance struct { ID uuid.UUID `json:"id"` ProfileID uuid.UUID `json:"profile_id"` 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/pull_requests.sql.go b/internal/db/pull_requests.sql.go deleted file mode 100644 index bb3887720a..0000000000 --- a/internal/db/pull_requests.sql.go +++ /dev/null @@ -1,123 +0,0 @@ -// Code generated by sqlc. DO NOT EDIT. -// versions: -// sqlc v1.27.0 -// source: pull_requests.sql - -package db - -import ( - "context" - - "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 -WHERE repository_id = $1 AND pr_number = $2 -` - -type GetPullRequestParams struct { - RepositoryID uuid.UUID `json:"repository_id"` - PrNumber int64 `json:"pr_number"` -} - -func (q *Queries) GetPullRequest(ctx context.Context, arg GetPullRequestParams) (PullRequest, error) { - row := q.db.QueryRowContext(ctx, getPullRequest, arg.RepositoryID, arg.PrNumber) - var i PullRequest - err := row.Scan( - &i.ID, - &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 -WHERE id = $1 -` - -func (q *Queries) GetPullRequestByID(ctx context.Context, id uuid.UUID) (PullRequest, error) { - row := q.db.QueryRowContext(ctx, getPullRequestByID, id) - var i PullRequest - err := row.Scan( - &i.ID, - &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..ae92770305 100644 --- a/internal/db/querier.go +++ b/internal/db/querier.go @@ -16,7 +16,6 @@ type Querier interface { BulkGetProfilesByID(ctx context.Context, profileIds []uuid.UUID) ([]BulkGetProfilesByIDRow, error) CountProfilesByEntityType(ctx context.Context) ([]CountProfilesByEntityTypeRow, error) CountProfilesByName(ctx context.Context, name string) (int64, error) - CountRepositories(ctx context.Context) (int64, error) CountUsers(ctx context.Context) (int64, error) // CreateEntity adds an entry to the entity_instances table so it can be tracked by Minder. CreateEntity(ctx context.Context, arg CreateEntityParams) (EntityInstance, error) @@ -35,8 +34,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 +41,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 +58,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 @@ -80,8 +74,6 @@ type Querier interface { GetAccessTokenByProvider(ctx context.Context, provider string) ([]ProviderAccessToken, error) GetAccessTokenSinceDate(ctx context.Context, arg GetAccessTokenSinceDateParams) (ProviderAccessToken, error) GetAllPropertiesForEntity(ctx context.Context, entityID uuid.UUID) ([]Property, error) - GetArtifactByID(ctx context.Context, arg GetArtifactByIDParams) (Artifact, error) - GetArtifactByName(ctx context.Context, arg GetArtifactByNameParams) (Artifact, error) GetBundle(ctx context.Context, arg GetBundleParams) (Bundle, error) GetChildrenProjects(ctx context.Context, id uuid.UUID) ([]GetChildrenProjectsRow, error) // GetEntitiesByType retrieves all entities of a given type for a project or hierarchy of projects. @@ -150,18 +142,6 @@ type Querier interface { // if it exists in the project or any of its ancestors. It'll return the first // provider that matches the name. GetProviderByName(ctx context.Context, arg GetProviderByNameParams) (Provider, error) - // get a list of repos with webhooks belonging to a provider - // is used for webhook cleanup during provider deletion - GetProviderWebhooks(ctx context.Context, providerID uuid.UUID) ([]GetProviderWebhooksRow, error) - GetPullRequest(ctx context.Context, arg GetPullRequestParams) (PullRequest, error) - GetPullRequestByID(ctx context.Context, id uuid.UUID) (PullRequest, error) - GetRepoPathFromArtifactID(ctx context.Context, id uuid.UUID) (GetRepoPathFromArtifactIDRow, error) - GetRepoPathFromPullRequestID(ctx context.Context, id uuid.UUID) (GetRepoPathFromPullRequestIDRow, error) - // avoid using this, where possible use GetRepositoryByIDAndProject instead - GetRepositoryByID(ctx context.Context, id uuid.UUID) (Repository, error) - GetRepositoryByIDAndProject(ctx context.Context, arg GetRepositoryByIDAndProjectParams) (Repository, error) - GetRepositoryByRepoID(ctx context.Context, repoID int64) (Repository, error) - GetRepositoryByRepoName(ctx context.Context, arg GetRepositoryByRepoNameParams) (Repository, error) GetRuleInstancesEntityInProjects(ctx context.Context, arg GetRuleInstancesEntityInProjectsParams) ([]RuleInstance, error) GetRuleInstancesForProfile(ctx context.Context, profileID uuid.UUID) ([]RuleInstance, error) GetRuleTypeByID(ctx context.Context, id uuid.UUID) (RuleType, error) @@ -185,7 +165,6 @@ type Querier interface { InsertEvaluationRuleEntity(ctx context.Context, arg InsertEvaluationRuleEntityParams) (uuid.UUID, error) InsertEvaluationStatus(ctx context.Context, arg InsertEvaluationStatusParams) (uuid.UUID, error) InsertRemediationEvent(ctx context.Context, arg InsertRemediationEventParams) error - ListArtifactsByRepoID(ctx context.Context, repositoryID uuid.NullUUID) ([]Artifact, error) ListEvaluationHistory(ctx context.Context, arg ListEvaluationHistoryParams) ([]ListEvaluationHistoryRow, error) ListEvaluationHistoryStaleRecords(ctx context.Context, arg ListEvaluationHistoryStaleRecordsParams) ([]ListEvaluationHistoryStaleRecordsRow, error) ListFlushCache(ctx context.Context) ([]FlushCache, error) @@ -205,9 +184,6 @@ type Querier interface { // ListProvidersByProjectIDPaginated allows us to lits all providers for a given project // with pagination taken into account. In this case, the cursor is the creation date. ListProvidersByProjectIDPaginated(ctx context.Context, arg ListProvidersByProjectIDPaginatedParams) ([]Provider, error) - ListRegisteredRepositoriesByProjectIDAndProvider(ctx context.Context, arg ListRegisteredRepositoriesByProjectIDAndProviderParams) ([]Repository, error) - ListRepositoriesAfterID(ctx context.Context, arg ListRepositoriesAfterIDParams) ([]Repository, error) - ListRepositoriesByProjectID(ctx context.Context, arg ListRepositoriesByProjectIDParams) ([]Repository, error) ListRuleEvaluationsByProfileId(ctx context.Context, arg ListRuleEvaluationsByProfileIdParams) ([]ListRuleEvaluationsByProfileIdRow, error) ListRuleTypesByProject(ctx context.Context, projectID uuid.UUID) ([]RuleType, error) // When doing a key/algorithm rotation, identify the secrets which need to be @@ -232,7 +208,6 @@ type Querier interface { // entity_execution_lock record if the lock is held by the given locked_by // value. ReleaseLock(ctx context.Context, arg ReleaseLockParams) error - RepositoryExistsAfterID(ctx context.Context, id uuid.UUID) (bool, error) SetCurrentVersion(ctx context.Context, arg SetCurrentVersionParams) error UpdateEncryptedSecret(ctx context.Context, arg UpdateEncryptedSecretParams) error // UpdateInvitationRole updates an invitation by its code. This is intended to be @@ -243,11 +218,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 +240,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 deleted file mode 100644 index 902aa56447..0000000000 --- a/internal/db/repositories.sql.go +++ /dev/null @@ -1,522 +0,0 @@ -// Code generated by sqlc. DO NOT EDIT. -// versions: -// sqlc v1.27.0 -// source: repositories.sql - -package db - -import ( - "context" - "database/sql" - - "github.com/google/uuid" - "github.com/lib/pq" -) - -const countRepositories = `-- name: CountRepositories :one -SELECT COUNT(*) FROM repositories -` - -func (q *Queries) CountRepositories(ctx context.Context) (int64, error) { - row := q.db.QueryRowContext(ctx, countRepositories) - var count int64 - err := row.Scan(&count) - 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"` -} - -// get a list of repos with webhooks belonging to a provider -// is used for webhook cleanup during provider deletion -func (q *Queries) GetProviderWebhooks(ctx context.Context, providerID uuid.UUID) ([]GetProviderWebhooksRow, error) { - rows, err := q.db.QueryContext(ctx, getProviderWebhooks, providerID) - if err != nil { - return nil, err - } - defer rows.Close() - items := []GetProviderWebhooksRow{} - for rows.Next() { - var i GetProviderWebhooksRow - if err := rows.Scan(&i.RepoOwner, &i.RepoName, &i.WebhookID); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Close(); err != nil { - return nil, err - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const getRepoPathFromArtifactID = `-- name: GetRepoPathFromArtifactID :one -SELECT r.repo_owner AS owner , r.repo_name AS name FROM repositories AS r -JOIN artifacts AS a ON a.repository_id = r.id -WHERE a.id = $1 -` - -type GetRepoPathFromArtifactIDRow struct { - Owner string `json:"owner"` - Name string `json:"name"` -} - -func (q *Queries) GetRepoPathFromArtifactID(ctx context.Context, id uuid.UUID) (GetRepoPathFromArtifactIDRow, error) { - row := q.db.QueryRowContext(ctx, getRepoPathFromArtifactID, id) - var i GetRepoPathFromArtifactIDRow - err := row.Scan(&i.Owner, &i.Name) - return i, err -} - -const getRepoPathFromPullRequestID = `-- name: GetRepoPathFromPullRequestID :one -SELECT r.repo_owner AS owner , r.repo_name AS name FROM repositories AS r -JOIN pull_requests AS p ON p.repository_id = r.id -WHERE p.id = $1 -` - -type GetRepoPathFromPullRequestIDRow struct { - Owner string `json:"owner"` - Name string `json:"name"` -} - -func (q *Queries) GetRepoPathFromPullRequestID(ctx context.Context, id uuid.UUID) (GetRepoPathFromPullRequestIDRow, error) { - row := q.db.QueryRowContext(ctx, getRepoPathFromPullRequestID, id) - var i GetRepoPathFromPullRequestIDRow - err := row.Scan(&i.Owner, &i.Name) - return i, err -} - -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 -` - -// avoid using this, where possible use GetRepositoryByIDAndProject instead -func (q *Queries) GetRepositoryByID(ctx context.Context, id uuid.UUID) (Repository, error) { - row := q.db.QueryRowContext(ctx, getRepositoryByID, id) - 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 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 -` - -type GetRepositoryByIDAndProjectParams struct { - ID uuid.UUID `json:"id"` - ProjectID uuid.UUID `json:"project_id"` -} - -func (q *Queries) GetRepositoryByIDAndProject(ctx context.Context, arg GetRepositoryByIDAndProjectParams) (Repository, error) { - row := q.db.QueryRowContext(ctx, getRepositoryByIDAndProject, arg.ID, arg.ProjectID) - 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 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 -` - -func (q *Queries) GetRepositoryByRepoID(ctx context.Context, repoID int64) (Repository, error) { - row := q.db.QueryRowContext(ctx, getRepositoryByRepoID, repoID) - 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 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 - WHERE repo_owner = $1 AND repo_name = $2 AND project_id = $3 - AND (lower(provider) = lower($4::text) OR $4::text IS NULL) -` - -type GetRepositoryByRepoNameParams struct { - RepoOwner string `json:"repo_owner"` - RepoName string `json:"repo_name"` - ProjectID uuid.UUID `json:"project_id"` - Provider sql.NullString `json:"provider"` -} - -func (q *Queries) GetRepositoryByRepoName(ctx context.Context, arg GetRepositoryByRepoNameParams) (Repository, error) { - row := q.db.QueryRowContext(ctx, getRepositoryByRepoName, - arg.RepoOwner, - arg.RepoName, - arg.ProjectID, - arg.Provider, - ) - 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 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 -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 -` - -type ListRegisteredRepositoriesByProjectIDAndProviderParams struct { - ProjectID uuid.UUID `json:"project_id"` - Provider sql.NullString `json:"provider"` -} - -func (q *Queries) ListRegisteredRepositoriesByProjectIDAndProvider(ctx context.Context, arg ListRegisteredRepositoriesByProjectIDAndProviderParams) ([]Repository, error) { - rows, err := q.db.QueryContext(ctx, listRegisteredRepositoriesByProjectIDAndProvider, arg.ProjectID, arg.Provider) - if err != nil { - return nil, err - } - defer rows.Close() - items := []Repository{} - for rows.Next() { - var i Repository - if err := rows.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, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Close(); err != nil { - return nil, err - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -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 -FROM repositories -WHERE id > $1 -ORDER BY id -LIMIT $2::bigint -` - -type ListRepositoriesAfterIDParams struct { - ID uuid.UUID `json:"id"` - Limit int64 `json:"limit"` -} - -func (q *Queries) ListRepositoriesAfterID(ctx context.Context, arg ListRepositoriesAfterIDParams) ([]Repository, error) { - rows, err := q.db.QueryContext(ctx, listRepositoriesAfterID, arg.ID, arg.Limit) - if err != nil { - return nil, err - } - defer rows.Close() - items := []Repository{} - for rows.Next() { - var i Repository - if err := rows.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, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Close(); err != nil { - return nil, err - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -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 -WHERE project_id = $1 - AND (repo_id >= $2 OR $2 IS NULL) - AND lower(provider) = lower(COALESCE($3, provider)::text) -ORDER BY project_id, provider, repo_id -LIMIT $4::bigint -` - -type ListRepositoriesByProjectIDParams struct { - ProjectID uuid.UUID `json:"project_id"` - RepoID sql.NullInt64 `json:"repo_id"` - Provider sql.NullString `json:"provider"` - Limit sql.NullInt64 `json:"limit"` -} - -func (q *Queries) ListRepositoriesByProjectID(ctx context.Context, arg ListRepositoriesByProjectIDParams) ([]Repository, error) { - rows, err := q.db.QueryContext(ctx, listRepositoriesByProjectID, - arg.ProjectID, - arg.RepoID, - arg.Provider, - arg.Limit, - ) - if err != nil { - return nil, err - } - defer rows.Close() - items := []Repository{} - for rows.Next() { - var i Repository - if err := rows.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, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Close(); err != nil { - return nil, err - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const repositoryExistsAfterID = `-- name: RepositoryExistsAfterID :one -SELECT EXISTS ( - SELECT 1 - FROM repositories - WHERE id > $1) -AS exists -` - -func (q *Queries) RepositoryExistsAfterID(ctx context.Context, id uuid.UUID) (bool, error) { - row := q.db.QueryRowContext(ctx, repositoryExistsAfterID, id) - var exists bool - 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, From 100f2b4380ee72985364b8dc185a339fb47a20eb Mon Sep 17 00:00:00 2001 From: Juan Antonio Osorio Date: Mon, 26 Aug 2024 16:25:00 +0300 Subject: [PATCH 2/2] Add per-entity tables as views to migrate Signed-off-by: Juan Antonio Osorio --- cmd/server/app/history_purge_test.go | 58 ++- .../000101_remove_entities_tables.up.sql | 23 - ...=> 000102_remove_entities_tables.down.sql} | 0 .../000102_remove_entities_tables.up.sql | 91 ++++ database/mock/store.go | 255 +++++++++++ database/query/artifacts.sql | 13 + database/query/eval_history.sql | 4 +- database/query/pull_requests.sql | 7 + database/query/repositories.sql | 61 +++ internal/db/artifacts.sql.go | 107 +++++ internal/db/eval_history.sql.go | 6 +- internal/db/eval_history_test.go | 28 -- internal/db/models.go | 38 ++ internal/db/profiles_test.go | 5 - internal/db/pull_requests.sql.go | 51 +++ internal/db/querier.go | 20 + internal/db/repositories.sql.go | 409 ++++++++++++++++++ 17 files changed, 1098 insertions(+), 78 deletions(-) delete mode 100644 database/migrations/000101_remove_entities_tables.up.sql rename database/migrations/{000101_remove_entities_tables.down.sql => 000102_remove_entities_tables.down.sql} (100%) create mode 100644 database/migrations/000102_remove_entities_tables.up.sql create mode 100644 database/query/artifacts.sql create mode 100644 database/query/pull_requests.sql create mode 100644 database/query/repositories.sql create mode 100644 internal/db/artifacts.sql.go create mode 100644 internal/db/pull_requests.sql.go create mode 100644 internal/db/repositories.sql.go 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/000101_remove_entities_tables.up.sql b/database/migrations/000101_remove_entities_tables.up.sql deleted file mode 100644 index df7d94f6a6..0000000000 --- a/database/migrations/000101_remove_entities_tables.up.sql +++ /dev/null @@ -1,23 +0,0 @@ --- 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; - -COMMIT; \ No newline at end of file diff --git a/database/migrations/000101_remove_entities_tables.down.sql b/database/migrations/000102_remove_entities_tables.down.sql similarity index 100% rename from database/migrations/000101_remove_entities_tables.down.sql rename to database/migrations/000102_remove_entities_tables.down.sql 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 9d314956c2..50dac44e3d 100644 --- a/database/mock/store.go +++ b/database/mock/store.go @@ -131,6 +131,21 @@ func (mr *MockStoreMockRecorder) CountProfilesByName(arg0, arg1 any) *gomock.Cal return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CountProfilesByName", reflect.TypeOf((*MockStore)(nil).CountProfilesByName), arg0, arg1) } +// CountRepositories mocks base method. +func (m *MockStore) CountRepositories(arg0 context.Context) (int64, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CountRepositories", arg0) + ret0, _ := ret[0].(int64) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// CountRepositories indicates an expected call of CountRepositories. +func (mr *MockStoreMockRecorder) CountRepositories(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CountRepositories", reflect.TypeOf((*MockStore)(nil).CountRepositories), arg0) +} + // CountUsers mocks base method. func (m *MockStore) CountUsers(arg0 context.Context) (int64, error) { m.ctrl.T.Helper() @@ -747,6 +762,36 @@ func (mr *MockStoreMockRecorder) GetAllPropertyValuesV1(arg0, arg1 any) *gomock. return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAllPropertyValuesV1", reflect.TypeOf((*MockStore)(nil).GetAllPropertyValuesV1), arg0, arg1) } +// GetArtifactByID mocks base method. +func (m *MockStore) GetArtifactByID(arg0 context.Context, arg1 db.GetArtifactByIDParams) (db.Artifact, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetArtifactByID", arg0, arg1) + ret0, _ := ret[0].(db.Artifact) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetArtifactByID indicates an expected call of GetArtifactByID. +func (mr *MockStoreMockRecorder) GetArtifactByID(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetArtifactByID", reflect.TypeOf((*MockStore)(nil).GetArtifactByID), arg0, arg1) +} + +// GetArtifactByName mocks base method. +func (m *MockStore) GetArtifactByName(arg0 context.Context, arg1 db.GetArtifactByNameParams) (db.Artifact, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetArtifactByName", arg0, arg1) + ret0, _ := ret[0].(db.Artifact) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetArtifactByName indicates an expected call of GetArtifactByName. +func (mr *MockStoreMockRecorder) GetArtifactByName(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetArtifactByName", reflect.TypeOf((*MockStore)(nil).GetArtifactByName), arg0, arg1) +} + // GetBundle mocks base method. func (m *MockStore) GetBundle(arg0 context.Context, arg1 db.GetBundleParams) (db.Bundle, error) { m.ctrl.T.Helper() @@ -1242,6 +1287,51 @@ func (mr *MockStoreMockRecorder) GetProviderByName(arg0, arg1 any) *gomock.Call return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetProviderByName", reflect.TypeOf((*MockStore)(nil).GetProviderByName), arg0, arg1) } +// GetProviderWebhooks mocks base method. +func (m *MockStore) GetProviderWebhooks(arg0 context.Context, arg1 uuid.UUID) ([]db.GetProviderWebhooksRow, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetProviderWebhooks", arg0, arg1) + ret0, _ := ret[0].([]db.GetProviderWebhooksRow) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetProviderWebhooks indicates an expected call of GetProviderWebhooks. +func (mr *MockStoreMockRecorder) GetProviderWebhooks(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetProviderWebhooks", reflect.TypeOf((*MockStore)(nil).GetProviderWebhooks), arg0, arg1) +} + +// GetPullRequest mocks base method. +func (m *MockStore) GetPullRequest(arg0 context.Context, arg1 db.GetPullRequestParams) (db.PullRequest, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetPullRequest", arg0, arg1) + ret0, _ := ret[0].(db.PullRequest) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetPullRequest indicates an expected call of GetPullRequest. +func (mr *MockStoreMockRecorder) GetPullRequest(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetPullRequest", reflect.TypeOf((*MockStore)(nil).GetPullRequest), arg0, arg1) +} + +// GetPullRequestByID mocks base method. +func (m *MockStore) GetPullRequestByID(arg0 context.Context, arg1 uuid.UUID) (db.PullRequest, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetPullRequestByID", arg0, arg1) + ret0, _ := ret[0].(db.PullRequest) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetPullRequestByID indicates an expected call of GetPullRequestByID. +func (mr *MockStoreMockRecorder) GetPullRequestByID(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetPullRequestByID", reflect.TypeOf((*MockStore)(nil).GetPullRequestByID), arg0, arg1) +} + // GetQuerierWithTransaction mocks base method. func (m *MockStore) GetQuerierWithTransaction(arg0 *sql.Tx) db.ExtendQuerier { m.ctrl.T.Helper() @@ -1256,6 +1346,96 @@ func (mr *MockStoreMockRecorder) GetQuerierWithTransaction(arg0 any) *gomock.Cal return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetQuerierWithTransaction", reflect.TypeOf((*MockStore)(nil).GetQuerierWithTransaction), arg0) } +// GetRepoPathFromArtifactID mocks base method. +func (m *MockStore) GetRepoPathFromArtifactID(arg0 context.Context, arg1 uuid.UUID) (db.GetRepoPathFromArtifactIDRow, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetRepoPathFromArtifactID", arg0, arg1) + ret0, _ := ret[0].(db.GetRepoPathFromArtifactIDRow) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetRepoPathFromArtifactID indicates an expected call of GetRepoPathFromArtifactID. +func (mr *MockStoreMockRecorder) GetRepoPathFromArtifactID(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetRepoPathFromArtifactID", reflect.TypeOf((*MockStore)(nil).GetRepoPathFromArtifactID), arg0, arg1) +} + +// GetRepoPathFromPullRequestID mocks base method. +func (m *MockStore) GetRepoPathFromPullRequestID(arg0 context.Context, arg1 uuid.UUID) (db.GetRepoPathFromPullRequestIDRow, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetRepoPathFromPullRequestID", arg0, arg1) + ret0, _ := ret[0].(db.GetRepoPathFromPullRequestIDRow) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetRepoPathFromPullRequestID indicates an expected call of GetRepoPathFromPullRequestID. +func (mr *MockStoreMockRecorder) GetRepoPathFromPullRequestID(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetRepoPathFromPullRequestID", reflect.TypeOf((*MockStore)(nil).GetRepoPathFromPullRequestID), arg0, arg1) +} + +// GetRepositoryByID mocks base method. +func (m *MockStore) GetRepositoryByID(arg0 context.Context, arg1 uuid.UUID) (db.Repository, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetRepositoryByID", arg0, arg1) + ret0, _ := ret[0].(db.Repository) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetRepositoryByID indicates an expected call of GetRepositoryByID. +func (mr *MockStoreMockRecorder) GetRepositoryByID(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetRepositoryByID", reflect.TypeOf((*MockStore)(nil).GetRepositoryByID), arg0, arg1) +} + +// GetRepositoryByIDAndProject mocks base method. +func (m *MockStore) GetRepositoryByIDAndProject(arg0 context.Context, arg1 db.GetRepositoryByIDAndProjectParams) (db.Repository, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetRepositoryByIDAndProject", arg0, arg1) + ret0, _ := ret[0].(db.Repository) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetRepositoryByIDAndProject indicates an expected call of GetRepositoryByIDAndProject. +func (mr *MockStoreMockRecorder) GetRepositoryByIDAndProject(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetRepositoryByIDAndProject", reflect.TypeOf((*MockStore)(nil).GetRepositoryByIDAndProject), arg0, arg1) +} + +// GetRepositoryByRepoID mocks base method. +func (m *MockStore) GetRepositoryByRepoID(arg0 context.Context, arg1 int64) (db.Repository, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetRepositoryByRepoID", arg0, arg1) + ret0, _ := ret[0].(db.Repository) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetRepositoryByRepoID indicates an expected call of GetRepositoryByRepoID. +func (mr *MockStoreMockRecorder) GetRepositoryByRepoID(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetRepositoryByRepoID", reflect.TypeOf((*MockStore)(nil).GetRepositoryByRepoID), arg0, arg1) +} + +// GetRepositoryByRepoName mocks base method. +func (m *MockStore) GetRepositoryByRepoName(arg0 context.Context, arg1 db.GetRepositoryByRepoNameParams) (db.Repository, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetRepositoryByRepoName", arg0, arg1) + ret0, _ := ret[0].(db.Repository) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetRepositoryByRepoName indicates an expected call of GetRepositoryByRepoName. +func (mr *MockStoreMockRecorder) GetRepositoryByRepoName(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetRepositoryByRepoName", reflect.TypeOf((*MockStore)(nil).GetRepositoryByRepoName), arg0, arg1) +} + // GetRuleEvaluationByProfileIdAndRuleType mocks base method. func (m *MockStore) GetRuleEvaluationByProfileIdAndRuleType(arg0 context.Context, arg1 uuid.UUID, arg2 db.NullEntities, arg3 sql.NullString, arg4 uuid.NullUUID, arg5 sql.NullString) (*db.ListRuleEvaluationsByProfileIdRow, error) { m.ctrl.T.Helper() @@ -1554,6 +1734,21 @@ func (mr *MockStoreMockRecorder) InsertRemediationEvent(arg0, arg1 any) *gomock. return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InsertRemediationEvent", reflect.TypeOf((*MockStore)(nil).InsertRemediationEvent), arg0, arg1) } +// ListArtifactsByRepoID mocks base method. +func (m *MockStore) ListArtifactsByRepoID(arg0 context.Context, arg1 uuid.NullUUID) ([]db.Artifact, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ListArtifactsByRepoID", arg0, arg1) + ret0, _ := ret[0].([]db.Artifact) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ListArtifactsByRepoID indicates an expected call of ListArtifactsByRepoID. +func (mr *MockStoreMockRecorder) ListArtifactsByRepoID(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListArtifactsByRepoID", reflect.TypeOf((*MockStore)(nil).ListArtifactsByRepoID), arg0, arg1) +} + // ListEvaluationHistory mocks base method. func (m *MockStore) ListEvaluationHistory(arg0 context.Context, arg1 db.ListEvaluationHistoryParams) ([]db.ListEvaluationHistoryRow, error) { m.ctrl.T.Helper() @@ -1689,6 +1884,51 @@ func (mr *MockStoreMockRecorder) ListProvidersByProjectIDPaginated(arg0, arg1 an return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListProvidersByProjectIDPaginated", reflect.TypeOf((*MockStore)(nil).ListProvidersByProjectIDPaginated), arg0, arg1) } +// ListRegisteredRepositoriesByProjectIDAndProvider mocks base method. +func (m *MockStore) ListRegisteredRepositoriesByProjectIDAndProvider(arg0 context.Context, arg1 db.ListRegisteredRepositoriesByProjectIDAndProviderParams) ([]db.Repository, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ListRegisteredRepositoriesByProjectIDAndProvider", arg0, arg1) + ret0, _ := ret[0].([]db.Repository) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ListRegisteredRepositoriesByProjectIDAndProvider indicates an expected call of ListRegisteredRepositoriesByProjectIDAndProvider. +func (mr *MockStoreMockRecorder) ListRegisteredRepositoriesByProjectIDAndProvider(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListRegisteredRepositoriesByProjectIDAndProvider", reflect.TypeOf((*MockStore)(nil).ListRegisteredRepositoriesByProjectIDAndProvider), arg0, arg1) +} + +// ListRepositoriesAfterID mocks base method. +func (m *MockStore) ListRepositoriesAfterID(arg0 context.Context, arg1 db.ListRepositoriesAfterIDParams) ([]db.Repository, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ListRepositoriesAfterID", arg0, arg1) + ret0, _ := ret[0].([]db.Repository) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ListRepositoriesAfterID indicates an expected call of ListRepositoriesAfterID. +func (mr *MockStoreMockRecorder) ListRepositoriesAfterID(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListRepositoriesAfterID", reflect.TypeOf((*MockStore)(nil).ListRepositoriesAfterID), arg0, arg1) +} + +// ListRepositoriesByProjectID mocks base method. +func (m *MockStore) ListRepositoriesByProjectID(arg0 context.Context, arg1 db.ListRepositoriesByProjectIDParams) ([]db.Repository, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ListRepositoriesByProjectID", arg0, arg1) + ret0, _ := ret[0].([]db.Repository) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ListRepositoriesByProjectID indicates an expected call of ListRepositoriesByProjectID. +func (mr *MockStoreMockRecorder) ListRepositoriesByProjectID(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListRepositoriesByProjectID", reflect.TypeOf((*MockStore)(nil).ListRepositoriesByProjectID), arg0, arg1) +} + // ListRuleEvaluationsByProfileId mocks base method. func (m *MockStore) ListRuleEvaluationsByProfileId(arg0 context.Context, arg1 db.ListRuleEvaluationsByProfileIdParams) ([]db.ListRuleEvaluationsByProfileIdRow, error) { m.ctrl.T.Helper() @@ -1793,6 +2033,21 @@ func (mr *MockStoreMockRecorder) ReleaseLock(arg0, arg1 any) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReleaseLock", reflect.TypeOf((*MockStore)(nil).ReleaseLock), arg0, arg1) } +// RepositoryExistsAfterID mocks base method. +func (m *MockStore) RepositoryExistsAfterID(arg0 context.Context, arg1 uuid.UUID) (bool, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "RepositoryExistsAfterID", arg0, arg1) + ret0, _ := ret[0].(bool) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// RepositoryExistsAfterID indicates an expected call of RepositoryExistsAfterID. +func (mr *MockStoreMockRecorder) RepositoryExistsAfterID(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RepositoryExistsAfterID", reflect.TypeOf((*MockStore)(nil).RepositoryExistsAfterID), arg0, arg1) +} + // Rollback mocks base method. func (m *MockStore) Rollback(arg0 *sql.Tx) error { m.ctrl.T.Helper() diff --git a/database/query/artifacts.sql b/database/query/artifacts.sql new file mode 100644 index 0000000000..3ec075f37b --- /dev/null +++ b/database/query/artifacts.sql @@ -0,0 +1,13 @@ +-- name: GetArtifactByID :one +SELECT * FROM artifacts +WHERE artifacts.id = $1 AND artifacts.project_id = $2; + +-- name: GetArtifactByName :one +SELECT * FROM artifacts +WHERE lower(artifacts.artifact_name) = lower(sqlc.arg(artifact_name)) +AND artifacts.repository_id = $1 AND artifacts.project_id = $2; + +-- name: ListArtifactsByRepoID :many +SELECT * FROM artifacts +WHERE repository_id = $1 +ORDER BY id; \ No newline at end of file diff --git a/database/query/eval_history.sql b/database/query/eval_history.sql index 3c33b6da7a..8c9275f40f 100644 --- a/database/query/eval_history.sql +++ b/database/query/eval_history.sql @@ -130,7 +130,7 @@ 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, + ei.entity_type, ere.entity_instance_id as entity_id, -- raw fields for entity names ei.name as entity_name, @@ -153,7 +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 entity_instances ei ON ei.id = ere.entity_instance_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 diff --git a/database/query/pull_requests.sql b/database/query/pull_requests.sql new file mode 100644 index 0000000000..ba9d7554cd --- /dev/null +++ b/database/query/pull_requests.sql @@ -0,0 +1,7 @@ +-- 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; \ No newline at end of file diff --git a/database/query/repositories.sql b/database/query/repositories.sql new file mode 100644 index 0000000000..fbe89a68bb --- /dev/null +++ b/database/query/repositories.sql @@ -0,0 +1,61 @@ +-- name: GetRepositoryByRepoID :one +SELECT * FROM repositories WHERE repo_id = $1; + +-- name: GetRepositoryByRepoName :one +SELECT * FROM repositories + WHERE repo_owner = $1 AND repo_name = $2 AND project_id = $3 + AND (lower(provider) = lower(sqlc.narg('provider')::text) OR sqlc.narg('provider')::text IS NULL); + +-- avoid using this, where possible use GetRepositoryByIDAndProject instead +-- name: GetRepositoryByID :one +SELECT * FROM repositories WHERE id = $1; + +-- name: GetRepositoryByIDAndProject :one +SELECT * FROM repositories WHERE id = $1 AND project_id = $2; + +-- name: ListRepositoriesByProjectID :many +SELECT * FROM repositories +WHERE project_id = $1 + AND (repo_id >= sqlc.narg('repo_id') OR sqlc.narg('repo_id') IS NULL) + AND lower(provider) = lower(COALESCE(sqlc.narg('provider'), provider)::text) +ORDER BY project_id, provider, repo_id +LIMIT sqlc.narg('limit')::bigint; + +-- name: ListRegisteredRepositoriesByProjectIDAndProvider :many +SELECT * FROM repositories +WHERE project_id = $1 AND webhook_id IS NOT NULL + AND (lower(provider) = lower(sqlc.narg('provider')::text) OR sqlc.narg('provider')::text IS NULL) +ORDER BY repo_name; + +-- name: ListRepositoriesAfterID :many +SELECT * +FROM repositories +WHERE id > $1 +ORDER BY id +LIMIT sqlc.arg('limit')::bigint; + +-- name: RepositoryExistsAfterID :one +SELECT EXISTS ( + SELECT 1 + FROM repositories + WHERE id > $1) +AS exists; + +-- name: CountRepositories :one +SELECT COUNT(*) FROM repositories; + +-- get a list of repos with webhooks belonging to a provider +-- is used for webhook cleanup during provider deletion +-- name: GetProviderWebhooks :many +SELECT repo_owner, repo_name, webhook_id FROM repositories +WHERE webhook_id IS NOT NULL AND provider_id = $1; + +-- name: GetRepoPathFromArtifactID :one +SELECT r.repo_owner AS owner , r.repo_name AS name FROM repositories AS r +JOIN artifacts AS a ON a.repository_id = r.id +WHERE a.id = $1; + +-- name: GetRepoPathFromPullRequestID :one +SELECT r.repo_owner AS owner , r.repo_name AS name FROM repositories AS r +JOIN pull_requests AS p ON p.repository_id = r.id +WHERE p.id = $1; \ No newline at end of file diff --git a/internal/db/artifacts.sql.go b/internal/db/artifacts.sql.go new file mode 100644 index 0000000000..fbb542b217 --- /dev/null +++ b/internal/db/artifacts.sql.go @@ -0,0 +1,107 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.27.0 +// source: artifacts.sql + +package db + +import ( + "context" + + "github.com/google/uuid" +) + +const getArtifactByID = `-- name: GetArtifactByID :one +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 +` + +type GetArtifactByIDParams struct { + ID uuid.UUID `json:"id"` + ProjectID uuid.UUID `json:"project_id"` +} + +func (q *Queries) GetArtifactByID(ctx context.Context, arg GetArtifactByIDParams) (Artifact, error) { + row := q.db.QueryRowContext(ctx, getArtifactByID, arg.ID, arg.ProjectID) + var i Artifact + err := row.Scan( + &i.ID, + &i.ProjectID, + &i.ProviderName, + &i.ProviderID, + &i.RepositoryID, + &i.ArtifactName, + &i.ArtifactType, + &i.ArtifactVisibility, + &i.CreatedAt, + ) + return i, err +} + +const getArtifactByName = `-- name: GetArtifactByName :one +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 +` + +type GetArtifactByNameParams struct { + RepositoryID uuid.NullUUID `json:"repository_id"` + ProjectID uuid.UUID `json:"project_id"` + ArtifactName string `json:"artifact_name"` +} + +func (q *Queries) GetArtifactByName(ctx context.Context, arg GetArtifactByNameParams) (Artifact, error) { + row := q.db.QueryRowContext(ctx, getArtifactByName, arg.RepositoryID, arg.ProjectID, arg.ArtifactName) + var i Artifact + err := row.Scan( + &i.ID, + &i.ProjectID, + &i.ProviderName, + &i.ProviderID, + &i.RepositoryID, + &i.ArtifactName, + &i.ArtifactType, + &i.ArtifactVisibility, + &i.CreatedAt, + ) + return i, err +} + +const listArtifactsByRepoID = `-- name: ListArtifactsByRepoID :many +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 +` + +func (q *Queries) ListArtifactsByRepoID(ctx context.Context, repositoryID uuid.NullUUID) ([]Artifact, error) { + rows, err := q.db.QueryContext(ctx, listArtifactsByRepoID, repositoryID) + if err != nil { + return nil, err + } + defer rows.Close() + items := []Artifact{} + for rows.Next() { + 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, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/internal/db/eval_history.sql.go b/internal/db/eval_history.sql.go index 9d94b0b9c6..87b2e8ab3f 100644 --- a/internal/db/eval_history.sql.go +++ b/internal/db/eval_history.sql.go @@ -312,7 +312,7 @@ 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, + ei.entity_type, ere.entity_instance_id as entity_id, -- raw fields for entity names ei.name as entity_name, @@ -335,7 +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 entity_instances ei ON ei.id = ere.entity_instance_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 @@ -396,7 +396,7 @@ type ListEvaluationHistoryRow struct { EvaluatedAt time.Time `json:"evaluated_at"` EntityType Entities `json:"entity_type"` EntityID uuid.UUID `json:"entity_id"` - EntityName sql.NullString `json:"entity_name"` + EntityName string `json:"entity_name"` RuleType string `json:"rule_type"` RuleName string `json:"rule_name"` RuleSeverity Severity `json:"rule_severity"` 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 3a68950b5d..506ad9adc1 100644 --- a/internal/db/models.go +++ b/internal/db/models.go @@ -476,6 +476,18 @@ type AlertEvent struct { CreatedAt time.Time `json:"created_at"` } +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"` +} + type Bundle struct { ID uuid.UUID `json:"id"` Namespace string `json:"namespace"` @@ -660,6 +672,13 @@ type ProviderGithubAppInstallation struct { IsOrg bool `json:"is_org"` } +type PullRequest struct { + 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 { ID uuid.UUID `json:"id"` EvaluationID uuid.UUID `json:"evaluation_id"` @@ -669,6 +688,25 @@ type RemediationEvent struct { CreatedAt time.Time `json:"created_at"` } +type Repository struct { + 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 { ID uuid.UUID `json:"id"` ProfileID uuid.UUID `json:"profile_id"` 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 new file mode 100644 index 0000000000..30da54c2b8 --- /dev/null +++ b/internal/db/pull_requests.sql.go @@ -0,0 +1,51 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.27.0 +// source: pull_requests.sql + +package db + +import ( + "context" + + "github.com/google/uuid" +) + +const getPullRequest = `-- name: GetPullRequest :one +SELECT id, repository_id, pr_number, created_at FROM pull_requests +WHERE repository_id = $1 AND pr_number = $2 +` + +type GetPullRequestParams struct { + RepositoryID uuid.NullUUID `json:"repository_id"` + PrNumber int64 `json:"pr_number"` +} + +func (q *Queries) GetPullRequest(ctx context.Context, arg GetPullRequestParams) (PullRequest, error) { + row := q.db.QueryRowContext(ctx, getPullRequest, arg.RepositoryID, arg.PrNumber) + var i PullRequest + err := row.Scan( + &i.ID, + &i.RepositoryID, + &i.PrNumber, + &i.CreatedAt, + ) + return i, err +} + +const getPullRequestByID = `-- name: GetPullRequestByID :one +SELECT id, repository_id, pr_number, created_at FROM pull_requests +WHERE id = $1 +` + +func (q *Queries) GetPullRequestByID(ctx context.Context, id uuid.UUID) (PullRequest, error) { + row := q.db.QueryRowContext(ctx, getPullRequestByID, id) + var i PullRequest + err := row.Scan( + &i.ID, + &i.RepositoryID, + &i.PrNumber, + &i.CreatedAt, + ) + return i, err +} diff --git a/internal/db/querier.go b/internal/db/querier.go index ae92770305..3d88746886 100644 --- a/internal/db/querier.go +++ b/internal/db/querier.go @@ -16,6 +16,7 @@ type Querier interface { BulkGetProfilesByID(ctx context.Context, profileIds []uuid.UUID) ([]BulkGetProfilesByIDRow, error) CountProfilesByEntityType(ctx context.Context) ([]CountProfilesByEntityTypeRow, error) CountProfilesByName(ctx context.Context, name string) (int64, error) + CountRepositories(ctx context.Context) (int64, error) CountUsers(ctx context.Context) (int64, error) // CreateEntity adds an entry to the entity_instances table so it can be tracked by Minder. CreateEntity(ctx context.Context, arg CreateEntityParams) (EntityInstance, error) @@ -74,6 +75,8 @@ type Querier interface { GetAccessTokenByProvider(ctx context.Context, provider string) ([]ProviderAccessToken, error) GetAccessTokenSinceDate(ctx context.Context, arg GetAccessTokenSinceDateParams) (ProviderAccessToken, error) GetAllPropertiesForEntity(ctx context.Context, entityID uuid.UUID) ([]Property, error) + GetArtifactByID(ctx context.Context, arg GetArtifactByIDParams) (Artifact, error) + GetArtifactByName(ctx context.Context, arg GetArtifactByNameParams) (Artifact, error) GetBundle(ctx context.Context, arg GetBundleParams) (Bundle, error) GetChildrenProjects(ctx context.Context, id uuid.UUID) ([]GetChildrenProjectsRow, error) // GetEntitiesByType retrieves all entities of a given type for a project or hierarchy of projects. @@ -142,6 +145,18 @@ type Querier interface { // if it exists in the project or any of its ancestors. It'll return the first // provider that matches the name. GetProviderByName(ctx context.Context, arg GetProviderByNameParams) (Provider, error) + // get a list of repos with webhooks belonging to a provider + // is used for webhook cleanup during provider deletion + GetProviderWebhooks(ctx context.Context, providerID uuid.UUID) ([]GetProviderWebhooksRow, error) + GetPullRequest(ctx context.Context, arg GetPullRequestParams) (PullRequest, error) + GetPullRequestByID(ctx context.Context, id uuid.UUID) (PullRequest, error) + GetRepoPathFromArtifactID(ctx context.Context, id uuid.UUID) (GetRepoPathFromArtifactIDRow, error) + GetRepoPathFromPullRequestID(ctx context.Context, id uuid.UUID) (GetRepoPathFromPullRequestIDRow, error) + // avoid using this, where possible use GetRepositoryByIDAndProject instead + GetRepositoryByID(ctx context.Context, id uuid.UUID) (Repository, error) + GetRepositoryByIDAndProject(ctx context.Context, arg GetRepositoryByIDAndProjectParams) (Repository, error) + GetRepositoryByRepoID(ctx context.Context, repoID int64) (Repository, error) + GetRepositoryByRepoName(ctx context.Context, arg GetRepositoryByRepoNameParams) (Repository, error) GetRuleInstancesEntityInProjects(ctx context.Context, arg GetRuleInstancesEntityInProjectsParams) ([]RuleInstance, error) GetRuleInstancesForProfile(ctx context.Context, profileID uuid.UUID) ([]RuleInstance, error) GetRuleTypeByID(ctx context.Context, id uuid.UUID) (RuleType, error) @@ -165,6 +180,7 @@ type Querier interface { InsertEvaluationRuleEntity(ctx context.Context, arg InsertEvaluationRuleEntityParams) (uuid.UUID, error) InsertEvaluationStatus(ctx context.Context, arg InsertEvaluationStatusParams) (uuid.UUID, error) InsertRemediationEvent(ctx context.Context, arg InsertRemediationEventParams) error + ListArtifactsByRepoID(ctx context.Context, repositoryID uuid.NullUUID) ([]Artifact, error) ListEvaluationHistory(ctx context.Context, arg ListEvaluationHistoryParams) ([]ListEvaluationHistoryRow, error) ListEvaluationHistoryStaleRecords(ctx context.Context, arg ListEvaluationHistoryStaleRecordsParams) ([]ListEvaluationHistoryStaleRecordsRow, error) ListFlushCache(ctx context.Context) ([]FlushCache, error) @@ -184,6 +200,9 @@ type Querier interface { // ListProvidersByProjectIDPaginated allows us to lits all providers for a given project // with pagination taken into account. In this case, the cursor is the creation date. ListProvidersByProjectIDPaginated(ctx context.Context, arg ListProvidersByProjectIDPaginatedParams) ([]Provider, error) + ListRegisteredRepositoriesByProjectIDAndProvider(ctx context.Context, arg ListRegisteredRepositoriesByProjectIDAndProviderParams) ([]Repository, error) + ListRepositoriesAfterID(ctx context.Context, arg ListRepositoriesAfterIDParams) ([]Repository, error) + ListRepositoriesByProjectID(ctx context.Context, arg ListRepositoriesByProjectIDParams) ([]Repository, error) ListRuleEvaluationsByProfileId(ctx context.Context, arg ListRuleEvaluationsByProfileIdParams) ([]ListRuleEvaluationsByProfileIdRow, error) ListRuleTypesByProject(ctx context.Context, projectID uuid.UUID) ([]RuleType, error) // When doing a key/algorithm rotation, identify the secrets which need to be @@ -208,6 +227,7 @@ type Querier interface { // entity_execution_lock record if the lock is held by the given locked_by // value. ReleaseLock(ctx context.Context, arg ReleaseLockParams) error + RepositoryExistsAfterID(ctx context.Context, id uuid.UUID) (bool, error) SetCurrentVersion(ctx context.Context, arg SetCurrentVersionParams) error UpdateEncryptedSecret(ctx context.Context, arg UpdateEncryptedSecretParams) error // UpdateInvitationRole updates an invitation by its code. This is intended to be diff --git a/internal/db/repositories.sql.go b/internal/db/repositories.sql.go new file mode 100644 index 0000000000..78a33aa0ed --- /dev/null +++ b/internal/db/repositories.sql.go @@ -0,0 +1,409 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.27.0 +// source: repositories.sql + +package db + +import ( + "context" + "database/sql" + + "github.com/google/uuid" +) + +const countRepositories = `-- name: CountRepositories :one +SELECT COUNT(*) FROM repositories +` + +func (q *Queries) CountRepositories(ctx context.Context) (int64, error) { + row := q.db.QueryRowContext(ctx, countRepositories) + var count int64 + err := row.Scan(&count) + return count, 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 int64 `json:"webhook_id"` +} + +// get a list of repos with webhooks belonging to a provider +// is used for webhook cleanup during provider deletion +func (q *Queries) GetProviderWebhooks(ctx context.Context, providerID uuid.UUID) ([]GetProviderWebhooksRow, error) { + rows, err := q.db.QueryContext(ctx, getProviderWebhooks, providerID) + if err != nil { + return nil, err + } + defer rows.Close() + items := []GetProviderWebhooksRow{} + for rows.Next() { + var i GetProviderWebhooksRow + if err := rows.Scan(&i.RepoOwner, &i.RepoName, &i.WebhookID); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const getRepoPathFromArtifactID = `-- name: GetRepoPathFromArtifactID :one +SELECT r.repo_owner AS owner , r.repo_name AS name FROM repositories AS r +JOIN artifacts AS a ON a.repository_id = r.id +WHERE a.id = $1 +` + +type GetRepoPathFromArtifactIDRow struct { + Owner string `json:"owner"` + Name string `json:"name"` +} + +func (q *Queries) GetRepoPathFromArtifactID(ctx context.Context, id uuid.UUID) (GetRepoPathFromArtifactIDRow, error) { + row := q.db.QueryRowContext(ctx, getRepoPathFromArtifactID, id) + var i GetRepoPathFromArtifactIDRow + err := row.Scan(&i.Owner, &i.Name) + return i, err +} + +const getRepoPathFromPullRequestID = `-- name: GetRepoPathFromPullRequestID :one +SELECT r.repo_owner AS owner , r.repo_name AS name FROM repositories AS r +JOIN pull_requests AS p ON p.repository_id = r.id +WHERE p.id = $1 +` + +type GetRepoPathFromPullRequestIDRow struct { + Owner string `json:"owner"` + Name string `json:"name"` +} + +func (q *Queries) GetRepoPathFromPullRequestID(ctx context.Context, id uuid.UUID) (GetRepoPathFromPullRequestIDRow, error) { + row := q.db.QueryRowContext(ctx, getRepoPathFromPullRequestID, id) + var i GetRepoPathFromPullRequestIDRow + err := row.Scan(&i.Owner, &i.Name) + return i, err +} + +const getRepositoryByID = `-- name: GetRepositoryByID :one +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 +func (q *Queries) GetRepositoryByID(ctx context.Context, id uuid.UUID) (Repository, error) { + row := q.db.QueryRowContext(ctx, getRepositoryByID, id) + var i Repository + err := row.Scan( + &i.ID, + &i.ProjectID, + &i.Provider, + &i.ProviderID, + &i.RepoOwner, + &i.RepoName, + &i.RepoID, + &i.IsPrivate, + &i.IsFork, + &i.WebhookID, + &i.WebhookUrl, + &i.DeployUrl, + &i.CloneUrl, + &i.DefaultBranch, + &i.License, + &i.CreatedAt, + ) + return i, err +} + +const getRepositoryByIDAndProject = `-- name: GetRepositoryByIDAndProject :one +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 { + ID uuid.UUID `json:"id"` + ProjectID uuid.UUID `json:"project_id"` +} + +func (q *Queries) GetRepositoryByIDAndProject(ctx context.Context, arg GetRepositoryByIDAndProjectParams) (Repository, error) { + row := q.db.QueryRowContext(ctx, getRepositoryByIDAndProject, arg.ID, arg.ProjectID) + var i Repository + err := row.Scan( + &i.ID, + &i.ProjectID, + &i.Provider, + &i.ProviderID, + &i.RepoOwner, + &i.RepoName, + &i.RepoID, + &i.IsPrivate, + &i.IsFork, + &i.WebhookID, + &i.WebhookUrl, + &i.DeployUrl, + &i.CloneUrl, + &i.DefaultBranch, + &i.License, + &i.CreatedAt, + ) + return i, err +} + +const getRepositoryByRepoID = `-- name: GetRepositoryByRepoID :one +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) { + row := q.db.QueryRowContext(ctx, getRepositoryByRepoID, repoID) + var i Repository + err := row.Scan( + &i.ID, + &i.ProjectID, + &i.Provider, + &i.ProviderID, + &i.RepoOwner, + &i.RepoName, + &i.RepoID, + &i.IsPrivate, + &i.IsFork, + &i.WebhookID, + &i.WebhookUrl, + &i.DeployUrl, + &i.CloneUrl, + &i.DefaultBranch, + &i.License, + &i.CreatedAt, + ) + return i, err +} + +const getRepositoryByRepoName = `-- name: GetRepositoryByRepoName :one +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) +` + +type GetRepositoryByRepoNameParams struct { + RepoOwner string `json:"repo_owner"` + RepoName string `json:"repo_name"` + ProjectID uuid.UUID `json:"project_id"` + Provider sql.NullString `json:"provider"` +} + +func (q *Queries) GetRepositoryByRepoName(ctx context.Context, arg GetRepositoryByRepoNameParams) (Repository, error) { + row := q.db.QueryRowContext(ctx, getRepositoryByRepoName, + arg.RepoOwner, + arg.RepoName, + arg.ProjectID, + arg.Provider, + ) + var i Repository + err := row.Scan( + &i.ID, + &i.ProjectID, + &i.Provider, + &i.ProviderID, + &i.RepoOwner, + &i.RepoName, + &i.RepoID, + &i.IsPrivate, + &i.IsFork, + &i.WebhookID, + &i.WebhookUrl, + &i.DeployUrl, + &i.CloneUrl, + &i.DefaultBranch, + &i.License, + &i.CreatedAt, + ) + return i, err +} + +const listRegisteredRepositoriesByProjectIDAndProvider = `-- name: ListRegisteredRepositoriesByProjectIDAndProvider :many +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 +` + +type ListRegisteredRepositoriesByProjectIDAndProviderParams struct { + ProjectID uuid.UUID `json:"project_id"` + Provider sql.NullString `json:"provider"` +} + +func (q *Queries) ListRegisteredRepositoriesByProjectIDAndProvider(ctx context.Context, arg ListRegisteredRepositoriesByProjectIDAndProviderParams) ([]Repository, error) { + rows, err := q.db.QueryContext(ctx, listRegisteredRepositoriesByProjectIDAndProvider, arg.ProjectID, arg.Provider) + if err != nil { + return nil, err + } + defer rows.Close() + items := []Repository{} + for rows.Next() { + var i Repository + if err := rows.Scan( + &i.ID, + &i.ProjectID, + &i.Provider, + &i.ProviderID, + &i.RepoOwner, + &i.RepoName, + &i.RepoID, + &i.IsPrivate, + &i.IsFork, + &i.WebhookID, + &i.WebhookUrl, + &i.DeployUrl, + &i.CloneUrl, + &i.DefaultBranch, + &i.License, + &i.CreatedAt, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const listRepositoriesAfterID = `-- name: ListRepositoriesAfterID :many +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 +LIMIT $2::bigint +` + +type ListRepositoriesAfterIDParams struct { + ID uuid.UUID `json:"id"` + Limit int64 `json:"limit"` +} + +func (q *Queries) ListRepositoriesAfterID(ctx context.Context, arg ListRepositoriesAfterIDParams) ([]Repository, error) { + rows, err := q.db.QueryContext(ctx, listRepositoriesAfterID, arg.ID, arg.Limit) + if err != nil { + return nil, err + } + defer rows.Close() + items := []Repository{} + for rows.Next() { + var i Repository + if err := rows.Scan( + &i.ID, + &i.ProjectID, + &i.Provider, + &i.ProviderID, + &i.RepoOwner, + &i.RepoName, + &i.RepoID, + &i.IsPrivate, + &i.IsFork, + &i.WebhookID, + &i.WebhookUrl, + &i.DeployUrl, + &i.CloneUrl, + &i.DefaultBranch, + &i.License, + &i.CreatedAt, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const listRepositoriesByProjectID = `-- name: ListRepositoriesByProjectID :many +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) +ORDER BY project_id, provider, repo_id +LIMIT $4::bigint +` + +type ListRepositoriesByProjectIDParams struct { + ProjectID uuid.UUID `json:"project_id"` + RepoID sql.NullInt64 `json:"repo_id"` + Provider sql.NullString `json:"provider"` + Limit sql.NullInt64 `json:"limit"` +} + +func (q *Queries) ListRepositoriesByProjectID(ctx context.Context, arg ListRepositoriesByProjectIDParams) ([]Repository, error) { + rows, err := q.db.QueryContext(ctx, listRepositoriesByProjectID, + arg.ProjectID, + arg.RepoID, + arg.Provider, + arg.Limit, + ) + if err != nil { + return nil, err + } + defer rows.Close() + items := []Repository{} + for rows.Next() { + var i Repository + if err := rows.Scan( + &i.ID, + &i.ProjectID, + &i.Provider, + &i.ProviderID, + &i.RepoOwner, + &i.RepoName, + &i.RepoID, + &i.IsPrivate, + &i.IsFork, + &i.WebhookID, + &i.WebhookUrl, + &i.DeployUrl, + &i.CloneUrl, + &i.DefaultBranch, + &i.License, + &i.CreatedAt, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const repositoryExistsAfterID = `-- name: RepositoryExistsAfterID :one +SELECT EXISTS ( + SELECT 1 + FROM repositories + WHERE id > $1) +AS exists +` + +func (q *Queries) RepositoryExistsAfterID(ctx context.Context, id uuid.UUID) (bool, error) { + row := q.db.QueryRowContext(ctx, repositoryExistsAfterID, id) + var exists bool + err := row.Scan(&exists) + return exists, err +}