From bb738eb5a677d547930ddff52375bd4c67945644 Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Mon, 22 Apr 2024 14:40:26 +0800 Subject: [PATCH 1/4] fix: interpolate runs-on with variables --- models/actions/run.go | 18 ++++++++++++++++++ models/actions/variable.go | 7 +++++++ services/actions/notifier_helper.go | 5 ----- services/actions/schedule_tasks.go | 7 ++++++- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/models/actions/run.go b/models/actions/run.go index fa9db0b55475..5c83f786f89e 100644 --- a/models/actions/run.go +++ b/models/actions/run.go @@ -120,6 +120,24 @@ func (run *ActionRun) LoadAttributes(ctx context.Context) error { return nil } +func (run *ActionRun) LoadRepo(ctx context.Context) error { + if run == nil || run.Repo != nil { + return nil + } + + repo, err := repo_model.GetRepositoryByID(ctx, run.RepoID) + if err != nil { + return err + } + run.Repo = repo + + if err := run.Repo.LoadAttributes(ctx); err != nil { + return err + } + + return nil +} + func (run *ActionRun) Duration() time.Duration { return calculateDuration(run.Started, run.Stopped, run.Status) + run.PreviousDuration } diff --git a/models/actions/variable.go b/models/actions/variable.go index b0a455e67575..470841ebede9 100644 --- a/models/actions/variable.go +++ b/models/actions/variable.go @@ -90,6 +90,13 @@ func DeleteVariable(ctx context.Context, id int64) error { } func GetVariablesOfRun(ctx context.Context, run *ActionRun) (map[string]string, error) { + if run.Repo == nil { + if err := run.LoadRepo(ctx); err != nil { + log.Error("LoadRepo: %v", err) + return nil, err + } + } + variables := map[string]string{} // Global diff --git a/services/actions/notifier_helper.go b/services/actions/notifier_helper.go index c48886a8246e..515a96f50774 100644 --- a/services/actions/notifier_helper.go +++ b/services/actions/notifier_helper.go @@ -300,11 +300,6 @@ func handleWorkflows( run.NeedApproval = need } - if err := run.LoadAttributes(ctx); err != nil { - log.Error("LoadAttributes: %v", err) - continue - } - vars, err := actions_model.GetVariablesOfRun(ctx, run) if err != nil { log.Error("GetVariablesOfRun: %v", err) diff --git a/services/actions/schedule_tasks.go b/services/actions/schedule_tasks.go index e4e56e512296..d6395667177d 100644 --- a/services/actions/schedule_tasks.go +++ b/services/actions/schedule_tasks.go @@ -132,8 +132,13 @@ func CreateScheduleTask(ctx context.Context, cron *actions_model.ActionSchedule) Status: actions_model.StatusWaiting, } + vars, err := actions_model.GetVariablesOfRun(ctx, run) + if err != nil { + log.Error("GetVariablesOfRun: %v", err) + } + // Parse the workflow specification from the cron schedule - workflows, err := jobparser.Parse(cron.Content) + workflows, err := jobparser.Parse(cron.Content, jobparser.WithVars(vars)) if err != nil { return err } From 7cff3610dda450fdf993d9d6da7d02de71f0d1f9 Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Mon, 22 Apr 2024 15:00:51 +0800 Subject: [PATCH 2/4] fix: lint --- models/actions/run.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/models/actions/run.go b/models/actions/run.go index 5c83f786f89e..97b40946f7f1 100644 --- a/models/actions/run.go +++ b/models/actions/run.go @@ -131,11 +131,7 @@ func (run *ActionRun) LoadRepo(ctx context.Context) error { } run.Repo = repo - if err := run.Repo.LoadAttributes(ctx); err != nil { - return err - } - - return nil + return run.Repo.LoadAttributes(ctx) } func (run *ActionRun) Duration() time.Duration { From 7d44034190fe31fc7418be58dc243ce774a70d85 Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Tue, 23 Apr 2024 09:57:56 +0800 Subject: [PATCH 3/4] fix: return err --- services/actions/schedule_tasks.go | 1 + 1 file changed, 1 insertion(+) diff --git a/services/actions/schedule_tasks.go b/services/actions/schedule_tasks.go index d6395667177d..18f3324fd2c2 100644 --- a/services/actions/schedule_tasks.go +++ b/services/actions/schedule_tasks.go @@ -135,6 +135,7 @@ func CreateScheduleTask(ctx context.Context, cron *actions_model.ActionSchedule) vars, err := actions_model.GetVariablesOfRun(ctx, run) if err != nil { log.Error("GetVariablesOfRun: %v", err) + return err } // Parse the workflow specification from the cron schedule From 9901b183c723a24867bef49f6114b05bbcfb3077 Mon Sep 17 00:00:00 2001 From: sillyguodong Date: Tue, 23 Apr 2024 16:54:02 +0800 Subject: [PATCH 4/4] fix --- models/actions/run.go | 12 ++++-------- models/actions/variable.go | 12 +++++------- services/actions/notifier_helper.go | 5 +++++ 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/models/actions/run.go b/models/actions/run.go index 97b40946f7f1..b75fa49f3c93 100644 --- a/models/actions/run.go +++ b/models/actions/run.go @@ -98,13 +98,10 @@ func (run *ActionRun) LoadAttributes(ctx context.Context) error { return nil } - if run.Repo == nil { - repo, err := repo_model.GetRepositoryByID(ctx, run.RepoID) - if err != nil { - return err - } - run.Repo = repo + if err := run.LoadRepo(ctx); err != nil { + return err } + if err := run.Repo.LoadAttributes(ctx); err != nil { return err } @@ -130,8 +127,7 @@ func (run *ActionRun) LoadRepo(ctx context.Context) error { return err } run.Repo = repo - - return run.Repo.LoadAttributes(ctx) + return nil } func (run *ActionRun) Duration() time.Duration { diff --git a/models/actions/variable.go b/models/actions/variable.go index 470841ebede9..8aff84465901 100644 --- a/models/actions/variable.go +++ b/models/actions/variable.go @@ -90,15 +90,13 @@ func DeleteVariable(ctx context.Context, id int64) error { } func GetVariablesOfRun(ctx context.Context, run *ActionRun) (map[string]string, error) { - if run.Repo == nil { - if err := run.LoadRepo(ctx); err != nil { - log.Error("LoadRepo: %v", err) - return nil, err - } - } - 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 { diff --git a/services/actions/notifier_helper.go b/services/actions/notifier_helper.go index 2adda174a2ad..1d09a222c024 100644 --- a/services/actions/notifier_helper.go +++ b/services/actions/notifier_helper.go @@ -307,6 +307,11 @@ func handleWorkflows( run.NeedApproval = need + if err := run.LoadAttributes(ctx); err != nil { + log.Error("LoadAttributes: %v", err) + continue + } + vars, err := actions_model.GetVariablesOfRun(ctx, run) if err != nil { log.Error("GetVariablesOfRun: %v", err)