From 153c05a3f607281ec1ff3380634c1b285be2077d Mon Sep 17 00:00:00 2001 From: PuneetPunamiya Date: Wed, 7 Jul 2021 12:28:10 +0530 Subject: [PATCH] Updates the Catalog Sync to handle Categories if not present in Hub - Initially if there are categories in task which are not present in Hub, that categories used to get added in Hub db, this patch handles it by adding a record in Catalog_Errors table and not adding the categories in DB Signed-off-by: Puneet Punamiya --- api/pkg/service/catalog/syncer.go | 36 ++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/api/pkg/service/catalog/syncer.go b/api/pkg/service/catalog/syncer.go index c7158a47bc..d8c369cb13 100644 --- a/api/pkg/service/catalog/syncer.go +++ b/api/pkg/service/catalog/syncer.go @@ -16,6 +16,7 @@ package catalog import ( "fmt" + "strings" "time" "github.com/tektoncd/hub/api/pkg/app" @@ -207,11 +208,14 @@ func (s *syncer) updateJob(syncJob model.SyncJob, sha string, res []parser.Resou } catalog.SHA = sha - if err := s.updateResources(txn, log, &catalog, res); err != nil { + rerr, err := s.updateResources(txn, log, &catalog, res) + if err != nil { txn.Rollback() return err } + result.Combine(rerr) + if err := s.updateCatalogResult(txn, log, &catalog, result); err != nil { txn.Rollback() return err @@ -228,12 +232,13 @@ func (s *syncer) updateJob(syncJob model.SyncJob, sha string, res []parser.Resou func (s *syncer) updateResources( txn *gorm.DB, log *zap.SugaredLogger, - catalog *model.Catalog, res []parser.Resource) error { + catalog *model.Catalog, res []parser.Resource) (parser.Result, error) { if len(res) == 0 { - return nil + return parser.Result{}, nil } + rerr := parser.Result{} for _, r := range res { log.Infof("Res: %s | Name: %s ", r.Kind, r.Name) @@ -250,17 +255,17 @@ func (s *syncer) updateResources( txn.Model(&model.Resource{}).Where(dbRes).FirstOrCreate(&dbRes) if err := txn.Save(&dbRes).Error; err != nil { - return err + return parser.Result{}, err } log.Infof("Resource: %s ID: %d stored", r.Name, dbRes.ID) - s.updateResourceCategory(txn, log, &dbRes, r.Categories) + rerr.Combine(s.updateResourceCategory(txn, log, &dbRes, r.Categories)) s.updateResourceTags(txn, log, &dbRes, r.Tags) s.updateResourceVersions(txn, log, catalog, dbRes.ID, r.Versions) } - return nil + return rerr, nil } func (s *syncer) updateResourceTags( @@ -284,20 +289,25 @@ func (s *syncer) updateResourceTags( } func (s *syncer) updateResourceCategory(txn *gorm.DB, log *zap.SugaredLogger, - res *model.Resource, categories []string) { + res *model.Resource, categories []string) parser.Result { if len(categories) == 0 { - return + return parser.Result{} } - + rerr := parser.Result{} for _, t := range categories { + c := model.Category{Name: t} - c := model.Category{} - txn.Model(&model.Category{}).Where(&model.Category{Name: t}).FirstOrCreate(&c) - + err := txn.Model(&model.Category{}).Where(&model.Category{Name: t}).First(&c).Error + if err != nil && err == gorm.ErrRecordNotFound { + rerr.AddError(fmt.Errorf("Category `%s` for Resource `%s` is Invalid", strings.ReplaceAll(c.Name, " ", ""), res.Name)) + continue + } resCategory := model.ResourceCategory{ResourceID: res.ID, CategoryID: c.ID} txn.Model(&model.ResourceCategory{}).Where(resCategory).FirstOrCreate(&resCategory) - + log.Infof("Resource: %d: %s | category: %s (%d)", res.ID, res.Name, c.Name, c.ID) } + + return rerr } func (s *syncer) updateResourceVersions(