From 36c0c1c3c1994ea9f05c3f5725b9c582e84ab37f Mon Sep 17 00:00:00 2001 From: erik Date: Fri, 22 Mar 2024 17:41:59 +0100 Subject: [PATCH] Add DB functionality for federated repos --- models/repo/repo_repository.go | 49 ++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/models/repo/repo_repository.go b/models/repo/repo_repository.go index 01dace0346..3e5f6ac466 100644 --- a/models/repo/repo_repository.go +++ b/models/repo/repo_repository.go @@ -4,9 +4,58 @@ package repo import ( + "context" + "fmt" + "strings" + "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/modules/validation" ) func init() { db.RegisterModel(new(FederatedRepo)) } + +func GetFederatedRepo(ctx context.Context, ID int64) (*FederatedRepo, error) { + repo := new(FederatedRepo) + has, err := db.GetEngine(ctx).Where("id=?", ID).Get(repo) + if err != nil { + return nil, err + } else if !has { + return nil, fmt.Errorf("FederationInfo record %v does not exist", ID) + } + if res, err := validation.IsValid(repo); !res { + return nil, fmt.Errorf("FederationInfo is not valid: %v", err) + } + return repo, nil +} + +func FindFederatedRepoByFQDN(ctx context.Context, fqdn string) (*FederatedRepo, error) { + repo := new(FederatedRepo) + has, err := db.GetEngine(ctx).Where("host_fqdn=?", strings.ToLower(fqdn)).Get(repo) + if err != nil { + return nil, err + } else if !has { + return nil, nil + } + if res, err := validation.IsValid(repo); !res { + return nil, fmt.Errorf("FederationInfo is not valid: %v", err) + } + return repo, nil +} + +func CreateFederatedRepo(ctx context.Context, repo *FederatedRepo) error { + if res, err := validation.IsValid(repo); !res { + return fmt.Errorf("FederationInfo is not valid: %v", err) + } + _, err := db.GetEngine(ctx).Insert(repo) + return err +} + +func UpdateFederatedRepo(ctx context.Context, repo *FederatedRepo) error { + if res, err := validation.IsValid(repo); !res { + return fmt.Errorf("FederationInfo is not valid: %v", err) + } + _, err := db.GetEngine(ctx).ID(repo.ID).Update(repo) + return err +}