/
uzer_007
/
Rixea
Обзор
Документация
Войти
/
uzer_007
/
Rixea
Код
Запросы
0
Задачи
Вики
Пакеты
5
Релизы
2
CI/CD
Аналитика
Безопасность
master
models/actions/variable.go
138 строк
4 KB
uzer_007
feat: первая версия Rixea
02 авг 2026, 22:16
02 авг 2026, 22:16
d4f7504
Код
Авторство
О чём код?
// Copyright 2026 Uzer_007. All rights reserved. // SPDX-License-Identifier: MIT package actions import ( "context" "strings" "unicode/utf8" "gitverse.ru/uzer_007/Rixea/models/db" "gitverse.ru/uzer_007/Rixea/modules/log" "gitverse.ru/uzer_007/Rixea/modules/timeutil" "gitverse.ru/uzer_007/Rixea/modules/util" ) // ActionVariable represents a variable that can be used in actions // // It can be: // 1. global variable, OwnerID is 0 and RepoID is 0 // 2. org/user level variable, OwnerID is org/user ID and RepoID is 0 // 3. repo level variable, OwnerID is 0 and RepoID is repo ID // // Please note that it's not acceptable to have both OwnerID and RepoID to be non-zero, // or it will be complicated to find variables belonging to a specific owner. // For example, conditions like `OwnerID = 1` will also return variable {OwnerID: 1, RepoID: 1}, // but it's a repo level variable, not an org/user level variable. // To avoid this, make it clear with {OwnerID: 0, RepoID: 1} for repo level variables. type ActionVariable struct { ID int64 OwnerID int64 RepoID int64 Name string Data string Description string CreatedUnix timeutil.TimeStamp UpdatedUnix timeutil.TimeStamp } const ( VariableDataMaxLength = 65536 VariableDescriptionMaxLength = 4096 ) func InsertVariable(ctx context.Context, ownerID, repoID int64, name, data, description string) (*ActionVariable, error) { if ownerID != 0 && repoID != 0 { // It's trying to create a variable that belongs to a repository, but OwnerID has been set accidentally. // Remove OwnerID to avoid confusion; it's not worth returning an error here. ownerID = 0 } if utf8.RuneCountInString(data) > VariableDataMaxLength { return nil, util.NewInvalidArgumentErrorf("data too long") } description = util.TruncateRunes(description, VariableDescriptionMaxLength) variable := &ActionVariable{ OwnerID: ownerID, RepoID: repoID, Name: strings.ToUpper(name), Data: data, Description: description, } return variable, insertVariableReindexer(ctx, variable) } type FindVariablesOpts struct { db.ListOptions IDs []int64 RepoID int64 OwnerID int64 // it will be ignored if RepoID is set Name string } func FindVariables(ctx context.Context, opts FindVariablesOpts) ([]*ActionVariable, error) { return db.Find[ActionVariable](ctx, opts) } func UpdateVariableCols(ctx context.Context, variable *ActionVariable, cols ...string) (bool, error) { if utf8.RuneCountInString(variable.Data) > VariableDataMaxLength { return false, util.NewInvalidArgumentErrorf("data too long") } variable.Description = util.TruncateRunes(variable.Description, VariableDescriptionMaxLength) variable.Name = strings.ToUpper(variable.Name) return updateVariableReindexer(ctx, variable) } func DeleteVariable(ctx context.Context, id int64) error { return deleteVariableReindexer(ctx, id) } func GetVariablesOfRun(ctx context.Context, run *ActionRun) (map[string]string, error) { variables := map[string]string{} if err := run.LoadRepo(ctx); err != nil { log.Error("LoadRepo: %v", err) return nil, err } // Global globalVariables, err := db.Find[ActionVariable](ctx, FindVariablesOpts{}) if err != nil { log.Error("find global variables: %v", err) return nil, err } // Org / User level ownerVariables, err := db.Find[ActionVariable](ctx, FindVariablesOpts{OwnerID: run.Repo.OwnerID}) if err != nil { log.Error("find variables of org: %d, error: %v", run.Repo.OwnerID, err) return nil, err } // Repo level repoVariables, err := db.Find[ActionVariable](ctx, FindVariablesOpts{RepoID: run.RepoID}) if err != nil { log.Error("find variables of repo: %d, error: %v", run.RepoID, err) return nil, err } // Level precedence: Repo > Org / User > Global for _, v := range append(globalVariables, append(ownerVariables, repoVariables...)...) { variables[v.Name] = v.Data } return variables, nil } func CountWrongRepoLevelVariables(ctx context.Context) (int64, error) { return countWrongVariablesReindexer(ctx) } func UpdateWrongRepoLevelVariables(ctx context.Context) (int64, error) { return updateWrongVariablesReindexer(ctx) }