diff --git a/registry-automation/cmd/ci.go b/registry-automation/cmd/ci.go index c642cba6..3ad85e4a 100644 --- a/registry-automation/cmd/ci.go +++ b/registry-automation/cmd/ci.go @@ -150,10 +150,10 @@ type fileProcessor struct { func processChangedFiles(changedFiles ChangedFiles) ProcessedChangedFiles { result := ProcessedChangedFiles{ NewConnectorVersions: make(map[Connector]map[string]string), - ModifiedLogos: make(map[Connector]string), + ModifiedLogos: make(map[Connector]Logo), ModifiedReadmes: make(map[Connector]string), NewConnectors: make(map[Connector]MetadataFile), - NewLogos: make(map[Connector]string), + NewLogos: make(map[Connector]Logo), NewReadmes: make(map[Connector]string), ModifiedConnectors: make(map[Connector]MetadataFile), } @@ -177,12 +177,12 @@ func processChangedFiles(changedFiles ChangedFiles) ProcessedChangedFiles { regex: regexp.MustCompile(`^registry/([^/]+)/([^/]+)/logo\.(png|svg)$`), newFileHandler: func(matches []string, file string) { connector := Connector{Name: matches[2], Namespace: matches[1]} - result.NewLogos[connector] = file + result.NewLogos[connector] = Logo{Path: file, Extension: LogoExtension(matches[3])} fmt.Printf("Processing logo file for new connector: %s\n", connector.Name) }, modifiedFileHandler: func(matches []string, file string) error { connector := Connector{Name: matches[2], Namespace: matches[1]} - result.ModifiedLogos[connector] = file + result.ModifiedLogos[connector] = Logo{Path: file, Extension: LogoExtension(matches[3])} fmt.Printf("Processing logo file for modified connector: %s\n", connector.Name) return nil }, @@ -274,7 +274,7 @@ func processModifiedConnector(metadataFile MetadataFile, connector Connector) (C return connectorOverviewUpdate, nil } -func processNewConnector(ciCtx Context, connector Connector, metadataFile MetadataFile) (ConnectorOverviewInsert, HubRegistryConnectorInsertInput, error) { +func processNewConnector(ciCtx Context, connector Connector, metadataFile MetadataFile, logoPath Logo) (ConnectorOverviewInsert, HubRegistryConnectorInsertInput, error) { // Process the newly added connector // Get the string value from metadataFile var connectorOverviewAndAuthor ConnectorOverviewInsert @@ -288,12 +288,9 @@ func processNewConnector(ciCtx Context, connector Connector, metadataFile Metada docs, err := readFile(fmt.Sprintf("registry/%s/%s/README.md", connector.Namespace, connector.Name)) if err != nil { - return connectorOverviewAndAuthor, hubRegistryConnectorInsertInput, fmt.Errorf("Failed to read the README file of the connector: %s : %v", connector.Name, err) } - logoPath := fmt.Sprintf("registry/%s/%s/logo.png", connector.Namespace, connector.Name) - uploadedLogoUrl, err := uploadLogoToCloudinary(ciCtx.Cloudinary, Connector{Name: connector.Name, Namespace: connector.Namespace}, logoPath) if err != nil { return connectorOverviewAndAuthor, hubRegistryConnectorInsertInput, err @@ -380,6 +377,7 @@ func runCI(cmd *cobra.Command, args []string) { newlyAddedConnectors := processChangedFiles.NewConnectors modifiedConnectors := processChangedFiles.ModifiedConnectors + newLogos := processChangedFiles.NewLogos var newConnectorsToBeAdded NewConnectorsInsertInput newConnectorsToBeAdded.HubRegistryConnectors = make([]HubRegistryConnectorInsertInput, 0) @@ -393,7 +391,9 @@ func runCI(cmd *cobra.Command, args []string) { fmt.Println("New connectors to be added to the registry: ", newlyAddedConnectors) for connector, metadataFile := range newlyAddedConnectors { - connectorOverviewAndAuthor, hubRegistryConnector, err := processNewConnector(ctx, connector, metadataFile) + // Find the logo corresponding to the connector from the newLogos map, throw error if not found + logoPath := newLogos[connector] + connectorOverviewAndAuthor, hubRegistryConnector, err := processNewConnector(ctx, connector, metadataFile, logoPath) if err != nil { log.Fatalf("Failed to process the new connector: %s/%s, Error: %v", connector.Namespace, connector.Name, err) @@ -462,8 +462,8 @@ func runCI(cmd *cobra.Command, args []string) { fmt.Println("Successfully processed the changed files in the PR") } -func uploadLogoToCloudinary(cloudinary CloudinaryInterface, connector Connector, logoPath string) (string, error) { - logoContent, err := readFile(logoPath) +func uploadLogoToCloudinary(cloudinary CloudinaryInterface, connector Connector, logo Logo) (string, error) { + logoContent, err := readFile(logo.Path) if err != nil { fmt.Printf("Failed to read the logo file: %v", err) return "", err @@ -473,7 +473,7 @@ func uploadLogoToCloudinary(cloudinary CloudinaryInterface, connector Connector, uploadResult, err := cloudinary.Upload(context.Background(), imageReader, uploader.UploadParams{ PublicID: fmt.Sprintf("%s-%s", connector.Namespace, connector.Name), - Format: "png", + Format: string(logo.Extension), }) if err != nil { return "", fmt.Errorf("Failed to upload the logo to cloudinary for the connector: %s, Error: %v\n", connector.Name, err) diff --git a/registry-automation/cmd/ci_test.go b/registry-automation/cmd/ci_test.go index ae53b25f..a3bc6114 100644 --- a/registry-automation/cmd/ci_test.go +++ b/registry-automation/cmd/ci_test.go @@ -69,10 +69,10 @@ func TestProcessChangedFiles(t *testing.T) { }, expected: ProcessedChangedFiles{ NewConnectorVersions: map[Connector]map[string]string{}, - ModifiedLogos: map[Connector]string{}, + ModifiedLogos: map[Connector]Logo{}, ModifiedReadmes: map[Connector]string{}, NewConnectors: map[Connector]MetadataFile{{Name: "connector1", Namespace: "namespace1"}: "registry/namespace1/connector1/metadata.json"}, - NewLogos: map[Connector]string{}, + NewLogos: map[Connector]Logo{}, NewReadmes: map[Connector]string{}, ModifiedConnectors: map[Connector]MetadataFile{}, }, @@ -87,10 +87,10 @@ func TestProcessChangedFiles(t *testing.T) { }, expected: ProcessedChangedFiles{ NewConnectorVersions: map[Connector]map[string]string{}, - ModifiedLogos: map[Connector]string{{Name: "connector1", Namespace: "namespace1"}: "registry/namespace1/connector1/logo.png"}, + ModifiedLogos: map[Connector]Logo{{Name: "connector1", Namespace: "namespace1"}: {Path: "registry/namespace1/connector1/logo.png", Extension: "png"}}, ModifiedReadmes: map[Connector]string{{Name: "connector1", Namespace: "namespace1"}: "registry/namespace1/connector1/README.md"}, NewConnectors: map[Connector]MetadataFile{}, - NewLogos: map[Connector]string{}, + NewLogos: map[Connector]Logo{}, NewReadmes: map[Connector]string{}, ModifiedConnectors: map[Connector]MetadataFile{}, }, @@ -106,10 +106,10 @@ func TestProcessChangedFiles(t *testing.T) { }, expected: ProcessedChangedFiles{ NewConnectorVersions: map[Connector]map[string]string{}, - ModifiedLogos: map[Connector]string{{Name: "connector1", Namespace: "namespace1"}: "registry/namespace1/connector1/logo.png"}, + ModifiedLogos: map[Connector]Logo{{Name: "connector1", Namespace: "namespace1"}: {Path: "registry/namespace1/connector1/logo.png", Extension: "png"}}, ModifiedReadmes: map[Connector]string{{Name: "connector1", Namespace: "namespace1"}: "registry/namespace1/connector1/README.md"}, NewConnectors: map[Connector]MetadataFile{}, - NewLogos: map[Connector]string{}, + NewLogos: map[Connector]Logo{}, NewReadmes: map[Connector]string{}, ModifiedConnectors: map[Connector]MetadataFile{{Name: "connector1", Namespace: "namespace1"}: "registry/namespace1/connector1/metadata.json"}, }, diff --git a/registry-automation/cmd/types.go b/registry-automation/cmd/types.go index 762397ee..27b29100 100644 --- a/registry-automation/cmd/types.go +++ b/registry-automation/cmd/types.go @@ -1,9 +1,10 @@ package cmd import ( - "cloud.google.com/go/storage" "context" "encoding/json" + + "cloud.google.com/go/storage" "github.com/cloudinary/cloudinary-go/v2" "github.com/cloudinary/cloudinary-go/v2/api/uploader" "github.com/machinebox/graphql" @@ -173,19 +174,31 @@ type Connector struct { Namespace string `json:"namespace"` } +type Logo struct { + Path string + Extension LogoExtension +} + +type LogoExtension string + +const ( + PNG LogoExtension = "png" + SVG LogoExtension = "svg" +) + type NewConnectorVersions map[Connector]map[string]string // ModifiedMetadata represents the modified metadata in the PR, the key is the connector name and the value is the path to the modified metadata type ModifiedMetadata map[Connector]MetadataFile // ModifiedLogos represents the modified logos in the PR, the key is the connector name and the value is the path to the modified logo -type ModifiedLogos map[Connector]string +type ModifiedLogos map[Connector]Logo // ModifiedReadmes represents the modified READMEs in the PR, the key is the connector name and the value is the path to the modified README type ModifiedReadmes map[Connector]string // ModifiedLogos represents the modified logos in the PR, the key is the connector name and the value is the path to the modified logo -type NewLogos map[Connector]string +type NewLogos map[Connector]Logo // ModifiedReadmes represents the modified READMEs in the PR, the key is the connector name and the value is the path to the modified README type NewReadmes map[Connector]string