code.gitea.io/gitea@v1.21.7/models/actions/task_output.go (about)

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