code.gitea.io/gitea@v1.22.3/models/repo/redirect.go (about)

     1  // Copyright 2017 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package repo
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"strings"
    10  
    11  	"code.gitea.io/gitea/models/db"
    12  	"code.gitea.io/gitea/modules/util"
    13  )
    14  
    15  // ErrRedirectNotExist represents a "RedirectNotExist" kind of error.
    16  type ErrRedirectNotExist struct {
    17  	OwnerID  int64
    18  	RepoName string
    19  }
    20  
    21  // IsErrRedirectNotExist check if an error is an ErrRepoRedirectNotExist.
    22  func IsErrRedirectNotExist(err error) bool {
    23  	_, ok := err.(ErrRedirectNotExist)
    24  	return ok
    25  }
    26  
    27  func (err ErrRedirectNotExist) Error() string {
    28  	return fmt.Sprintf("repository redirect does not exist [uid: %d, name: %s]", err.OwnerID, err.RepoName)
    29  }
    30  
    31  func (err ErrRedirectNotExist) Unwrap() error {
    32  	return util.ErrNotExist
    33  }
    34  
    35  // Redirect represents that a repo name should be redirected to another
    36  type Redirect struct {
    37  	ID             int64  `xorm:"pk autoincr"`
    38  	OwnerID        int64  `xorm:"UNIQUE(s)"`
    39  	LowerName      string `xorm:"UNIQUE(s) INDEX NOT NULL"`
    40  	RedirectRepoID int64  // repoID to redirect to
    41  }
    42  
    43  // TableName represents real table name in database
    44  func (Redirect) TableName() string {
    45  	return "repo_redirect"
    46  }
    47  
    48  func init() {
    49  	db.RegisterModel(new(Redirect))
    50  }
    51  
    52  // LookupRedirect look up if a repository has a redirect name
    53  func LookupRedirect(ctx context.Context, ownerID int64, repoName string) (int64, error) {
    54  	repoName = strings.ToLower(repoName)
    55  	redirect := &Redirect{OwnerID: ownerID, LowerName: repoName}
    56  	if has, err := db.GetEngine(ctx).Get(redirect); err != nil {
    57  		return 0, err
    58  	} else if !has {
    59  		return 0, ErrRedirectNotExist{OwnerID: ownerID, RepoName: repoName}
    60  	}
    61  	return redirect.RedirectRepoID, nil
    62  }
    63  
    64  // NewRedirect create a new repo redirect
    65  func NewRedirect(ctx context.Context, ownerID, repoID int64, oldRepoName, newRepoName string) error {
    66  	oldRepoName = strings.ToLower(oldRepoName)
    67  	newRepoName = strings.ToLower(newRepoName)
    68  
    69  	if err := DeleteRedirect(ctx, ownerID, newRepoName); err != nil {
    70  		return err
    71  	}
    72  
    73  	return db.Insert(ctx, &Redirect{
    74  		OwnerID:        ownerID,
    75  		LowerName:      oldRepoName,
    76  		RedirectRepoID: repoID,
    77  	})
    78  }
    79  
    80  // DeleteRedirect delete any redirect from the specified repo name to
    81  // anything else
    82  func DeleteRedirect(ctx context.Context, ownerID int64, repoName string) error {
    83  	repoName = strings.ToLower(repoName)
    84  	_, err := db.GetEngine(ctx).Delete(&Redirect{OwnerID: ownerID, LowerName: repoName})
    85  	return err
    86  }