 2457f5ff22
			
		
	
	
	2457f5ff22
	
	
	
		
			
			- Massive replacement of changing `code.gitea.io/gitea` to `forgejo.org`. - Resolves forgejo/discussions#258 Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7337 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Reviewed-by: Michael Kriese <michael.kriese@gmx.de> Reviewed-by: Beowulf <beowulf@beocode.eu> Reviewed-by: Panagiotis "Ivory" Vasilopoulos <git@n0toose.net> Co-authored-by: Gusted <postmaster@gusted.xyz> Co-committed-by: Gusted <postmaster@gusted.xyz>
		
			
				
	
	
		
			105 lines
		
	
	
	
		
			2.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			105 lines
		
	
	
	
		
			2.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Copyright 2023 The Gitea Authors. All rights reserved.
 | |
| // SPDX-License-Identifier: MIT
 | |
| 
 | |
| package actions
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 
 | |
| 	"forgejo.org/models/db"
 | |
| 	repo_model "forgejo.org/models/repo"
 | |
| 	"forgejo.org/modules/container"
 | |
| 
 | |
| 	"xorm.io/builder"
 | |
| )
 | |
| 
 | |
| type SpecList []*ActionScheduleSpec
 | |
| 
 | |
| func (specs SpecList) GetScheduleIDs() []int64 {
 | |
| 	return container.FilterSlice(specs, func(spec *ActionScheduleSpec) (int64, bool) {
 | |
| 		return spec.ScheduleID, true
 | |
| 	})
 | |
| }
 | |
| 
 | |
| func (specs SpecList) LoadSchedules(ctx context.Context) error {
 | |
| 	if len(specs) == 0 {
 | |
| 		return nil
 | |
| 	}
 | |
| 
 | |
| 	scheduleIDs := specs.GetScheduleIDs()
 | |
| 	schedules, err := GetSchedulesMapByIDs(ctx, scheduleIDs)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	for _, spec := range specs {
 | |
| 		spec.Schedule = schedules[spec.ScheduleID]
 | |
| 	}
 | |
| 
 | |
| 	repoIDs := specs.GetRepoIDs()
 | |
| 	repos, err := repo_model.GetRepositoriesMapByIDs(ctx, repoIDs)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	for _, spec := range specs {
 | |
| 		spec.Repo = repos[spec.RepoID]
 | |
| 	}
 | |
| 
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| func (specs SpecList) GetRepoIDs() []int64 {
 | |
| 	return container.FilterSlice(specs, func(spec *ActionScheduleSpec) (int64, bool) {
 | |
| 		return spec.RepoID, true
 | |
| 	})
 | |
| }
 | |
| 
 | |
| func (specs SpecList) LoadRepos(ctx context.Context) error {
 | |
| 	if len(specs) == 0 {
 | |
| 		return nil
 | |
| 	}
 | |
| 
 | |
| 	repoIDs := specs.GetRepoIDs()
 | |
| 	repos, err := repo_model.GetRepositoriesMapByIDs(ctx, repoIDs)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	for _, spec := range specs {
 | |
| 		spec.Repo = repos[spec.RepoID]
 | |
| 	}
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| type FindSpecOptions struct {
 | |
| 	db.ListOptions
 | |
| 	RepoID int64
 | |
| 	Next   int64
 | |
| }
 | |
| 
 | |
| func (opts FindSpecOptions) ToConds() builder.Cond {
 | |
| 	cond := builder.NewCond()
 | |
| 	if opts.RepoID > 0 {
 | |
| 		cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
 | |
| 	}
 | |
| 
 | |
| 	if opts.Next > 0 {
 | |
| 		cond = cond.And(builder.Lte{"next": opts.Next})
 | |
| 	}
 | |
| 
 | |
| 	return cond
 | |
| }
 | |
| 
 | |
| func (opts FindSpecOptions) ToOrders() string {
 | |
| 	return "`id` DESC"
 | |
| }
 | |
| 
 | |
| func FindSpecs(ctx context.Context, opts FindSpecOptions) (SpecList, int64, error) {
 | |
| 	specs, total, err := db.FindAndCount[ActionScheduleSpec](ctx, opts)
 | |
| 	if err != nil {
 | |
| 		return nil, 0, err
 | |
| 	}
 | |
| 
 | |
| 	if err := SpecList(specs).LoadSchedules(ctx); err != nil {
 | |
| 		return nil, 0, err
 | |
| 	}
 | |
| 	return specs, total, nil
 | |
| }
 |