A fork of Gitea (see branch `mj`) adding Majority Judgment Polls 𐄷 over Issues and Merge Requests. https://git.mieuxvoter.fr
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.

258 lines
6.3 KiB

  1. // Copyright 2019 Gitea. 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 models
  5. import (
  6. "fmt"
  7. migration "code.gitea.io/gitea/modules/migrations/base"
  8. "code.gitea.io/gitea/modules/secret"
  9. "code.gitea.io/gitea/modules/setting"
  10. "code.gitea.io/gitea/modules/structs"
  11. "code.gitea.io/gitea/modules/timeutil"
  12. "code.gitea.io/gitea/modules/util"
  13. jsoniter "github.com/json-iterator/go"
  14. "xorm.io/builder"
  15. )
  16. // Task represents a task
  17. type Task struct {
  18. ID int64
  19. DoerID int64 `xorm:"index"` // operator
  20. Doer *User `xorm:"-"`
  21. OwnerID int64 `xorm:"index"` // repo owner id, when creating, the repoID maybe zero
  22. Owner *User `xorm:"-"`
  23. RepoID int64 `xorm:"index"`
  24. Repo *Repository `xorm:"-"`
  25. Type structs.TaskType
  26. Status structs.TaskStatus `xorm:"index"`
  27. StartTime timeutil.TimeStamp
  28. EndTime timeutil.TimeStamp
  29. PayloadContent string `xorm:"TEXT"`
  30. Errors string `xorm:"TEXT"` // if task failed, saved the error reason
  31. Created timeutil.TimeStamp `xorm:"created"`
  32. }
  33. // LoadRepo loads repository of the task
  34. func (task *Task) LoadRepo() error {
  35. return task.loadRepo(x)
  36. }
  37. func (task *Task) loadRepo(e Engine) error {
  38. if task.Repo != nil {
  39. return nil
  40. }
  41. var repo Repository
  42. has, err := e.ID(task.RepoID).Get(&repo)
  43. if err != nil {
  44. return err
  45. } else if !has {
  46. return ErrRepoNotExist{
  47. ID: task.RepoID,
  48. }
  49. }
  50. task.Repo = &repo
  51. return nil
  52. }
  53. // LoadDoer loads do user
  54. func (task *Task) LoadDoer() error {
  55. if task.Doer != nil {
  56. return nil
  57. }
  58. var doer User
  59. has, err := x.ID(task.DoerID).Get(&doer)
  60. if err != nil {
  61. return err
  62. } else if !has {
  63. return ErrUserNotExist{
  64. UID: task.DoerID,
  65. }
  66. }
  67. task.Doer = &doer
  68. return nil
  69. }
  70. // LoadOwner loads owner user
  71. func (task *Task) LoadOwner() error {
  72. if task.Owner != nil {
  73. return nil
  74. }
  75. var owner User
  76. has, err := x.ID(task.OwnerID).Get(&owner)
  77. if err != nil {
  78. return err
  79. } else if !has {
  80. return ErrUserNotExist{
  81. UID: task.OwnerID,
  82. }
  83. }
  84. task.Owner = &owner
  85. return nil
  86. }
  87. // UpdateCols updates some columns
  88. func (task *Task) UpdateCols(cols ...string) error {
  89. _, err := x.ID(task.ID).Cols(cols...).Update(task)
  90. return err
  91. }
  92. // MigrateConfig returns task config when migrate repository
  93. func (task *Task) MigrateConfig() (*migration.MigrateOptions, error) {
  94. if task.Type == structs.TaskTypeMigrateRepo {
  95. var opts migration.MigrateOptions
  96. json := jsoniter.ConfigCompatibleWithStandardLibrary
  97. err := json.Unmarshal([]byte(task.PayloadContent), &opts)
  98. if err != nil {
  99. return nil, err
  100. }
  101. // decrypt credentials
  102. if opts.CloneAddrEncrypted != "" {
  103. if opts.CloneAddr, err = secret.DecryptSecret(setting.SecretKey, opts.CloneAddrEncrypted); err != nil {
  104. return nil, err
  105. }
  106. }
  107. if opts.AuthPasswordEncrypted != "" {
  108. if opts.AuthPassword, err = secret.DecryptSecret(setting.SecretKey, opts.AuthPasswordEncrypted); err != nil {
  109. return nil, err
  110. }
  111. }
  112. if opts.AuthTokenEncrypted != "" {
  113. if opts.AuthToken, err = secret.DecryptSecret(setting.SecretKey, opts.AuthTokenEncrypted); err != nil {
  114. return nil, err
  115. }
  116. }
  117. return &opts, nil
  118. }
  119. return nil, fmt.Errorf("Task type is %s, not Migrate Repo", task.Type.Name())
  120. }
  121. // ErrTaskDoesNotExist represents a "TaskDoesNotExist" kind of error.
  122. type ErrTaskDoesNotExist struct {
  123. ID int64
  124. RepoID int64
  125. Type structs.TaskType
  126. }
  127. // IsErrTaskDoesNotExist checks if an error is a ErrTaskIsNotExist.
  128. func IsErrTaskDoesNotExist(err error) bool {
  129. _, ok := err.(ErrTaskDoesNotExist)
  130. return ok
  131. }
  132. func (err ErrTaskDoesNotExist) Error() string {
  133. return fmt.Sprintf("task is not exist [id: %d, repo_id: %d, type: %d]",
  134. err.ID, err.RepoID, err.Type)
  135. }
  136. // GetMigratingTask returns the migrating task by repo's id
  137. func GetMigratingTask(repoID int64) (*Task, error) {
  138. task := Task{
  139. RepoID: repoID,
  140. Type: structs.TaskTypeMigrateRepo,
  141. }
  142. has, err := x.Get(&task)
  143. if err != nil {
  144. return nil, err
  145. } else if !has {
  146. return nil, ErrTaskDoesNotExist{0, repoID, task.Type}
  147. }
  148. return &task, nil
  149. }
  150. // GetMigratingTaskByID returns the migrating task by repo's id
  151. func GetMigratingTaskByID(id, doerID int64) (*Task, *migration.MigrateOptions, error) {
  152. task := Task{
  153. ID: id,
  154. DoerID: doerID,
  155. Type: structs.TaskTypeMigrateRepo,
  156. }
  157. has, err := x.Get(&task)
  158. if err != nil {
  159. return nil, nil, err
  160. } else if !has {
  161. return nil, nil, ErrTaskDoesNotExist{id, 0, task.Type}
  162. }
  163. var opts migration.MigrateOptions
  164. json := jsoniter.ConfigCompatibleWithStandardLibrary
  165. if err := json.Unmarshal([]byte(task.PayloadContent), &opts); err != nil {
  166. return nil, nil, err
  167. }
  168. return &task, &opts, nil
  169. }
  170. // FindTaskOptions find all tasks
  171. type FindTaskOptions struct {
  172. Status int
  173. }
  174. // ToConds generates conditions for database operation.
  175. func (opts FindTaskOptions) ToConds() builder.Cond {
  176. cond := builder.NewCond()
  177. if opts.Status >= 0 {
  178. cond = cond.And(builder.Eq{"status": opts.Status})
  179. }
  180. return cond
  181. }
  182. // FindTasks find all tasks
  183. func FindTasks(opts FindTaskOptions) ([]*Task, error) {
  184. tasks := make([]*Task, 0, 10)
  185. err := x.Where(opts.ToConds()).Find(&tasks)
  186. return tasks, err
  187. }
  188. // CreateTask creates a task on database
  189. func CreateTask(task *Task) error {
  190. return createTask(x, task)
  191. }
  192. func createTask(e Engine, task *Task) error {
  193. _, err := e.Insert(task)
  194. return err
  195. }
  196. // FinishMigrateTask updates database when migrate task finished
  197. func FinishMigrateTask(task *Task) error {
  198. task.Status = structs.TaskStatusFinished
  199. task.EndTime = timeutil.TimeStampNow()
  200. // delete credentials when we're done, they're a liability.
  201. conf, err := task.MigrateConfig()
  202. if err != nil {
  203. return err
  204. }
  205. conf.AuthPassword = ""
  206. conf.AuthToken = ""
  207. conf.CloneAddr = util.SanitizeURLCredentials(conf.CloneAddr, true)
  208. conf.AuthPasswordEncrypted = ""
  209. conf.AuthTokenEncrypted = ""
  210. conf.CloneAddrEncrypted = ""
  211. json := jsoniter.ConfigCompatibleWithStandardLibrary
  212. confBytes, err := json.Marshal(conf)
  213. if err != nil {
  214. return err
  215. }
  216. task.PayloadContent = string(confBytes)
  217. sess := x.NewSession()
  218. defer sess.Close()
  219. if err := sess.Begin(); err != nil {
  220. return err
  221. }
  222. if _, err := sess.ID(task.ID).Cols("status", "end_time", "payload_content").Update(task); err != nil {
  223. return err
  224. }
  225. return sess.Commit()
  226. }