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 { func sourcePackageToDto(pkg domain.SourcePackage) domain.SourcePackageDTO {
dto := domain.SourcePackageDTO{ dto := domain.SourcePackageDTO{
Name: pkg.Name, 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), Packages: make([]domain.PackageInfo, 0),
} }
pkg.Packages.ForEach(func(k string, v domain.PackageInfo) bool { 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 { func sourcePackageDtoToDomain(dto domain.SourcePackageDTO) domain.SourcePackage {
pkg := domain.SourcePackage{ pkg := domain.SourcePackage{
Name: dto.Name, 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](), Packages: haxmap.New[string, domain.PackageInfo](),
} }
for _, v := range dto.Packages { for _, v := range dto.Packages {

View File

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

View File

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

View File

@ -53,37 +53,15 @@ func ProcessPackages() error {
mergedPackage := domain.SourcePackage{ mergedPackage := domain.SourcePackage{
Name: curr.Name, 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](), 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) updatedPackages.Set(k, mergedPackage)
return true return true
}) })
@ -101,20 +79,6 @@ func ProcessPackages() error {
return true 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 currentPackages = updatedPackages
LastUpdateTime = time.Now() LastUpdateTime = time.Now()
@ -149,18 +113,6 @@ func UpdateSourcePackage(pkg domain.SourcePackage) error {
return saveSingleToDb(pkg) 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 { func saveSingleToDb(pkg domain.SourcePackage) error {
err := helpers.DBInst.UpdatePackage(pkg) err := helpers.DBInst.UpdatePackage(pkg)
if err != nil { if err != nil {
@ -243,7 +195,7 @@ func LoadInternalPackages(internalPackages *haxmap.Map[string, domain.SourcePack
return true return true
} }
mVer, _ := version.Parse(pkg.Version) mVer, _ := version.Parse(pkg.Version)
extVer, _ := version.Parse(newPkg.Version) extVer, _ := version.Parse(strings.Split(newPkg.Version, "+b")[0])
cmpVal := version.Compare(extVer, mVer) cmpVal := version.Compare(extVer, mVer)
if cmpVal >= 0 { if cmpVal >= 0 {
pk.Version = getHighestVer(pkg.Version, newPkg.Version) pk.Version = getHighestVer(pkg.Version, newPkg.Version)
@ -277,27 +229,31 @@ func LoadExternalPackages(externalPackages *haxmap.Map[string, domain.SourcePack
if err != nil { if err != nil {
return err return err
} }
packages.ForEach(func(k string, v domain.PackageInfo) bool { packages.ForEach(func(newKey string, newPkg domain.PackageInfo) bool {
pk, ok := externalPackages.Get(v.Source) pk, ok := externalPackages.Get(newPkg.Source)
if !ok { if !ok {
newMap := haxmap.New[string, domain.PackageInfo]() newMap := haxmap.New[string, domain.PackageInfo]()
newMap.Set(k, v) newMap.Set(newKey, newPkg)
externalPackages.Set(v.Source, domain.SourcePackage{ externalPackages.Set(newPkg.Source, domain.SourcePackage{
Name: v.Source, Name: newPkg.Source,
Packages: newMap, Packages: newMap,
Status: domain.Current,
Version: newPkg.Version,
}) })
return true return true
} }
pkg, ok := pk.Packages.Get(k) pkg, ok := pk.Packages.Get(newKey)
if !ok { if !ok {
pk.Packages.Set(k, v) pk.Packages.Set(newKey, newPkg)
return true return true
} }
mVer, _ := version.Parse(pkg.Version) mVer, _ := version.Parse(pkg.Version)
extVer, _ := version.Parse(v.Version) extVer, _ := version.Parse(strings.Split(newPkg.Version, "+b")[0])
cmpVal := version.Compare(extVer, mVer) cmpVal := version.Compare(extVer, mVer)
if cmpVal >= 0 { 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
} }
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 { externalPackages.ForEach(func(k string, src domain.SourcePackage) bool {
_, ok := internalPackages.Get(k) _, ok := internalPackages.Get(k)
if !ok && src.Packages.Len() > 0 { if !ok && src.Packages.Len() > 0 {
newStatus := domain.Missing src.Status = 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
internalPackages.Set(k, src) internalPackages.Set(k, src)
} }
return true 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]) { func ProcessStalePackages(internalPackages *haxmap.Map[string, domain.SourcePackage], externalPackages *haxmap.Map[string, domain.SourcePackage]) {
externalPackages.ForEach(func(newPackage string, newSource domain.SourcePackage) bool { externalPackages.ForEach(func(newPackage string, newSource domain.SourcePackage) bool {
matchedPackage, ok := internalPackages.Get(newPackage) matchedPackage, ok := internalPackages.Get(newPackage)
if !ok || matchedPackage.Packages.Len() == 0 { if !ok || matchedPackage.Packages.Len() == 0 {
return true return true
} }
ver := "" ver := newSource.Version
status := matchedPackage.Status ver = getHighestVer(ver, matchedPackage.Version)
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
})
if ver != matchedPackage.Version { if ver != matchedPackage.Version {
matchedPackage.NewVersion = ver matchedPackage.NewVersion = ver
matchedPackage.Status = domain.Stale
} }
matchedPackage.Status = status
internalPackages.Set(newPackage, matchedPackage) internalPackages.Set(newPackage, matchedPackage)
return true return true
}) })
@ -551,3 +464,13 @@ func combinePackages(packages *haxmap.Map[string, domain.SourcePackage]) {
return true 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
}