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.

83 lines
2.1 KiB

  1. // Copyright 2019 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 migrations
  5. import (
  6. "net/url"
  7. "strings"
  8. "time"
  9. "xorm.io/xorm"
  10. )
  11. func updateMigrationServiceTypes(x *xorm.Engine) error {
  12. type Repository struct {
  13. ID int64
  14. OriginalServiceType int `xorm:"index default(0)"`
  15. OriginalURL string `xorm:"VARCHAR(2048)"`
  16. }
  17. if err := x.Sync2(new(Repository)); err != nil {
  18. return err
  19. }
  20. var last int
  21. const batchSize = 50
  22. for {
  23. var results = make([]Repository, 0, batchSize)
  24. err := x.Where("original_url <> '' AND original_url IS NOT NULL").
  25. And("original_service_type = 0 OR original_service_type IS NULL").
  26. OrderBy("id").
  27. Limit(batchSize, last).
  28. Find(&results)
  29. if err != nil {
  30. return err
  31. }
  32. if len(results) == 0 {
  33. break
  34. }
  35. last += len(results)
  36. const PlainGitService = 1 // 1 plain git service
  37. const GithubService = 2 // 2 github.com
  38. for _, res := range results {
  39. u, err := url.Parse(res.OriginalURL)
  40. if err != nil {
  41. return err
  42. }
  43. var serviceType = PlainGitService
  44. if strings.EqualFold(u.Host, "github.com") {
  45. serviceType = GithubService
  46. }
  47. _, err = x.Exec("UPDATE repository SET original_service_type = ? WHERE id = ?", serviceType, res.ID)
  48. if err != nil {
  49. return err
  50. }
  51. }
  52. }
  53. type ExternalLoginUser struct {
  54. ExternalID string `xorm:"pk NOT NULL"`
  55. UserID int64 `xorm:"INDEX NOT NULL"`
  56. LoginSourceID int64 `xorm:"pk NOT NULL"`
  57. RawData map[string]interface{} `xorm:"TEXT JSON"`
  58. Provider string `xorm:"index VARCHAR(25)"`
  59. Email string
  60. Name string
  61. FirstName string
  62. LastName string
  63. NickName string
  64. Description string
  65. AvatarURL string
  66. Location string
  67. AccessToken string
  68. AccessTokenSecret string
  69. RefreshToken string
  70. ExpiresAt time.Time
  71. }
  72. return x.Sync2(new(ExternalLoginUser))
  73. }