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.

variable.go 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package actions
  4. import (
  5. "context"
  6. "errors"
  7. "fmt"
  8. "strings"
  9. "code.gitea.io/gitea/models/db"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/timeutil"
  12. "code.gitea.io/gitea/modules/util"
  13. "xorm.io/builder"
  14. )
  15. type ActionVariable struct {
  16. ID int64 `xorm:"pk autoincr"`
  17. OwnerID int64 `xorm:"UNIQUE(owner_repo_name)"`
  18. RepoID int64 `xorm:"INDEX UNIQUE(owner_repo_name)"`
  19. Name string `xorm:"UNIQUE(owner_repo_name) NOT NULL"`
  20. Data string `xorm:"LONGTEXT NOT NULL"`
  21. CreatedUnix timeutil.TimeStamp `xorm:"created NOT NULL"`
  22. UpdatedUnix timeutil.TimeStamp `xorm:"updated"`
  23. }
  24. func init() {
  25. db.RegisterModel(new(ActionVariable))
  26. }
  27. func (v *ActionVariable) Validate() error {
  28. if v.OwnerID == 0 && v.RepoID == 0 {
  29. return errors.New("the variable is not bound to any scope")
  30. }
  31. return nil
  32. }
  33. func InsertVariable(ctx context.Context, ownerID, repoID int64, name, data string) (*ActionVariable, error) {
  34. variable := &ActionVariable{
  35. OwnerID: ownerID,
  36. RepoID: repoID,
  37. Name: strings.ToUpper(name),
  38. Data: data,
  39. }
  40. if err := variable.Validate(); err != nil {
  41. return variable, err
  42. }
  43. return variable, db.Insert(ctx, variable)
  44. }
  45. type FindVariablesOpts struct {
  46. db.ListOptions
  47. OwnerID int64
  48. RepoID int64
  49. }
  50. func (opts *FindVariablesOpts) toConds() builder.Cond {
  51. cond := builder.NewCond()
  52. if opts.OwnerID > 0 {
  53. cond = cond.And(builder.Eq{"owner_id": opts.OwnerID})
  54. }
  55. if opts.RepoID > 0 {
  56. cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
  57. }
  58. return cond
  59. }
  60. func FindVariables(ctx context.Context, opts FindVariablesOpts) ([]*ActionVariable, error) {
  61. var variables []*ActionVariable
  62. sess := db.GetEngine(ctx)
  63. if opts.PageSize != 0 {
  64. sess = db.SetSessionPagination(sess, &opts.ListOptions)
  65. }
  66. return variables, sess.Where(opts.toConds()).Find(&variables)
  67. }
  68. func GetVariableByID(ctx context.Context, variableID int64) (*ActionVariable, error) {
  69. var variable ActionVariable
  70. has, err := db.GetEngine(ctx).Where("id=?", variableID).Get(&variable)
  71. if err != nil {
  72. return nil, err
  73. } else if !has {
  74. return nil, fmt.Errorf("variable with id %d: %w", variableID, util.ErrNotExist)
  75. }
  76. return &variable, nil
  77. }
  78. func UpdateVariable(ctx context.Context, variable *ActionVariable) (bool, error) {
  79. count, err := db.GetEngine(ctx).ID(variable.ID).Cols("name", "data").
  80. Update(&ActionVariable{
  81. Name: variable.Name,
  82. Data: variable.Data,
  83. })
  84. return count != 0, err
  85. }
  86. func GetVariablesOfRun(ctx context.Context, run *ActionRun) (map[string]string, error) {
  87. variables := map[string]string{}
  88. // Global
  89. globalVariables, err := FindVariables(ctx, FindVariablesOpts{})
  90. if err != nil {
  91. log.Error("find global variables: %v", err)
  92. return nil, err
  93. }
  94. // Org / User level
  95. ownerVariables, err := FindVariables(ctx, FindVariablesOpts{OwnerID: run.Repo.OwnerID})
  96. if err != nil {
  97. log.Error("find variables of org: %d, error: %v", run.Repo.OwnerID, err)
  98. return nil, err
  99. }
  100. // Repo level
  101. repoVariables, err := FindVariables(ctx, FindVariablesOpts{RepoID: run.RepoID})
  102. if err != nil {
  103. log.Error("find variables of repo: %d, error: %v", run.RepoID, err)
  104. return nil, err
  105. }
  106. // Level precedence: Repo > Org / User > Global
  107. for _, v := range append(globalVariables, append(ownerVariables, repoVariables...)...) {
  108. variables[v.Name] = v.Data
  109. }
  110. return variables, nil
  111. }