gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package actions
  4. import (
  5. "context"
  6. "strings"
  7. "unicode/utf8"
  8. "code.gitea.io/gitea/models/db"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/timeutil"
  11. "code.gitea.io/gitea/modules/util"
  12. "xorm.io/builder"
  13. )
  14. // ActionVariable represents a variable that can be used in actions
  15. //
  16. // It can be:
  17. // 1. global variable, OwnerID is 0 and RepoID is 0
  18. // 2. org/user level variable, OwnerID is org/user ID and RepoID is 0
  19. // 3. repo level variable, OwnerID is 0 and RepoID is repo ID
  20. //
  21. // Please note that it's not acceptable to have both OwnerID and RepoID to be non-zero,
  22. // or it will be complicated to find variables belonging to a specific owner.
  23. // For example, conditions like `OwnerID = 1` will also return variable {OwnerID: 1, RepoID: 1},
  24. // but it's a repo level variable, not an org/user level variable.
  25. // To avoid this, make it clear with {OwnerID: 0, RepoID: 1} for repo level variables.
  26. type ActionVariable struct {
  27. ID int64 `xorm:"pk autoincr"`
  28. OwnerID int64 `xorm:"UNIQUE(owner_repo_name)"`
  29. RepoID int64 `xorm:"INDEX UNIQUE(owner_repo_name)"`
  30. Name string `xorm:"UNIQUE(owner_repo_name) NOT NULL"`
  31. Data string `xorm:"LONGTEXT NOT NULL"`
  32. Description string `xorm:"TEXT"`
  33. CreatedUnix timeutil.TimeStamp `xorm:"created NOT NULL"`
  34. UpdatedUnix timeutil.TimeStamp `xorm:"updated"`
  35. }
  36. const (
  37. VariableDataMaxLength = 65536
  38. VariableDescriptionMaxLength = 4096
  39. )
  40. func init() {
  41. db.RegisterModel(new(ActionVariable))
  42. }
  43. func InsertVariable(ctx context.Context, ownerID, repoID int64, name, data, description string) (*ActionVariable, error) {
  44. if ownerID != 0 && repoID != 0 {
  45. // It's trying to create a variable that belongs to a repository, but OwnerID has been set accidentally.
  46. // Remove OwnerID to avoid confusion; it's not worth returning an error here.
  47. ownerID = 0
  48. }
  49. if utf8.RuneCountInString(data) > VariableDataMaxLength {
  50. return nil, util.NewInvalidArgumentErrorf("data too long")
  51. }
  52. description = util.TruncateRunes(description, VariableDescriptionMaxLength)
  53. variable := &ActionVariable{
  54. OwnerID: ownerID,
  55. RepoID: repoID,
  56. Name: strings.ToUpper(name),
  57. Data: data,
  58. Description: description,
  59. }
  60. return variable, db.Insert(ctx, variable)
  61. }
  62. type FindVariablesOpts struct {
  63. db.ListOptions
  64. IDs []int64
  65. RepoID int64
  66. OwnerID int64 // it will be ignored if RepoID is set
  67. Name string
  68. }
  69. func (opts FindVariablesOpts) ToConds() builder.Cond {
  70. cond := builder.NewCond()
  71. if len(opts.IDs) > 0 {
  72. if len(opts.IDs) == 1 {
  73. cond = cond.And(builder.Eq{"id": opts.IDs[0]})
  74. } else {
  75. cond = cond.And(builder.In("id", opts.IDs))
  76. }
  77. }
  78. // Since we now support instance-level variables,
  79. // there is no need to check for null values for `owner_id` and `repo_id`
  80. cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
  81. if opts.RepoID != 0 { // if RepoID is set
  82. // ignore OwnerID and treat it as 0
  83. cond = cond.And(builder.Eq{"owner_id": 0})
  84. } else {
  85. cond = cond.And(builder.Eq{"owner_id": opts.OwnerID})
  86. }
  87. if opts.Name != "" {
  88. cond = cond.And(builder.Eq{"name": strings.ToUpper(opts.Name)})
  89. }
  90. return cond
  91. }
  92. func FindVariables(ctx context.Context, opts FindVariablesOpts) ([]*ActionVariable, error) {
  93. return db.Find[ActionVariable](ctx, opts)
  94. }
  95. func UpdateVariableCols(ctx context.Context, variable *ActionVariable, cols ...string) (bool, error) {
  96. if utf8.RuneCountInString(variable.Data) > VariableDataMaxLength {
  97. return false, util.NewInvalidArgumentErrorf("data too long")
  98. }
  99. variable.Description = util.TruncateRunes(variable.Description, VariableDescriptionMaxLength)
  100. variable.Name = strings.ToUpper(variable.Name)
  101. count, err := db.GetEngine(ctx).
  102. ID(variable.ID).
  103. Cols(cols...).
  104. Update(variable)
  105. return count != 0, err
  106. }
  107. func DeleteVariable(ctx context.Context, id int64) error {
  108. if _, err := db.DeleteByID[ActionVariable](ctx, id); err != nil {
  109. return err
  110. }
  111. return nil
  112. }
  113. func GetVariablesOfRun(ctx context.Context, run *ActionRun) (map[string]string, error) {
  114. variables := map[string]string{}
  115. if err := run.LoadRepo(ctx); err != nil {
  116. log.Error("LoadRepo: %v", err)
  117. return nil, err
  118. }
  119. // Global
  120. globalVariables, err := db.Find[ActionVariable](ctx, FindVariablesOpts{})
  121. if err != nil {
  122. log.Error("find global variables: %v", err)
  123. return nil, err
  124. }
  125. // Org / User level
  126. ownerVariables, err := db.Find[ActionVariable](ctx, FindVariablesOpts{OwnerID: run.Repo.OwnerID})
  127. if err != nil {
  128. log.Error("find variables of org: %d, error: %v", run.Repo.OwnerID, err)
  129. return nil, err
  130. }
  131. // Repo level
  132. repoVariables, err := db.Find[ActionVariable](ctx, FindVariablesOpts{RepoID: run.RepoID})
  133. if err != nil {
  134. log.Error("find variables of repo: %d, error: %v", run.RepoID, err)
  135. return nil, err
  136. }
  137. // Level precedence: Repo > Org / User > Global
  138. for _, v := range append(globalVariables, append(ownerVariables, repoVariables...)...) {
  139. variables[v.Name] = v.Data
  140. }
  141. return variables, nil
  142. }
  143. func CountWrongRepoLevelVariables(ctx context.Context) (int64, error) {
  144. var result int64
  145. _, err := db.GetEngine(ctx).SQL("SELECT count(`id`) FROM `action_variable` WHERE `repo_id` > 0 AND `owner_id` > 0").Get(&result)
  146. return result, err
  147. }
  148. func UpdateWrongRepoLevelVariables(ctx context.Context) (int64, error) {
  149. result, err := db.GetEngine(ctx).Exec("UPDATE `action_variable` SET `owner_id` = 0 WHERE `repo_id` > 0 AND `owner_id` > 0")
  150. if err != nil {
  151. return 0, err
  152. }
  153. return result.RowsAffected()
  154. }