/
wax_boy
/
semaphore_custom
Обзор
Документация
Войти
/
wax_boy
/
semaphore_custom
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
db/Environment.go
130 строк
3 KB
Denis Gukov
fix: bug reports
19 июл 2026, 17:15
19 июл 2026, 17:15
048df99
Код
Авторство
О чём код?
package db import ( "encoding/json" "errors" "time" "github.com/semaphoreui/semaphore/pkg/common_errors" ) type EnvironmentSecretOperation string const ( EnvironmentSecretCreate EnvironmentSecretOperation = "create" EnvironmentSecretUpdate EnvironmentSecretOperation = "update" EnvironmentSecretDelete EnvironmentSecretOperation = "delete" ) type EnvironmentSecretType string const ( EnvironmentSecretVar EnvironmentSecretType = "var" EnvironmentSecretEnv EnvironmentSecretType = "env" ) func (t EnvironmentSecretType) GetAccessKeyOwner() AccessKeyOwner { switch t { case EnvironmentSecretVar: return AccessKeyVariable case EnvironmentSecretEnv: return AccessKeyEnvironment default: panic("unknown secret type: " + t) } } type EnvironmentSecret struct { ID int `json:"id"` Type EnvironmentSecretType `json:"type"` Name string `json:"name"` Secret string `json:"secret"` Operation EnvironmentSecretOperation `json:"operation"` } // Environment is used to pass additional arguments, in json form to ansible type Environment struct { ID int `db:"id" json:"id" backup:"-"` Name string `db:"name" json:"name" binding:"required"` ProjectID int `db:"project_id" json:"project_id" backup:"-"` Password *string `db:"password" json:"password"` JSON string `db:"json" json:"json" binding:"required"` ENV *string `db:"env" json:"env" binding:"required"` // Secrets is a field which used to update secrets associated with the environment. Secrets []EnvironmentSecret `db:"-" json:"secrets,omitempty" backup:"-"` SecretStorageID *int `db:"secret_storage_id" json:"secret_storage_id,omitempty" backup:"-"` SecretStorageKeyPrefix *string `db:"secret_storage_key_prefix" json:"secret_storage_key_prefix,omitempty"` // Sync fields are transfer-only; persisted in project__secret_sync. SyncEnabled bool `db:"-" json:"sync_enabled"` SyncInterval int `db:"-" json:"sync_interval"` LastSyncedAt *time.Time `db:"-" json:"last_synced_at,omitempty"` LastSyncFailedAt *time.Time `db:"-" json:"last_sync_failed_at,omitempty"` SyncPaths []SecretSyncPath `db:"-" json:"sync_paths"` } func (s *EnvironmentSecret) Validate() error { if s.Type == EnvironmentSecretVar || s.Type == EnvironmentSecretEnv { return nil } if s.Secret == "" { return errors.New("missing secret") } return errors.New("invalid environment secret type") } func validateJSON(s string, mustValuesBeScalar bool) error { if s == "" { return nil } var data map[string]any err := json.Unmarshal([]byte(s), &data) if err != nil { return errors.New("must be valid JSON") } for k, v := range data { if k == "" { return errors.New("key can not be empty") } if mustValuesBeScalar { switch v.(type) { case []any, map[string]any: return errors.New("values must be scalar") } } } return nil } func (env *Environment) Validate() (err error) { if env.Name == "" { err = common_errors.NewValidationError("Environment name can not be empty") return } err = validateJSON(env.JSON, false) if err != nil { err = common_errors.NewValidationError("Extra variables " + err.Error()) return } if env.ENV == nil { return } err = validateJSON(*env.ENV, true) if err != nil { err = common_errors.NewValidationError("Environment variables " + err.Error()) } return }