Repository: go-gitea/gitea
File: services/packages/cleanup/cleanup.go (Go)

Code before:
			if skip, err := container_service.ShouldBeSkipped(ctx, pcr, p, pv); err != nil {
				return false, fmt.Errorf("CleanupRule [%d]: container.ShouldBeSkipped failed: %w", pcr.ID, err)
			} else if skip {
				log.Debug("Rule[%d]: keep '%s/%s' (container)", pcr.ID, p.Name, pv.Version)
				continue
			}
		}
		toMatch := pv.LowerVersion
		if pcr.MatchFullName {
			toMatch = p.LowerName + "/" + pv.LowerVersion
		}
		if pcr.KeepPatternMatcher != nil && pcr.KeepPatternMatcher.MatchString(toMatch) {
			log.Debug("Rule[%d]: keep '%s/%s' (keep pattern)", pcr.ID, p.Name, pv.Version)
			continue
		}
		if pv.CreatedUnix.AsLocalTime().After(olderThan) {
			log.Debug("Rule[%d]: keep '%s/%s' (remove days) %v", pcr.ID, p.Name, pv.Version, pv.CreatedUnix.FormatDate())
			continue
		}
		if pcr.RemovePatternMatcher != nil && !pcr.RemovePatternMatcher.MatchString(toMatch) {
			log.Debug("Rule[%d]: keep '%s/%s' (remove pattern)", pcr.ID, p.Name, pv.Version)
			continue
		}
		log.Debug("Rule[%d]: remove '%s/%s'", pcr.ID, p.Name, pv.Version)
		if err := packages_service.DeletePackageVersionAndReferences(ctx, pv); err != nil {
			log.Error("CleanupRule [%d]: DeletePackageVersionAndReferences failed: %w", pcr.ID, err)
			continue
		}
		versionDeleted = true
	}
	return versionDeleted, nil
}

func executeCleanupOneRule(ctx context.Context, pcr *packages_model.PackageCleanupRule) error {
	if err := pcr.CompiledPattern(); err != nil {
		return fmt.Errorf("CleanupRule [%d]: CompilePattern failed: %w", pcr.ID, err)
	}

	packages, err := packages_model.GetPackagesByType(ctx, pcr.OwnerID, pcr.Type)
	if err != nil {
		return fmt.Errorf("CleanupRule [%d]: GetPackagesByType failed: %w", pcr.ID, err)
	}

	anyVersionDeleted := false
	for _, p := range packages {
		versionDeleted := false
		err = db.WithTx(ctx, func(ctx context.Context) (err error) {
			versionDeleted, err = executeCleanupOneRulePackage(ctx, pcr, p)
			return err
		})
		if err != nil {

Review comment (bug):
```suggestion
			log.Error("CleanupRule [%d]: DeletePackageVersionAndReferences failed: %v", pcr.ID, err)
```

Code after:
			if skip, err := container_service.ShouldBeSkipped(ctx, pcr, p, pv); err != nil {
				return false, fmt.Errorf("CleanupRule [%d]: container.ShouldBeSkipped failed: %w", pcr.ID, err)
			} else if skip {
				log.Debug("Rule[%d]: keep '%s/%s' (container)", pcr.ID, p.Name, pv.Version)
				continue
			}
		}
		toMatch := pv.LowerVersion
		if pcr.MatchFullName {
			toMatch = p.LowerName + "/" + pv.LowerVersion
		}
		if pcr.KeepPatternMatcher != nil && pcr.KeepPatternMatcher.MatchString(toMatch) {
			log.Debug("Rule[%d]: keep '%s/%s' (keep pattern)", pcr.ID, p.Name, pv.Version)
			continue
		}
		if pv.CreatedUnix.AsLocalTime().After(olderThan) {
			log.Debug("Rule[%d]: keep '%s/%s' (remove days) %v", pcr.ID, p.Name, pv.Version, pv.CreatedUnix.FormatDate())
			continue
		}
		if pcr.RemovePatternMatcher != nil && !pcr.RemovePatternMatcher.MatchString(toMatch) {
			log.Debug("Rule[%d]: keep '%s/%s' (remove pattern)", pcr.ID, p.Name, pv.Version)
			continue
		}
		log.Debug("Rule[%d]: remove '%s/%s'", pcr.ID, p.Name, pv.Version)
		if err := packages_service.DeletePackageVersionAndReferences(ctx, pv); err != nil {
			log.Error("CleanupRule [%d]: DeletePackageVersionAndReferences failed: %v", pcr.ID, err)
			continue
		}
		versionDeleted = true
	}
	return versionDeleted, nil
}

func executeCleanupOneRule(ctx context.Context, pcr *packages_model.PackageCleanupRule) error {
	if err := pcr.CompiledPattern(); err != nil {
		return fmt.Errorf("CleanupRule [%d]: CompilePattern failed: %w", pcr.ID, err)
	}

	packages, err := packages_model.GetPackagesByType(ctx, pcr.OwnerID, pcr.Type)
	if err != nil {
		return fmt.Errorf("CleanupRule [%d]: GetPackagesByType failed: %w", pcr.ID, err)
	}

	anyVersionDeleted := false
	for _, p := range packages {
		versionDeleted := false
		err = db.WithTx(ctx, func(ctx context.Context) (err error) {
			versionDeleted, err = executeCleanupOneRulePackage(ctx, pcr, p)
			return err
		})
		if err != nil {