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.

task_output.go 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package actions
  4. import (
  5. "context"
  6. "code.gitea.io/gitea/models/db"
  7. )
  8. // ActionTaskOutput represents an output of ActionTask.
  9. // So the outputs are bound to a task, that means when a completed job has been rerun,
  10. // the outputs of the job will be reset because the task is new.
  11. // It's by design, to avoid the outputs of the old task to be mixed with the new task.
  12. type ActionTaskOutput struct {
  13. ID int64
  14. TaskID int64 `xorm:"INDEX UNIQUE(task_id_output_key)"`
  15. OutputKey string `xorm:"VARCHAR(255) UNIQUE(task_id_output_key)"`
  16. OutputValue string `xorm:"MEDIUMTEXT"`
  17. }
  18. func init() {
  19. db.RegisterModel(new(ActionTaskOutput))
  20. }
  21. // FindTaskOutputByTaskID returns the outputs of the task.
  22. func FindTaskOutputByTaskID(ctx context.Context, taskID int64) ([]*ActionTaskOutput, error) {
  23. var outputs []*ActionTaskOutput
  24. return outputs, db.GetEngine(ctx).Where("task_id=?", taskID).Find(&outputs)
  25. }
  26. // FindTaskOutputKeyByTaskID returns the keys of the outputs of the task.
  27. func FindTaskOutputKeyByTaskID(ctx context.Context, taskID int64) ([]string, error) {
  28. var keys []string
  29. return keys, db.GetEngine(ctx).Table(ActionTaskOutput{}).Where("task_id=?", taskID).Cols("output_key").Find(&keys)
  30. }
  31. // InsertTaskOutputIfNotExist inserts a new task output if it does not exist.
  32. func InsertTaskOutputIfNotExist(ctx context.Context, taskID int64, key, value string) error {
  33. return db.WithTx(ctx, func(ctx context.Context) error {
  34. sess := db.GetEngine(ctx)
  35. if exist, err := sess.Exist(&ActionTaskOutput{TaskID: taskID, OutputKey: key}); err != nil {
  36. return err
  37. } else if exist {
  38. return nil
  39. }
  40. _, err := sess.Insert(&ActionTaskOutput{
  41. TaskID: taskID,
  42. OutputKey: key,
  43. OutputValue: value,
  44. })
  45. return err
  46. })
  47. }