1// Copyright 2021 The Gitea Authors. All rights reserved.
2// Use of this source code is governed by a MIT-style
3// license that can be found in the LICENSE file.
4
5package repo
6
7import (
8	"errors"
9	"time"
10
11	"code.gitea.io/gitea/models/db"
12	"code.gitea.io/gitea/modules/log"
13	"code.gitea.io/gitea/modules/timeutil"
14
15	"xorm.io/xorm"
16)
17
18var (
19	// ErrPushMirrorNotExist mirror does not exist error
20	ErrPushMirrorNotExist = errors.New("PushMirror does not exist")
21)
22
23// PushMirror represents mirror information of a repository.
24type PushMirror struct {
25	ID         int64       `xorm:"pk autoincr"`
26	RepoID     int64       `xorm:"INDEX"`
27	Repo       *Repository `xorm:"-"`
28	RemoteName string
29
30	Interval       time.Duration
31	CreatedUnix    timeutil.TimeStamp `xorm:"created"`
32	LastUpdateUnix timeutil.TimeStamp `xorm:"INDEX last_update"`
33	LastError      string             `xorm:"text"`
34}
35
36func init() {
37	db.RegisterModel(new(PushMirror))
38}
39
40// AfterLoad is invoked from XORM after setting the values of all fields of this object.
41func (m *PushMirror) AfterLoad(session *xorm.Session) {
42	if m == nil {
43		return
44	}
45
46	var err error
47	m.Repo, err = getRepositoryByID(session, m.RepoID)
48	if err != nil {
49		log.Error("getRepositoryByID[%d]: %v", m.ID, err)
50	}
51}
52
53// GetRepository returns the path of the repository.
54func (m *PushMirror) GetRepository() *Repository {
55	return m.Repo
56}
57
58// GetRemoteName returns the name of the remote.
59func (m *PushMirror) GetRemoteName() string {
60	return m.RemoteName
61}
62
63// InsertPushMirror inserts a push-mirror to database
64func InsertPushMirror(m *PushMirror) error {
65	_, err := db.GetEngine(db.DefaultContext).Insert(m)
66	return err
67}
68
69// UpdatePushMirror updates the push-mirror
70func UpdatePushMirror(m *PushMirror) error {
71	_, err := db.GetEngine(db.DefaultContext).ID(m.ID).AllCols().Update(m)
72	return err
73}
74
75// DeletePushMirrorByID deletes a push-mirrors by ID
76func DeletePushMirrorByID(ID int64) error {
77	_, err := db.GetEngine(db.DefaultContext).ID(ID).Delete(&PushMirror{})
78	return err
79}
80
81// DeletePushMirrorsByRepoID deletes all push-mirrors by repoID
82func DeletePushMirrorsByRepoID(repoID int64) error {
83	_, err := db.GetEngine(db.DefaultContext).Delete(&PushMirror{RepoID: repoID})
84	return err
85}
86
87// GetPushMirrorByID returns push-mirror information.
88func GetPushMirrorByID(ID int64) (*PushMirror, error) {
89	m := &PushMirror{}
90	has, err := db.GetEngine(db.DefaultContext).ID(ID).Get(m)
91	if err != nil {
92		return nil, err
93	} else if !has {
94		return nil, ErrPushMirrorNotExist
95	}
96	return m, nil
97}
98
99// GetPushMirrorsByRepoID returns push-mirror information of a repository.
100func GetPushMirrorsByRepoID(repoID int64) ([]*PushMirror, error) {
101	mirrors := make([]*PushMirror, 0, 10)
102	return mirrors, db.GetEngine(db.DefaultContext).Where("repo_id=?", repoID).Find(&mirrors)
103}
104
105// PushMirrorsIterate iterates all push-mirror repositories.
106func PushMirrorsIterate(f func(idx int, bean interface{}) error) error {
107	return db.GetEngine(db.DefaultContext).
108		Where("last_update + (`interval` / ?) <= ?", time.Second, time.Now().Unix()).
109		And("`interval` != 0").
110		OrderBy("last_update ASC").
111		Iterate(new(PushMirror), f)
112}
113