gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package doctor
  4. import (
  5. "bytes"
  6. "context"
  7. "errors"
  8. "fmt"
  9. "code.gitea.io/gitea/models/db"
  10. repo_model "code.gitea.io/gitea/models/repo"
  11. "code.gitea.io/gitea/models/unit"
  12. "code.gitea.io/gitea/modules/json"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/timeutil"
  15. "xorm.io/builder"
  16. )
  17. // #16831 revealed that the dump command that was broken in 1.14.3-1.14.6 and 1.15.0 (#15885).
  18. // This led to repo_unit and login_source cfg not being converted to JSON in the dump
  19. // Unfortunately although it was hoped that there were only a few users affected it
  20. // appears that many users are affected.
  21. // We therefore need to provide a doctor command to fix this repeated issue #16961
  22. func parseBool16961(bs []byte) (bool, error) {
  23. if bytes.EqualFold(bs, []byte("%!s(bool=false)")) {
  24. return false, nil
  25. }
  26. if bytes.EqualFold(bs, []byte("%!s(bool=true)")) {
  27. return true, nil
  28. }
  29. return false, fmt.Errorf("unexpected bool format: %s", string(bs))
  30. }
  31. func fixUnitConfig16961(bs []byte, cfg *repo_model.UnitConfig) (fixed bool, err error) {
  32. err = json.UnmarshalHandleDoubleEncode(bs, &cfg)
  33. if err == nil {
  34. return false, nil
  35. }
  36. // Handle #16961
  37. if string(bs) != "&{}" && len(bs) != 0 {
  38. return false, err
  39. }
  40. return true, nil
  41. }
  42. func fixExternalWikiConfig16961(bs []byte, cfg *repo_model.ExternalWikiConfig) (fixed bool, err error) {
  43. err = json.UnmarshalHandleDoubleEncode(bs, &cfg)
  44. if err == nil {
  45. return false, nil
  46. }
  47. if len(bs) < 3 {
  48. return false, err
  49. }
  50. if bs[0] != '&' || bs[1] != '{' || bs[len(bs)-1] != '}' {
  51. return false, err
  52. }
  53. cfg.ExternalWikiURL = string(bs[2 : len(bs)-1])
  54. return true, nil
  55. }
  56. func fixExternalTrackerConfig16961(bs []byte, cfg *repo_model.ExternalTrackerConfig) (fixed bool, err error) {
  57. err = json.UnmarshalHandleDoubleEncode(bs, &cfg)
  58. if err == nil {
  59. return false, nil
  60. }
  61. // Handle #16961
  62. if len(bs) < 3 {
  63. return false, err
  64. }
  65. if bs[0] != '&' || bs[1] != '{' || bs[len(bs)-1] != '}' {
  66. return false, err
  67. }
  68. parts := bytes.Split(bs[2:len(bs)-1], []byte{' '})
  69. if len(parts) != 3 {
  70. return false, err
  71. }
  72. cfg.ExternalTrackerURL = string(bytes.Join(parts[:len(parts)-2], []byte{' '}))
  73. cfg.ExternalTrackerFormat = string(parts[len(parts)-2])
  74. cfg.ExternalTrackerStyle = string(parts[len(parts)-1])
  75. return true, nil
  76. }
  77. func fixPullRequestsConfig16961(bs []byte, cfg *repo_model.PullRequestsConfig) (fixed bool, err error) {
  78. err = json.UnmarshalHandleDoubleEncode(bs, &cfg)
  79. if err == nil {
  80. return false, nil
  81. }
  82. // Handle #16961
  83. if len(bs) < 3 {
  84. return false, err
  85. }
  86. if bs[0] != '&' || bs[1] != '{' || bs[len(bs)-1] != '}' {
  87. return false, err
  88. }
  89. // PullRequestsConfig was the following in 1.14
  90. // type PullRequestsConfig struct {
  91. // IgnoreWhitespaceConflicts bool
  92. // AllowMerge bool
  93. // AllowRebase bool
  94. // AllowRebaseMerge bool
  95. // AllowSquash bool
  96. // AllowManualMerge bool
  97. // AutodetectManualMerge bool
  98. // }
  99. //
  100. // 1.15 added in addition:
  101. // DefaultDeleteBranchAfterMerge bool
  102. // DefaultMergeStyle MergeStyle
  103. parts := bytes.Split(bs[2:len(bs)-1], []byte{' '})
  104. if len(parts) < 7 {
  105. return false, err
  106. }
  107. var parseErr error
  108. cfg.IgnoreWhitespaceConflicts, parseErr = parseBool16961(parts[0])
  109. if parseErr != nil {
  110. return false, errors.Join(err, parseErr)
  111. }
  112. cfg.AllowMerge, parseErr = parseBool16961(parts[1])
  113. if parseErr != nil {
  114. return false, errors.Join(err, parseErr)
  115. }
  116. cfg.AllowRebase, parseErr = parseBool16961(parts[2])
  117. if parseErr != nil {
  118. return false, errors.Join(err, parseErr)
  119. }
  120. cfg.AllowRebaseMerge, parseErr = parseBool16961(parts[3])
  121. if parseErr != nil {
  122. return false, errors.Join(err, parseErr)
  123. }
  124. cfg.AllowSquash, parseErr = parseBool16961(parts[4])
  125. if parseErr != nil {
  126. return false, errors.Join(err, parseErr)
  127. }
  128. cfg.AllowManualMerge, parseErr = parseBool16961(parts[5])
  129. if parseErr != nil {
  130. return false, errors.Join(err, parseErr)
  131. }
  132. cfg.AutodetectManualMerge, parseErr = parseBool16961(parts[6])
  133. if parseErr != nil {
  134. return false, errors.Join(err, parseErr)
  135. }
  136. // 1.14 unit
  137. if len(parts) == 7 {
  138. return true, nil
  139. }
  140. if len(parts) < 9 {
  141. return false, err
  142. }
  143. cfg.DefaultDeleteBranchAfterMerge, parseErr = parseBool16961(parts[7])
  144. if parseErr != nil {
  145. return false, errors.Join(err, parseErr)
  146. }
  147. cfg.DefaultMergeStyle = repo_model.MergeStyle(string(bytes.Join(parts[8:], []byte{' '})))
  148. return true, nil
  149. }
  150. func fixIssuesConfig16961(bs []byte, cfg *repo_model.IssuesConfig) (fixed bool, err error) {
  151. err = json.UnmarshalHandleDoubleEncode(bs, &cfg)
  152. if err == nil {
  153. return false, nil
  154. }
  155. // Handle #16961
  156. if len(bs) < 3 {
  157. return false, err
  158. }
  159. if bs[0] != '&' || bs[1] != '{' || bs[len(bs)-1] != '}' {
  160. return false, err
  161. }
  162. parts := bytes.Split(bs[2:len(bs)-1], []byte{' '})
  163. if len(parts) != 3 {
  164. return false, err
  165. }
  166. var parseErr error
  167. cfg.EnableTimetracker, parseErr = parseBool16961(parts[0])
  168. if parseErr != nil {
  169. return false, errors.Join(err, parseErr)
  170. }
  171. cfg.AllowOnlyContributorsToTrackTime, parseErr = parseBool16961(parts[1])
  172. if parseErr != nil {
  173. return false, errors.Join(err, parseErr)
  174. }
  175. cfg.EnableDependencies, parseErr = parseBool16961(parts[2])
  176. if parseErr != nil {
  177. return false, errors.Join(err, parseErr)
  178. }
  179. return true, nil
  180. }
  181. func fixBrokenRepoUnit16961(repoUnit *repo_model.RepoUnit, bs []byte) (fixed bool, err error) {
  182. // Shortcut empty or null values
  183. if len(bs) == 0 {
  184. return false, nil
  185. }
  186. var cfg any
  187. err = json.UnmarshalHandleDoubleEncode(bs, &cfg)
  188. if err == nil {
  189. return false, nil
  190. }
  191. switch repoUnit.Type {
  192. case unit.TypeCode, unit.TypeReleases, unit.TypeWiki, unit.TypeProjects:
  193. cfg := &repo_model.UnitConfig{}
  194. repoUnit.Config = cfg
  195. if fixed, err := fixUnitConfig16961(bs, cfg); !fixed {
  196. return false, err
  197. }
  198. case unit.TypeExternalWiki:
  199. cfg := &repo_model.ExternalWikiConfig{}
  200. repoUnit.Config = cfg
  201. if fixed, err := fixExternalWikiConfig16961(bs, cfg); !fixed {
  202. return false, err
  203. }
  204. case unit.TypeExternalTracker:
  205. cfg := &repo_model.ExternalTrackerConfig{}
  206. repoUnit.Config = cfg
  207. if fixed, err := fixExternalTrackerConfig16961(bs, cfg); !fixed {
  208. return false, err
  209. }
  210. case unit.TypePullRequests:
  211. cfg := &repo_model.PullRequestsConfig{}
  212. repoUnit.Config = cfg
  213. if fixed, err := fixPullRequestsConfig16961(bs, cfg); !fixed {
  214. return false, err
  215. }
  216. case unit.TypeIssues:
  217. cfg := &repo_model.IssuesConfig{}
  218. repoUnit.Config = cfg
  219. if fixed, err := fixIssuesConfig16961(bs, cfg); !fixed {
  220. return false, err
  221. }
  222. default:
  223. panic(fmt.Sprintf("unrecognized repo unit type: %v", repoUnit.Type))
  224. }
  225. return true, nil
  226. }
  227. func fixBrokenRepoUnits16961(ctx context.Context, logger log.Logger, autofix bool) error {
  228. // RepoUnit describes all units of a repository
  229. type RepoUnit struct {
  230. ID int64
  231. RepoID int64
  232. Type unit.Type
  233. Config []byte
  234. CreatedUnix timeutil.TimeStamp `xorm:"INDEX CREATED"`
  235. }
  236. count := 0
  237. err := db.Iterate(
  238. ctx,
  239. builder.Gt{
  240. "id": 0,
  241. },
  242. func(ctx context.Context, unit *RepoUnit) error {
  243. bs := unit.Config
  244. repoUnit := &repo_model.RepoUnit{
  245. ID: unit.ID,
  246. RepoID: unit.RepoID,
  247. Type: unit.Type,
  248. CreatedUnix: unit.CreatedUnix,
  249. }
  250. if fixed, err := fixBrokenRepoUnit16961(repoUnit, bs); !fixed {
  251. return err
  252. }
  253. count++
  254. if !autofix {
  255. return nil
  256. }
  257. return repo_model.UpdateRepoUnit(ctx, repoUnit)
  258. },
  259. )
  260. if err != nil {
  261. logger.Critical("Unable to iterate across repounits to fix the broken units: Error %v", err)
  262. return err
  263. }
  264. if !autofix {
  265. if count == 0 {
  266. logger.Info("Found no broken repo_units")
  267. } else {
  268. logger.Warn("Found %d broken repo_units", count)
  269. }
  270. return nil
  271. }
  272. logger.Info("Fixed %d broken repo_units", count)
  273. return nil
  274. }
  275. func init() {
  276. Register(&Check{
  277. Title: "Check for incorrectly dumped repo_units (See #16961)",
  278. Name: "fix-broken-repo-units",
  279. IsDefault: false,
  280. Run: fixBrokenRepoUnits16961,
  281. Priority: 7,
  282. })
  283. }