2020-12-01 20:06:49 +01:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2024-07-15 10:57:12 +02:00
|
|
|
"database/sql/driver"
|
2022-01-19 13:05:48 +01:00
|
|
|
"encoding/json"
|
2020-12-03 14:51:15 +01:00
|
|
|
"errors"
|
2024-02-27 12:06:05 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-09-16 22:51:53 +02:00
|
|
|
"reflect"
|
2022-02-03 08:05:13 +01:00
|
|
|
"strings"
|
2020-12-04 23:22:05 +01:00
|
|
|
"time"
|
2020-12-01 20:06:49 +01:00
|
|
|
)
|
|
|
|
|
2020-12-04 23:22:05 +01:00
|
|
|
const databaseTimeFormat = "2006-01-02T15:04:05:99Z"
|
|
|
|
|
|
|
|
// GetParsedTime returns the timestamp as it will retrieved from the database
|
|
|
|
// This allows us to create timestamp consistency on return values from create requests
|
|
|
|
func GetParsedTime(t time.Time) time.Time {
|
|
|
|
parsedTime, err := time.Parse(databaseTimeFormat, t.Format(databaseTimeFormat))
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
return parsedTime
|
|
|
|
}
|
|
|
|
|
2022-01-19 13:05:48 +01:00
|
|
|
func ObjectToJSON(obj interface{}) *string {
|
|
|
|
if obj == nil || (reflect.ValueOf(obj).Kind() == reflect.Ptr && reflect.ValueOf(obj).IsNil()) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
bytes, err := json.Marshal(obj)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
str := string(bytes)
|
|
|
|
return &str
|
|
|
|
}
|
|
|
|
|
2020-12-01 20:06:49 +01:00
|
|
|
type RetrieveQueryParams struct {
|
|
|
|
Offset int
|
|
|
|
Count int
|
|
|
|
SortBy string
|
|
|
|
SortInverted bool
|
2024-07-07 19:12:21 +02:00
|
|
|
Filter string
|
2020-12-01 20:06:49 +01:00
|
|
|
}
|
|
|
|
|
2022-02-03 08:05:13 +01:00
|
|
|
type ObjectReferrer struct {
|
|
|
|
ID int `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ObjectReferrers struct {
|
|
|
|
Templates []ObjectReferrer `json:"templates"`
|
|
|
|
Inventories []ObjectReferrer `json:"inventories"`
|
|
|
|
Repositories []ObjectReferrer `json:"repositories"`
|
|
|
|
}
|
|
|
|
|
2024-02-11 20:52:14 +01:00
|
|
|
type IntegrationReferrers struct {
|
|
|
|
IntegrationMatchers []ObjectReferrer `json:"matchers"`
|
|
|
|
IntegrationExtractValues []ObjectReferrer `json:"values"`
|
2023-07-03 01:41:13 +02:00
|
|
|
}
|
|
|
|
|
2024-02-11 20:52:14 +01:00
|
|
|
type IntegrationExtractorChildReferrers struct {
|
2024-03-06 14:28:24 +01:00
|
|
|
Integrations []ObjectReferrer `json:"integrations"`
|
2023-07-03 01:41:13 +02:00
|
|
|
}
|
|
|
|
|
2022-02-03 08:05:13 +01:00
|
|
|
// ObjectProps describe database entities.
|
2021-10-12 13:37:51 +02:00
|
|
|
// It mainly used for NoSQL implementations (currently BoltDB) to preserve same
|
|
|
|
// data structure of different implementations and easy change it if required.
|
2022-02-03 08:05:13 +01:00
|
|
|
type ObjectProps struct {
|
|
|
|
TableName string
|
|
|
|
Type reflect.Type // to which type the table bust be mapped.
|
|
|
|
IsGlobal bool // doesn't belong to other table, for example to project or user.
|
|
|
|
ReferringColumnSuffix string
|
|
|
|
PrimaryColumnName string
|
|
|
|
SortableColumns []string
|
|
|
|
DefaultSortingColumn string
|
|
|
|
SortInverted bool // sort from high to low object ID by default. It is useful for some NoSQL implementations.
|
2021-05-07 12:08:34 +02:00
|
|
|
}
|
|
|
|
|
2021-05-14 13:47:32 +02:00
|
|
|
var ErrNotFound = errors.New("no rows in result set")
|
|
|
|
var ErrInvalidOperation = errors.New("invalid operation")
|
2020-12-01 20:06:49 +01:00
|
|
|
|
2021-11-02 20:30:45 +01:00
|
|
|
type ValidationError struct {
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ValidationError) Error() string {
|
|
|
|
return e.Message
|
|
|
|
}
|
|
|
|
|
2020-12-01 20:06:49 +01:00
|
|
|
type Store interface {
|
2022-11-09 18:04:35 +01:00
|
|
|
// Connect connects to the database.
|
2022-11-19 11:58:46 +01:00
|
|
|
// Token parameter used if PermanentConnection returns false.
|
|
|
|
// Token used for debugging of session connections.
|
2022-11-18 23:23:30 +01:00
|
|
|
Connect(token string)
|
|
|
|
Close(token string)
|
2022-11-09 18:04:35 +01:00
|
|
|
|
2022-11-18 23:23:30 +01:00
|
|
|
// PermanentConnection returns true if connection should be kept from start to finish of the app.
|
2022-11-09 18:04:35 +01:00
|
|
|
// This mode is suitable for MySQL and Postgres but not for BoltDB.
|
|
|
|
// For BoltDB we should reconnect for each request because BoltDB support only one connection at time.
|
2022-11-18 23:23:30 +01:00
|
|
|
PermanentConnection() bool
|
2022-01-23 14:03:23 +01:00
|
|
|
|
2021-12-19 13:31:23 +01:00
|
|
|
// IsInitialized indicates is database already initialized, or it is empty.
|
|
|
|
// The method is useful for creating required entities in database during first run.
|
2021-12-18 14:16:34 +01:00
|
|
|
IsInitialized() (bool, error)
|
2022-01-23 14:03:23 +01:00
|
|
|
// IsMigrationApplied queries the database to see if a migration table with
|
|
|
|
// this version id exists already
|
|
|
|
IsMigrationApplied(version Migration) (bool, error)
|
|
|
|
// ApplyMigration runs executes a database migration
|
|
|
|
ApplyMigration(version Migration) error
|
2022-02-03 08:05:13 +01:00
|
|
|
// TryRollbackMigration attempts to roll back the database to an earlier version
|
2022-01-23 14:03:23 +01:00
|
|
|
// if a rollback exists
|
|
|
|
TryRollbackMigration(version Migration)
|
2020-12-01 20:06:49 +01:00
|
|
|
|
2024-07-07 19:12:21 +02:00
|
|
|
GetOptions(params RetrieveQueryParams) (map[string]string, error)
|
2024-03-10 18:55:42 +01:00
|
|
|
GetOption(key string) (string, error)
|
|
|
|
SetOption(key string, value string) error
|
2024-07-08 13:15:04 +02:00
|
|
|
DeleteOption(key string) error
|
|
|
|
DeleteOptions(filter string) error
|
2024-03-10 18:55:42 +01:00
|
|
|
|
2020-12-04 23:41:26 +01:00
|
|
|
GetEnvironment(projectID int, environmentID int) (Environment, error)
|
2022-02-03 08:05:13 +01:00
|
|
|
GetEnvironmentRefs(projectID int, environmentID int) (ObjectReferrers, error)
|
2020-12-04 23:41:26 +01:00
|
|
|
GetEnvironments(projectID int, params RetrieveQueryParams) ([]Environment, error)
|
|
|
|
UpdateEnvironment(env Environment) error
|
|
|
|
CreateEnvironment(env Environment) (Environment, error)
|
2020-12-04 17:29:37 +01:00
|
|
|
DeleteEnvironment(projectID int, templateID int) error
|
2024-07-02 19:23:45 +02:00
|
|
|
GetEnvironmentSecrets(projectID int, environmentID int) ([]AccessKey, error)
|
2020-12-03 14:51:15 +01:00
|
|
|
|
2020-12-07 13:13:59 +01:00
|
|
|
GetInventory(projectID int, inventoryID int) (Inventory, error)
|
2022-02-03 08:05:13 +01:00
|
|
|
GetInventoryRefs(projectID int, inventoryID int) (ObjectReferrers, error)
|
2020-12-07 13:13:59 +01:00
|
|
|
GetInventories(projectID int, params RetrieveQueryParams) ([]Inventory, error)
|
|
|
|
UpdateInventory(inventory Inventory) error
|
|
|
|
CreateInventory(inventory Inventory) (Inventory, error)
|
2020-12-07 20:48:52 +01:00
|
|
|
DeleteInventory(projectID int, inventoryID int) error
|
|
|
|
|
|
|
|
GetRepository(projectID int, repositoryID int) (Repository, error)
|
2022-02-03 08:05:13 +01:00
|
|
|
GetRepositoryRefs(projectID int, repositoryID int) (ObjectReferrers, error)
|
2020-12-07 20:48:52 +01:00
|
|
|
GetRepositories(projectID int, params RetrieveQueryParams) ([]Repository, error)
|
|
|
|
UpdateRepository(repository Repository) error
|
|
|
|
CreateRepository(repository Repository) (Repository, error)
|
|
|
|
DeleteRepository(projectID int, repositoryID int) error
|
|
|
|
|
2021-09-10 00:41:36 +02:00
|
|
|
GetAccessKey(projectID int, accessKeyID int) (AccessKey, error)
|
2022-02-03 08:05:13 +01:00
|
|
|
GetAccessKeyRefs(projectID int, accessKeyID int) (ObjectReferrers, error)
|
2020-12-08 08:23:33 +01:00
|
|
|
GetAccessKeys(projectID int, params RetrieveQueryParams) ([]AccessKey, error)
|
2023-09-09 14:41:41 +02:00
|
|
|
RekeyAccessKeys(oldKey string) error
|
2021-09-09 23:31:06 +02:00
|
|
|
|
2024-02-11 20:52:14 +01:00
|
|
|
CreateIntegration(integration Integration) (newIntegration Integration, err error)
|
|
|
|
GetIntegrations(projectID int, params RetrieveQueryParams) ([]Integration, error)
|
|
|
|
GetIntegration(projectID int, integrationID int) (integration Integration, err error)
|
|
|
|
UpdateIntegration(integration Integration) error
|
|
|
|
GetIntegrationRefs(projectID int, integrationID int) (IntegrationReferrers, error)
|
|
|
|
DeleteIntegration(projectID int, integrationID int) error
|
|
|
|
|
2024-03-04 12:24:47 +01:00
|
|
|
CreateIntegrationExtractValue(projectId int, value IntegrationExtractValue) (newValue IntegrationExtractValue, err error)
|
2024-03-06 14:28:24 +01:00
|
|
|
GetIntegrationExtractValues(projectID int, params RetrieveQueryParams, integrationID int) ([]IntegrationExtractValue, error)
|
|
|
|
GetIntegrationExtractValue(projectID int, valueID int, integrationID int) (value IntegrationExtractValue, err error)
|
2024-03-04 12:24:47 +01:00
|
|
|
UpdateIntegrationExtractValue(projectID int, integrationExtractValue IntegrationExtractValue) error
|
2024-03-06 14:28:24 +01:00
|
|
|
GetIntegrationExtractValueRefs(projectID int, valueID int, integrationID int) (IntegrationExtractorChildReferrers, error)
|
|
|
|
DeleteIntegrationExtractValue(projectID int, valueID int, integrationID int) error
|
2024-02-11 20:52:14 +01:00
|
|
|
|
2024-03-04 12:36:24 +01:00
|
|
|
CreateIntegrationMatcher(projectID int, matcher IntegrationMatcher) (newMatcher IntegrationMatcher, err error)
|
2024-03-06 14:28:24 +01:00
|
|
|
GetIntegrationMatchers(projectID int, params RetrieveQueryParams, integrationID int) ([]IntegrationMatcher, error)
|
|
|
|
GetIntegrationMatcher(projectID int, matcherID int, integrationID int) (matcher IntegrationMatcher, err error)
|
2024-03-04 12:36:24 +01:00
|
|
|
UpdateIntegrationMatcher(projectID int, integrationMatcher IntegrationMatcher) error
|
2024-03-06 14:28:24 +01:00
|
|
|
GetIntegrationMatcherRefs(projectID int, matcherID int, integrationID int) (IntegrationExtractorChildReferrers, error)
|
|
|
|
DeleteIntegrationMatcher(projectID int, matcherID int, integrationID int) error
|
2023-07-03 01:41:13 +02:00
|
|
|
|
2024-03-07 10:32:25 +01:00
|
|
|
CreateIntegrationAlias(alias IntegrationAlias) (IntegrationAlias, error)
|
2024-03-20 22:35:47 +01:00
|
|
|
GetIntegrationAliases(projectID int, integrationID *int) ([]IntegrationAlias, error)
|
2024-03-21 00:53:54 +01:00
|
|
|
GetIntegrationsByAlias(alias string) ([]Integration, error)
|
2024-03-20 22:35:47 +01:00
|
|
|
DeleteIntegrationAlias(projectID int, aliasID int) error
|
2024-03-23 13:37:15 +01:00
|
|
|
GetAllSearchableIntegrations() ([]Integration, error)
|
2024-03-07 10:32:25 +01:00
|
|
|
|
2020-12-08 08:23:33 +01:00
|
|
|
UpdateAccessKey(accessKey AccessKey) error
|
|
|
|
CreateAccessKey(accessKey AccessKey) (AccessKey, error)
|
|
|
|
DeleteAccessKey(projectID int, accessKeyID int) error
|
|
|
|
|
2024-04-25 13:28:28 +02:00
|
|
|
GetUserCount() (int, error)
|
2020-12-04 23:41:26 +01:00
|
|
|
GetUsers(params RetrieveQueryParams) ([]User, error)
|
2021-05-06 14:41:31 +02:00
|
|
|
CreateUserWithoutPassword(user User) (User, error)
|
2021-03-12 22:13:39 +01:00
|
|
|
CreateUser(user UserWithPwd) (User, error)
|
2020-12-01 20:06:49 +01:00
|
|
|
DeleteUser(userID int) error
|
2021-08-28 13:44:41 +02:00
|
|
|
|
|
|
|
// UpdateUser updates all fields of the entity except Pwd.
|
|
|
|
// Pwd should be present of you want update user password. Empty Pwd ignored.
|
2021-03-12 22:13:39 +01:00
|
|
|
UpdateUser(user UserWithPwd) error
|
2020-12-01 20:06:49 +01:00
|
|
|
SetUserPassword(userID int, password string) error
|
2020-12-04 23:41:26 +01:00
|
|
|
GetUser(userID int) (User, error)
|
2020-12-17 15:00:05 +01:00
|
|
|
GetUserByLoginOrEmail(login string, email string) (User, error)
|
2020-12-01 20:06:49 +01:00
|
|
|
|
2020-12-16 20:19:20 +01:00
|
|
|
GetProject(projectID int) (Project, error)
|
2023-09-17 16:15:44 +02:00
|
|
|
GetAllProjects() ([]Project, error)
|
2020-12-16 20:19:20 +01:00
|
|
|
GetProjects(userID int) ([]Project, error)
|
2020-12-04 23:41:26 +01:00
|
|
|
CreateProject(project Project) (Project, error)
|
2020-12-16 20:19:20 +01:00
|
|
|
DeleteProject(projectID int) error
|
|
|
|
UpdateProject(project Project) error
|
|
|
|
|
2022-01-19 20:35:59 +01:00
|
|
|
GetTemplates(projectID int, filter TemplateFilter, params RetrieveQueryParams) ([]Template, error)
|
2022-02-04 18:35:08 +01:00
|
|
|
GetTemplateRefs(projectID int, templateID int) (ObjectReferrers, error)
|
2020-12-04 23:41:26 +01:00
|
|
|
CreateTemplate(template Template) (Template, error)
|
|
|
|
UpdateTemplate(template Template) error
|
|
|
|
GetTemplate(projectID int, templateID int) (Template, error)
|
2020-12-03 14:51:15 +01:00
|
|
|
DeleteTemplate(projectID int, templateID int) error
|
|
|
|
|
2021-09-06 13:05:10 +02:00
|
|
|
GetSchedules() ([]Schedule, error)
|
2024-06-23 19:24:22 +02:00
|
|
|
GetProjectSchedules(projectID int) ([]ScheduleWithTpl, error)
|
2021-09-06 13:05:10 +02:00
|
|
|
GetTemplateSchedules(projectID int, templateID int) ([]Schedule, error)
|
|
|
|
CreateSchedule(schedule Schedule) (Schedule, error)
|
|
|
|
UpdateSchedule(schedule Schedule) error
|
2022-01-30 18:43:15 +01:00
|
|
|
SetScheduleCommitHash(projectID int, scheduleID int, hash string) error
|
2024-06-30 23:12:49 +02:00
|
|
|
SetScheduleActive(projectID int, scheduleID int, active bool) error
|
2021-09-06 13:05:10 +02:00
|
|
|
GetSchedule(projectID int, scheduleID int) (Schedule, error)
|
|
|
|
DeleteSchedule(projectID int, scheduleID int) error
|
|
|
|
|
2024-04-02 23:50:52 +02:00
|
|
|
GetAllAdmins() ([]User, error)
|
2023-07-08 12:41:57 +02:00
|
|
|
GetProjectUsers(projectID int, params RetrieveQueryParams) ([]UserWithProjectRole, error)
|
2020-12-04 23:41:26 +01:00
|
|
|
CreateProjectUser(projectUser ProjectUser) (ProjectUser, error)
|
2020-12-17 15:00:05 +01:00
|
|
|
DeleteProjectUser(projectID int, userID int) error
|
|
|
|
GetProjectUser(projectID int, userID int) (ProjectUser, error)
|
|
|
|
UpdateProjectUser(projectUser ProjectUser) error
|
2020-12-03 14:51:15 +01:00
|
|
|
|
2020-12-04 23:41:26 +01:00
|
|
|
CreateEvent(event Event) (Event, error)
|
2020-12-20 19:00:59 +01:00
|
|
|
GetUserEvents(userID int, params RetrieveQueryParams) ([]Event, error)
|
|
|
|
GetEvents(projectID int, params RetrieveQueryParams) ([]Event, error)
|
2020-12-01 20:06:49 +01:00
|
|
|
|
2020-12-04 23:41:26 +01:00
|
|
|
GetAPITokens(userID int) ([]APIToken, error)
|
|
|
|
CreateAPIToken(token APIToken) (APIToken, error)
|
|
|
|
GetAPIToken(tokenID string) (APIToken, error)
|
2020-12-03 14:51:15 +01:00
|
|
|
ExpireAPIToken(userID int, tokenID string) error
|
2022-11-09 09:10:42 +01:00
|
|
|
DeleteAPIToken(userID int, tokenID string) error
|
2020-12-03 14:51:15 +01:00
|
|
|
|
2020-12-04 23:41:26 +01:00
|
|
|
GetSession(userID int, sessionID int) (Session, error)
|
2021-03-12 18:41:41 +01:00
|
|
|
CreateSession(session Session) (Session, error)
|
2020-12-03 14:51:15 +01:00
|
|
|
ExpireSession(userID int, sessionID int) error
|
|
|
|
TouchSession(userID int, sessionID int) error
|
|
|
|
|
2024-06-30 09:48:36 +02:00
|
|
|
CreateTask(task Task, maxTasks int) (Task, error)
|
2021-03-12 18:41:41 +01:00
|
|
|
UpdateTask(task Task) error
|
|
|
|
|
2024-07-19 21:38:35 +02:00
|
|
|
GetTemplateTasks(projectID int, templateID int, params RetrieveQueryParams) ([]TaskWithTpl, error)
|
2021-03-12 18:41:41 +01:00
|
|
|
GetProjectTasks(projectID int, params RetrieveQueryParams) ([]TaskWithTpl, error)
|
|
|
|
GetTask(projectID int, taskID int) (Task, error)
|
|
|
|
DeleteTaskWithOutputs(projectID int, taskID int) error
|
|
|
|
GetTaskOutputs(projectID int, taskID int) ([]TaskOutput, error)
|
|
|
|
CreateTaskOutput(output TaskOutput) (TaskOutput, error)
|
2024-06-17 20:37:45 +02:00
|
|
|
GetTaskStages(projectID int, taskID int) ([]TaskStage, error)
|
|
|
|
CreateTaskStage(stage TaskStage) (TaskStage, error)
|
2021-10-26 20:19:12 +02:00
|
|
|
|
|
|
|
GetView(projectID int, viewID int) (View, error)
|
|
|
|
GetViews(projectID int) ([]View, error)
|
|
|
|
UpdateView(view View) error
|
|
|
|
CreateView(view View) (View, error)
|
|
|
|
DeleteView(projectID int, viewID int) error
|
2021-10-27 13:43:04 +02:00
|
|
|
SetViewPositions(projectID int, viewPositions map[int]int) error
|
2023-08-27 18:02:51 +02:00
|
|
|
|
|
|
|
GetRunner(projectID int, runnerID int) (Runner, error)
|
|
|
|
GetRunners(projectID int) ([]Runner, error)
|
|
|
|
DeleteRunner(projectID int, runnerID int) error
|
|
|
|
GetGlobalRunner(runnerID int) (Runner, error)
|
|
|
|
GetGlobalRunners() ([]Runner, error)
|
|
|
|
DeleteGlobalRunner(runnerID int) error
|
|
|
|
UpdateRunner(runner Runner) error
|
|
|
|
CreateRunner(runner Runner) (Runner, error)
|
2020-12-01 20:06:49 +01:00
|
|
|
}
|
2020-12-07 20:48:52 +01:00
|
|
|
|
2022-02-03 08:05:13 +01:00
|
|
|
var AccessKeyProps = ObjectProps{
|
|
|
|
TableName: "access_key",
|
|
|
|
Type: reflect.TypeOf(AccessKey{}),
|
|
|
|
PrimaryColumnName: "id",
|
|
|
|
ReferringColumnSuffix: "key_id",
|
|
|
|
SortableColumns: []string{"name", "type"},
|
|
|
|
DefaultSortingColumn: "name",
|
2021-05-07 12:08:34 +02:00
|
|
|
}
|
|
|
|
|
2024-02-11 20:52:14 +01:00
|
|
|
var IntegrationProps = ObjectProps{
|
|
|
|
TableName: "project__integration",
|
|
|
|
Type: reflect.TypeOf(Integration{}),
|
2023-07-03 01:41:13 +02:00
|
|
|
PrimaryColumnName: "id",
|
2024-03-03 18:01:18 +01:00
|
|
|
ReferringColumnSuffix: "integration_id",
|
2023-07-03 01:41:13 +02:00
|
|
|
SortableColumns: []string{"name"},
|
|
|
|
DefaultSortingColumn: "name",
|
|
|
|
}
|
|
|
|
|
2024-02-11 20:52:14 +01:00
|
|
|
var IntegrationExtractValueProps = ObjectProps{
|
2024-03-06 14:40:50 +01:00
|
|
|
TableName: "project__integration_extract_value",
|
|
|
|
Type: reflect.TypeOf(IntegrationExtractValue{}),
|
|
|
|
PrimaryColumnName: "id",
|
2024-03-03 18:01:18 +01:00
|
|
|
SortableColumns: []string{"name"},
|
|
|
|
DefaultSortingColumn: "name",
|
2023-07-03 01:41:13 +02:00
|
|
|
}
|
|
|
|
|
2024-02-11 20:52:14 +01:00
|
|
|
var IntegrationMatcherProps = ObjectProps{
|
2024-03-06 14:40:50 +01:00
|
|
|
TableName: "project__integration_matcher",
|
|
|
|
Type: reflect.TypeOf(IntegrationMatcher{}),
|
|
|
|
PrimaryColumnName: "id",
|
2024-03-03 18:01:18 +01:00
|
|
|
SortableColumns: []string{"name"},
|
|
|
|
DefaultSortingColumn: "name",
|
2023-07-03 01:41:13 +02:00
|
|
|
}
|
|
|
|
|
2024-03-07 10:32:25 +01:00
|
|
|
var IntegrationAliasProps = ObjectProps{
|
|
|
|
TableName: "project__integration_alias",
|
|
|
|
Type: reflect.TypeOf(IntegrationAlias{}),
|
|
|
|
PrimaryColumnName: "id",
|
|
|
|
}
|
|
|
|
|
2022-02-03 08:05:13 +01:00
|
|
|
var EnvironmentProps = ObjectProps{
|
|
|
|
TableName: "project__environment",
|
|
|
|
Type: reflect.TypeOf(Environment{}),
|
|
|
|
PrimaryColumnName: "id",
|
|
|
|
ReferringColumnSuffix: "environment_id",
|
|
|
|
SortableColumns: []string{"name"},
|
|
|
|
DefaultSortingColumn: "name",
|
2021-05-07 12:08:34 +02:00
|
|
|
}
|
|
|
|
|
2022-02-03 08:05:13 +01:00
|
|
|
var InventoryProps = ObjectProps{
|
|
|
|
TableName: "project__inventory",
|
|
|
|
Type: reflect.TypeOf(Inventory{}),
|
|
|
|
PrimaryColumnName: "id",
|
|
|
|
ReferringColumnSuffix: "inventory_id",
|
|
|
|
SortableColumns: []string{"name"},
|
|
|
|
DefaultSortingColumn: "name",
|
2021-05-07 12:08:34 +02:00
|
|
|
}
|
|
|
|
|
2022-02-03 08:05:13 +01:00
|
|
|
var RepositoryProps = ObjectProps{
|
|
|
|
TableName: "project__repository",
|
|
|
|
Type: reflect.TypeOf(Repository{}),
|
|
|
|
PrimaryColumnName: "id",
|
|
|
|
ReferringColumnSuffix: "repository_id",
|
|
|
|
DefaultSortingColumn: "name",
|
2021-05-07 12:08:34 +02:00
|
|
|
}
|
2020-12-07 20:48:52 +01:00
|
|
|
|
2022-02-03 08:05:13 +01:00
|
|
|
var TemplateProps = ObjectProps{
|
2022-02-04 18:35:08 +01:00
|
|
|
TableName: "project__template",
|
|
|
|
Type: reflect.TypeOf(Template{}),
|
|
|
|
PrimaryColumnName: "id",
|
|
|
|
ReferringColumnSuffix: "template_id",
|
|
|
|
SortableColumns: []string{"name"},
|
|
|
|
DefaultSortingColumn: "name",
|
2021-05-08 22:25:31 +02:00
|
|
|
}
|
|
|
|
|
2022-02-03 08:05:13 +01:00
|
|
|
var ScheduleProps = ObjectProps{
|
2021-09-06 13:05:10 +02:00
|
|
|
TableName: "project__schedule",
|
2021-09-16 22:51:53 +02:00
|
|
|
Type: reflect.TypeOf(Schedule{}),
|
2022-02-03 08:05:13 +01:00
|
|
|
PrimaryColumnName: "id",
|
2021-09-06 13:05:10 +02:00
|
|
|
}
|
|
|
|
|
2022-02-03 08:05:13 +01:00
|
|
|
var ProjectUserProps = ObjectProps{
|
2021-06-24 19:45:28 +02:00
|
|
|
TableName: "project__user",
|
2021-09-16 22:51:53 +02:00
|
|
|
Type: reflect.TypeOf(ProjectUser{}),
|
2022-02-03 08:05:13 +01:00
|
|
|
PrimaryColumnName: "user_id",
|
2021-05-09 22:08:10 +02:00
|
|
|
}
|
|
|
|
|
2022-02-03 08:05:13 +01:00
|
|
|
var ProjectProps = ObjectProps{
|
2024-02-11 20:52:14 +01:00
|
|
|
TableName: "project",
|
|
|
|
Type: reflect.TypeOf(Project{}),
|
|
|
|
PrimaryColumnName: "id",
|
2023-07-03 01:41:13 +02:00
|
|
|
ReferringColumnSuffix: "project_id",
|
2024-02-11 20:52:14 +01:00
|
|
|
DefaultSortingColumn: "name",
|
|
|
|
IsGlobal: true,
|
2021-05-09 22:08:10 +02:00
|
|
|
}
|
|
|
|
|
2022-02-03 08:05:13 +01:00
|
|
|
var UserProps = ObjectProps{
|
2021-06-24 19:45:28 +02:00
|
|
|
TableName: "user",
|
2021-09-16 22:51:53 +02:00
|
|
|
Type: reflect.TypeOf(User{}),
|
2022-02-03 08:05:13 +01:00
|
|
|
PrimaryColumnName: "id",
|
|
|
|
IsGlobal: true,
|
2021-05-09 22:08:10 +02:00
|
|
|
}
|
|
|
|
|
2022-02-03 08:05:13 +01:00
|
|
|
var SessionProps = ObjectProps{
|
2021-06-24 19:45:28 +02:00
|
|
|
TableName: "session",
|
2021-09-16 22:51:53 +02:00
|
|
|
Type: reflect.TypeOf(Session{}),
|
2022-02-03 08:05:13 +01:00
|
|
|
PrimaryColumnName: "id",
|
2021-05-09 22:08:10 +02:00
|
|
|
}
|
|
|
|
|
2022-02-03 08:05:13 +01:00
|
|
|
var TokenProps = ObjectProps{
|
2021-06-24 19:45:28 +02:00
|
|
|
TableName: "user__token",
|
2022-01-24 21:14:30 +01:00
|
|
|
Type: reflect.TypeOf(APIToken{}),
|
2022-02-03 08:05:13 +01:00
|
|
|
PrimaryColumnName: "id",
|
2021-05-09 22:08:10 +02:00
|
|
|
}
|
2021-05-13 15:49:32 +02:00
|
|
|
|
2022-02-03 08:05:13 +01:00
|
|
|
var TaskProps = ObjectProps{
|
2021-06-24 19:45:28 +02:00
|
|
|
TableName: "task",
|
2022-02-03 08:05:13 +01:00
|
|
|
Type: reflect.TypeOf(Task{}),
|
2021-05-16 23:44:42 +02:00
|
|
|
PrimaryColumnName: "id",
|
2022-02-03 08:05:13 +01:00
|
|
|
IsGlobal: true,
|
2021-06-24 21:53:36 +02:00
|
|
|
SortInverted: true,
|
2021-05-13 15:49:32 +02:00
|
|
|
}
|
|
|
|
|
2022-02-03 08:05:13 +01:00
|
|
|
var TaskOutputProps = ObjectProps{
|
2021-09-16 22:51:53 +02:00
|
|
|
TableName: "task__output",
|
|
|
|
Type: reflect.TypeOf(TaskOutput{}),
|
2021-05-13 15:49:32 +02:00
|
|
|
}
|
2021-10-27 13:43:04 +02:00
|
|
|
|
2024-06-17 20:37:45 +02:00
|
|
|
var TaskStageProps = ObjectProps{
|
|
|
|
TableName: "task__stage",
|
|
|
|
Type: reflect.TypeOf(TaskStage{}),
|
|
|
|
}
|
|
|
|
|
2022-02-03 08:05:13 +01:00
|
|
|
var ViewProps = ObjectProps{
|
2021-10-27 18:22:52 +02:00
|
|
|
TableName: "project__view",
|
|
|
|
Type: reflect.TypeOf(View{}),
|
2022-02-03 08:05:13 +01:00
|
|
|
PrimaryColumnName: "id",
|
2021-10-27 18:22:52 +02:00
|
|
|
DefaultSortingColumn: "position",
|
2021-10-27 13:43:04 +02:00
|
|
|
}
|
2022-02-03 08:05:13 +01:00
|
|
|
|
2023-09-11 01:02:10 +02:00
|
|
|
var GlobalRunnerProps = ObjectProps{
|
2023-08-27 18:02:51 +02:00
|
|
|
TableName: "runner",
|
|
|
|
Type: reflect.TypeOf(Runner{}),
|
|
|
|
PrimaryColumnName: "id",
|
|
|
|
IsGlobal: true,
|
|
|
|
}
|
|
|
|
|
2024-03-10 18:55:42 +01:00
|
|
|
var OptionProps = ObjectProps{
|
|
|
|
TableName: "option",
|
|
|
|
Type: reflect.TypeOf(Option{}),
|
|
|
|
PrimaryColumnName: "key",
|
|
|
|
IsGlobal: true,
|
|
|
|
}
|
|
|
|
|
2022-02-03 08:05:13 +01:00
|
|
|
func (p ObjectProps) GetReferringFieldsFrom(t reflect.Type) (fields []string, err error) {
|
|
|
|
n := t.NumField()
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
if !strings.HasSuffix(t.Field(i).Tag.Get("db"), p.ReferringColumnSuffix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fields = append(fields, t.Field(i).Tag.Get("db"))
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
if t.Field(i).Tag != "" || t.Field(i).Type.Kind() != reflect.Struct {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var nested []string
|
|
|
|
nested, err = p.GetReferringFieldsFrom(t.Field(i).Type)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fields = append(fields, nested...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2022-11-18 23:23:30 +01:00
|
|
|
|
|
|
|
func StoreSession(store Store, token string, callback func()) {
|
|
|
|
if !store.PermanentConnection() {
|
|
|
|
store.Connect(token)
|
|
|
|
}
|
|
|
|
|
|
|
|
callback()
|
|
|
|
|
|
|
|
if !store.PermanentConnection() {
|
|
|
|
store.Close(token)
|
|
|
|
}
|
|
|
|
}
|
2024-03-18 15:33:40 +01:00
|
|
|
|
2024-03-18 15:37:40 +01:00
|
|
|
func ValidateRepository(store Store, repo *Repository) (err error) {
|
|
|
|
_, err = store.GetAccessKey(repo.ProjectID, repo.SSHKeyID)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-18 15:33:40 +01:00
|
|
|
func ValidateInventory(store Store, inventory *Inventory) (err error) {
|
|
|
|
if inventory.SSHKeyID != nil {
|
|
|
|
_, err = store.GetAccessKey(inventory.ProjectID, *inventory.SSHKeyID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if inventory.BecomeKeyID != nil {
|
|
|
|
_, err = store.GetAccessKey(inventory.ProjectID, *inventory.BecomeKeyID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if inventory.HolderID != nil {
|
|
|
|
_, err = store.GetTemplate(inventory.ProjectID, *inventory.HolderID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2024-07-15 10:57:12 +02:00
|
|
|
|
|
|
|
type MapStringAnyField map[string]interface{}
|
|
|
|
|
|
|
|
func (m *MapStringAnyField) Scan(value interface{}) error {
|
|
|
|
if value == nil {
|
|
|
|
*m = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v := value.(type) {
|
|
|
|
case []byte:
|
|
|
|
return json.Unmarshal(v, m)
|
|
|
|
case string:
|
|
|
|
return json.Unmarshal([]byte(v), m)
|
|
|
|
default:
|
|
|
|
return errors.New("unsupported type for MapStringAnyField")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value implements the driver.Valuer interface for MapStringAnyField
|
|
|
|
func (m MapStringAnyField) Value() (driver.Value, error) {
|
|
|
|
if m == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return json.Marshal(m)
|
|
|
|
}
|