From d772b7f64ed51d1fa5376b6e719af8ae92aab365 Mon Sep 17 00:00:00 2001 From: Jordan Dubrick Date: Wed, 31 Jul 2024 09:34:47 -0400 Subject: [PATCH] Add Last Modified Field For Non-Versioned Items (#250) * update tests for non versioned stacks/samples Signed-off-by: Jordan Dubrick * add logic for handling versioned vs non-versioned stacks/samples Signed-off-by: Jordan Dubrick * run go mod vendor for generator changes Signed-off-by: Jordan Dubrick * replace null with undefined Signed-off-by: Jordan Dubrick --------- Signed-off-by: Jordan Dubrick --- index/generator/library/library.go | 32 ++++++--- index/generator/library/library_test.go | 29 ++++++++ .../tests/registry/extraDevfileEntries.yaml | 13 +++- .../generator/tests/registry/index_extra.json | 19 +++++ .../generator/tests/registry/index_main.json | 20 ++++++ .../tests/registry/last_modified.json | 5 ++ .../code-with-quarkus-no-version/devfile.yaml | 71 +++++++++++++++++++ .../index/generator/library/library.go | 32 ++++++--- tests/registry/last_modified.json | 10 +++ 9 files changed, 208 insertions(+), 23 deletions(-) create mode 100644 index/generator/tests/registry/samples/code-with-quarkus-no-version/devfile.yaml diff --git a/index/generator/library/library.go b/index/generator/library/library.go index d735619a..a54e6ebc 100644 --- a/index/generator/library/library.go +++ b/index/generator/library/library.go @@ -737,19 +737,25 @@ func SetLastModifiedValue(index []schema.Schema, registryDirPath string) ([]sche } for i := range index { - var mostCurrentLastModifiedDate time.Time - for j := range index[i].Versions { - schemaItem := index[i] // a stack or sample - version := schemaItem.Versions[j] - versionNum := version.Version - lastModifiedDate := lastModifiedEntriesMap[schemaItem.Name][versionNum] - updateSchemaLastModified(&schemaItem, j, lastModifiedDate) - if lastModifiedDate.After(mostCurrentLastModifiedDate) { - mostCurrentLastModifiedDate = lastModifiedDate + // Separate handling for versioned vs. non-versioned items + if len(index[i].Versions) > 0 { + var mostCurrentLastModifiedDate time.Time + for j := range index[i].Versions { + schemaItem := index[i] // a stack or sample + version := schemaItem.Versions[j] + versionNum := version.Version + lastModifiedDate := lastModifiedEntriesMap[schemaItem.Name][versionNum] + updateSchemaLastModified(&schemaItem, j, lastModifiedDate) + if lastModifiedDate.After(mostCurrentLastModifiedDate) { + mostCurrentLastModifiedDate = lastModifiedDate + } } + // lastModified of a stack or sample will be the date any version of it was last changed + index[i].LastModified = mostCurrentLastModifiedDate.Format(time.RFC3339) + } else { + lastModifiedDate := lastModifiedEntriesMap[index[i].Name]["undefined"] + updateSchemaLastModifiedNoVersion(&index[i], lastModifiedDate) } - // lastModified of a stack or sample will be the date any version of it was last changed - index[i].LastModified = mostCurrentLastModifiedDate.Format(time.RFC3339) } return index, nil @@ -767,6 +773,10 @@ func updateSchemaLastModified(s *schema.Schema, versionIndx int, lastModifiedDat s.Versions[versionIndx].LastModified = lastModifiedDate.Format(time.RFC3339) } +func updateSchemaLastModifiedNoVersion(s *schema.Schema, lastModifiedDate time.Time) { + s.LastModified = lastModifiedDate.Format(time.RFC3339) +} + // In checks if the value is in the array func inArray(arr []string, value string) bool { for _, item := range arr { diff --git a/index/generator/library/library_test.go b/index/generator/library/library_test.go index 8db6d17c..480449ba 100644 --- a/index/generator/library/library_test.go +++ b/index/generator/library/library_test.go @@ -911,6 +911,20 @@ func TestSetLastModifiedValue(t *testing.T) { }, }, }, + schema.Schema{ + Name: "code-with-quarkus-no-version", + DisplayName: "Basic Quarkus Without Version", + Description: "A simple Hello World Java application using Quarkus", + Type: "sample", + Tags: []string{"Java", "Quarkus"}, + Icon: "https://raw.githubusercontent.com/elsony/devfile-sample-code-with-quarkus/main/.devfile/icon/quarkus.png", + ProjectType: "quarkus", + Language: "java", + Provider: "Red Hat", + Git: &schema.Git{ + Remotes: map[string]string{"origin": "https://github.com/elsony/devfile-sample-code-with-quarkus.git"}, + }, + }, } wantIndex := []schema.Schema{ @@ -976,6 +990,21 @@ func TestSetLastModifiedValue(t *testing.T) { }, }, }, + schema.Schema{ + Name: "code-with-quarkus-no-version", + DisplayName: "Basic Quarkus Without Version", + Description: "A simple Hello World Java application using Quarkus", + Type: "sample", + Tags: []string{"Java", "Quarkus"}, + Icon: "https://raw.githubusercontent.com/elsony/devfile-sample-code-with-quarkus/main/.devfile/icon/quarkus.png", + ProjectType: "quarkus", + Language: "java", + Provider: "Red Hat", + Git: &schema.Git{ + Remotes: map[string]string{"origin": "https://github.com/elsony/devfile-sample-code-with-quarkus.git"}, + }, + LastModified: "2024-04-19T11:45:48+01:00", + }, } tests := []struct { diff --git a/index/generator/tests/registry/extraDevfileEntries.yaml b/index/generator/tests/registry/extraDevfileEntries.yaml index ecb4b763..89029b92 100644 --- a/index/generator/tests/registry/extraDevfileEntries.yaml +++ b/index/generator/tests/registry/extraDevfileEntries.yaml @@ -36,4 +36,15 @@ samples: description: java quarkus with devfile v2.0.0 git: remotes: - origin: https://github.com/elsony/devfile-sample-code-with-quarkus.git \ No newline at end of file + origin: https://github.com/elsony/devfile-sample-code-with-quarkus.git + - name: code-with-quarkus-no-version + displayName: Basic Quarkus Without Version + description: A simple Hello World Java application using Quarkus + icon: https://raw.githubusercontent.com/elsony/devfile-sample-code-with-quarkus/main/.devfile/icon/quarkus.png + tags: ["Java", "Quarkus"] + projectType: quarkus + language: java + provider: Red Hat + git: + remotes: + origin: https://github.com/elsony/devfile-sample-code-with-quarkus.git \ No newline at end of file diff --git a/index/generator/tests/registry/index_extra.json b/index/generator/tests/registry/index_extra.json index a63da1da..62422309 100644 --- a/index/generator/tests/registry/index_extra.json +++ b/index/generator/tests/registry/index_extra.json @@ -61,5 +61,24 @@ "description": "java quarkus with devfile v2.0.0" } ] + }, + { + "name": "code-with-quarkus-no-version", + "displayName": "Basic Quarkus Without Version", + "description": "A simple Hello World Java application using Quarkus", + "type": "sample", + "tags": [ + "Java", + "Quarkus" + ], + "icon": "https://raw.githubusercontent.com/elsony/devfile-sample-code-with-quarkus/main/.devfile/icon/quarkus.png", + "projectType": "quarkus", + "language": "java", + "provider": "Red Hat", + "git": { + "remotes": { + "origin": "https://github.com/elsony/devfile-sample-code-with-quarkus.git" + } + } } ] \ No newline at end of file diff --git a/index/generator/tests/registry/index_main.json b/index/generator/tests/registry/index_main.json index 939abcee..1fcffe80 100644 --- a/index/generator/tests/registry/index_main.json +++ b/index/generator/tests/registry/index_main.json @@ -502,5 +502,25 @@ "lastModified": "2024-04-19T11:45:48+01:00" } ] + }, + { + "name": "code-with-quarkus-no-version", + "displayName": "Basic Quarkus Without Version", + "description": "A simple Hello World Java application using Quarkus", + "type": "sample", + "tags": [ + "Java", + "Quarkus" + ], + "icon": "https://raw.githubusercontent.com/elsony/devfile-sample-code-with-quarkus/main/.devfile/icon/quarkus.png", + "projectType": "quarkus", + "language": "java", + "provider": "Red Hat", + "git": { + "remotes": { + "origin": "https://github.com/elsony/devfile-sample-code-with-quarkus.git" + } + }, + "lastModified": "2024-04-19T11:45:48+01:00" } ] diff --git a/index/generator/tests/registry/last_modified.json b/index/generator/tests/registry/last_modified.json index 0c26bba5..737bc480 100644 --- a/index/generator/tests/registry/last_modified.json +++ b/index/generator/tests/registry/last_modified.json @@ -76,6 +76,11 @@ "name": "code-with-quarkus", "version": "1.1.0", "lastModified": "2024-04-19T11:45:48+01:00" + }, + { + "name": "code-with-quarkus-no-version", + "version": "undefined", + "lastModified": "2024-04-19T11:45:48+01:00" } ] } diff --git a/index/generator/tests/registry/samples/code-with-quarkus-no-version/devfile.yaml b/index/generator/tests/registry/samples/code-with-quarkus-no-version/devfile.yaml new file mode 100644 index 00000000..f031535d --- /dev/null +++ b/index/generator/tests/registry/samples/code-with-quarkus-no-version/devfile.yaml @@ -0,0 +1,71 @@ +schemaVersion: 2.0.0 +metadata: + name: java-quarkus-no-version + icon: https://design.jboss.org/quarkus/logo/final/SVG/quarkus_icon_rgb_default.svg + attributes: + alpha.build-context: . + alpha.build-dockerfile: src/main/docker/Dockerfile.jvm.staged + website: https://quarkus.io + displayName: Quarkus Java + description: Upstream Quarkus with Java+GraalVM + tags: ["Java", "Quarkus"] + projectType: "quarkus" + language: "java" +starterProjects: + - name: community + zip: + location: https://code.quarkus.io/d?e=io.quarkus%3Aquarkus-resteasy&e=io.quarkus%3Aquarkus-micrometer&e=io.quarkus%3Aquarkus-smallrye-health&e=io.quarkus%3Aquarkus-openshift&cn=devfile + - name: redhat-product + zip: + location: https://code.quarkus.redhat.com/d?e=io.quarkus%3Aquarkus-resteasy&e=io.quarkus%3Aquarkus-smallrye-health&e=io.quarkus%3Aquarkus-openshift +components: + - name: buildguidance + attributes: + tool: console-import + container: + image: buildguidanceimage-placeholder + memoryLimit: 1024Mi + endpoints: + - name: http-8081 + targetPort: 8081 + - name: tools + container: + image: quay.io/eclipse/che-quarkus:nightly + memoryLimit: 1512Mi + mountSources: true + volumeMounts: + - name: m2 + path: /home/user/.m2 + endpoints: + - name: '8080-http' + targetPort: 8080 + - name: m2 + volume: + size: 3Gi +commands: + - id: init-compile + exec: + component: tools + commandLine: "mvn -Dmaven.repo.local=/home/user/.m2/repository compile" + workingDir: $PROJECTS_ROOT + - id: dev-run + exec: + component: tools + commandLine: "mvn -Dmaven.repo.local=/home/user/.m2/repository quarkus:dev -Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager" + hotReloadCapable: true + group: + kind: run + isDefault: true + workingDir: $PROJECTS_ROOT + - id: dev-debug + exec: + component: tools + commandLine: "mvn -Dmaven.repo.local=/home/user/.m2/repository quarkus:dev -Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager -Ddebug=${DEBUG_PORT}" + hotReloadCapable: true + group: + kind: debug + isDefault: true + workingDir: $PROJECTS_ROOT +events: + postStart: + - init-compile diff --git a/index/server/vendor/github.com/devfile/registry-support/index/generator/library/library.go b/index/server/vendor/github.com/devfile/registry-support/index/generator/library/library.go index d735619a..a54e6ebc 100644 --- a/index/server/vendor/github.com/devfile/registry-support/index/generator/library/library.go +++ b/index/server/vendor/github.com/devfile/registry-support/index/generator/library/library.go @@ -737,19 +737,25 @@ func SetLastModifiedValue(index []schema.Schema, registryDirPath string) ([]sche } for i := range index { - var mostCurrentLastModifiedDate time.Time - for j := range index[i].Versions { - schemaItem := index[i] // a stack or sample - version := schemaItem.Versions[j] - versionNum := version.Version - lastModifiedDate := lastModifiedEntriesMap[schemaItem.Name][versionNum] - updateSchemaLastModified(&schemaItem, j, lastModifiedDate) - if lastModifiedDate.After(mostCurrentLastModifiedDate) { - mostCurrentLastModifiedDate = lastModifiedDate + // Separate handling for versioned vs. non-versioned items + if len(index[i].Versions) > 0 { + var mostCurrentLastModifiedDate time.Time + for j := range index[i].Versions { + schemaItem := index[i] // a stack or sample + version := schemaItem.Versions[j] + versionNum := version.Version + lastModifiedDate := lastModifiedEntriesMap[schemaItem.Name][versionNum] + updateSchemaLastModified(&schemaItem, j, lastModifiedDate) + if lastModifiedDate.After(mostCurrentLastModifiedDate) { + mostCurrentLastModifiedDate = lastModifiedDate + } } + // lastModified of a stack or sample will be the date any version of it was last changed + index[i].LastModified = mostCurrentLastModifiedDate.Format(time.RFC3339) + } else { + lastModifiedDate := lastModifiedEntriesMap[index[i].Name]["undefined"] + updateSchemaLastModifiedNoVersion(&index[i], lastModifiedDate) } - // lastModified of a stack or sample will be the date any version of it was last changed - index[i].LastModified = mostCurrentLastModifiedDate.Format(time.RFC3339) } return index, nil @@ -767,6 +773,10 @@ func updateSchemaLastModified(s *schema.Schema, versionIndx int, lastModifiedDat s.Versions[versionIndx].LastModified = lastModifiedDate.Format(time.RFC3339) } +func updateSchemaLastModifiedNoVersion(s *schema.Schema, lastModifiedDate time.Time) { + s.LastModified = lastModifiedDate.Format(time.RFC3339) +} + // In checks if the value is in the array func inArray(arr []string, value string) bool { for _, item := range arr { diff --git a/tests/registry/last_modified.json b/tests/registry/last_modified.json index 6d158bde..51dc4f09 100644 --- a/tests/registry/last_modified.json +++ b/tests/registry/last_modified.json @@ -51,6 +51,16 @@ "name": "code-with-quarkus", "version": "1.0.0", "lastModified": "2024-02-15T11:45:48+01:00" + }, + { + "name": "java-springboot-basic", + "version": "undefined", + "lastModified": "2024-02-15T11:45:48+01:00" + }, + { + "name": "python-basic", + "version": "undefined", + "lastModified": "2024-02-15T11:45:48+01:00" } ] }