Skip to content

Commit

Permalink
Fixes 4151: remove support for YYYY-MM-DD date formats (#839)
Browse files Browse the repository at this point in the history
Fixes 4151: remove support for yyyymmdd date formats
  • Loading branch information
xbhouse authored Oct 10, 2024
1 parent 30370c9 commit 533684f
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 40 deletions.
32 changes: 2 additions & 30 deletions pkg/api/snapshots.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package api

import (
"encoding/json"
"time"
)

Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/dao/rpms.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/dao/snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions pkg/dao/snapshots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down Expand Up @@ -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()
Expand Down
7 changes: 4 additions & 3 deletions pkg/handler/snapshots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/tasks/update_template_content.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 533684f

Please sign in to comment.