From 533684ffc8622ffb551bbde1952aa344197e72af Mon Sep 17 00:00:00 2001 From: Bryttanie <28575816+xbhouse@users.noreply.github.com> Date: Thu, 10 Oct 2024 05:57:14 -0400 Subject: [PATCH] Fixes 4151: remove support for YYYY-MM-DD date formats (#839) Fixes 4151: remove support for yyyymmdd date formats --- pkg/api/snapshots.go | 32 ++-------------------------- pkg/dao/rpms.go | 2 +- pkg/dao/snapshots.go | 6 +++--- pkg/dao/snapshots_test.go | 4 ++-- pkg/handler/snapshots_test.go | 7 +++--- pkg/tasks/update_template_content.go | 2 +- 6 files changed, 13 insertions(+), 40 deletions(-) diff --git a/pkg/api/snapshots.go b/pkg/api/snapshots.go index 8ce3aa3f5..39a50a537 100644 --- a/pkg/api/snapshots.go +++ b/pkg/api/snapshots.go @@ -1,7 +1,6 @@ package api import ( - "encoding/json" "time" ) @@ -18,35 +17,8 @@ type SnapshotResponse struct { } type ListSnapshotByDateRequest struct { - RepositoryUUIDS []string `json:"repository_uuids"` // Repository UUIDs to find snapshots for - Date Date `json:"date"` // Exact date to search by. -} - -type Date time.Time - -func (d Date) MarshalJSON() ([]byte, error) { - return json.Marshal(time.Time(d).Format(time.RFC3339)) -} - -func (d *Date) UnmarshalJSON(b []byte) error { - // try parsing as YYYY-MM-DD first - t, err := time.Parse(`"2006-01-02"`, string(b)) - if err == nil { - *d = Date(t) - return nil - } - - // if parsing as YYYY-MM-DD fails, try parsing as RFC3339 - var t2 time.Time - if err := json.Unmarshal(b, &t2); err != nil { - return err - } - *d = Date(t2) - return nil -} - -func (d *Date) Format(layout string) string { - return time.Time(*d).Format(layout) + RepositoryUUIDS []string `json:"repository_uuids"` // Repository UUIDs to find snapshots for + Date time.Time `json:"date"` // Exact date to search by. } type ListSnapshotByDateResponse struct { diff --git a/pkg/dao/rpms.go b/pkg/dao/rpms.go index 92a42a0c4..773a0c925 100644 --- a/pkg/dao/rpms.go +++ b/pkg/dao/rpms.go @@ -744,7 +744,7 @@ func (r *rpmDaoImpl) fetchSnapshotsForTemplate(ctx context.Context, orgId string templateDate = template.Date } - snapshots, err := GetSnapshotDao(r.db).FetchSnapshotsModelByDateAndRepository(ctx, orgId, api.ListSnapshotByDateRequest{RepositoryUUIDS: repoUuids, Date: api.Date(templateDate)}) + snapshots, err := GetSnapshotDao(r.db).FetchSnapshotsModelByDateAndRepository(ctx, orgId, api.ListSnapshotByDateRequest{RepositoryUUIDS: repoUuids, Date: templateDate}) if err != nil { return []models.Snapshot{}, err } diff --git a/pkg/dao/snapshots.go b/pkg/dao/snapshots.go index a214dd1ed..9c79b7d14 100644 --- a/pkg/dao/snapshots.go +++ b/pkg/dao/snapshots.go @@ -135,11 +135,11 @@ func (sDao *snapshotDaoImpl) ListByTemplate( } // Get snapshots for template date - var date api.Date + var date time.Time if template.UseLatest { - date = api.Date(time.Now()) + date = time.Now() } else { - date = api.Date(template.Date) + date = template.Date } snapshotsForTemplateDate, err := sDao.FetchSnapshotsByDateAndRepository(ctx, orgID, api.ListSnapshotByDateRequest{ RepositoryUUIDS: template.RepositoryUUIDS, diff --git a/pkg/dao/snapshots_test.go b/pkg/dao/snapshots_test.go index 2097b2b9f..d77d37569 100644 --- a/pkg/dao/snapshots_test.go +++ b/pkg/dao/snapshots_test.go @@ -482,7 +482,7 @@ func (s *SnapshotsSuite) TestFetchSnapshotsByDateAndRepository() { request := api.ListSnapshotByDateRequest{} - request.Date = api.Date(second.Base.CreatedAt) + request.Date = second.Base.CreatedAt request.RepositoryUUIDS = []string{repoConfig.UUID} @@ -522,7 +522,7 @@ func (s *SnapshotsSuite) TestFetchSnapshotsByDateAndRepositoryMulti() { target3 := s.createSnapshotAtSpecifiedTime(redhatRepo, baseTime.Add(-time.Hour*100)) // Closest to Target Date request := api.ListSnapshotByDateRequest{} - request.Date = api.Date(target1.Base.CreatedAt) + request.Date = target1.Base.CreatedAt // Intentionally not found ID randomUUID, _ := uuid2.NewUUID() diff --git a/pkg/handler/snapshots_test.go b/pkg/handler/snapshots_test.go index a35161318..14a55228d 100644 --- a/pkg/handler/snapshots_test.go +++ b/pkg/handler/snapshots_test.go @@ -8,6 +8,7 @@ import ( "net/http" "net/http/httptest" "testing" + "time" "github.com/content-services/content-sources-backend/pkg/api" "github.com/content-services/content-sources-backend/pkg/config" @@ -63,7 +64,7 @@ func (suite *SnapshotSuite) serveSnapshotsRouter(req *http.Request) (int, []byte func (suite *SnapshotSuite) TestListSnapshotsByDate() { t := suite.T() repoUUID := "abcadaba" - request := api.ListSnapshotByDateRequest{Date: api.Date{}, RepositoryUUIDS: []string{repoUUID}} + request := api.ListSnapshotByDateRequest{Date: time.Time{}, RepositoryUUIDS: []string{repoUUID}} response := api.ListSnapshotByDateResponse{Data: []api.SnapshotForDate{{RepositoryUUID: repoUUID}}} suite.reg.Snapshot.On("FetchSnapshotsByDateAndRepository", test.MockCtx(), test_handler.MockOrgId, request).Return(response, nil) @@ -84,7 +85,7 @@ func (suite *SnapshotSuite) TestListSnapshotsByDateBadRequestError() { t := suite.T() RepositoryUUIDS := []string{} - request := api.ListSnapshotByDateRequest{Date: api.Date{}, RepositoryUUIDS: RepositoryUUIDS} + request := api.ListSnapshotByDateRequest{Date: time.Time{}, RepositoryUUIDS: RepositoryUUIDS} body, err := json.Marshal(request) assert.NoError(t, err) @@ -105,7 +106,7 @@ func (suite *SnapshotSuite) TestListSnapshotsByDateExceedLimitError() { RepositoryUUIDS = append(RepositoryUUIDS, seeds.RandomOrgId()) } - request := api.ListSnapshotByDateRequest{Date: api.Date{}, RepositoryUUIDS: RepositoryUUIDS} + request := api.ListSnapshotByDateRequest{Date: time.Time{}, RepositoryUUIDS: RepositoryUUIDS} body, err := json.Marshal(request) assert.NoError(t, err) diff --git a/pkg/tasks/update_template_content.go b/pkg/tasks/update_template_content.go index 25dfe9e2f..1d2df3449 100644 --- a/pkg/tasks/update_template_content.go +++ b/pkg/tasks/update_template_content.go @@ -129,7 +129,7 @@ func (t *UpdateTemplateContent) RunPulp() error { templateDate = t.template.Date } - l := api.ListSnapshotByDateRequest{Date: api.Date(templateDate), RepositoryUUIDS: allRepos} + l := api.ListSnapshotByDateRequest{Date: templateDate, RepositoryUUIDS: allRepos} snapshots, err := t.daoReg.Snapshot.FetchSnapshotsModelByDateAndRepository(t.ctx, t.orgId, l) if err != nil { return err