code.gitea.io/gitea@v1.21.7/models/actions/variable.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  	"errors"
     9  	"fmt"
    10  	"strings"
    11  
    12  	"code.gitea.io/gitea/models/db"
    13  	"code.gitea.io/gitea/modules/timeutil"
    14  	"code.gitea.io/gitea/modules/util"
    15  
    16  	"xorm.io/builder"
    17  )
    18  
    19  type ActionVariable struct {
    20  	ID          int64              `xorm:"pk autoincr"`
    21  	OwnerID     int64              `xorm:"UNIQUE(owner_repo_name)"`
    22  	RepoID      int64              `xorm:"INDEX UNIQUE(owner_repo_name)"`
    23  	Name        string             `xorm:"UNIQUE(owner_repo_name) NOT NULL"`
    24  	Data        string             `xorm:"LONGTEXT NOT NULL"`
    25  	CreatedUnix timeutil.TimeStamp `xorm:"created NOT NULL"`
    26  	UpdatedUnix timeutil.TimeStamp `xorm:"updated"`
    27  }
    28  
    29  func init() {
    30  	db.RegisterModel(new(ActionVariable))
    31  }
    32  
    33  func (v *ActionVariable) Validate() error {
    34  	if v.OwnerID == 0 && v.RepoID == 0 {
    35  		return errors.New("the variable is not bound to any scope")
    36  	}
    37  	return nil
    38  }
    39  
    40  func InsertVariable(ctx context.Context, ownerID, repoID int64, name, data string) (*ActionVariable, error) {
    41  	variable := &ActionVariable{
    42  		OwnerID: ownerID,
    43  		RepoID:  repoID,
    44  		Name:    strings.ToUpper(name),
    45  		Data:    data,
    46  	}
    47  	if err := variable.Validate(); err != nil {
    48  		return variable, err
    49  	}
    50  	return variable, db.Insert(ctx, variable)
    51  }
    52  
    53  type FindVariablesOpts struct {
    54  	db.ListOptions
    55  	OwnerID int64
    56  	RepoID  int64
    57  }
    58  
    59  func (opts *FindVariablesOpts) toConds() builder.Cond {
    60  	cond := builder.NewCond()
    61  	if opts.OwnerID > 0 {
    62  		cond = cond.And(builder.Eq{"owner_id": opts.OwnerID})
    63  	}
    64  	if opts.RepoID > 0 {
    65  		cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
    66  	}
    67  	return cond
    68  }
    69  
    70  func FindVariables(ctx context.Context, opts FindVariablesOpts) ([]*ActionVariable, error) {
    71  	var variables []*ActionVariable
    72  	sess := db.GetEngine(ctx)
    73  	if opts.PageSize != 0 {
    74  		sess = db.SetSessionPagination(sess, &opts.ListOptions)
    75  	}
    76  	return variables, sess.Where(opts.toConds()).Find(&variables)
    77  }
    78  
    79  func GetVariableByID(ctx context.Context, variableID int64) (*ActionVariable, error) {
    80  	var variable ActionVariable
    81  	has, err := db.GetEngine(ctx).Where("id=?", variableID).Get(&variable)
    82  	if err != nil {
    83  		return nil, err
    84  	} else if !has {
    85  		return nil, fmt.Errorf("variable with id %d: %w", variableID, util.ErrNotExist)
    86  	}
    87  	return &variable, nil
    88  }
    89  
    90  func UpdateVariable(ctx context.Context, variable *ActionVariable) (bool, error) {
    91  	count, err := db.GetEngine(ctx).ID(variable.ID).Cols("name", "data").
    92  		Update(&ActionVariable{
    93  			Name: variable.Name,
    94  			Data: variable.Data,
    95  		})
    96  	return count != 0, err
    97  }