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

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