Files
Gitea/services/packages/debian/spec.go
T
b0b31bc593 feat(packages): expose package file properties in the package files API (#40066)
Package files carry type specific properties like `debian.distribution`,
`debian.component`, `rpm.group` or `alpine.branch` that decide where a
file lives in a registry, but the API never returned them. A client
listing `GET /packages/{owner}/{type}/{name}/{version}/files` could not
tell which distribution or component a file belongs to, or tell apart
same-named files in different ones. Tools copying packages between
instances, like https://gitea.com/gitea/importer, need them to re-upload
a file to the same place.

This adds a `properties` map to `PackageFile`, limited to the properties
that locate a file. Internal ones like the rpm, alpine, arch and conda
metadata or the Debian control file stay hidden, as they can be many MiB
per file.

Co-authored-by: silverwind <me@silverwind.io>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
2026-10-29 18:10:36 +00:00

92 lines
3.1 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package debian
import (
"context"
"html/template"
"strings"
packages_model "gitea.dev/models/packages"
"gitea.dev/modules/container"
"gitea.dev/modules/htmlutil"
"gitea.dev/modules/httplib"
debian_module "gitea.dev/modules/packages/debian"
"gitea.dev/modules/util"
packages_service "gitea.dev/services/packages"
)
type Specialization struct {
packages_service.SpecOnBeforeRemovePackageAll
packages_service.SpecOnBeforeRemovePackageVersion
}
var _ packages_service.Specialization = (*Specialization)(nil)
type debianViewData struct {
Distributions []string
Components []string
Architectures []string
}
func (s Specialization) GetViewPackageVersionData(ctx context.Context, pd *packages_model.PackageDescriptor) (any, error) {
distributions := make(container.Set[string])
components := make(container.Set[string])
architectures := make(container.Set[string])
for _, f := range pd.Files {
for _, pp := range f.Properties {
switch pp.Name {
case debian_module.PropertyDistribution:
distributions.Add(pp.Value)
case debian_module.PropertyComponent:
components.Add(pp.Value)
case debian_module.PropertyArchitecture:
architectures.Add(pp.Value)
}
}
}
viewData := debianViewData{
Distributions: util.Sorted(distributions.Values()),
Components: util.Sorted(components.Values()),
Architectures: util.Sorted(architectures.Values()),
}
return viewData, nil
}
func writeVarSelect(w *htmlutil.HTMLBuilder, varName string, varItems []string) {
w.WriteFormatf(`<div>%s=<select data-code-var="%s">`, varName, varName)
for _, v := range varItems {
w.WriteFormatf(`<option value="%s">%s</option>`, v, v)
}
w.WriteHTML("</select></div>")
}
func (s Specialization) RenderSetupManual(ctx context.Context, pkg *packages_model.PackageDescriptor, viewDataAny any) template.HTML {
viewData := viewDataAny.(debianViewData) //nolint:forcetypeassert // must be valid
const nl = "\n"
appFullLink := strings.TrimSuffix(httplib.GuessCurrentAppURL(ctx), "/")
w := &htmlutil.HTMLBuilder{}
w.WriteHTML(`<div data-global-init="initPackagesManualVars">`)
{
w.WriteHTML(`<div class="package-manual-vars">`)
writeVarSelect(w, "distribution", viewData.Distributions)
writeVarSelect(w, "component", viewData.Components)
w.WriteHTML(`</div>`)
}
{
w.WriteHTML(`<div class="markup"><pre class="code-block"><code>`)
w.WriteFormatf(`sudo curl %s/api/packages/%s/debian/repository.key -o /etc/apt/keyrings/gitea-%s.asc`+nl+nl, appFullLink, pkg.Owner.Name, pkg.Owner.Name)
w.WriteFormatf(`echo "deb [signed-by=/etc/apt/keyrings/gitea-%s.asc] %s/api/packages/%s/debian <span>$distribution</span> <span>$component</span>" | sudo tee -a /etc/apt/sources.list.d/gitea.list`+nl+nl, pkg.Owner.Name, appFullLink, pkg.Owner.Name)
w.WriteHTML(`sudo apt update` + nl)
w.WriteHTML(`</code></pre></div>`)
}
w.WriteHTML(`</div>`)
return w.HTMLString()
}
func (s Specialization) PublicPropertyNames() []string {
return []string{debian_module.PropertyDistribution, debian_module.PropertyComponent, debian_module.PropertyArchitecture}
}