Simplify and fix versioning logic

This commit is contained in:
ferreo 2024-08-04 12:25:24 +01:00
parent 80e0b87bd1
commit dfe0e97d80
4 changed files with 67 additions and 130 deletions

View File

@ -167,6 +167,12 @@ func (r *Repository) GetFailedBuilds() ([]domain.BuildState, error) {
func sourcePackageToDto(pkg domain.SourcePackage) domain.SourcePackageDTO {
dto := domain.SourcePackageDTO{
Name: pkg.Name,
Version: pkg.Version,
NewVersion: pkg.NewVersion,
Status: pkg.Status,
LastBuildStatus: pkg.LastBuildStatus,
BuildAttempts: pkg.BuildAttempts,
Has32bit: pkg.Has32bit,
Packages: make([]domain.PackageInfo, 0),
}
pkg.Packages.ForEach(func(k string, v domain.PackageInfo) bool {
@ -179,6 +185,12 @@ func sourcePackageToDto(pkg domain.SourcePackage) domain.SourcePackageDTO {
func sourcePackageDtoToDomain(dto domain.SourcePackageDTO) domain.SourcePackage {
pkg := domain.SourcePackage{
Name: dto.Name,
Version: dto.Version,
NewVersion: dto.NewVersion,
Status: dto.Status,
LastBuildStatus: dto.LastBuildStatus,
BuildAttempts: dto.BuildAttempts,
Has32bit: dto.Has32bit,
Packages: haxmap.New[string, domain.PackageInfo](),
}
for _, v := range dto.Packages {

View File

@ -29,6 +29,11 @@ type SourcePackage struct {
type SourcePackageDTO struct {
Name string `gorm:"primarykey"`
Has32bit bool
Version string
NewVersion string
Status PackageStatus
LastBuildStatus PackageStatus
BuildAttempts int
Packages []PackageInfo `gorm:"foreignKey:PackageName"`
}

View File

@ -33,15 +33,12 @@ func Packages(c *fiber.Ctx) error {
matchesFilter := filter == ""
matchesSearch := search == "" || strings.Contains(strings.ToLower(k), search)
source.Packages.ForEach(func(key string, value domain.PackageInfo) bool {
if !matchesFilter && value.Status == domain.PackageStatus(filter) {
if !matchesFilter && source.Status == domain.PackageStatus(filter) {
matchesFilter = true
}
if !matchesSearch && strings.Contains(strings.ToLower(key), search) {
if !matchesSearch && strings.Contains(strings.ToLower(k), search) {
matchesSearch = true
}
return !(matchesFilter && matchesSearch)
})
if matchesFilter && matchesSearch {
finalReturn = append(finalReturn, source)

View File

@ -53,37 +53,15 @@ func ProcessPackages() error {
mergedPackage := domain.SourcePackage{
Name: curr.Name,
Has32bit: curr.Has32bit,
Version: v.Version,
NewVersion: v.NewVersion,
Status: v.Status,
BuildAttempts: curr.BuildAttempts,
LastBuildStatus: curr.LastBuildStatus,
Packages: haxmap.New[string, domain.PackageInfo](),
}
v.Packages.ForEach(func(pkgName string, newPkg domain.PackageInfo) bool {
if existingPkg, ok := curr.Packages.Get(pkgName); ok {
if newPkg.Version != existingPkg.Version ||
newPkg.Status != existingPkg.Status ||
newPkg.NewVersion != existingPkg.NewVersion {
if newPkg.Status == domain.Current {
mergedPackage.Packages.Set(pkgName, newPkg)
return true
}
newPkg.LastBuildStatus = existingPkg.LastBuildStatus
newPkg.BuildAttempts = existingPkg.BuildAttempts
mergedPackage.Packages.Set(pkgName, newPkg)
} else {
mergedPackage.Packages.Set(pkgName, existingPkg)
}
} else {
mergedPackage.Packages.Set(pkgName, newPkg)
}
return true
})
curr.Packages.ForEach(func(pkgName string, pkg domain.PackageInfo) bool {
if _, exists := mergedPackage.Packages.Get(pkgName); !exists {
mergedPackage.Packages.Set(pkgName, pkg)
}
return true
})
updatedPackages.Set(k, mergedPackage)
return true
})
@ -101,20 +79,6 @@ func ProcessPackages() error {
return true
})
for _, pkg := range config.Configs.I386List {
if _, ok := updatedPackages.Get(pkg); !ok {
_, ok := updatedPackages.Get(pkg + "-dmo")
if ok {
continue
}
updatedPackages.Set(pkg, domain.SourcePackage{
Name: pkg,
Has32bit: true,
Packages: haxmap.New[string, domain.PackageInfo](),
})
}
}
currentPackages = updatedPackages
LastUpdateTime = time.Now()
@ -149,18 +113,6 @@ func UpdateSourcePackage(pkg domain.SourcePackage) error {
return saveSingleToDb(pkg)
}
func IsBuilt(pkg domain.PackageInfo) bool {
curr, ok := currentPackages.Get(pkg.Source)
if !ok {
return false
}
pk, ok := curr.Packages.Get(pkg.PackageName)
if !ok {
return false
}
return pk.Status == domain.Built || pk.Status == domain.Current
}
func saveSingleToDb(pkg domain.SourcePackage) error {
err := helpers.DBInst.UpdatePackage(pkg)
if err != nil {
@ -243,7 +195,7 @@ func LoadInternalPackages(internalPackages *haxmap.Map[string, domain.SourcePack
return true
}
mVer, _ := version.Parse(pkg.Version)
extVer, _ := version.Parse(newPkg.Version)
extVer, _ := version.Parse(strings.Split(newPkg.Version, "+b")[0])
cmpVal := version.Compare(extVer, mVer)
if cmpVal >= 0 {
pk.Version = getHighestVer(pkg.Version, newPkg.Version)
@ -277,27 +229,31 @@ func LoadExternalPackages(externalPackages *haxmap.Map[string, domain.SourcePack
if err != nil {
return err
}
packages.ForEach(func(k string, v domain.PackageInfo) bool {
pk, ok := externalPackages.Get(v.Source)
packages.ForEach(func(newKey string, newPkg domain.PackageInfo) bool {
pk, ok := externalPackages.Get(newPkg.Source)
if !ok {
newMap := haxmap.New[string, domain.PackageInfo]()
newMap.Set(k, v)
externalPackages.Set(v.Source, domain.SourcePackage{
Name: v.Source,
newMap.Set(newKey, newPkg)
externalPackages.Set(newPkg.Source, domain.SourcePackage{
Name: newPkg.Source,
Packages: newMap,
Status: domain.Current,
Version: newPkg.Version,
})
return true
}
pkg, ok := pk.Packages.Get(k)
pkg, ok := pk.Packages.Get(newKey)
if !ok {
pk.Packages.Set(k, v)
pk.Packages.Set(newKey, newPkg)
return true
}
mVer, _ := version.Parse(pkg.Version)
extVer, _ := version.Parse(v.Version)
extVer, _ := version.Parse(strings.Split(newPkg.Version, "+b")[0])
cmpVal := version.Compare(extVer, mVer)
if cmpVal >= 0 {
pk.Packages.Set(k, v)
pk.Version = getHighestVer(pkg.Version, newPkg.Version)
pk.Packages.Set(newKey, newPkg)
externalPackages.Set(newPkg.Source, pk)
return true
}
return true
@ -312,69 +268,26 @@ func ProcessMissingPackages(internalPackages *haxmap.Map[string, domain.SourcePa
externalPackages.ForEach(func(k string, src domain.SourcePackage) bool {
_, ok := internalPackages.Get(k)
if !ok && src.Packages.Len() > 0 {
newStatus := domain.Missing
vers := ""
src.Packages.ForEach(func(k string, v domain.PackageInfo) bool {
v.Status = newStatus
v.Version = strings.Split(v.Version, "+b")[0]
vers = getHighestVer(vers, v.Version)
src.Packages.Set(k, v)
return true
})
src.Version = vers
src.Status = newStatus
src.Status = domain.Missing
internalPackages.Set(k, src)
}
return true
})
}
func getHighestVer(ver string, newVer string) string {
mVer, _ := version.Parse(ver)
extVer, _ := version.Parse(newVer)
cmpVal := version.Compare(mVer, extVer)
if cmpVal < 0 {
return newVer
}
return ver
}
func ProcessStalePackages(internalPackages *haxmap.Map[string, domain.SourcePackage], externalPackages *haxmap.Map[string, domain.SourcePackage]) {
externalPackages.ForEach(func(newPackage string, newSource domain.SourcePackage) bool {
matchedPackage, ok := internalPackages.Get(newPackage)
if !ok || matchedPackage.Packages.Len() == 0 {
return true
}
ver := ""
status := matchedPackage.Status
matchedPackage.Packages.ForEach(func(currentKey string, currentPackage domain.PackageInfo) bool {
if currentPackage.Status == domain.Missing {
return true
}
newSource.Packages.ForEach(func(newKey string, newPackage domain.PackageInfo) bool {
if currentKey != newKey {
return true
}
newVersion := strings.Split(newPackage.Version, "+b")[0]
mVer, _ := version.Parse(currentPackage.Version)
extVer, _ := version.Parse(newVersion)
cmpVal := version.Compare(mVer, extVer)
if cmpVal < 0 {
currentPackage.Status = domain.Stale
status = domain.Stale
ver = getHighestVer(currentPackage.Version, extVer.String())
currentPackage.NewVersion = extVer.String()
matchedPackage.Packages.Set(currentKey, currentPackage)
}
return false
})
return true
})
ver := newSource.Version
ver = getHighestVer(ver, matchedPackage.Version)
if ver != matchedPackage.Version {
matchedPackage.NewVersion = ver
matchedPackage.Status = domain.Stale
}
matchedPackage.Status = status
internalPackages.Set(newPackage, matchedPackage)
return true
})
@ -551,3 +464,13 @@ func combinePackages(packages *haxmap.Map[string, domain.SourcePackage]) {
return true
})
}
func getHighestVer(ver string, newVer string) string {
mVer, _ := version.Parse(ver)
extVer, _ := version.Parse(newVer)
cmpVal := version.Compare(mVer, extVer)
if cmpVal < 0 {
return newVer
}
return ver
}