2024-03-21 03:25:40 -04:00
|
|
|
// Copyright 2024 The forgejo Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
2024-03-22 12:51:20 -04:00
|
|
|
// ToDo: Is this package the right place for federated repo? May need to diskuss this.
|
2024-03-21 03:25:40 -04:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2024-03-22 12:41:59 -04:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2024-03-21 03:25:40 -04:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2024-03-22 12:41:59 -04:00
|
|
|
"code.gitea.io/gitea/modules/validation"
|
2024-03-21 03:25:40 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(FederatedRepo))
|
|
|
|
}
|
2024-03-22 12:41:59 -04:00
|
|
|
|
2024-03-25 11:30:02 -04:00
|
|
|
// ToDo: Validate before returning
|
2024-03-25 04:15:32 -04:00
|
|
|
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)
|
2024-03-22 12:41:59 -04:00
|
|
|
}
|
|
|
|
|
2024-03-25 11:35:35 -04:00
|
|
|
func StoreFederatedRepos(ctx context.Context, localRepoId int64, federatedRepoList []*FederatedRepo) error {
|
2024-03-22 15:14:20 -04:00
|
|
|
for _, federatedRepo := range federatedRepoList {
|
2024-03-23 12:01:25 -04:00
|
|
|
if res, err := validation.IsValid(*federatedRepo); !res {
|
2024-03-22 15:14:20 -04:00
|
|
|
return fmt.Errorf("FederationInfo is not valid: %v", err)
|
|
|
|
}
|
2024-03-22 12:41:59 -04:00
|
|
|
}
|
2024-03-22 15:14:20 -04:00
|
|
|
|
|
|
|
// Begin transaction
|
|
|
|
ctx, committer, err := db.TxContext((ctx))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer committer.Close()
|
|
|
|
|
|
|
|
_, err = db.GetEngine(ctx).Where("repo_id=?", localRepoId).Delete(FederatedRepo{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, federatedRepo := range federatedRepoList {
|
|
|
|
_, err = db.GetEngine(ctx).Insert(federatedRepo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit transaction
|
|
|
|
return committer.Commit()
|
2024-03-22 12:41:59 -04:00
|
|
|
}
|