aboutsummaryrefslogtreecommitdiffstats
path: root/models/repo
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2021-12-06 15:19:28 +0800
committerGitHub <noreply@github.com>2021-12-06 15:19:28 +0800
commitdcdb4873c8d77a444526fad5b1c8e705fdfe149d (patch)
tree599efda4334775ef40db9dd845355dfde7d73b33 /models/repo
parentf49d160447899270fbca6370cb7ab2742dce85dc (diff)
downloadgitea-dcdb4873c8d77a444526fad5b1c8e705fdfe149d.tar.gz
gitea-dcdb4873c8d77a444526fad5b1c8e705fdfe149d.zip
Move repo archiver to models/repo (#17913)
* Move repo archiver to models/repo * Move archiver service into services/repository/ * Fix imports * Fix test * Fix test
Diffstat (limited to 'models/repo')
-rw-r--r--models/repo/archiver.go109
-rw-r--r--models/repo/main_test.go1
2 files changed, 110 insertions, 0 deletions
diff --git a/models/repo/archiver.go b/models/repo/archiver.go
new file mode 100644
index 0000000000..cee6013ca3
--- /dev/null
+++ b/models/repo/archiver.go
@@ -0,0 +1,109 @@
+// Copyright 2021 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package repo
+
+import (
+ "context"
+ "fmt"
+ "time"
+
+ "code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/modules/git"
+ "code.gitea.io/gitea/modules/timeutil"
+
+ "xorm.io/builder"
+)
+
+// ArchiverStatus represents repo archive status
+type ArchiverStatus int
+
+// enumerate all repo archive statuses
+const (
+ ArchiverGenerating = iota // the archiver is generating
+ ArchiverReady // it's ready
+)
+
+// RepoArchiver represents all archivers
+type RepoArchiver struct { //revive:disable-line:exported
+ ID int64 `xorm:"pk autoincr"`
+ RepoID int64 `xorm:"index unique(s)"`
+ Type git.ArchiveType `xorm:"unique(s)"`
+ Status ArchiverStatus
+ CommitID string `xorm:"VARCHAR(40) unique(s)"`
+ CreatedUnix timeutil.TimeStamp `xorm:"INDEX NOT NULL created"`
+}
+
+func init() {
+ db.RegisterModel(new(RepoArchiver))
+}
+
+// RelativePath returns relative path
+func (archiver *RepoArchiver) RelativePath() (string, error) {
+ return fmt.Sprintf("%d/%s/%s.%s", archiver.RepoID, archiver.CommitID[:2], archiver.CommitID, archiver.Type.String()), nil
+}
+
+var delRepoArchiver = new(RepoArchiver)
+
+// DeleteRepoArchiver delete archiver
+func DeleteRepoArchiver(ctx context.Context, archiver *RepoArchiver) error {
+ _, err := db.GetEngine(db.DefaultContext).ID(archiver.ID).Delete(delRepoArchiver)
+ return err
+}
+
+// GetRepoArchiver get an archiver
+func GetRepoArchiver(ctx context.Context, repoID int64, tp git.ArchiveType, commitID string) (*RepoArchiver, error) {
+ var archiver RepoArchiver
+ has, err := db.GetEngine(ctx).Where("repo_id=?", repoID).And("`type`=?", tp).And("commit_id=?", commitID).Get(&archiver)
+ if err != nil {
+ return nil, err
+ }
+ if has {
+ return &archiver, nil
+ }
+ return nil, nil
+}
+
+// AddRepoArchiver adds an archiver
+func AddRepoArchiver(ctx context.Context, archiver *RepoArchiver) error {
+ _, err := db.GetEngine(ctx).Insert(archiver)
+ return err
+}
+
+// UpdateRepoArchiverStatus updates archiver's status
+func UpdateRepoArchiverStatus(ctx context.Context, archiver *RepoArchiver) error {
+ _, err := db.GetEngine(ctx).ID(archiver.ID).Cols("status").Update(archiver)
+ return err
+}
+
+// DeleteAllRepoArchives deletes all repo archives records
+func DeleteAllRepoArchives() error {
+ _, err := db.GetEngine(db.DefaultContext).Where("1=1").Delete(new(RepoArchiver))
+ return err
+}
+
+// FindRepoArchiversOption represents an archiver options
+type FindRepoArchiversOption struct {
+ db.ListOptions
+ OlderThan time.Duration
+}
+
+func (opts FindRepoArchiversOption) toConds() builder.Cond {
+ var cond = builder.NewCond()
+ if opts.OlderThan > 0 {
+ cond = cond.And(builder.Lt{"created_unix": time.Now().Add(-opts.OlderThan).Unix()})
+ }
+ return cond
+}
+
+// FindRepoArchives find repo archivers
+func FindRepoArchives(opts FindRepoArchiversOption) ([]*RepoArchiver, error) {
+ var archivers = make([]*RepoArchiver, 0, opts.PageSize)
+ start, limit := opts.GetSkipTake()
+ err := db.GetEngine(db.DefaultContext).Where(opts.toConds()).
+ Asc("created_unix").
+ Limit(limit, start).
+ Find(&archivers)
+ return archivers, err
+}
diff --git a/models/repo/main_test.go b/models/repo/main_test.go
index ac62df9d9c..aa960bf132 100644
--- a/models/repo/main_test.go
+++ b/models/repo/main_test.go
@@ -14,5 +14,6 @@ import (
func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", ".."),
"attachment.yml",
+ "repo_archiver.yml",
)
}