1
0
Fork 0

Remove hardcoded filenames for better readability

This commit is contained in:
Alexandre Oliveira 2024-06-24 14:31:24 +02:00
parent 20f5f050f3
commit ef815db8f2
3 changed files with 77 additions and 57 deletions

View file

@ -120,7 +120,7 @@ func GetRepositoryFile(ctx *context.Context) {
ctx, ctx,
pv, pv,
&packages_service.PackageFileInfo{ &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")), 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) { 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, OwnerID: ctx.Package.Owner.ID,
PackageType: packages_model.TypeAlpine, PackageType: packages_model.TypeAlpine,
Query: ctx.Params("filename"), 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 { if err != nil {
apiError(ctx, http.StatusInternalServerError, err) apiError(ctx, http.StatusInternalServerError, err)
return return
} }
if len(pfs) != 1 { if len(pfs) == 0 {
apiError(ctx, http.StatusNotFound, nil) apiError(ctx, http.StatusNotFound, nil)
return return
} }

View file

@ -30,7 +30,10 @@ import (
packages_service "code.gitea.io/gitea/services/packages" 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 // GetOrCreateRepositoryVersion gets or creates the internal repository package
// The Alpine registry needs multiple index files which are stored in this 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 // Delete the package indices if there are no packages
if len(pfs) == 0 { 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) { if err != nil && !errors.Is(err, util.ErrNotExist) {
return err return err
} else if pf == nil { } else if pf == nil {
@ -244,7 +247,7 @@ func buildPackagesIndex(ctx context.Context, ownerID int64, repoVersion *package
h := sha1.New() 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 return err
} }
@ -299,7 +302,7 @@ func buildPackagesIndex(ctx context.Context, ownerID int64, repoVersion *package
repoVersion, repoVersion,
&packages_service.PackageFileCreationInfo{ &packages_service.PackageFileCreationInfo{
PackageFileInfo: packages_service.PackageFileInfo{ PackageFileInfo: packages_service.PackageFileInfo{
Filename: IndexFilename, Filename: IndexArchiveFilename,
CompositeKey: fmt.Sprintf("%s|%s|%s", branch, repository, architecture), CompositeKey: fmt.Sprintf("%s|%s|%s", branch, repository, architecture),
}, },
Creator: user_model.NewGhostUser(), Creator: user_model.NewGhostUser(),

View file

@ -19,6 +19,7 @@ import (
"code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
alpine_module "code.gitea.io/gitea/modules/packages/alpine" alpine_module "code.gitea.io/gitea/modules/packages/alpine"
alpine_service "code.gitea.io/gitea/services/packages/alpine"
"code.gitea.io/gitea/tests" "code.gitea.io/gitea/tests"
"github.com/stretchr/testify/assert" "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) { t.Run("Index", func(t *testing.T) {
defer tests.PrintCurrentTest(t)() defer tests.PrintCurrentTest(t)()
@ -147,55 +191,22 @@ Djfa/2q5bH4699v++uMAAAAAAAAAAAAAAAAAAAAAAHbgA/eXQh8AKAAA`
req := NewRequest(t, "GET", url) req := NewRequest(t, "GET", url)
resp := MakeRequest(t, req, http.StatusOK) resp := MakeRequest(t, req, http.StatusOK)
assert.Condition(t, func() bool { content, err := readIndexContent(resp.Body)
br := bufio.NewReader(resp.Body) assert.NoError(t, err)
gzr, err := gzip.NewReader(br) assert.Contains(t, content, "C:Q1/se1PjO94hYXbfpNR1/61hVORIc=\n")
assert.NoError(t, err) assert.Contains(t, content, "P:"+packageName+"\n")
assert.Contains(t, content, "V:"+packageVersion+"\n")
for { assert.Contains(t, content, "A:x86_64\n")
gzr.Multistream(false) assert.NotContains(t, content, "A:noarch\n")
assert.Contains(t, content, "T:Gitea Test Package\n")
tr := tar.NewReader(gzr) assert.Contains(t, content, "U:https://gitea.io/\n")
for { assert.Contains(t, content, "L:MIT\n")
hd, err := tr.Next() assert.Contains(t, content, "S:1353\n")
if err == io.EOF { assert.Contains(t, content, "I:4096\n")
break assert.Contains(t, content, "o:gitea-test\n")
} assert.Contains(t, content, "m:KN4CK3R <kn4ck3r@gitea.io>\n")
assert.NoError(t, err) assert.Contains(t, content, "t:1679498030\n")
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 <kn4ck3r@gitea.io>\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
})
}) })
t.Run("Download", func(t *testing.T) { t.Run("Download", func(t *testing.T) {