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

Code before:
	"code.gitea.io/gitea/models/db"
	packages_model "code.gitea.io/gitea/models/packages"
	user_model "code.gitea.io/gitea/models/user"
	"code.gitea.io/gitea/modules/log"
	"code.gitea.io/gitea/modules/optional"
	packages_module "code.gitea.io/gitea/modules/packages"
	packages_service "code.gitea.io/gitea/services/packages"
	alpine_service "code.gitea.io/gitea/services/packages/alpine"
	arch_service "code.gitea.io/gitea/services/packages/arch"
	cargo_service "code.gitea.io/gitea/services/packages/cargo"
	container_service "code.gitea.io/gitea/services/packages/container"
	debian_service "code.gitea.io/gitea/services/packages/debian"
	rpm_service "code.gitea.io/gitea/services/packages/rpm"
)

// CleanupTask executes cleanup rules and cleanup expired package data
func CleanupTask(ctx context.Context, olderThan time.Duration) error {
	if err := ExecuteCleanupRules(ctx); err != nil {
		return err
	}

	return CleanupExpiredData(ctx, olderThan)
}

func ExecuteCleanupRules(outerCtx context.Context) error {
	return packages_model.IterateEnabledCleanupRules(outerCtx, func(ctx context.Context, pcr *packages_model.PackageCleanupRule) error {
		select {
		case <-outerCtx.Done():
			return db.ErrCancelledf("While processing package cleanup rules")
		default:
		}

		if err := pcr.CompiledPattern(); err != nil {
			return fmt.Errorf("CleanupRule [%d]: CompilePattern failed: %w", pcr.ID, err)
		}

		olderThan := time.Now().AddDate(0, 0, -pcr.RemoveDays)

		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 {
			pvs, _, err := packages_model.SearchVersions(ctx, &packages_model.PackageSearchOptions{
				PackageID:  p.ID,
				IsInternal: optional.Some(false),
				Sort:       packages_model.SortCreatedDesc,
			})
			if err != nil {

Review comment (bug):
Why the `db.TxContext` is removed? `outerCtx` seems to be a bug

Code after:
	"code.gitea.io/gitea/models/db"
	packages_model "code.gitea.io/gitea/models/packages"
	user_model "code.gitea.io/gitea/models/user"
	"code.gitea.io/gitea/modules/log"
	"code.gitea.io/gitea/modules/optional"
	packages_module "code.gitea.io/gitea/modules/packages"
	packages_service "code.gitea.io/gitea/services/packages"
	alpine_service "code.gitea.io/gitea/services/packages/alpine"
	arch_service "code.gitea.io/gitea/services/packages/arch"
	cargo_service "code.gitea.io/gitea/services/packages/cargo"
	container_service "code.gitea.io/gitea/services/packages/container"
	debian_service "code.gitea.io/gitea/services/packages/debian"
	rpm_service "code.gitea.io/gitea/services/packages/rpm"
)

// CleanupTask executes cleanup rules and cleanup expired package data
func CleanupTask(ctx context.Context, olderThan time.Duration) error {
	if err := ExecuteCleanupRules(ctx); err != nil {
		return err
	}

	return CleanupExpiredData(ctx, olderThan)
}

func executeCleanupOneRulePackage(ctx context.Context, pcr *packages_model.PackageCleanupRule, p *packages_model.Package) (versionDeleted bool, err error) {
	olderThan := time.Now().AddDate(0, 0, -pcr.RemoveDays)
	pvs, _, err := packages_model.SearchVersions(ctx, &packages_model.PackageSearchOptions{
		PackageID:  p.ID,
		IsInternal: optional.Some(false),
		Sort:       packages_model.SortCreatedDesc,
	})
	if err != nil {
		return false, fmt.Errorf("CleanupRule [%d]: SearchVersions failed: %w", pcr.ID, err)
	}
	if pcr.KeepCount > 0 {
		if pcr.KeepCount < len(pvs) {
			pvs = pvs[pcr.KeepCount:]
		} else {
			pvs = nil
		}
	}
	for _, pv := range pvs {
		if pcr.Type == packages_model.TypeContainer {
			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