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.

migrate.go 1.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package repo_migrate
  2. import (
  3. "code.gitea.io/gitea/modules/log"
  4. "code.gitea.io/gitea/modules/setting"
  5. "code.gitea.io/gitea/routers/response"
  6. "crypto/tls"
  7. "errors"
  8. "fmt"
  9. "github.com/go-resty/resty/v2"
  10. "strings"
  11. )
  12. var (
  13. restyClient *resty.Client
  14. )
  15. func getRestyClient() *resty.Client {
  16. if restyClient == nil {
  17. restyClient = resty.New()
  18. restyClient.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
  19. }
  20. return restyClient
  21. }
  22. func CommitFile(fileUrl string) (interface{}, error) {
  23. client := getRestyClient()
  24. var result response.AiforgeResponse
  25. _, err := client.R().
  26. SetResult(&result).
  27. Post(fmt.Sprintf("%s/api/repo/migrate/commit?file=%s", setting.RepoMigrateHost, fileUrl))
  28. if err != nil {
  29. return "", errors.New("service not available")
  30. }
  31. if result.Code > 0 {
  32. log.Error("commitFile failed(%d): %s", result.Code, result.Msg)
  33. return "", errors.New(result.Msg)
  34. }
  35. return result.Data, nil
  36. }
  37. func QueryFileStatus(md5 string) (interface{}, error) {
  38. client := getRestyClient()
  39. var result response.AiforgeResponse
  40. _, err := client.R().
  41. SetResult(&result).
  42. Get(fmt.Sprintf("%s/api/repo/task/query?md5=%s", setting.RepoMigrateHost, md5))
  43. if err != nil {
  44. return "", errors.New("service not available")
  45. }
  46. if result.Code > 0 {
  47. log.Error("QueryFileStatus failed(%d): %s", result.Code, result.Msg)
  48. return "", errors.New(result.Msg)
  49. }
  50. return result.Data, nil
  51. }
  52. func Auth(userName string) bool {
  53. if isWhiteListEffect() && isInWhiteList(userName) {
  54. return true
  55. }
  56. if !isWhiteListEffect() {
  57. return true
  58. }
  59. return false
  60. }
  61. func isWhiteListEffect() bool {
  62. return setting.IsRepoMigrateWhiteListEffect
  63. }
  64. func isInWhiteList(userName string) bool {
  65. for _, name := range getWhiteList() {
  66. if strings.ToUpper(name) == strings.ToUpper(userName) {
  67. return true
  68. }
  69. }
  70. return false
  71. }
  72. func getWhiteList() []string {
  73. return setting.RepoMigrateWhiteList
  74. }