From ac4d535dbf6b669997dd01560e692dd215b1321b Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Fri, 10 May 2024 11:12:41 +0200 Subject: [PATCH] templates: Be more forgiving about missing package metadata When rendering templates for packages, be more forgiving about missing metadata. For some repository types - like maven - metadata is uploaded separately. If that upload fails, or does not happen, there will be no metadata. In that case, Forgejo should handle it gracefully, and render as much of the information as possible, without erroring out. Rendering without metadata allows one to delete a partial package, while if we throw errors, that becomes a whole lot harder. This patch adjusts the generic metadata template, and also the maven template. There may be more cases of the same problem lying around. Fixes #3663. Signed-off-by: Gergely Nagy --- templates/package/content/maven.tmpl | 76 ++++++++++---------- templates/package/metadata/maven.tmpl | 2 +- tests/integration/api_packages_maven_test.go | 11 +++ 3 files changed, 52 insertions(+), 37 deletions(-) diff --git a/templates/package/content/maven.tmpl b/templates/package/content/maven.tmpl index 49ada6a3a3..7f7e3fafcc 100644 --- a/templates/package/content/maven.tmpl +++ b/templates/package/content/maven.tmpl @@ -23,49 +23,53 @@ </snapshotRepository> </distributionManagement> -
- -
<dependency>
+			{{if .PackageDescriptor.Metadata}}
+				
+ +
<dependency>
 	<groupId>{{.PackageDescriptor.Metadata.GroupID}}</groupId>
 	<artifactId>{{.PackageDescriptor.Metadata.ArtifactID}}</artifactId>
 	<version>{{.PackageDescriptor.Version.Version}}</version>
 </dependency>
-
-
- -
mvn install
-
-
- -
mvn dependency:get -DremoteRepositories= -Dartifact={{.PackageDescriptor.Metadata.GroupID}}:{{.PackageDescriptor.Metadata.ArtifactID}}:{{.PackageDescriptor.Version.Version}}
-
-
- -
+
+
+ +
mvn install
+
+
+ +
mvn dependency:get -DremoteRepositories= -Dartifact={{.PackageDescriptor.Metadata.GroupID}}:{{.PackageDescriptor.Metadata.ArtifactID}}:{{.PackageDescriptor.Version.Version}}
+
+
+ +
+ {{end}}
- {{if .PackageDescriptor.Metadata.Description}} -

{{ctx.Locale.Tr "packages.about"}}

-
- {{.PackageDescriptor.Metadata.Description}} -
- {{end}} - - {{if .PackageDescriptor.Metadata.Dependencies}} -

{{ctx.Locale.Tr "packages.dependencies"}}

-
-
- {{range .PackageDescriptor.Metadata.Dependencies}} -
- {{svg "octicon-package-dependencies" 16 ""}} -
-
{{.GroupID}}:{{.ArtifactID}}
-
{{.Version}}
-
-
- {{end}} + {{if .PackageDescriptor.Metadata}} + {{if .PackageDescriptor.Metadata.Description}} +

{{ctx.Locale.Tr "packages.about"}}

+
+ {{.PackageDescriptor.Metadata.Description}}
-
+ {{end}} + + {{if .PackageDescriptor.Metadata.Dependencies}} +

{{ctx.Locale.Tr "packages.dependencies"}}

+
+
+ {{range .PackageDescriptor.Metadata.Dependencies}} +
+ {{svg "octicon-package-dependencies" 16 ""}} +
+
{{.GroupID}}:{{.ArtifactID}}
+
{{.Version}}
+
+
+ {{end}} +
+
+ {{end}} {{end}} {{end}} diff --git a/templates/package/metadata/maven.tmpl b/templates/package/metadata/maven.tmpl index 548be61790..62573a146a 100644 --- a/templates/package/metadata/maven.tmpl +++ b/templates/package/metadata/maven.tmpl @@ -1,4 +1,4 @@ -{{if eq .PackageDescriptor.Package.Type "maven"}} +{{if and (eq .PackageDescriptor.Package.Type "maven") .PackageDescriptor.Metadata}} {{if .PackageDescriptor.Metadata.Name}}
{{svg "octicon-note" 16 "tw-mr-2"}} {{.PackageDescriptor.Metadata.Name}}
{{end}} {{if .PackageDescriptor.Metadata.ProjectURL}}
{{svg "octicon-link-external" 16 "tw-mr-2"}} {{ctx.Locale.Tr "packages.details.project_site"}}
{{end}} {{range .PackageDescriptor.Metadata.Licenses}}
{{svg "octicon-law" 16 "tw-mr-2"}} {{.}}
{{end}} diff --git a/tests/integration/api_packages_maven_test.go b/tests/integration/api_packages_maven_test.go index b6c213abcf..2fc4a0f6de 100644 --- a/tests/integration/api_packages_maven_test.go +++ b/tests/integration/api_packages_maven_test.go @@ -241,4 +241,15 @@ func TestPackageMaven(t *testing.T) { putFile(t, fmt.Sprintf("/%s/maven-metadata.xml", snapshotVersion), "test", http.StatusCreated) putFile(t, fmt.Sprintf("/%s/maven-metadata.xml", snapshotVersion), "test-overwrite", http.StatusCreated) }) + + t.Run("Partial upload", func(t *testing.T) { + defer tests.PrintCurrentTest(t)() + + partialVersion := packageVersion + "-PARTIAL" + putFile(t, fmt.Sprintf("/%s/%s", partialVersion, filename), "test", http.StatusCreated) + pkgUIURL := fmt.Sprintf("/%s/-/packages/maven/%s-%s/%s", user.Name, groupID, artifactID, partialVersion) + req := NewRequest(t, "GET", pkgUIURL) + resp := MakeRequest(t, req, http.StatusOK) + assert.NotContains(t, resp.Body.String(), "Internal server error") + }) }