Skip to content

Commit

Permalink
Updates the Catalog Sync to handle Categories if not present in Hub
Browse files Browse the repository at this point in the history
  - 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 <[email protected]>
  • Loading branch information
PuneetPunamiya committed Jul 7, 2021
1 parent 60de4f5 commit 153c05a
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions api/pkg/service/catalog/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package catalog

import (
"fmt"
"strings"
"time"

"github.com/tektoncd/hub/api/pkg/app"
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit 153c05a

Please sign in to comment.