You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

url.go 1.0 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package repository
  2. import (
  3. "code.gitea.io/gitea/models"
  4. "code.gitea.io/gitea/modules/setting"
  5. "errors"
  6. "net/url"
  7. "strings"
  8. )
  9. func FindRepoByUrl(url string) (*models.Repository, error) {
  10. ownerName, repoName := parseOpenIUrl(url)
  11. if ownerName == "" || repoName == "" {
  12. return nil, errors.New("tech.incorrect_openi_format")
  13. }
  14. r, err := models.GetRepositoryByOwnerAndName(ownerName, repoName)
  15. if err != nil {
  16. if models.IsErrRepoNotExist(err) {
  17. return nil, errors.New("tech.openi_repo_not_exist")
  18. }
  19. return nil, err
  20. }
  21. return r, nil
  22. }
  23. //parseOpenIUrl parse openI repo url,return ownerName and repoName
  24. func parseOpenIUrl(u string) (string, string) {
  25. url, err := url.Parse(u)
  26. if err != nil {
  27. return "", ""
  28. }
  29. appUrl, err := url.Parse(setting.AppURL)
  30. if err != nil {
  31. return "", ""
  32. }
  33. if appUrl.Host != url.Host {
  34. return "", ""
  35. }
  36. array := strings.Split(url.Path, "/")
  37. if len(array) < 3 {
  38. return "", ""
  39. }
  40. ownerName := array[1]
  41. repoName := array[2]
  42. return ownerName, repoName
  43. }