From 828e7a76f6b3a16552cf1d3ba7ad0fcbd1c493a9 Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Mon, 25 Mar 2024 09:15:32 +0100 Subject: [PATCH] use new table to populate settings --- models/repo/repo_repository.go | 44 ++++------------------------ modules/context/repo.go | 9 ++++++ templates/repo/settings/options.tmpl | 2 +- 3 files changed, 16 insertions(+), 39 deletions(-) diff --git a/models/repo/repo_repository.go b/models/repo/repo_repository.go index a814279f8c..756b223c14 100644 --- a/models/repo/repo_repository.go +++ b/models/repo/repo_repository.go @@ -6,7 +6,6 @@ package repo import ( "context" "fmt" - "strings" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/validation" @@ -16,43 +15,12 @@ func init() { db.RegisterModel(new(FederatedRepo)) } -// TODO: do we need this? -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 -} - -// TODO: do we need this? -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 -} - -// TODO: do we need this? -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 FindFederatedRepoByRepoID(ctx context.Context, repoId int64) ([]*FederatedRepo, error) { + maxFederatedRepos := 10 + sess := db.GetEngine(ctx).Where("repo_id=?", repoId) + sess = sess.Limit(maxFederatedRepos, 0) + federatedRepoList := make([]*FederatedRepo, 0, maxFederatedRepos) + return federatedRepoList, sess.Find(&federatedRepoList) } func UpdateFederatedRepo(ctx context.Context, localRepoId int64, federatedRepoList []*FederatedRepo) error { diff --git a/modules/context/repo.go b/modules/context/repo.go index b48f6ded26..35667b5a19 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -380,6 +380,15 @@ func repoAssignment(ctx *Context, repo *repo_model.Repository) { ctx.Data["HasAccess"] = true ctx.Data["Permission"] = &ctx.Repo.Permission + federatedRepo, err := repo_model.FindFederatedRepoByRepoID(ctx, repo.ID) + if err == nil { + // TODO: concat string to semicolon sperated list here + ctx.Data["FederatedRepos"] = federatedRepo + } else if err != repo_model.ErrMirrorNotExist { + ctx.ServerError("FindFederatedRepoByRepoID", err) + return + } + if repo.IsMirror { pullMirror, err := repo_model.GetMirrorByRepoID(ctx, repo.ID) if err == nil { diff --git a/templates/repo/settings/options.tmpl b/templates/repo/settings/options.tmpl index 5d8df5c247..b6a4f6a88f 100644 --- a/templates/repo/settings/options.tmpl +++ b/templates/repo/settings/options.tmpl @@ -73,7 +73,7 @@
- +