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>
This commit is contained in:
parent
e819da0837
commit
0e61a74e5a
14 changed files with 787 additions and 44 deletions
|
@ -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)
|
||||
func UpdatePushMirror(ctx context.Context, m *PushMirror) error {
|
||||
_, err := db.GetEngine(ctx).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{})
|
||||
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")
|
||||
}
|
||||
|
||||
// DeletePushMirrorsByRepoID deletes all push-mirrors by repoID
|
||||
func DeletePushMirrorsByRepoID(repoID int64) error {
|
||||
_, err := db.GetEngine(db.DefaultContext).Delete(&PushMirror{RepoID: repoID})
|
||||
return err
|
||||
}
|
||||
|
||||
// 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").
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue