summaryrefslogtreecommitdiffstats
path: root/models/repo/redirect.go
diff options
context:
space:
mode:
authorDaniel Baumann <daniel@debian.org>2024-10-18 20:33:49 +0200
committerDaniel Baumann <daniel@debian.org>2024-10-18 20:33:49 +0200
commitdd136858f1ea40ad3c94191d647487fa4f31926c (patch)
tree58fec94a7b2a12510c9664b21793f1ed560c6518 /models/repo/redirect.go
parentInitial commit. (diff)
downloadforgejo-upstream/9.0.0.tar.xz
forgejo-upstream/9.0.0.zip
Adding upstream version 9.0.0.HEADupstream/9.0.0upstreamdebian
Signed-off-by: Daniel Baumann <daniel@debian.org>
Diffstat (limited to 'models/repo/redirect.go')
-rw-r--r--models/repo/redirect.go86
1 files changed, 86 insertions, 0 deletions
diff --git a/models/repo/redirect.go b/models/repo/redirect.go
new file mode 100644
index 0000000..61789eb
--- /dev/null
+++ b/models/repo/redirect.go
@@ -0,0 +1,86 @@
+// Copyright 2017 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package repo
+
+import (
+ "context"
+ "fmt"
+ "strings"
+
+ "code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/modules/util"
+)
+
+// ErrRedirectNotExist represents a "RedirectNotExist" kind of error.
+type ErrRedirectNotExist struct {
+ OwnerID int64
+ RepoName string
+}
+
+// IsErrRedirectNotExist check if an error is an ErrRepoRedirectNotExist.
+func IsErrRedirectNotExist(err error) bool {
+ _, ok := err.(ErrRedirectNotExist)
+ return ok
+}
+
+func (err ErrRedirectNotExist) Error() string {
+ return fmt.Sprintf("repository redirect does not exist [uid: %d, name: %s]", err.OwnerID, err.RepoName)
+}
+
+func (err ErrRedirectNotExist) Unwrap() error {
+ return util.ErrNotExist
+}
+
+// Redirect represents that a repo name should be redirected to another
+type Redirect struct {
+ ID int64 `xorm:"pk autoincr"`
+ OwnerID int64 `xorm:"UNIQUE(s)"`
+ LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"`
+ RedirectRepoID int64 // repoID to redirect to
+}
+
+// TableName represents real table name in database
+func (Redirect) TableName() string {
+ return "repo_redirect"
+}
+
+func init() {
+ db.RegisterModel(new(Redirect))
+}
+
+// LookupRedirect look up if a repository has a redirect name
+func LookupRedirect(ctx context.Context, ownerID int64, repoName string) (int64, error) {
+ repoName = strings.ToLower(repoName)
+ redirect := &Redirect{OwnerID: ownerID, LowerName: repoName}
+ if has, err := db.GetEngine(ctx).Get(redirect); err != nil {
+ return 0, err
+ } else if !has {
+ return 0, ErrRedirectNotExist{OwnerID: ownerID, RepoName: repoName}
+ }
+ return redirect.RedirectRepoID, nil
+}
+
+// NewRedirect create a new repo redirect
+func NewRedirect(ctx context.Context, ownerID, repoID int64, oldRepoName, newRepoName string) error {
+ oldRepoName = strings.ToLower(oldRepoName)
+ newRepoName = strings.ToLower(newRepoName)
+
+ if err := DeleteRedirect(ctx, ownerID, newRepoName); err != nil {
+ return err
+ }
+
+ return db.Insert(ctx, &Redirect{
+ OwnerID: ownerID,
+ LowerName: oldRepoName,
+ RedirectRepoID: repoID,
+ })
+}
+
+// DeleteRedirect delete any redirect from the specified repo name to
+// anything else
+func DeleteRedirect(ctx context.Context, ownerID int64, repoName string) error {
+ repoName = strings.ToLower(repoName)
+ _, err := db.GetEngine(ctx).Delete(&Redirect{OwnerID: ownerID, LowerName: repoName})
+ return err
+}