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 957 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. if !strings.Contains(setting.AppURL, url.Host) {
  30. return "", ""
  31. }
  32. array := strings.Split(url.Path, "/")
  33. if len(array) < 3 {
  34. return "", ""
  35. }
  36. ownerName := array[1]
  37. repoName := array[2]
  38. return ownerName, repoName
  39. }