0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-12-22 23:33:15 -05:00

Merge pull request 'Cleanup package/arch route' (#6206) from dragon/forgejo:cleanup-pkg-arch-route into forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6206
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
This commit is contained in:
Earl Warren 2024-12-09 05:24:17 +00:00
commit 4c64583377
5 changed files with 42 additions and 77 deletions

View file

@ -153,66 +153,10 @@ func CommonRoutes() *web.Route {
}) })
}, reqPackageAccess(perm.AccessModeRead)) }, reqPackageAccess(perm.AccessModeRead))
r.Group("/arch", func() { r.Group("/arch", func() {
r.Group("/repository.key", func() { r.Methods("HEAD,GET", "/repository.key", arch.GetRepositoryKey)
r.Head("", arch.GetRepositoryKey) r.Methods("HEAD,GET", "*", arch.GetPackageOrDB)
r.Get("", arch.GetRepositoryKey) r.Methods("PUT", "*", reqPackageAccess(perm.AccessModeWrite), arch.PushPackage)
}) r.Methods("DELETE", "*", reqPackageAccess(perm.AccessModeWrite), arch.RemovePackage)
r.Methods("HEAD,GET,PUT,DELETE", "*", func(ctx *context.Context) {
pathGroups := strings.Split(strings.Trim(ctx.Params("*"), "/"), "/")
groupLen := len(pathGroups)
isGetHead := ctx.Req.Method == "HEAD" || ctx.Req.Method == "GET"
isPut := ctx.Req.Method == "PUT"
isDelete := ctx.Req.Method == "DELETE"
if isGetHead {
if groupLen < 2 {
ctx.Status(http.StatusNotFound)
return
}
if groupLen == 2 {
ctx.SetParams("group", "")
ctx.SetParams("arch", pathGroups[0])
ctx.SetParams("file", pathGroups[1])
} else {
ctx.SetParams("group", strings.Join(pathGroups[:groupLen-2], "/"))
ctx.SetParams("arch", pathGroups[groupLen-2])
ctx.SetParams("file", pathGroups[groupLen-1])
}
arch.GetPackageOrDB(ctx)
return
} else if isPut {
ctx.SetParams("group", strings.Join(pathGroups, "/"))
reqPackageAccess(perm.AccessModeWrite)(ctx)
if ctx.Written() {
return
}
arch.PushPackage(ctx)
return
} else if isDelete {
if groupLen < 3 {
ctx.Status(http.StatusBadRequest)
return
}
if groupLen == 3 {
ctx.SetParams("group", "")
ctx.SetParams("package", pathGroups[0])
ctx.SetParams("version", pathGroups[1])
ctx.SetParams("arch", pathGroups[2])
} else {
ctx.SetParams("group", strings.Join(pathGroups[:groupLen-3], "/"))
ctx.SetParams("package", pathGroups[groupLen-3])
ctx.SetParams("version", pathGroups[groupLen-2])
ctx.SetParams("arch", pathGroups[groupLen-1])
}
reqPackageAccess(perm.AccessModeWrite)(ctx)
if ctx.Written() {
return
}
arch.RemovePackage(ctx)
return
}
ctx.Status(http.StatusNotFound)
})
}, reqPackageAccess(perm.AccessModeRead)) }, reqPackageAccess(perm.AccessModeRead))
r.Group("/cargo", func() { r.Group("/cargo", func() {
r.Group("/api/v1/crates", func() { r.Group("/api/v1/crates", func() {

View file

@ -59,7 +59,7 @@ func GetRepositoryKey(ctx *context.Context) {
} }
func PushPackage(ctx *context.Context) { func PushPackage(ctx *context.Context) {
group := ctx.Params("group") group := strings.Trim(ctx.Params("*"), "/")
releaser := refreshLocker(ctx, group) releaser := refreshLocker(ctx, group)
defer releaser() defer releaser()
upload, needToClose, err := ctx.UploadStream() upload, needToClose, err := ctx.UploadStream()
@ -183,11 +183,21 @@ func PushPackage(ctx *context.Context) {
} }
func GetPackageOrDB(ctx *context.Context) { func GetPackageOrDB(ctx *context.Context) {
var ( pathGroups := strings.Split(strings.Trim(ctx.Params("*"), "/"), "/")
file = ctx.Params("file") groupLen := len(pathGroups)
group = ctx.Params("group") if groupLen < 2 {
arch = ctx.Params("arch") ctx.Status(http.StatusNotFound)
) return
}
var file, group, arch string
if groupLen == 2 {
arch = pathGroups[0]
file = pathGroups[1]
} else {
group = strings.Join(pathGroups[:groupLen-2], "/")
arch = pathGroups[groupLen-2]
file = pathGroups[groupLen-1]
}
if archPkgOrSig.MatchString(file) { if archPkgOrSig.MatchString(file) {
pkg, u, pf, err := arch_service.GetPackageFile(ctx, group, file, ctx.Package.Owner.ID) pkg, u, pf, err := arch_service.GetPackageFile(ctx, group, file, ctx.Package.Owner.ID)
if err != nil { if err != nil {
@ -220,12 +230,23 @@ func GetPackageOrDB(ctx *context.Context) {
} }
func RemovePackage(ctx *context.Context) { func RemovePackage(ctx *context.Context) {
var ( pathGroups := strings.Split(strings.Trim(ctx.Params("*"), "/"), "/")
group = ctx.Params("group") groupLen := len(pathGroups)
pkg = ctx.Params("package") if groupLen < 3 {
ver = ctx.Params("version") ctx.Status(http.StatusBadRequest)
pkgArch = ctx.Params("arch") return
) }
var group, pkg, ver, pkgArch string
if groupLen == 3 {
pkg = pathGroups[0]
ver = pathGroups[1]
pkgArch = pathGroups[2]
} else {
group = strings.Join(pathGroups[:groupLen-3], "/")
pkg = pathGroups[groupLen-3]
ver = pathGroups[groupLen-2]
pkgArch = pathGroups[groupLen-1]
}
releaser := refreshLocker(ctx, group) releaser := refreshLocker(ctx, group)
defer releaser() defer releaser()
pv, err := packages_model.GetVersionByNameAndVersion( pv, err := packages_model.GetVersionByNameAndVersion(

View file

@ -177,10 +177,11 @@ func ViewPackageVersion(ctx *context.Context) {
ctx.Data["Title"] = pd.Package.Name ctx.Data["Title"] = pd.Package.Name
ctx.Data["IsPackagesPage"] = true ctx.Data["IsPackagesPage"] = true
ctx.Data["PackageDescriptor"] = pd ctx.Data["PackageDescriptor"] = pd
ctx.Data["PackageRegistryHost"] = setting.Packages.RegistryHost
switch pd.Package.Type { switch pd.Package.Type {
case packages_model.TypeContainer: case packages_model.TypeContainer:
ctx.Data["RegistryHost"] = setting.Packages.RegistryHost
case packages_model.TypeAlpine: case packages_model.TypeAlpine:
branches := make(container.Set[string]) branches := make(container.Set[string])
repositories := make(container.Set[string]) repositories := make(container.Set[string])
@ -203,7 +204,6 @@ func ViewPackageVersion(ctx *context.Context) {
ctx.Data["Repositories"] = util.Sorted(repositories.Values()) ctx.Data["Repositories"] = util.Sorted(repositories.Values())
ctx.Data["Architectures"] = util.Sorted(architectures.Values()) ctx.Data["Architectures"] = util.Sorted(architectures.Values())
case packages_model.TypeArch: case packages_model.TypeArch:
ctx.Data["RegistryHost"] = setting.Packages.RegistryHost
ctx.Data["SignMail"] = fmt.Sprintf("%s@noreply.%s", ctx.Package.Owner.Name, setting.Packages.RegistryHost) ctx.Data["SignMail"] = fmt.Sprintf("%s@noreply.%s", ctx.Package.Owner.Name, setting.Packages.RegistryHost)
groups := make(container.Set[string]) groups := make(container.Set[string])
for _, f := range pd.Files { for _, f := range pd.Files {

View file

@ -25,7 +25,7 @@ pacman-key --lsign-key '{{$.SignMail}}'</code></pre>
{{end -}}{{- if gt $GroupSize 1 -}} {{end -}}{{- if gt $GroupSize 1 -}}
# {{ctx.Locale.Tr "packages.arch.pacman.repo.multi.item" .}} # {{ctx.Locale.Tr "packages.arch.pacman.repo.multi.item" .}}
{{end -}} {{end -}}
[{{$.PackageDescriptor.Owner.LowerName}}.{{$.RegistryHost}}] [{{$.PackageDescriptor.Owner.LowerName}}.{{$.PackageRegistryHost}}]
SigLevel = Required SigLevel = Required
Server = <origin-url data-url="{{AppSubUrl}}/api/packages/{{$.PackageDescriptor.Owner.Name}}/arch/{{.}}/$arch"></origin-url> Server = <origin-url data-url="{{AppSubUrl}}/api/packages/{{$.PackageDescriptor.Owner.Name}}/arch/{{.}}/$arch"></origin-url>
{{end -}} {{end -}}

View file

@ -5,13 +5,13 @@
<div class="field"> <div class="field">
<label>{{svg "octicon-terminal"}} {{ctx.Locale.Tr "packages.container.pull"}}</label> <label>{{svg "octicon-terminal"}} {{ctx.Locale.Tr "packages.container.pull"}}</label>
{{if eq .PackageDescriptor.Metadata.Type "helm"}} {{if eq .PackageDescriptor.Metadata.Type "helm"}}
<div class="markup"><pre class="code-block"><code>helm pull oci://{{.RegistryHost}}/{{.PackageDescriptor.Owner.LowerName}}/{{.PackageDescriptor.Package.LowerName}} --version {{.PackageDescriptor.Version.LowerVersion}}</code></pre></div> <div class="markup"><pre class="code-block"><code>helm pull oci://{{.PackageRegistryHost}}/{{.PackageDescriptor.Owner.LowerName}}/{{.PackageDescriptor.Package.LowerName}} --version {{.PackageDescriptor.Version.LowerVersion}}</code></pre></div>
{{else}} {{else}}
{{$separator := ":"}} {{$separator := ":"}}
{{if not .PackageDescriptor.Metadata.IsTagged}} {{if not .PackageDescriptor.Metadata.IsTagged}}
{{$separator = "@"}} {{$separator = "@"}}
{{end}} {{end}}
<div class="markup"><pre class="code-block"><code>docker pull {{.RegistryHost}}/{{.PackageDescriptor.Owner.LowerName}}/{{.PackageDescriptor.Package.LowerName}}{{$separator}}{{.PackageDescriptor.Version.LowerVersion}}</code></pre></div> <div class="markup"><pre class="code-block"><code>docker pull {{.PackageRegistryHost}}/{{.PackageDescriptor.Owner.LowerName}}/{{.PackageDescriptor.Package.LowerName}}{{$separator}}{{.PackageDescriptor.Version.LowerVersion}}</code></pre></div>
{{end}} {{end}}
</div> </div>
<div class="field"> <div class="field">