Skip to content

Commit 9cb1978

Browse files
authored
Support task params for Schedules/Integations (#3159)
* feat(ui): refactor task form * refactor(tasks): move ansible params to separate component * feat(tasks): add task params table * feat(schedule): support task params * feat(integrations): save task settings * feat(schedule): spaces * fix(schedule): cron builder * fix(schedule): sql query * fix(tasks): override env via survay variables * feat(schedule): use task params * fix(integrations): empty task params * test: fix test
1 parent ee62721 commit 9cb1978

20 files changed

+986
-305
lines changed

api/integration.go

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -249,16 +249,13 @@ func MatchCompare(value any, method db.IntegrationMatchMethodType, expected stri
249249
}
250250
}
251251

252-
func RunIntegration(integration db.Integration, project db.Project, r *http.Request, payload []byte) {
253-
254-
log.Info(fmt.Sprintf("Running integration %d", integration.ID))
252+
func GetTaskDefinition(integration db.Integration, payload []byte, r *http.Request) (taskDefinition db.Task, err error) {
255253

256254
var envValues = make([]db.IntegrationExtractValue, 0)
257255
var taskValues = make([]db.IntegrationExtractValue, 0)
258256

259-
extractValuesForExtractor, err := helpers.Store(r).GetIntegrationExtractValues(project.ID, db.RetrieveQueryParams{}, integration.ID)
257+
extractValuesForExtractor, err := helpers.Store(r).GetIntegrationExtractValues(integration.ProjectID, db.RetrieveQueryParams{}, integration.ID)
260258
if err != nil {
261-
log.Error(err)
262259
return
263260
}
264261

@@ -273,25 +270,45 @@ func RunIntegration(integration db.Integration, project db.Project, r *http.Requ
273270

274271
var extractedEnvResults = Extract(envValues, r, payload)
275272

276-
environmentJSONBytes, err := json.Marshal(extractedEnvResults)
273+
taskDefinition = integration.TaskParams.CreateTask()
274+
taskDefinition.TemplateID = integration.TemplateID
275+
276+
var env map[string]any
277+
err = json.Unmarshal([]byte(taskDefinition.Environment), &env)
277278
if err != nil {
278-
log.Error(err)
279279
return
280280
}
281281

282-
var extractedTaskResults = ExtractAsAnyForTaskParams(taskValues, r, payload)
282+
for k, v := range extractedEnvResults {
283+
env[k] = v
284+
}
283285

284-
var environmentJSONString = string(environmentJSONBytes)
285-
var taskDefinition = db.Task{
286-
TemplateID: integration.TemplateID,
287-
ProjectID: integration.ProjectID,
288-
Environment: environmentJSONString,
289-
IntegrationID: &integration.ID,
286+
envStr, err := json.Marshal(env)
287+
if err != nil {
288+
return
290289
}
291290

292-
// Only assign extractedTaskResults to Params if it's not empty
293-
if len(extractedTaskResults) > 0 {
294-
taskDefinition.Params = extractedTaskResults
291+
taskDefinition.Environment = string(envStr)
292+
293+
extractedTaskResults := ExtractAsAnyForTaskParams(taskValues, r, payload)
294+
for k, v := range extractedTaskResults {
295+
taskDefinition.Params[k] = v
296+
}
297+
298+
return
299+
}
300+
301+
func RunIntegration(integration db.Integration, project db.Project, r *http.Request, payload []byte) {
302+
303+
log.Info(fmt.Sprintf("Running integration %d", integration.ID))
304+
305+
taskDefinition, err := GetTaskDefinition(integration, payload, r)
306+
if err != nil {
307+
log.WithError(err).WithFields(log.Fields{
308+
"context": "integrations",
309+
"integration_id": integration.ID,
310+
}).Error("Failed to get task definition")
311+
return
295312
}
296313

297314
tpl, err := helpers.Store(r).GetTemplate(integration.ProjectID, integration.TemplateID)

db/Integration.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ type Integration struct {
9898
AuthHeader string `db:"auth_header" json:"auth_header"`
9999
AuthSecret AccessKey `db:"-" json:"-" backup:"-"`
100100
Searchable bool `db:"searchable" json:"searchable"`
101-
TaskParams MapStringAnyField `db:"task_params" json:"task_params"`
101+
//TaskParams MapStringAnyField `db:"task_params" json:"task_params"`
102+
103+
TaskParamsID *int `db:"task_params_id" json:"-" backup:"-"`
104+
TaskParams *TaskParams `db:"-" json:"task_params,omitempty" backup:"task_params"`
102105
}
103106

104107
func (alias IntegrationAlias) ToAlias() Alias {

db/Migration.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func GetMigrations(dialect string) []Migration {
3131
{Version: "2.15.4"},
3232
{Version: "2.16.0"},
3333
{Version: "2.16.1"},
34+
{Version: "2.16.2"},
3435
}
3536
}
3637

@@ -111,6 +112,7 @@ func GetMigrations(dialect string) []Migration {
111112
{Version: "2.15.4"},
112113
{Version: "2.16.0"},
113114
{Version: "2.16.1"},
115+
{Version: "2.16.2"},
114116
}
115117
}
116118

db/Schedule.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ type Schedule struct {
1010

1111
LastCommitHash *string `db:"last_commit_hash" json:"-" backup:"-"`
1212
RepositoryID *int `db:"repository_id" json:"repository_id" backup:"-"`
13+
14+
TaskParamsID *int `db:"task_params_id" json:"-" backup:"-"`
15+
TaskParams *TaskParams `db:"-" json:"task_params,omitempty" backup:"task_params"`
1316
}
1417

1518
type ScheduleWithTpl struct {

db/Store.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,13 @@ var IntegrationProps = ObjectProps{
484484
DefaultSortingColumn: "name",
485485
}
486486

487+
var TaskParamsProps = ObjectProps{
488+
TableName: "project__task_params",
489+
Type: reflect.TypeOf(TaskParams{}),
490+
PrimaryColumnName: "id",
491+
ReferringColumnSuffix: "params_id",
492+
}
493+
487494
var IntegrationExtractValueProps = ObjectProps{
488495
TableName: "project__integration_extract_value",
489496
Type: reflect.TypeOf(IntegrationExtractValue{}),

db/TaskParams.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package db
2+
3+
type TaskParams struct {
4+
ID int `db:"id" json:"-"`
5+
ProjectID int `db:"project_id" json:"-"`
6+
7+
Environment string `db:"environment" json:"environment,omitempty"`
8+
Arguments *string `db:"arguments" json:"arguments,omitempty"`
9+
GitBranch *string `db:"git_branch" json:"git_branch,omitempty"`
10+
11+
Message string `db:"message" json:"message,omitempty"`
12+
13+
// Version is a build version.
14+
// This field available only for Build tasks.
15+
Version *string `db:"version" json:"version,omitempty"`
16+
17+
InventoryID *int `db:"inventory_id" json:"inventory_id,omitempty"`
18+
19+
Params MapStringAnyField `db:"params" json:"params,omitempty"`
20+
}
21+
22+
func (p TaskParams) CreateTask() (task Task) {
23+
task = Task{
24+
ProjectID: p.ProjectID,
25+
Environment: p.Environment,
26+
Arguments: p.Arguments,
27+
GitBranch: p.GitBranch,
28+
Message: p.Message,
29+
Version: p.Version,
30+
InventoryID: p.InventoryID,
31+
Params: p.Params,
32+
}
33+
34+
return
35+
}

db/sql/SqlDb.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ func (d *SqlDbConnection) Connect() {
9494
d.sql.AddTableWithName(db.Template{}, "project__template").SetKeys(true, "id")
9595
d.sql.AddTableWithName(db.User{}, "user").SetKeys(true, "id")
9696
d.sql.AddTableWithName(db.Session{}, "session").SetKeys(true, "id")
97+
d.sql.AddTableWithName(db.TaskParams{}, "project__task_params").SetKeys(true, "id")
9798

9899
//if d.GetDialect() == util.DbDriverSQLite {
99100
// _, err = d.exec("PRAGMA foreign_keys = ON")

db/sql/integration.go

Lines changed: 82 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,29 @@ func (d *SqlDb) CreateIntegration(integration db.Integration) (newIntegration db
1212
return
1313
}
1414

15+
if integration.TaskParams != nil {
16+
params := *integration.TaskParams
17+
params.ProjectID = integration.ProjectID
18+
err = d.Sql().Insert(&params)
19+
if err != nil {
20+
return
21+
}
22+
integration.TaskParamsID = &params.ID
23+
}
24+
1525
insertID, err := d.insert(
1626
"id",
1727
"insert into project__integration "+
18-
"(project_id, name, template_id, auth_method, auth_secret_id, auth_header, searchable) values "+
19-
"(?, ?, ?, ?, ?, ?, ?)",
28+
"(project_id, name, template_id, auth_method, auth_secret_id, auth_header, searchable, task_params_id) values "+
29+
"(?, ?, ?, ?, ?, ?, ?, ?)",
2030
integration.ProjectID,
2131
integration.Name,
2232
integration.TemplateID,
2333
integration.AuthMethod,
2434
integration.AuthSecretID,
2535
integration.AuthHeader,
26-
integration.Searchable)
36+
integration.Searchable,
37+
integration.TaskParamsID)
2738

2839
if err != nil {
2940
return
@@ -42,6 +53,20 @@ func (d *SqlDb) GetIntegrations(projectID int, params db.RetrieveQueryParams) (i
4253

4354
func (d *SqlDb) GetIntegration(projectID int, integrationID int) (integration db.Integration, err error) {
4455
err = d.getObject(projectID, db.IntegrationProps, integrationID, &integration)
56+
if err != nil {
57+
return
58+
}
59+
60+
if integration.TaskParamsID != nil {
61+
var taskParams db.TaskParams
62+
err = d.getObject(projectID, db.TaskParamsProps, *integration.TaskParamsID, &taskParams)
63+
if err != nil {
64+
return
65+
}
66+
67+
integration.TaskParams = &taskParams
68+
}
69+
4570
return
4671
}
4772

@@ -54,25 +79,72 @@ func (d *SqlDb) GetIntegrationRefs(projectID int, integrationID int) (referrers
5479
return
5580
}
5681

57-
func (d *SqlDb) DeleteIntegration(projectID int, integrationID int) error {
58-
return d.deleteObject(projectID, db.IntegrationProps, integrationID)
82+
func (d *SqlDb) DeleteIntegration(projectID int, integrationID int) (err error) {
83+
var integration db.Integration
84+
err = d.getObject(projectID, db.IntegrationProps, integrationID, &integration)
85+
if err != nil {
86+
return
87+
}
88+
89+
err = d.deleteObject(projectID, db.IntegrationProps, integrationID)
90+
if err != nil {
91+
return
92+
}
93+
94+
if integration.TaskParamsID != nil {
95+
err = d.deleteObject(projectID, db.TaskParamsProps, *integration.TaskParamsID)
96+
}
97+
return
5998
}
6099

61-
func (d *SqlDb) UpdateIntegration(integration db.Integration) error {
62-
err := integration.Validate()
100+
func (d *SqlDb) UpdateIntegration(integration db.Integration) (err error) {
63101

64-
if err != nil {
65-
return err
102+
if err = integration.Validate(); err != nil {
103+
return
104+
}
105+
106+
if integration.TaskParams != nil {
107+
var curr db.Integration
108+
err = d.getObject(integration.ProjectID, db.IntegrationProps, integration.ID, &curr)
109+
if err != nil {
110+
return
111+
}
112+
113+
params := *integration.TaskParams
114+
params.ProjectID = integration.ProjectID
115+
116+
if curr.TaskParamsID == nil {
117+
err = d.Sql().Insert(&params)
118+
} else {
119+
params.ID = *curr.TaskParamsID
120+
_, err = d.Sql().Update(&params)
121+
}
122+
123+
if err != nil {
124+
return
125+
}
126+
127+
integration.TaskParamsID = &params.ID
66128
}
67129

68130
_, err = d.exec(
69-
"update project__integration set `name`=?, template_id=?, auth_method=?, auth_secret_id=?, auth_header=?, searchable=? where `id`=?",
131+
"update project__integration set "+
132+
"`name`=?, "+
133+
"template_id=?, "+
134+
"auth_method=?, "+
135+
"auth_secret_id=?, "+
136+
"auth_header=?, "+
137+
"searchable=?, "+
138+
"task_params_id=? "+
139+
"where project_id=? AND `id`=?",
70140
integration.Name,
71141
integration.TemplateID,
72142
integration.AuthMethod,
73143
integration.AuthSecretID,
74144
integration.AuthHeader,
75145
integration.Searchable,
146+
integration.TaskParamsID,
147+
integration.ProjectID,
76148
integration.ID)
77149

78150
return err

db/sql/migrations/v2.16.2.err.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
alter table project__schedule drop task_params_id;
2+
alter table project__integration drop task_params_id;
3+
alter table project__integration add task_params TEXT;
4+
5+
drop table project__task_params;

db/sql/migrations/v2.16.2.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
create table project__task_params
2+
(
3+
id integer primary key autoincrement,
4+
5+
environment TEXT,
6+
project_id int not null,
7+
arguments TEXT,
8+
inventory_id int,
9+
git_branch varchar(255),
10+
params TEXT,
11+
version varchar(20),
12+
message varchar(250),
13+
14+
foreign key (`project_id`) references project (`id`) on delete cascade,
15+
foreign key (`inventory_id`) references project__inventory (`id`) on delete cascade
16+
);
17+
18+
alter table project__integration drop task_params;
19+
alter table project__schedule add task_params_id int references `project__task_params`(`id`);
20+
alter table project__integration add task_params_id int references `project__task_params`(`id`);

0 commit comments

Comments
 (0)