From ef815db8f24d2269b39356614706670dffa0b75a Mon Sep 17 00:00:00 2001 From: Alexandre Oliveira Date: Mon, 24 Jun 2024 14:31:24 +0200 Subject: [PATCH] Remove hardcoded filenames for better readability --- routers/api/packages/alpine/alpine.go | 16 ++- services/packages/alpine/repository.go | 11 +- tests/integration/api_packages_alpine_test.go | 107 ++++++++++-------- 3 files changed, 77 insertions(+), 57 deletions(-) diff --git a/routers/api/packages/alpine/alpine.go b/routers/api/packages/alpine/alpine.go index 481cf70d33..831a910e36 100644 --- a/routers/api/packages/alpine/alpine.go +++ b/routers/api/packages/alpine/alpine.go @@ -120,7 +120,7 @@ func GetRepositoryFile(ctx *context.Context) { ctx, pv, &packages_service.PackageFileInfo{ - Filename: alpine_service.IndexFilename, + Filename: alpine_service.IndexArchiveFilename, CompositeKey: fmt.Sprintf("%s|%s|%s", ctx.Params("branch"), ctx.Params("repository"), ctx.Params("architecture")), }, ) @@ -217,17 +217,23 @@ func UploadPackageFile(ctx *context.Context) { } func DownloadPackageFile(ctx *context.Context) { - pfs, _, err := packages_model.SearchFiles(ctx, &packages_model.PackageFileSearchOptions{ + branch := ctx.Params("branch") + repository := ctx.Params("repository") + architecture := ctx.Params("architecture") + + opts := &packages_model.PackageFileSearchOptions{ OwnerID: ctx.Package.Owner.ID, PackageType: packages_model.TypeAlpine, Query: ctx.Params("filename"), - CompositeKey: fmt.Sprintf("%s|%s|%s", ctx.Params("branch"), ctx.Params("repository"), ctx.Params("architecture")), - }) + CompositeKey: fmt.Sprintf("%s|%s|%s", branch, repository, architecture), + } + + pfs, _, err := packages_model.SearchFiles(ctx, opts) if err != nil { apiError(ctx, http.StatusInternalServerError, err) return } - if len(pfs) != 1 { + if len(pfs) == 0 { apiError(ctx, http.StatusNotFound, nil) return } diff --git a/services/packages/alpine/repository.go b/services/packages/alpine/repository.go index 104548b421..92f475bb7b 100644 --- a/services/packages/alpine/repository.go +++ b/services/packages/alpine/repository.go @@ -30,7 +30,10 @@ import ( packages_service "code.gitea.io/gitea/services/packages" ) -const IndexFilename = "APKINDEX.tar.gz" +const ( + IndexFilename = "APKINDEX" + IndexArchiveFilename = IndexFilename + ".tar.gz" +) // GetOrCreateRepositoryVersion gets or creates the internal repository package // The Alpine registry needs multiple index files which are stored in this package. @@ -151,7 +154,7 @@ func buildPackagesIndex(ctx context.Context, ownerID int64, repoVersion *package // Delete the package indices if there are no packages if len(pfs) == 0 { - pf, err := packages_model.GetFileForVersionByName(ctx, repoVersion.ID, IndexFilename, fmt.Sprintf("%s|%s|%s", branch, repository, architecture)) + pf, err := packages_model.GetFileForVersionByName(ctx, repoVersion.ID, IndexArchiveFilename, fmt.Sprintf("%s|%s|%s", branch, repository, architecture)) if err != nil && !errors.Is(err, util.ErrNotExist) { return err } else if pf == nil { @@ -244,7 +247,7 @@ func buildPackagesIndex(ctx context.Context, ownerID int64, repoVersion *package h := sha1.New() - if err := writeGzipStream(io.MultiWriter(unsignedIndexContent, h), "APKINDEX", buf.Bytes(), true); err != nil { + if err := writeGzipStream(io.MultiWriter(unsignedIndexContent, h), IndexFilename, buf.Bytes(), true); err != nil { return err } @@ -299,7 +302,7 @@ func buildPackagesIndex(ctx context.Context, ownerID int64, repoVersion *package repoVersion, &packages_service.PackageFileCreationInfo{ PackageFileInfo: packages_service.PackageFileInfo{ - Filename: IndexFilename, + Filename: IndexArchiveFilename, CompositeKey: fmt.Sprintf("%s|%s|%s", branch, repository, architecture), }, Creator: user_model.NewGhostUser(), diff --git a/tests/integration/api_packages_alpine_test.go b/tests/integration/api_packages_alpine_test.go index f70d3c23af..e7f866d0e8 100644 --- a/tests/integration/api_packages_alpine_test.go +++ b/tests/integration/api_packages_alpine_test.go @@ -19,6 +19,7 @@ import ( "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" alpine_module "code.gitea.io/gitea/modules/packages/alpine" + alpine_service "code.gitea.io/gitea/services/packages/alpine" "code.gitea.io/gitea/tests" "github.com/stretchr/testify/assert" @@ -139,6 +140,49 @@ Djfa/2q5bH4699v++uMAAAAAAAAAAAAAAAAAAAAAAHbgA/eXQh8AKAAA` }) }) + readIndexContent := func(r io.Reader) (string, error) { + br := bufio.NewReader(r) + + gzr, err := gzip.NewReader(br) + if err != nil { + return "", err + } + + for { + gzr.Multistream(false) + + tr := tar.NewReader(gzr) + for { + hd, err := tr.Next() + if err == io.EOF { + break + } + if err != nil { + return "", err + } + + if hd.Name == alpine_service.IndexFilename { + buf, err := io.ReadAll(tr) + if err != nil { + return "", err + } + + return string(buf), nil + } + } + + err = gzr.Reset(br) + if err == io.EOF { + break + } + if err != nil { + return "", err + } + } + + return "", io.EOF + } + t.Run("Index", func(t *testing.T) { defer tests.PrintCurrentTest(t)() @@ -147,55 +191,22 @@ Djfa/2q5bH4699v++uMAAAAAAAAAAAAAAAAAAAAAAHbgA/eXQh8AKAAA` req := NewRequest(t, "GET", url) resp := MakeRequest(t, req, http.StatusOK) - assert.Condition(t, func() bool { - br := bufio.NewReader(resp.Body) + content, err := readIndexContent(resp.Body) + assert.NoError(t, err) - gzr, err := gzip.NewReader(br) - assert.NoError(t, err) - - for { - gzr.Multistream(false) - - tr := tar.NewReader(gzr) - for { - hd, err := tr.Next() - if err == io.EOF { - break - } - assert.NoError(t, err) - - if hd.Name == "APKINDEX" { - buf, err := io.ReadAll(tr) - assert.NoError(t, err) - - s := string(buf) - - assert.Contains(t, s, "C:Q1/se1PjO94hYXbfpNR1/61hVORIc=\n") - assert.Contains(t, s, "P:"+packageName+"\n") - assert.Contains(t, s, "V:"+packageVersion+"\n") - assert.Contains(t, s, "A:x86_64\n") - assert.Contains(t, s, "T:Gitea Test Package\n") - assert.Contains(t, s, "U:https://gitea.io/\n") - assert.Contains(t, s, "L:MIT\n") - assert.Contains(t, s, "S:1353\n") - assert.Contains(t, s, "I:4096\n") - assert.Contains(t, s, "o:gitea-test\n") - assert.Contains(t, s, "m:KN4CK3R \n") - assert.Contains(t, s, "t:1679498030\n") - - return true - } - } - - err = gzr.Reset(br) - if err == io.EOF { - break - } - assert.NoError(t, err) - } - - return false - }) + assert.Contains(t, content, "C:Q1/se1PjO94hYXbfpNR1/61hVORIc=\n") + assert.Contains(t, content, "P:"+packageName+"\n") + assert.Contains(t, content, "V:"+packageVersion+"\n") + assert.Contains(t, content, "A:x86_64\n") + assert.NotContains(t, content, "A:noarch\n") + assert.Contains(t, content, "T:Gitea Test Package\n") + assert.Contains(t, content, "U:https://gitea.io/\n") + assert.Contains(t, content, "L:MIT\n") + assert.Contains(t, content, "S:1353\n") + assert.Contains(t, content, "I:4096\n") + assert.Contains(t, content, "o:gitea-test\n") + assert.Contains(t, content, "m:KN4CK3R \n") + assert.Contains(t, content, "t:1679498030\n") }) t.Run("Download", func(t *testing.T) {