From 9c0a3532a4765263f279caeabd7f696372ce1d1f Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 8 Sep 2023 23:01:19 +0800 Subject: [PATCH] Add a new column schedule_id for action_run to track (#26975) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix #26971 And the UI now will display it's scheduled but not triggered by a push. 图片 --- models/actions/run.go | 3 ++- models/migrations/migrations.go | 2 ++ models/migrations/v1_21/v275.go | 15 +++++++++++++++ options/locale/locale_en-US.ini | 1 + routers/api/actions/runner/runner.go | 8 +++++++- services/actions/schedule_tasks.go | 14 +------------- templates/repo/actions/runs_list.tmpl | 14 +++++++++----- 7 files changed, 37 insertions(+), 20 deletions(-) create mode 100644 models/migrations/v1_21/v275.go diff --git a/models/actions/run.go b/models/actions/run.go index 4853afc15b..8078613fb8 100644 --- a/models/actions/run.go +++ b/models/actions/run.go @@ -35,7 +35,8 @@ type ActionRun struct { Index int64 `xorm:"index unique(repo_index)"` // a unique number for each run of a repository TriggerUserID int64 `xorm:"index"` TriggerUser *user_model.User `xorm:"-"` - Ref string `xorm:"index"` // the commit/tag/… that caused the run + ScheduleID int64 + Ref string `xorm:"index"` // the commit/tag/… that caused the run CommitSHA string IsForkPullRequest bool // If this is triggered by a PR from a forked repository or an untrusted user, we need to check if it is approved and limit permissions when running the workflow. NeedApproval bool // may need approval if it's a fork pull request diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 40df1cd624..f0a8b05d53 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -530,6 +530,8 @@ var migrations = []Migration{ NewMigration("Add Action Schedule Table", v1_21.AddActionScheduleTable), // v274 -> v275 NewMigration("Add Actions artifacts expiration date", v1_21.AddExpiredUnixColumnInActionArtifactTable), + // v275 -> v276 + NewMigration("Add ScheduleID for ActionRun", v1_21.AddScheduleIDForActionRun), } // GetCurrentDBVersion returns the current db version diff --git a/models/migrations/v1_21/v275.go b/models/migrations/v1_21/v275.go new file mode 100644 index 0000000000..78804a59d6 --- /dev/null +++ b/models/migrations/v1_21/v275.go @@ -0,0 +1,15 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package v1_21 //nolint + +import ( + "xorm.io/xorm" +) + +func AddScheduleIDForActionRun(x *xorm.Engine) error { + type ActionRun struct { + ScheduleID int64 + } + return x.Sync(new(ActionRun)) +} diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index ea9f6147ae..39da4be179 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -3504,6 +3504,7 @@ runners.reset_registration_token_success = Runner registration token reset succe runs.all_workflows = All Workflows runs.commit = Commit +runs.scheduled = Scheduled runs.pushed_by = pushed by runs.invalid_workflow_helper = Workflow config file is invalid. Please check your config file: %s runs.no_matching_runner_helper = No matching runner: %s diff --git a/routers/api/actions/runner/runner.go b/routers/api/actions/runner/runner.go index 6de5964cb7..cb206f5685 100644 --- a/routers/api/actions/runner/runner.go +++ b/routers/api/actions/runner/runner.go @@ -202,8 +202,14 @@ func (s *Service) UpdateTask( if err := task.LoadJob(ctx); err != nil { return nil, status.Errorf(codes.Internal, "load job: %v", err) } + if err := task.Job.LoadRun(ctx); err != nil { + return nil, status.Errorf(codes.Internal, "load run: %v", err) + } - actions_service.CreateCommitStatus(ctx, task.Job) + // don't create commit status for cron job + if task.Job.Run.ScheduleID == 0 { + actions_service.CreateCommitStatus(ctx, task.Job) + } if req.Msg.State.Result != runnerv1.Result_RESULT_UNSPECIFIED { if err := actions_service.EmitJobsIfReady(task.Job.RunID); err != nil { diff --git a/services/actions/schedule_tasks.go b/services/actions/schedule_tasks.go index 12227377ab..40b606c777 100644 --- a/services/actions/schedule_tasks.go +++ b/services/actions/schedule_tasks.go @@ -113,6 +113,7 @@ func CreateScheduleTask(ctx context.Context, cron *actions_model.ActionSchedule) CommitSHA: cron.CommitSHA, Event: cron.Event, EventPayload: cron.EventPayload, + ScheduleID: cron.ID, Status: actions_model.StatusWaiting, } @@ -127,19 +128,6 @@ func CreateScheduleTask(ctx context.Context, cron *actions_model.ActionSchedule) return err } - // Retrieve the jobs for the newly created action run - jobs, _, err := actions_model.FindRunJobs(ctx, actions_model.FindRunJobOptions{RunID: run.ID}) - if err != nil { - return err - } - - // Create commit statuses for each job - for _, job := range jobs { - if err := createCommitStatus(ctx, job); err != nil { - return err - } - } - // Return nil if no errors occurred return nil } diff --git a/templates/repo/actions/runs_list.tmpl b/templates/repo/actions/runs_list.tmpl index 37a584079f..961dd52c62 100644 --- a/templates/repo/actions/runs_list.tmpl +++ b/templates/repo/actions/runs_list.tmpl @@ -15,11 +15,15 @@ {{- .Title -}}
- {{if not $.CurWorkflow}}{{.WorkflowID}} {{end}}#{{.Index}} - : {{$.locale.Tr "actions.runs.commit"}} - {{ShortSha .CommitSHA}} - {{$.locale.Tr "actions.runs.pushed_by"}} - {{.TriggerUser.GetDisplayName}} + {{if not $.CurWorkflow}}{{.WorkflowID}} {{end}}#{{.Index}}: + {{- if .ScheduleID -}} + {{$.locale.Tr "actions.runs.scheduled"}} + {{- else -}} + {{$.locale.Tr "actions.runs.commit"}} + {{ShortSha .CommitSHA}} + {{$.locale.Tr "actions.runs.pushed_by"}} + {{.TriggerUser.GetDisplayName}} + {{- end -}}