diff --git a/base/mqueue/template_event.go b/base/mqueue/template_event.go index c82d222e7..634b5209c 100644 --- a/base/mqueue/template_event.go +++ b/base/mqueue/template_event.go @@ -12,7 +12,7 @@ type TemplateResponse struct { UUID string `json:"uuid" readonly:"true"` Name string `json:"name"` // Name of the template OrgID string `json:"org_id"` // Organization ID of the owner - Description string `json:"description"` // Description of the template + Description *string `json:"description"` // Description of the template Arch string `json:"arch"` // Architecture of the template Version string `json:"version"` // Version of the template Date time.Time `json:"date"` // Latest date to include snapshots for diff --git a/listener/template_test.go b/listener/template_test.go index bb50c99b4..f898cf4cb 100644 --- a/listener/template_test.go +++ b/listener/template_test.go @@ -23,11 +23,12 @@ func createTempateMsg(t *testing.T, eventName, orgID string, nTemplates int) mqu templates := make([]mqueue.TemplateResponse, nTemplates) for i := 0; i < nTemplates; i++ { + description := fmt.Sprintf("Template%d description", i) templates[i] = mqueue.TemplateResponse{ UUID: fmt.Sprintf("77777777-0000-0000-0000-00000000000%1d", i), Name: fmt.Sprintf("Template%d", i), OrgID: orgID, - Description: fmt.Sprintf("Template%d description", i), + Description: &description, Date: time.Now(), } } @@ -87,7 +88,8 @@ func TestCreateTemplate(t *testing.T) { updateEvent := createTempateMsg(t, "template-updated", "org_1", 2) updateEvent.Data[0].Name = "Updated Template0" - updateEvent.Data[1].Description = "Updated Template1 description" + description := "Updated Template1 description" + updateEvent.Data[1].Description = &description msg, err = json.Marshal(updateEvent) assert.Nil(t, err) @@ -143,3 +145,31 @@ func TestTemplateErrors(t *testing.T) { assert.Equal(t, 1, len(after)) deleteTemplatesInDB(t, after) } + +func TestTemplateEmptyDescription(t *testing.T) { + utils.SkipWithoutDB(t) + core.SetupTestEnvironment() + configure() + + event := createTempateMsg(t, "template-created", "org_1", 3) + empty := "" + spaces := " " + event.Data[0].Description = &empty + event.Data[1].Description = &spaces + event.Data[2].Description = nil + msg, err := json.Marshal(event) + assert.Nil(t, err) + + // process message + err = TemplatesMessageHandler(mqueue.KafkaMessage{Value: msg}) + assert.Nil(t, err) + + after := testTemplatesInDB(t) + assert.Equal(t, 3, len(after)) + for _, event := range after { + assert.Nil(t, event.Description) + } + + // cleanup + deleteTemplatesInDB(t, after) +} diff --git a/listener/templates.go b/listener/templates.go index d101a3f17..a7aa212b1 100644 --- a/listener/templates.go +++ b/listener/templates.go @@ -24,17 +24,14 @@ const ( ) func TemplatesMessageHandler(m mqueue.KafkaMessage) error { - var event mqueue.TemplateEvent - utils.LogTrace("kafka message data", string(m.Value)) - if err := json.Unmarshal(m.Value, &event); err != nil { - utils.LogError("msg", string(m.Value), "message is not a valid JSON") + eType, event, err := processTemplateEvent(m.Value) + if err != nil { + utils.LogError("msg", err.Error(), "skipping template event") // Skip invalid messages return nil } - eType := strings.TrimPrefix(event.Type, "com.redhat.console.repositories.") errs := []error{} - var err error for _, template := range event.Data { if enableBypass { utils.LogInfo("template", template.UUID, "Processing bypassed") @@ -97,7 +94,7 @@ func TemplateUpdate(template mqueue.TemplateResponse) error { UUID: template.UUID, Name: template.Name, //Config: nil, - Description: &template.Description, + Description: template.Description, Creator: nil, Published: &template.Date, } @@ -109,3 +106,19 @@ func TemplateUpdate(template mqueue.TemplateResponse) error { } return nil } + +func processTemplateEvent(value json.RawMessage) (eType string, event mqueue.TemplateEvent, err error) { + utils.LogTrace("kafka message data", string(value)) + if err := json.Unmarshal(value, &event); err != nil { + err = errors.Wrap(err, fmt.Sprintf("value: %s", string(value))) + return "", event, errors.Wrap(err, "message is not a valid JSON") + } + + for i, d := range event.Data { + if d.Description != nil && (len(*d.Description) == 0 || spacesRegex.MatchString(*d.Description)) { + d.Description = nil + event.Data[i] = d + } + } + return strings.TrimPrefix(event.Type, "com.redhat.console.repositories."), event, nil +} diff --git a/platform/platform.go b/platform/platform.go index 653be42b1..8bc37e904 100644 --- a/platform/platform.go +++ b/platform/platform.go @@ -152,12 +152,14 @@ var yumUpdates = `{ "metadata_time": "2022-05-30T14:00:25Z" }` +var desc1 = "template_1_description" +var desc2 = "template_2_description" var templates = []mqueue.TemplateResponse{ { UUID: "10000000-0000-0000-0000-000000000001", Name: "template_1", OrgID: "org_1", - Description: "template_1_description", + Description: &desc1, Arch: "x86_64", Version: "1", Date: time.Now(), @@ -167,7 +169,7 @@ var templates = []mqueue.TemplateResponse{ UUID: "10000000-0000-0000-0000-000000000002", Name: "template_2", OrgID: "org_1", - Description: "template_2_description", + Description: &desc2, Arch: "x86_64", Version: "1", Date: time.Now(),