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.

fix16961.go 7.3KB

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