A fork of Gitea (see branch `mj`) adding Majority Judgment Polls 𐄷 over Issues and Merge Requests. https://git.mieuxvoter.fr
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.7 KiB

  1. // Copyright 2017 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. package models
  5. import (
  6. "strings"
  7. )
  8. // RepoRedirect represents that a repo name should be redirected to another
  9. type RepoRedirect struct {
  10. ID int64 `xorm:"pk autoincr"`
  11. OwnerID int64 `xorm:"UNIQUE(s)"`
  12. LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"`
  13. RedirectRepoID int64 // repoID to redirect to
  14. }
  15. // LookupRepoRedirect look up if a repository has a redirect name
  16. func LookupRepoRedirect(ownerID int64, repoName string) (int64, error) {
  17. repoName = strings.ToLower(repoName)
  18. redirect := &RepoRedirect{OwnerID: ownerID, LowerName: repoName}
  19. if has, err := x.Get(redirect); err != nil {
  20. return 0, err
  21. } else if !has {
  22. return 0, ErrRepoRedirectNotExist{OwnerID: ownerID, RepoName: repoName}
  23. }
  24. return redirect.RedirectRepoID, nil
  25. }
  26. // newRepoRedirect create a new repo redirect
  27. func newRepoRedirect(e Engine, ownerID, repoID int64, oldRepoName, newRepoName string) error {
  28. oldRepoName = strings.ToLower(oldRepoName)
  29. newRepoName = strings.ToLower(newRepoName)
  30. if err := deleteRepoRedirect(e, ownerID, newRepoName); err != nil {
  31. return err
  32. }
  33. if _, err := e.Insert(&RepoRedirect{
  34. OwnerID: ownerID,
  35. LowerName: oldRepoName,
  36. RedirectRepoID: repoID,
  37. }); err != nil {
  38. return err
  39. }
  40. return nil
  41. }
  42. // deleteRepoRedirect delete any redirect from the specified repo name to
  43. // anything else
  44. func deleteRepoRedirect(e Engine, ownerID int64, repoName string) error {
  45. repoName = strings.ToLower(repoName)
  46. _, err := e.Delete(&RepoRedirect{OwnerID: ownerID, LowerName: repoName})
  47. return err
  48. }