summaryrefslogtreecommitdiffstats
path: root/models/repo
diff options
context:
space:
mode:
authorMohamed Sekour <mohamed.sekour@exfo.com>2022-07-30 18:45:59 +0200
committerGitHub <noreply@github.com>2022-07-30 18:45:59 +0200
commit0e61a74e5a3fc14ff26d1c85065ba336d3f3994b (patch)
tree63282d44a77c410f33595e000d5fef7a9e700a35 /models/repo
parente819da083734ddbf30afbc62afd48e9d0e8f3d48 (diff)
downloadgitea-0e61a74e5a3fc14ff26d1c85065ba336d3f3994b.tar.gz
gitea-0e61a74e5a3fc14ff26d1c85065ba336d3f3994b.zip
Add new API endpoints for push mirrors management (#19841)
- Add a new push mirror to specific repository - Sync now ( send all the changes to the configured push mirrors ) - Get list of all push mirrors of a repository - Get a push mirror by ID - Delete push mirror by ID Signed-off-by: Mohamed Sekour <mohamed.sekour@exfo.com> Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'models/repo')
-rw-r--r--models/repo/pushmirror.go73
-rw-r--r--models/repo/pushmirror_test.go9
2 files changed, 54 insertions, 28 deletions
diff --git a/models/repo/pushmirror.go b/models/repo/pushmirror.go
index 0a7dea79c9..02ffcbee76 100644
--- a/models/repo/pushmirror.go
+++ b/models/repo/pushmirror.go
@@ -5,12 +5,15 @@
package repo
import (
+ "context"
"errors"
"time"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/timeutil"
+
+ "xorm.io/builder"
)
// ErrPushMirrorNotExist mirror does not exist error
@@ -29,6 +32,25 @@ type PushMirror struct {
LastUpdateUnix timeutil.TimeStamp `xorm:"INDEX last_update"`
LastError string `xorm:"text"`
}
+type PushMirrorOptions struct {
+ ID int64
+ RepoID int64
+ RemoteName string
+}
+
+func (opts *PushMirrorOptions) toConds() builder.Cond {
+ cond := builder.NewCond()
+ if opts.RepoID > 0 {
+ cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
+ }
+ if opts.RemoteName != "" {
+ cond = cond.And(builder.Eq{"remote_name": opts.RemoteName})
+ }
+ if opts.ID > 0 {
+ cond = cond.And(builder.Eq{"id": opts.ID})
+ }
+ return cond
+}
func init() {
db.RegisterModel(new(PushMirror))
@@ -53,45 +75,48 @@ func (m *PushMirror) GetRemoteName() string {
}
// InsertPushMirror inserts a push-mirror to database
-func InsertPushMirror(m *PushMirror) error {
- _, err := db.GetEngine(db.DefaultContext).Insert(m)
+func InsertPushMirror(ctx context.Context, m *PushMirror) error {
+ _, err := db.GetEngine(ctx).Insert(m)
return err
}
// UpdatePushMirror updates the push-mirror
-func UpdatePushMirror(m *PushMirror) error {
- _, err := db.GetEngine(db.DefaultContext).ID(m.ID).AllCols().Update(m)
- return err
-}
-
-// DeletePushMirrorByID deletes a push-mirrors by ID
-func DeletePushMirrorByID(ID int64) error {
- _, err := db.GetEngine(db.DefaultContext).ID(ID).Delete(&PushMirror{})
+func UpdatePushMirror(ctx context.Context, m *PushMirror) error {
+ _, err := db.GetEngine(ctx).ID(m.ID).AllCols().Update(m)
return err
}
-// DeletePushMirrorsByRepoID deletes all push-mirrors by repoID
-func DeletePushMirrorsByRepoID(repoID int64) error {
- _, err := db.GetEngine(db.DefaultContext).Delete(&PushMirror{RepoID: repoID})
- return err
+func DeletePushMirrors(ctx context.Context, opts PushMirrorOptions) error {
+ if opts.RepoID > 0 {
+ _, err := db.GetEngine(ctx).Where(opts.toConds()).Delete(&PushMirror{})
+ return err
+ }
+ return errors.New("repoID required and must be set")
}
-// GetPushMirrorByID returns push-mirror information.
-func GetPushMirrorByID(ID int64) (*PushMirror, error) {
- m := &PushMirror{}
- has, err := db.GetEngine(db.DefaultContext).ID(ID).Get(m)
+func GetPushMirror(ctx context.Context, opts PushMirrorOptions) (*PushMirror, error) {
+ mirror := &PushMirror{}
+ exist, err := db.GetEngine(ctx).Where(opts.toConds()).Get(mirror)
if err != nil {
return nil, err
- } else if !has {
+ } else if !exist {
return nil, ErrPushMirrorNotExist
}
- return m, nil
+ return mirror, nil
}
// GetPushMirrorsByRepoID returns push-mirror information of a repository.
-func GetPushMirrorsByRepoID(repoID int64) ([]*PushMirror, error) {
+func GetPushMirrorsByRepoID(ctx context.Context, repoID int64, listOptions db.ListOptions) ([]*PushMirror, int64, error) {
+ sess := db.GetEngine(ctx).Where("repo_id = ?", repoID)
+ if listOptions.Page != 0 {
+ sess = db.SetSessionPagination(sess, &listOptions)
+ mirrors := make([]*PushMirror, 0, listOptions.PageSize)
+ count, err := sess.FindAndCount(&mirrors)
+ return mirrors, count, err
+ }
mirrors := make([]*PushMirror, 0, 10)
- return mirrors, db.GetEngine(db.DefaultContext).Where("repo_id=?", repoID).Find(&mirrors)
+ count, err := sess.FindAndCount(&mirrors)
+ return mirrors, count, err
}
// GetPushMirrorsSyncedOnCommit returns push-mirrors for this repo that should be updated by new commits
@@ -103,8 +128,8 @@ func GetPushMirrorsSyncedOnCommit(repoID int64) ([]*PushMirror, error) {
}
// PushMirrorsIterate iterates all push-mirror repositories.
-func PushMirrorsIterate(limit int, f func(idx int, bean interface{}) error) error {
- return db.GetEngine(db.DefaultContext).
+func PushMirrorsIterate(ctx context.Context, limit int, f func(idx int, bean interface{}) error) error {
+ return db.GetEngine(ctx).
Where("last_update + (`interval` / ?) <= ?", time.Second, time.Now().Unix()).
And("`interval` != 0").
OrderBy("last_update ASC").
diff --git a/models/repo/pushmirror_test.go b/models/repo/pushmirror_test.go
index d36a48547e..5087e30095 100644
--- a/models/repo/pushmirror_test.go
+++ b/models/repo/pushmirror_test.go
@@ -8,6 +8,7 @@ import (
"testing"
"time"
+ "code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/timeutil"
@@ -20,20 +21,20 @@ func TestPushMirrorsIterate(t *testing.T) {
now := timeutil.TimeStampNow()
- repo_model.InsertPushMirror(&repo_model.PushMirror{
+ repo_model.InsertPushMirror(db.DefaultContext, &repo_model.PushMirror{
RemoteName: "test-1",
LastUpdateUnix: now,
Interval: 1,
})
long, _ := time.ParseDuration("24h")
- repo_model.InsertPushMirror(&repo_model.PushMirror{
+ repo_model.InsertPushMirror(db.DefaultContext, &repo_model.PushMirror{
RemoteName: "test-2",
LastUpdateUnix: now,
Interval: long,
})
- repo_model.InsertPushMirror(&repo_model.PushMirror{
+ repo_model.InsertPushMirror(db.DefaultContext, &repo_model.PushMirror{
RemoteName: "test-3",
LastUpdateUnix: now,
Interval: 0,
@@ -41,7 +42,7 @@ func TestPushMirrorsIterate(t *testing.T) {
time.Sleep(1 * time.Millisecond)
- repo_model.PushMirrorsIterate(1, func(idx int, bean interface{}) error {
+ repo_model.PushMirrorsIterate(db.DefaultContext, 1, func(idx int, bean interface{}) error {
m, ok := bean.(*repo_model.PushMirror)
assert.True(t, ok)
assert.Equal(t, "test-1", m.RemoteName)