2023-07-03 01:41:13 +02:00
|
|
|
package sql
|
|
|
|
|
|
|
|
import (
|
2024-03-20 14:55:47 +01:00
|
|
|
"database/sql"
|
2024-03-03 11:57:39 +01:00
|
|
|
"github.com/Masterminds/squirrel"
|
2024-10-26 14:56:17 +02:00
|
|
|
"github.com/semaphoreui/semaphore/db"
|
2023-07-03 01:41:13 +02:00
|
|
|
)
|
|
|
|
|
2024-02-11 20:52:14 +01:00
|
|
|
func (d *SqlDb) CreateIntegration(integration db.Integration) (newIntegration db.Integration, err error) {
|
|
|
|
err = integration.Validate()
|
2023-07-03 01:41:13 +02:00
|
|
|
|
2024-01-10 07:49:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-07-03 01:41:13 +02:00
|
|
|
|
2024-01-10 07:49:39 +01:00
|
|
|
insertID, err := d.insert(
|
|
|
|
"id",
|
2024-02-12 10:50:01 +01:00
|
|
|
"insert into project__integration "+
|
2024-03-21 00:17:34 +01:00
|
|
|
"(project_id, name, template_id, auth_method, auth_secret_id, auth_header, searchable) values "+
|
2024-03-21 00:17:34 +01:00
|
|
|
"(?, ?, ?, ?, ?, ?, ?)",
|
2024-02-11 20:52:14 +01:00
|
|
|
integration.ProjectID,
|
|
|
|
integration.Name,
|
2024-02-12 10:50:01 +01:00
|
|
|
integration.TemplateID,
|
|
|
|
integration.AuthMethod,
|
|
|
|
integration.AuthSecretID,
|
2024-03-21 00:17:34 +01:00
|
|
|
integration.AuthHeader,
|
|
|
|
integration.Searchable)
|
2023-07-03 01:41:13 +02:00
|
|
|
|
2024-01-10 07:49:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-07-03 01:41:13 +02:00
|
|
|
|
2024-02-11 20:52:14 +01:00
|
|
|
newIntegration = integration
|
|
|
|
newIntegration.ID = insertID
|
2023-07-03 01:41:13 +02:00
|
|
|
|
2024-01-10 07:49:39 +01:00
|
|
|
return
|
2023-07-03 01:41:13 +02:00
|
|
|
}
|
|
|
|
|
2024-02-11 20:52:14 +01:00
|
|
|
func (d *SqlDb) GetIntegrations(projectID int, params db.RetrieveQueryParams) (integrations []db.Integration, err error) {
|
2024-07-07 19:12:21 +02:00
|
|
|
err = d.getObjects(projectID, db.IntegrationProps, params, nil, &integrations)
|
2024-02-11 20:52:14 +01:00
|
|
|
return integrations, err
|
2023-07-03 01:41:13 +02:00
|
|
|
}
|
|
|
|
|
2024-02-11 20:52:14 +01:00
|
|
|
func (d *SqlDb) GetIntegration(projectID int, integrationID int) (integration db.Integration, err error) {
|
|
|
|
err = d.getObject(projectID, db.IntegrationProps, integrationID, &integration)
|
2024-01-10 07:49:39 +01:00
|
|
|
return
|
2023-07-03 01:41:13 +02:00
|
|
|
}
|
|
|
|
|
2024-02-11 20:52:14 +01:00
|
|
|
func (d *SqlDb) GetIntegrationRefs(projectID int, integrationID int) (referrers db.IntegrationReferrers, err error) {
|
2024-03-06 14:28:24 +01:00
|
|
|
//var extractorReferrer []db.ObjectReferrer
|
|
|
|
//extractorReferrer, err = d.GetObjectReferences(db.IntegrationProps, db.IntegrationExtractorProps, integrationID)
|
|
|
|
//referrers = db.IntegrationReferrers{
|
|
|
|
// IntegrationExtractors: extractorReferrer,
|
|
|
|
//}
|
2024-01-10 07:49:39 +01:00
|
|
|
return
|
2023-07-03 01:41:13 +02:00
|
|
|
}
|
|
|
|
|
2024-02-11 20:52:14 +01:00
|
|
|
func (d *SqlDb) DeleteIntegration(projectID int, integrationID int) error {
|
|
|
|
return d.deleteObject(projectID, db.IntegrationProps, integrationID)
|
2023-07-03 01:41:13 +02:00
|
|
|
}
|
|
|
|
|
2024-02-11 20:52:14 +01:00
|
|
|
func (d *SqlDb) UpdateIntegration(integration db.Integration) error {
|
|
|
|
err := integration.Validate()
|
2023-07-03 01:41:13 +02:00
|
|
|
|
2024-01-10 07:49:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-03 01:41:13 +02:00
|
|
|
|
2024-01-10 07:49:39 +01:00
|
|
|
_, err = d.exec(
|
2024-03-21 00:17:34 +01:00
|
|
|
"update project__integration set `name`=?, template_id=?, auth_method=?, auth_secret_id=?, auth_header=?, searchable=? where `id`=?",
|
2024-02-11 20:52:14 +01:00
|
|
|
integration.Name,
|
|
|
|
integration.TemplateID,
|
2024-02-12 10:50:01 +01:00
|
|
|
integration.AuthMethod,
|
|
|
|
integration.AuthSecretID,
|
2024-03-03 19:49:00 +01:00
|
|
|
integration.AuthHeader,
|
2024-03-21 00:17:34 +01:00
|
|
|
integration.Searchable,
|
2024-03-03 19:49:00 +01:00
|
|
|
integration.ID)
|
2023-07-03 01:41:13 +02:00
|
|
|
|
2024-01-10 07:49:39 +01:00
|
|
|
return err
|
2023-07-03 01:41:13 +02:00
|
|
|
}
|
|
|
|
|
2024-03-04 12:24:47 +01:00
|
|
|
func (d *SqlDb) CreateIntegrationExtractValue(projectId int, value db.IntegrationExtractValue) (newValue db.IntegrationExtractValue, err error) {
|
2024-01-10 07:49:39 +01:00
|
|
|
err = value.Validate()
|
2023-07-03 01:41:13 +02:00
|
|
|
|
2024-01-10 07:49:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-07-03 01:41:13 +02:00
|
|
|
|
2024-01-10 07:49:39 +01:00
|
|
|
insertID, err := d.insert("id",
|
2024-02-11 23:39:57 +01:00
|
|
|
"insert into project__integration_extract_value "+
|
2024-03-06 14:28:24 +01:00
|
|
|
"(value_source, body_data_type, `key`, `variable`, `name`, integration_id) values "+
|
2024-02-11 23:39:57 +01:00
|
|
|
"(?, ?, ?, ?, ?, ?)",
|
2024-01-10 07:49:39 +01:00
|
|
|
value.ValueSource,
|
|
|
|
value.BodyDataType,
|
|
|
|
value.Key,
|
|
|
|
value.Variable,
|
|
|
|
value.Name,
|
2024-03-06 14:28:24 +01:00
|
|
|
value.IntegrationID)
|
2024-01-10 07:49:39 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-07-03 01:41:13 +02:00
|
|
|
|
2024-01-10 07:49:39 +01:00
|
|
|
newValue = value
|
|
|
|
newValue.ID = insertID
|
2023-07-03 01:41:13 +02:00
|
|
|
|
2024-01-10 07:49:39 +01:00
|
|
|
return
|
2023-07-03 01:41:13 +02:00
|
|
|
}
|
|
|
|
|
2024-03-06 15:51:59 +01:00
|
|
|
func (d *SqlDb) GetIntegrationExtractValues(projectID int, params db.RetrieveQueryParams, integrationID int) ([]db.IntegrationExtractValue, error) {
|
2024-02-11 20:52:14 +01:00
|
|
|
var values []db.IntegrationExtractValue
|
2024-03-06 15:51:59 +01:00
|
|
|
err := d.getObjectsByReferrer(integrationID, db.IntegrationProps, db.IntegrationExtractValueProps, params, &values)
|
2024-01-10 07:49:39 +01:00
|
|
|
return values, err
|
2023-07-03 01:41:13 +02:00
|
|
|
}
|
|
|
|
|
2024-03-06 15:51:59 +01:00
|
|
|
func (d *SqlDb) GetIntegrationExtractValue(projectID int, valueID int, integrationID int) (value db.IntegrationExtractValue, err error) {
|
2024-01-10 07:49:39 +01:00
|
|
|
query, args, err := squirrel.Select("v.*").
|
2024-02-11 20:52:14 +01:00
|
|
|
From("project__integration_extract_value as v").
|
2024-01-10 07:49:39 +01:00
|
|
|
Where(squirrel.Eq{"id": valueID}).
|
|
|
|
OrderBy("v.id").
|
|
|
|
ToSql()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-07-03 01:41:13 +02:00
|
|
|
|
2024-01-10 07:49:39 +01:00
|
|
|
err = d.selectOne(&value, query, args...)
|
2023-07-03 01:41:13 +02:00
|
|
|
|
2024-01-10 07:49:39 +01:00
|
|
|
return value, err
|
2023-07-03 01:41:13 +02:00
|
|
|
}
|
|
|
|
|
2024-03-06 15:51:59 +01:00
|
|
|
func (d *SqlDb) GetIntegrationExtractValueRefs(projectID int, valueID int, integrationID int) (refs db.IntegrationExtractorChildReferrers, err error) {
|
|
|
|
refs.Integrations, err = d.GetObjectReferences(db.IntegrationProps, db.IntegrationExtractValueProps, integrationID)
|
2024-01-10 07:49:39 +01:00
|
|
|
return
|
2023-07-03 01:41:13 +02:00
|
|
|
}
|
|
|
|
|
2024-03-06 15:51:59 +01:00
|
|
|
func (d *SqlDb) DeleteIntegrationExtractValue(projectID int, valueID int, integrationID int) error {
|
|
|
|
return d.deleteObjectByReferencedID(integrationID, db.IntegrationProps, db.IntegrationExtractValueProps, valueID)
|
2023-07-03 01:41:13 +02:00
|
|
|
}
|
|
|
|
|
2024-03-04 12:24:47 +01:00
|
|
|
func (d *SqlDb) UpdateIntegrationExtractValue(projectID int, integrationExtractValue db.IntegrationExtractValue) error {
|
2024-02-11 20:52:14 +01:00
|
|
|
err := integrationExtractValue.Validate()
|
2024-01-10 07:49:39 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = d.exec(
|
2024-03-03 16:39:18 +01:00
|
|
|
"update project__integration_extract_value set value_source=?, body_data_type=?, `key`=?, `variable`=?, `name`=? where `id`=?",
|
2024-02-11 20:52:14 +01:00
|
|
|
integrationExtractValue.ValueSource,
|
|
|
|
integrationExtractValue.BodyDataType,
|
|
|
|
integrationExtractValue.Key,
|
|
|
|
integrationExtractValue.Variable,
|
|
|
|
integrationExtractValue.Name,
|
|
|
|
integrationExtractValue.ID)
|
2024-01-10 07:49:39 +01:00
|
|
|
|
|
|
|
return err
|
2023-07-03 01:41:13 +02:00
|
|
|
}
|
|
|
|
|
2024-03-04 12:36:24 +01:00
|
|
|
func (d *SqlDb) CreateIntegrationMatcher(projectID int, matcher db.IntegrationMatcher) (newMatcher db.IntegrationMatcher, err error) {
|
2024-01-10 07:49:39 +01:00
|
|
|
err = matcher.Validate()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
insertID, err := d.insert(
|
|
|
|
"id",
|
2024-02-11 23:27:03 +01:00
|
|
|
"insert into project__integration_matcher "+
|
2024-03-06 14:28:24 +01:00
|
|
|
"(match_type, `method`, body_data_type, `key`, `value`, integration_id, `name`) values "+
|
2024-02-11 23:27:03 +01:00
|
|
|
"(?, ?, ?, ?, ?, ?, ?)",
|
2024-01-10 07:49:39 +01:00
|
|
|
matcher.MatchType,
|
|
|
|
matcher.Method,
|
|
|
|
matcher.BodyDataType,
|
|
|
|
matcher.Key,
|
|
|
|
matcher.Value,
|
2024-03-06 14:28:24 +01:00
|
|
|
matcher.IntegrationID,
|
2024-01-10 07:49:39 +01:00
|
|
|
matcher.Name)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
newMatcher = matcher
|
|
|
|
newMatcher.ID = insertID
|
|
|
|
|
|
|
|
return
|
2023-07-03 01:41:13 +02:00
|
|
|
}
|
|
|
|
|
2024-03-06 15:51:59 +01:00
|
|
|
func (d *SqlDb) GetIntegrationMatchers(projectID int, params db.RetrieveQueryParams, integrationID int) (matchers []db.IntegrationMatcher, err error) {
|
2024-01-10 07:49:39 +01:00
|
|
|
query, args, err := squirrel.Select("m.*").
|
2024-02-11 20:52:14 +01:00
|
|
|
From("project__integration_matcher as m").
|
2024-03-06 15:51:59 +01:00
|
|
|
Where(squirrel.Eq{"integration_id": integrationID}).
|
2024-01-10 07:49:39 +01:00
|
|
|
OrderBy("m.id").
|
|
|
|
ToSql()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-07-03 01:41:13 +02:00
|
|
|
|
2024-01-10 07:49:39 +01:00
|
|
|
_, err = d.selectAll(&matchers, query, args...)
|
2023-07-03 01:41:13 +02:00
|
|
|
|
2024-01-10 07:49:39 +01:00
|
|
|
return
|
2023-07-03 01:41:13 +02:00
|
|
|
}
|
|
|
|
|
2024-03-06 15:51:59 +01:00
|
|
|
func (d *SqlDb) GetIntegrationMatcher(projectID int, matcherID int, integrationID int) (matcher db.IntegrationMatcher, err error) {
|
2024-01-10 07:49:39 +01:00
|
|
|
query, args, err := squirrel.Select("m.*").
|
2024-02-11 20:52:14 +01:00
|
|
|
From("project__integration_matcher as m").
|
2024-01-10 07:49:39 +01:00
|
|
|
Where(squirrel.Eq{"id": matcherID}).
|
|
|
|
OrderBy("m.id").
|
|
|
|
ToSql()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-07-03 01:41:13 +02:00
|
|
|
|
2024-01-10 07:49:39 +01:00
|
|
|
err = d.selectOne(&matcher, query, args...)
|
2023-07-03 01:41:13 +02:00
|
|
|
|
2024-01-10 07:49:39 +01:00
|
|
|
return matcher, err
|
2023-07-03 01:41:13 +02:00
|
|
|
}
|
|
|
|
|
2024-03-06 15:51:59 +01:00
|
|
|
func (d *SqlDb) GetIntegrationMatcherRefs(projectID int, matcherID int, integrationID int) (refs db.IntegrationExtractorChildReferrers, err error) {
|
2024-03-06 14:28:24 +01:00
|
|
|
refs.Integrations, err = d.GetObjectReferences(db.IntegrationProps, db.IntegrationMatcherProps, matcherID)
|
2023-07-03 01:41:13 +02:00
|
|
|
|
2024-01-10 07:49:39 +01:00
|
|
|
return
|
2023-07-03 01:41:13 +02:00
|
|
|
}
|
|
|
|
|
2024-03-06 15:51:59 +01:00
|
|
|
func (d *SqlDb) DeleteIntegrationMatcher(projectID int, matcherID int, integrationID int) error {
|
|
|
|
return d.deleteObjectByReferencedID(integrationID, db.IntegrationProps, db.IntegrationMatcherProps, matcherID)
|
2023-07-03 01:41:13 +02:00
|
|
|
}
|
|
|
|
|
2024-03-04 12:36:24 +01:00
|
|
|
func (d *SqlDb) UpdateIntegrationMatcher(projectID int, integrationMatcher db.IntegrationMatcher) error {
|
2024-02-11 20:52:14 +01:00
|
|
|
err := integrationMatcher.Validate()
|
2024-01-10 07:49:39 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = d.exec(
|
2024-03-21 11:21:21 +01:00
|
|
|
"update project__integration_matcher set match_type=?, `method`=?, body_data_type=?, `key`=?, `value`=?, `name`=? where integration_id=? and `id`=?",
|
2024-02-11 20:52:14 +01:00
|
|
|
integrationMatcher.MatchType,
|
|
|
|
integrationMatcher.Method,
|
|
|
|
integrationMatcher.BodyDataType,
|
|
|
|
integrationMatcher.Key,
|
|
|
|
integrationMatcher.Value,
|
|
|
|
integrationMatcher.Name,
|
2024-03-21 11:21:21 +01:00
|
|
|
integrationMatcher.IntegrationID,
|
2024-02-11 20:52:14 +01:00
|
|
|
integrationMatcher.ID)
|
2024-01-10 07:49:39 +01:00
|
|
|
|
|
|
|
return err
|
2023-07-03 01:41:13 +02:00
|
|
|
}
|
2024-03-07 10:32:25 +01:00
|
|
|
|
|
|
|
func (d *SqlDb) CreateIntegrationAlias(alias db.IntegrationAlias) (res db.IntegrationAlias, err error) {
|
2024-03-20 16:30:55 +01:00
|
|
|
|
2024-03-20 14:55:47 +01:00
|
|
|
insertID, err := d.insert(
|
|
|
|
"id",
|
|
|
|
"insert into project__integration_alias (project_id, integration_id, alias) values (?, ?, ?)",
|
|
|
|
alias.ProjectID,
|
|
|
|
alias.IntegrationID,
|
|
|
|
alias.Alias)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res = alias
|
|
|
|
res.ID = insertID
|
2024-03-07 10:32:25 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-20 22:35:47 +01:00
|
|
|
func (d *SqlDb) GetIntegrationAliases(projectID int, integrationID *int) (res []db.IntegrationAlias, err error) {
|
|
|
|
|
|
|
|
q := squirrel.Select("*").From(db.IntegrationAliasProps.TableName)
|
|
|
|
|
|
|
|
if integrationID == nil {
|
|
|
|
q = q.Where("project_id=? AND integration_id is null", projectID)
|
|
|
|
} else {
|
|
|
|
q = q.Where("project_id=? AND integration_id=?", projectID, integrationID)
|
|
|
|
}
|
2024-03-20 14:55:47 +01:00
|
|
|
|
|
|
|
query, args, err := q.ToSql()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-20 22:35:47 +01:00
|
|
|
_, err = d.selectAll(&res, query, args...)
|
2024-03-20 14:55:47 +01:00
|
|
|
|
2024-03-07 10:32:25 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-21 00:53:54 +01:00
|
|
|
func (d *SqlDb) GetIntegrationsByAlias(alias string) (res []db.Integration, err error) {
|
2024-03-20 14:55:47 +01:00
|
|
|
|
|
|
|
var aliasObj db.IntegrationAlias
|
|
|
|
|
|
|
|
q := squirrel.Select("*").
|
|
|
|
From(db.IntegrationAliasProps.TableName).
|
|
|
|
Where("alias=?", alias)
|
|
|
|
|
|
|
|
query, args, err := q.ToSql()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.selectOne(&aliasObj, query, args...)
|
|
|
|
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
err = db.ErrNotFound
|
|
|
|
}
|
|
|
|
|
2024-03-21 00:53:54 +01:00
|
|
|
if aliasObj.IntegrationID == nil {
|
|
|
|
var projIntegrations []db.Integration
|
|
|
|
projIntegrations, err = d.GetIntegrations(aliasObj.ProjectID, db.RetrieveQueryParams{})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, integration := range projIntegrations {
|
|
|
|
if integration.Searchable {
|
|
|
|
res = append(res, integration)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var integration db.Integration
|
|
|
|
integration, err = d.GetIntegration(aliasObj.ProjectID, *aliasObj.IntegrationID)
|
|
|
|
res = append(res, integration)
|
|
|
|
}
|
2024-03-20 14:55:47 +01:00
|
|
|
|
2024-03-07 10:32:25 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-20 22:35:47 +01:00
|
|
|
func (d *SqlDb) DeleteIntegrationAlias(projectID int, aliasID int) error {
|
2024-03-21 10:56:17 +01:00
|
|
|
return d.deleteObject(projectID, db.IntegrationAliasProps, aliasID)
|
2024-03-07 10:32:25 +01:00
|
|
|
}
|
2024-03-23 13:37:15 +01:00
|
|
|
|
|
|
|
func (d *SqlDb) GetAllSearchableIntegrations() (integrations []db.Integration, err error) {
|
|
|
|
q := squirrel.Select("*").From(db.IntegrationProps.TableName)
|
|
|
|
|
|
|
|
q = q.Where("searchable")
|
|
|
|
|
|
|
|
query, args, err := q.ToSql()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = d.selectAll(&integrations, query, args...)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|