選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

redirect.go 2.4KB

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