From eaad6266a6cf5c92734d0828e6e69fbdf8aa8358 Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Fri, 5 Aug 2022 21:00:47 +0200 Subject: [PATCH] Allow multiple metadata files for Maven packages (#20674) * Allow file overwrite for meta files. * Added tests. * lint Co-authored-by: Lauris BH Co-authored-by: techknowlogick --- integrations/api_packages_maven_test.go | 22 +++++++++++++++++----- routers/api/packages/maven/maven.go | 5 +++-- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/integrations/api_packages_maven_test.go b/integrations/api_packages_maven_test.go index c7c45426859e7..e7ab3bfe4b640 100644 --- a/integrations/api_packages_maven_test.go +++ b/integrations/api_packages_maven_test.go @@ -42,6 +42,7 @@ func TestPackageMaven(t *testing.T) { defer PrintCurrentTest(t)() putFile(t, fmt.Sprintf("/%s/%s", packageVersion, filename), "test", http.StatusCreated) + putFile(t, fmt.Sprintf("/%s/%s", packageVersion, filename), "test", http.StatusBadRequest) putFile(t, "/maven-metadata.xml", "test", http.StatusOK) pvs, err := packages.GetVersionsByPackageType(db.DefaultContext, user.ID, packages.TypeMaven) @@ -135,12 +136,14 @@ func TestPackageMaven(t *testing.T) { pfs, err := packages.GetFilesByVersionID(db.DefaultContext, pvs[0].ID) assert.NoError(t, err) assert.Len(t, pfs, 2) - i := 0 - if strings.HasSuffix(pfs[1].Name, ".pom") { - i = 1 + for _, pf := range pfs { + if strings.HasSuffix(pf.Name, ".pom") { + assert.Equal(t, filename+".pom", pf.Name) + assert.True(t, pf.IsLead) + } else { + assert.False(t, pf.IsLead) + } } - assert.Equal(t, filename+".pom", pfs[i].Name) - assert.True(t, pfs[i].IsLead) }) t.Run("DownloadPOM", func(t *testing.T) { @@ -202,4 +205,13 @@ func TestPackageMaven(t *testing.T) { assert.Equal(t, checksum, resp.Body.String()) } }) + + t.Run("UploadSnapshot", func(t *testing.T) { + snapshotVersion := packageVersion + "-SNAPSHOT" + + putFile(t, fmt.Sprintf("/%s/%s", snapshotVersion, filename), "test", http.StatusCreated) + putFile(t, "/maven-metadata.xml", "test", http.StatusOK) + 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) + }) } diff --git a/routers/api/packages/maven/maven.go b/routers/api/packages/maven/maven.go index bba4babf04f6f..072a15f95c41d 100644 --- a/routers/api/packages/maven/maven.go +++ b/routers/api/packages/maven/maven.go @@ -266,8 +266,9 @@ func UploadPackageFile(ctx *context.Context) { PackageFileInfo: packages_service.PackageFileInfo{ Filename: params.Filename, }, - Data: buf, - IsLead: false, + Data: buf, + IsLead: false, + OverwriteExisting: params.IsMeta, } // If it's the package pom file extract the metadata