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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. switch repoUnit.Type {
  187. case unit.TypeCode, unit.TypeReleases, unit.TypeWiki, unit.TypeProjects:
  188. cfg := &repo_model.UnitConfig{}
  189. repoUnit.Config = cfg
  190. if fixed, err := fixUnitConfig16961(bs, cfg); !fixed {
  191. return false, err
  192. }
  193. case unit.TypeExternalWiki:
  194. cfg := &repo_model.ExternalWikiConfig{}
  195. repoUnit.Config = cfg
  196. if fixed, err := fixExternalWikiConfig16961(bs, cfg); !fixed {
  197. return false, err
  198. }
  199. case unit.TypeExternalTracker:
  200. cfg := &repo_model.ExternalTrackerConfig{}
  201. repoUnit.Config = cfg
  202. if fixed, err := fixExternalTrackerConfig16961(bs, cfg); !fixed {
  203. return false, err
  204. }
  205. case unit.TypePullRequests:
  206. cfg := &repo_model.PullRequestsConfig{}
  207. repoUnit.Config = cfg
  208. if fixed, err := fixPullRequestsConfig16961(bs, cfg); !fixed {
  209. return false, err
  210. }
  211. case unit.TypeIssues:
  212. cfg := &repo_model.IssuesConfig{}
  213. repoUnit.Config = cfg
  214. if fixed, err := fixIssuesConfig16961(bs, cfg); !fixed {
  215. return false, err
  216. }
  217. default:
  218. panic(fmt.Sprintf("unrecognized repo unit type: %v", repoUnit.Type))
  219. }
  220. return true, nil
  221. }
  222. func fixBrokenRepoUnits16961(ctx context.Context, logger log.Logger, autofix bool) error {
  223. // RepoUnit describes all units of a repository
  224. type RepoUnit struct {
  225. ID int64
  226. RepoID int64
  227. Type unit.Type
  228. Config []byte
  229. CreatedUnix timeutil.TimeStamp `xorm:"INDEX CREATED"`
  230. }
  231. count := 0
  232. err := db.Iterate(
  233. ctx,
  234. builder.Gt{
  235. "id": 0,
  236. },
  237. func(ctx context.Context, unit *RepoUnit) error {
  238. bs := unit.Config
  239. repoUnit := &repo_model.RepoUnit{
  240. ID: unit.ID,
  241. RepoID: unit.RepoID,
  242. Type: unit.Type,
  243. CreatedUnix: unit.CreatedUnix,
  244. }
  245. if fixed, err := fixBrokenRepoUnit16961(repoUnit, bs); !fixed {
  246. return err
  247. }
  248. count++
  249. if !autofix {
  250. return nil
  251. }
  252. return repo_model.UpdateRepoUnit(repoUnit)
  253. },
  254. )
  255. if err != nil {
  256. logger.Critical("Unable to iterate across repounits to fix the broken units: Error %v", err)
  257. return err
  258. }
  259. if !autofix {
  260. if count == 0 {
  261. logger.Info("Found no broken repo_units")
  262. } else {
  263. logger.Warn("Found %d broken repo_units", count)
  264. }
  265. return nil
  266. }
  267. logger.Info("Fixed %d broken repo_units", count)
  268. return nil
  269. }
  270. func init() {
  271. Register(&Check{
  272. Title: "Check for incorrectly dumped repo_units (See #16961)",
  273. Name: "fix-broken-repo-units",
  274. IsDefault: false,
  275. Run: fixBrokenRepoUnits16961,
  276. Priority: 7,
  277. })
  278. }