From 623c79dfd24cc376bb60a7b3e52d91e676ac3d3f Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Mon, 25 Mar 2024 16:07:17 +0100 Subject: [PATCH 1/9] Revert "[ACTIONS] on.schedule: the event is always "schedule"" This reverts commit 403bb4bf488fc6c52d52d73e9f2ece26717df13e. --- modules/actions/github.go | 4 ---- modules/actions/workflows.go | 1 - modules/actions/workflows_test.go | 7 ------- modules/webhook/type.go | 1 - services/actions/notifier_helper.go | 2 +- services/actions/schedule_tasks.go | 1 - 6 files changed, 1 insertion(+), 15 deletions(-) diff --git a/modules/actions/github.go b/modules/actions/github.go index cc04ca5c5..278b7cb74 100644 --- a/modules/actions/github.go +++ b/modules/actions/github.go @@ -22,7 +22,6 @@ const ( GithubEventRelease = "release" GithubEventPullRequestComment = "pull_request_comment" GithubEventGollum = "gollum" - GithubEventSchedule = "schedule" ) // IsDefaultBranchWorkflow returns true if the event only triggers workflows on the default branch @@ -74,9 +73,6 @@ func canGithubEventMatch(eventName string, triggedEvent webhook_module.HookEvent case GithubEventGollum: return triggedEvent == webhook_module.HookEventWiki - case GithubEventSchedule: - return triggedEvent == webhook_module.HookEventSchedule - case GithubEventIssues: switch triggedEvent { case webhook_module.HookEventIssues, diff --git a/modules/actions/workflows.go b/modules/actions/workflows.go index 8317f16db..60dd05f0b 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -153,7 +153,6 @@ func detectMatched(gitRepo *git.Repository, commit *git.Commit, triggedEvent web switch triggedEvent { case // events with no activity types - webhook_module.HookEventSchedule, webhook_module.HookEventCreate, webhook_module.HookEventDelete, webhook_module.HookEventFork, diff --git a/modules/actions/workflows_test.go b/modules/actions/workflows_test.go index c8e1e553f..2d57f1948 100644 --- a/modules/actions/workflows_test.go +++ b/modules/actions/workflows_test.go @@ -118,13 +118,6 @@ func TestDetectMatched(t *testing.T) { yamlOn: "on: gollum", expected: true, }, - { - desc: "HookEventSchedue(schedule) matches GithubEventSchedule(schedule)", - triggedEvent: webhook_module.HookEventSchedule, - payload: nil, - yamlOn: "on: schedule", - expected: true, - }, } for _, tc := range testCases { diff --git a/modules/webhook/type.go b/modules/webhook/type.go index 0d2aef5e1..7f427f2ea 100644 --- a/modules/webhook/type.go +++ b/modules/webhook/type.go @@ -31,7 +31,6 @@ const ( HookEventRepository HookEventType = "repository" HookEventRelease HookEventType = "release" HookEventPackage HookEventType = "package" - HookEventSchedule HookEventType = "schedule" ) // Event returns the HookEventType as an event string diff --git a/services/actions/notifier_helper.go b/services/actions/notifier_helper.go index 99f2dd07d..58e67ffae 100644 --- a/services/actions/notifier_helper.go +++ b/services/actions/notifier_helper.go @@ -508,7 +508,7 @@ func handleSchedules( TriggerUserID: input.Doer.ID, Ref: input.Repo.DefaultBranch, CommitSHA: commit.ID.String(), - Event: webhook_module.HookEventType(api.HookScheduleCreated), + Event: input.Event, EventPayload: string(p), Specs: schedules, Content: dwf.Content, diff --git a/services/actions/schedule_tasks.go b/services/actions/schedule_tasks.go index dfde34d99..8eef2b67b 100644 --- a/services/actions/schedule_tasks.go +++ b/services/actions/schedule_tasks.go @@ -112,7 +112,6 @@ func CreateScheduleTask(ctx context.Context, cron *actions_model.ActionSchedule) Ref: cron.Ref, CommitSHA: cron.CommitSHA, Event: cron.Event, - TriggerEvent: string(webhook_module.HookEventSchedule), EventPayload: cron.EventPayload, ScheduleID: cron.ID, Status: actions_model.StatusWaiting, From 3b3747ffe8ebd2b15001161b017e393b8121f50a Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 13 Jan 2024 05:50:38 +0800 Subject: [PATCH 2/9] Fix schedule tasks bugs (#28691) Fix #28157 This PR fix the possible bugs about actions schedule. - Move `UpdateRepositoryUnit` and `SetRepoDefaultBranch` from models to service layer - Remove schedules plan from database and cancel waiting & running schedules tasks in this repository when actions unit has been disabled or global disabled. - Remove schedules plan from database and cancel waiting & running schedules tasks in this repository when default branch changed. (cherry picked from commit 97292da96048b036cbe36b3ea66503ac568a73e7) Conflicts: modules/actions/github.go routers/web/repo/setting/default_branch.go routers/web/repo/setting/setting.go services/repository/branch.go services/repository/setting.go tests/integration/actions_trigger_test.go --- models/actions/run.go | 11 ++-- models/actions/run_list.go | 5 ++ models/actions/schedule.go | 20 +++++++ models/git/branch.go | 4 +- models/git/branch_test.go | 3 +- models/repo/repo_unit.go | 26 --------- modules/actions/github.go | 4 ++ modules/actions/workflows.go | 24 ++++---- modules/actions/workflows_test.go | 7 +++ modules/webhook/type.go | 1 + routers/api/v1/repo/repo.go | 2 +- routers/web/repo/setting/default_branch.go | 24 +++----- routers/web/repo/setting/setting.go | 2 +- services/actions/notifier_helper.go | 35 +++++------ services/actions/schedule_tasks.go | 2 + services/repository/branch.go | 68 +++++++++++++++++++++- services/repository/setting.go | 9 +++ services/wiki/wiki.go | 3 +- 18 files changed, 164 insertions(+), 86 deletions(-) diff --git a/models/actions/run.go b/models/actions/run.go index 9c7f049bb..5d4e3b74d 100644 --- a/models/actions/run.go +++ b/models/actions/run.go @@ -171,13 +171,14 @@ func updateRepoRunsNumbers(ctx context.Context, repo *repo_model.Repository) err } // CancelRunningJobs cancels all running and waiting jobs associated with a specific workflow. -func CancelRunningJobs(ctx context.Context, repoID int64, ref, workflowID string) error { +func CancelRunningJobs(ctx context.Context, repoID int64, ref, workflowID string, event webhook_module.HookEventType) error { // Find all runs in the specified repository, reference, and workflow with statuses 'Running' or 'Waiting'. runs, total, err := db.FindAndCount[ActionRun](ctx, FindRunOptions{ - RepoID: repoID, - Ref: ref, - WorkflowID: workflowID, - Status: []Status{StatusRunning, StatusWaiting}, + RepoID: repoID, + Ref: ref, + WorkflowID: workflowID, + TriggerEvent: event, + Status: []Status{StatusRunning, StatusWaiting}, }) if err != nil { return err diff --git a/models/actions/run_list.go b/models/actions/run_list.go index 375c46221..388bfc4f8 100644 --- a/models/actions/run_list.go +++ b/models/actions/run_list.go @@ -10,6 +10,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/container" + webhook_module "code.gitea.io/gitea/modules/webhook" "xorm.io/builder" ) @@ -71,6 +72,7 @@ type FindRunOptions struct { WorkflowID string Ref string // the commit/tag/… that caused this workflow TriggerUserID int64 + TriggerEvent webhook_module.HookEventType Approved bool // not util.OptionalBool, it works only when it's true Status []Status } @@ -98,6 +100,9 @@ func (opts FindRunOptions) ToConds() builder.Cond { if opts.Ref != "" { cond = cond.And(builder.Eq{"ref": opts.Ref}) } + if opts.TriggerEvent != "" { + cond = cond.And(builder.Eq{"trigger_event": opts.TriggerEvent}) + } return cond } diff --git a/models/actions/schedule.go b/models/actions/schedule.go index 34d23f1c0..d450e7aa0 100644 --- a/models/actions/schedule.go +++ b/models/actions/schedule.go @@ -5,6 +5,7 @@ package actions import ( "context" + "fmt" "time" "code.gitea.io/gitea/models/db" @@ -118,3 +119,22 @@ func DeleteScheduleTaskByRepo(ctx context.Context, id int64) error { return committer.Commit() } + +func CleanRepoScheduleTasks(ctx context.Context, repo *repo_model.Repository) error { + // If actions disabled when there is schedule task, this will remove the outdated schedule tasks + // There is no other place we can do this because the app.ini will be changed manually + if err := DeleteScheduleTaskByRepo(ctx, repo.ID); err != nil { + return fmt.Errorf("DeleteCronTaskByRepo: %v", err) + } + // cancel running cron jobs of this repository and delete old schedules + if err := CancelRunningJobs( + ctx, + repo.ID, + repo.DefaultBranch, + "", + webhook_module.HookEventSchedule, + ); err != nil { + return fmt.Errorf("CancelRunningJobs: %v", err) + } + return nil +} diff --git a/models/git/branch.go b/models/git/branch.go index a5ee2bde6..a12cbd4de 100644 --- a/models/git/branch.go +++ b/models/git/branch.go @@ -292,7 +292,7 @@ func FindRenamedBranch(ctx context.Context, repoID int64, from string) (branch * } // RenameBranch rename a branch -func RenameBranch(ctx context.Context, repo *repo_model.Repository, from, to string, gitAction func(isDefault bool) error) (err error) { +func RenameBranch(ctx context.Context, repo *repo_model.Repository, from, to string, gitAction func(ctx context.Context, isDefault bool) error) (err error) { ctx, committer, err := db.TxContext(ctx) if err != nil { return err @@ -367,7 +367,7 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, from, to str } // 5. do git action - if err = gitAction(isDefault); err != nil { + if err = gitAction(ctx, isDefault); err != nil { return err } diff --git a/models/git/branch_test.go b/models/git/branch_test.go index b984244cd..b8ea663e8 100644 --- a/models/git/branch_test.go +++ b/models/git/branch_test.go @@ -4,6 +4,7 @@ package git_test import ( + "context" "testing" "code.gitea.io/gitea/models/db" @@ -132,7 +133,7 @@ func TestRenameBranch(t *testing.T) { }, git_model.WhitelistOptions{})) assert.NoError(t, committer.Commit()) - assert.NoError(t, git_model.RenameBranch(db.DefaultContext, repo1, "master", "main", func(isDefault bool) error { + assert.NoError(t, git_model.RenameBranch(db.DefaultContext, repo1, "master", "main", func(ctx context.Context, isDefault bool) error { _isDefault = isDefault return nil })) diff --git a/models/repo/repo_unit.go b/models/repo/repo_unit.go index 08058b0d4..ca82d54cb 100644 --- a/models/repo/repo_unit.go +++ b/models/repo/repo_unit.go @@ -316,29 +316,3 @@ func UpdateRepoUnit(ctx context.Context, unit *RepoUnit) error { _, err := db.GetEngine(ctx).ID(unit.ID).Update(unit) return err } - -// UpdateRepositoryUnits updates a repository's units -func UpdateRepositoryUnits(ctx context.Context, repo *Repository, units []RepoUnit, deleteUnitTypes []unit.Type) (err error) { - ctx, committer, err := db.TxContext(ctx) - if err != nil { - return err - } - defer committer.Close() - - // Delete existing settings of units before adding again - for _, u := range units { - deleteUnitTypes = append(deleteUnitTypes, u.Type) - } - - if _, err = db.GetEngine(ctx).Where("repo_id = ?", repo.ID).In("type", deleteUnitTypes).Delete(new(RepoUnit)); err != nil { - return err - } - - if len(units) > 0 { - if err = db.Insert(ctx, units); err != nil { - return err - } - } - - return committer.Commit() -} diff --git a/modules/actions/github.go b/modules/actions/github.go index 278b7cb74..749bcd7c8 100644 --- a/modules/actions/github.go +++ b/modules/actions/github.go @@ -22,6 +22,7 @@ const ( GithubEventRelease = "release" GithubEventPullRequestComment = "pull_request_comment" GithubEventGollum = "gollum" + GithubEventSchedule = "schedule" ) // IsDefaultBranchWorkflow returns true if the event only triggers workflows on the default branch @@ -115,6 +116,9 @@ func canGithubEventMatch(eventName string, triggedEvent webhook_module.HookEvent return triggedEvent == webhook_module.HookEventIssueComment || triggedEvent == webhook_module.HookEventPullRequestComment + case GithubEventSchedule: + return triggedEvent == webhook_module.HookEventSchedule + default: return eventName == string(triggedEvent) } diff --git a/modules/actions/workflows.go b/modules/actions/workflows.go index 60dd05f0b..b0d53b83c 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -22,7 +22,7 @@ import ( type DetectedWorkflow struct { EntryName string - TriggerEvent string + TriggerEvent *jobparser.Event Content []byte } @@ -103,6 +103,7 @@ func DetectWorkflows( commit *git.Commit, triggedEvent webhook_module.HookEventType, payload api.Payloader, + detectSchedule bool, ) ([]*DetectedWorkflow, []*DetectedWorkflow, error) { entries, err := ListWorkflows(commit) if err != nil { @@ -117,6 +118,7 @@ func DetectWorkflows( return nil, nil, err } + // one workflow may have multiple events events, err := GetEventsFromContent(content) if err != nil { log.Warn("ignore invalid workflow %q: %v", entry.Name(), err) @@ -125,17 +127,18 @@ func DetectWorkflows( for _, evt := range events { log.Trace("detect workflow %q for event %#v matching %q", entry.Name(), evt, triggedEvent) if evt.IsSchedule() { - dwf := &DetectedWorkflow{ - EntryName: entry.Name(), - TriggerEvent: evt.Name, - Content: content, + if detectSchedule { + dwf := &DetectedWorkflow{ + EntryName: entry.Name(), + TriggerEvent: evt, + Content: content, + } + schedules = append(schedules, dwf) } - schedules = append(schedules, dwf) - } - if detectMatched(gitRepo, commit, triggedEvent, payload, evt) { + } else if detectMatched(gitRepo, commit, triggedEvent, payload, evt) { dwf := &DetectedWorkflow{ EntryName: entry.Name(), - TriggerEvent: evt.Name, + TriggerEvent: evt, Content: content, } workflows = append(workflows, dwf) @@ -156,7 +159,8 @@ func detectMatched(gitRepo *git.Repository, commit *git.Commit, triggedEvent web webhook_module.HookEventCreate, webhook_module.HookEventDelete, webhook_module.HookEventFork, - webhook_module.HookEventWiki: + webhook_module.HookEventWiki, + webhook_module.HookEventSchedule: if len(evt.Acts()) != 0 { log.Warn("Ignore unsupported %s event arguments %v", triggedEvent, evt.Acts()) } diff --git a/modules/actions/workflows_test.go b/modules/actions/workflows_test.go index 2d57f1948..c8e1e553f 100644 --- a/modules/actions/workflows_test.go +++ b/modules/actions/workflows_test.go @@ -118,6 +118,13 @@ func TestDetectMatched(t *testing.T) { yamlOn: "on: gollum", expected: true, }, + { + desc: "HookEventSchedue(schedule) matches GithubEventSchedule(schedule)", + triggedEvent: webhook_module.HookEventSchedule, + payload: nil, + yamlOn: "on: schedule", + expected: true, + }, } for _, tc := range testCases { diff --git a/modules/webhook/type.go b/modules/webhook/type.go index 7f427f2ea..0d2aef5e1 100644 --- a/modules/webhook/type.go +++ b/modules/webhook/type.go @@ -31,6 +31,7 @@ const ( HookEventRepository HookEventType = "repository" HookEventRelease HookEventType = "release" HookEventPackage HookEventType = "package" + HookEventSchedule HookEventType = "schedule" ) // Event returns the HookEventType as an event string diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index 316a1161d..bee605dd7 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -1001,7 +1001,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error { } if len(units)+len(deleteUnitTypes) > 0 { - if err := repo_model.UpdateRepositoryUnits(ctx, repo, units, deleteUnitTypes); err != nil { + if err := repo_service.UpdateRepositoryUnits(ctx, repo, units, deleteUnitTypes); err != nil { ctx.Error(http.StatusInternalServerError, "UpdateRepositoryUnits", err) return err } diff --git a/routers/web/repo/setting/default_branch.go b/routers/web/repo/setting/default_branch.go index d0b32ef07..12e1ab7d1 100644 --- a/routers/web/repo/setting/default_branch.go +++ b/routers/web/repo/setting/default_branch.go @@ -12,8 +12,7 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/routers/web/repo" - "code.gitea.io/gitea/services/context" - notify_service "code.gitea.io/gitea/services/notify" + repo_service "code.gitea.io/gitea/services/repository" ) // SetDefaultBranchPost set default branch @@ -36,23 +35,14 @@ func SetDefaultBranchPost(ctx *context.Context) { } branch := ctx.FormString("branch") - if !ctx.Repo.GitRepo.IsBranchExist(branch) { - ctx.Status(http.StatusNotFound) - return - } else if repo.DefaultBranch != branch { - repo.DefaultBranch = branch - if err := gitrepo.SetDefaultBranch(ctx, repo, branch); err != nil { - if !git.IsErrUnsupportedVersion(err) { - ctx.ServerError("SetDefaultBranch", err) - return - } - } - if err := repo_model.UpdateDefaultBranch(ctx, repo); err != nil { + if err := repo_service.SetRepoDefaultBranch(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, branch); err != nil { + switch { + case ctx.Repo.GitRepo.IsErrBranchNotExist(err): + ctx.Status(http.StatusNotFound) + default: ctx.ServerError("SetDefaultBranch", err) - return } - - notify_service.ChangeDefaultBranch(ctx, repo) + return } log.Trace("Repository basic settings updated: %s/%s", ctx.Repo.Owner.Name, repo.Name) diff --git a/routers/web/repo/setting/setting.go b/routers/web/repo/setting/setting.go index 99c15b74f..00af58b44 100644 --- a/routers/web/repo/setting/setting.go +++ b/routers/web/repo/setting/setting.go @@ -269,7 +269,7 @@ func UnitsPost(ctx *context.Context) { return } - if err := repo_model.UpdateRepositoryUnits(ctx, repo, units, deleteUnitTypes); err != nil { + if err := repo_service.UpdateRepositoryUnits(ctx, repo, units, deleteUnitTypes); err != nil { ctx.ServerError("UpdateRepositoryUnits", err) return } diff --git a/services/actions/notifier_helper.go b/services/actions/notifier_helper.go index 58e67ffae..7de2fb78e 100644 --- a/services/actions/notifier_helper.go +++ b/services/actions/notifier_helper.go @@ -123,6 +123,9 @@ func notify(ctx context.Context, input *notifyInput) error { return nil } if unit_model.TypeActions.UnitGlobalDisabled() { + if err := actions_model.CleanRepoScheduleTasks(ctx, input.Repo); err != nil { + log.Error("CleanRepoScheduleTasks: %v", err) + } return nil } if err := input.Repo.LoadUnits(ctx); err != nil { @@ -167,7 +170,11 @@ func notify(ctx context.Context, input *notifyInput) error { var detectedWorkflows []*actions_module.DetectedWorkflow actionsConfig := input.Repo.MustGetUnit(ctx, unit_model.TypeActions).ActionsConfig() - workflows, schedules, err := actions_module.DetectWorkflows(gitRepo, commit, input.Event, input.Payload) + workflows, schedules, err := actions_module.DetectWorkflows(gitRepo, commit, + input.Event, + input.Payload, + input.Event == webhook_module.HookEventPush && input.Ref == input.Repo.DefaultBranch, + ) if err != nil { return fmt.Errorf("DetectWorkflows: %w", err) } @@ -181,7 +188,7 @@ func notify(ctx context.Context, input *notifyInput) error { continue } - if wf.TriggerEvent != actions_module.GithubEventPullRequestTarget { + if wf.TriggerEvent.Name != actions_module.GithubEventPullRequestTarget { detectedWorkflows = append(detectedWorkflows, wf) } } @@ -200,7 +207,7 @@ func notify(ctx context.Context, input *notifyInput) error { } return fmt.Errorf("gitRepo.GetCommit: %w", err) } - baseWorkflows, _, err := actions_module.DetectWorkflows(gitRepo, baseCommit, input.Event, input.Payload) + baseWorkflows, _, err := actions_module.DetectWorkflows(gitRepo, baseCommit, input.Event, input.Payload, false) if err != nil { return fmt.Errorf("DetectWorkflows: %w", err) } @@ -208,7 +215,7 @@ func notify(ctx context.Context, input *notifyInput) error { log.Trace("repo %s with commit %s couldn't find pull_request_target workflows", input.Repo.RepoPath(), baseCommit.ID) } else { for _, wf := range baseWorkflows { - if wf.TriggerEvent == actions_module.GithubEventPullRequestTarget { + if wf.TriggerEvent.Name == actions_module.GithubEventPullRequestTarget { detectedWorkflows = append(detectedWorkflows, wf) } } @@ -307,7 +314,7 @@ func handleWorkflows( IsForkPullRequest: isForkPullRequest, Event: input.Event, EventPayload: string(p), - TriggerEvent: dwf.TriggerEvent, + TriggerEvent: dwf.TriggerEvent.Name, Status: actions_model.StatusWaiting, } if need, err := ifNeedApproval(ctx, run, input.Repo, input.Doer); err != nil { @@ -342,6 +349,7 @@ func handleWorkflows( run.RepoID, run.Ref, run.WorkflowID, + run.Event, ); err != nil { log.Error("CancelRunningJobs: %v", err) } @@ -467,8 +475,8 @@ func handleSchedules( log.Error("CountSchedules: %v", err) return err } else if count > 0 { - if err := actions_model.DeleteScheduleTaskByRepo(ctx, input.Repo.ID); err != nil { - log.Error("DeleteCronTaskByRepo: %v", err) + if err := actions_model.CleanRepoScheduleTasks(ctx, input.Repo); err != nil { + log.Error("CleanRepoScheduleTasks: %v", err) } } @@ -513,19 +521,6 @@ func handleSchedules( Specs: schedules, Content: dwf.Content, } - - // cancel running jobs if the event is push - if run.Event == webhook_module.HookEventPush { - // cancel running jobs of the same workflow - if err := actions_model.CancelRunningJobs( - ctx, - run.RepoID, - run.Ref, - run.WorkflowID, - ); err != nil { - log.Error("CancelRunningJobs: %v", err) - } - } crons = append(crons, run) } diff --git a/services/actions/schedule_tasks.go b/services/actions/schedule_tasks.go index 8eef2b67b..e7aa4a39a 100644 --- a/services/actions/schedule_tasks.go +++ b/services/actions/schedule_tasks.go @@ -59,6 +59,7 @@ func startTasks(ctx context.Context) error { row.RepoID, row.Schedule.Ref, row.Schedule.WorkflowID, + webhook_module.HookEventSchedule, ); err != nil { log.Error("CancelRunningJobs: %v", err) } @@ -113,6 +114,7 @@ func CreateScheduleTask(ctx context.Context, cron *actions_model.ActionSchedule) CommitSHA: cron.CommitSHA, Event: cron.Event, EventPayload: cron.EventPayload, + TriggerEvent: string(webhook_module.HookEventSchedule), ScheduleID: cron.ID, Status: actions_model.StatusWaiting, } diff --git a/services/repository/branch.go b/services/repository/branch.go index b68355324..cab1d5639 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -10,6 +10,7 @@ import ( "strings" "code.gitea.io/gitea/models" + actions_model "code.gitea.io/gitea/models/actions" "code.gitea.io/gitea/models/db" git_model "code.gitea.io/gitea/models/git" issues_model "code.gitea.io/gitea/models/issues" @@ -23,6 +24,7 @@ import ( "code.gitea.io/gitea/modules/queue" repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/timeutil" + webhook_module "code.gitea.io/gitea/modules/webhook" notify_service "code.gitea.io/gitea/services/notify" files_service "code.gitea.io/gitea/services/repository/files" @@ -357,14 +359,29 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, doer *user_m return "from_not_exist", nil } - if err := git_model.RenameBranch(ctx, repo, from, to, func(isDefault bool) error { + if err := git_model.RenameBranch(ctx, repo, from, to, func(ctx context.Context, isDefault bool) error { err2 := gitRepo.RenameBranch(from, to) if err2 != nil { return err2 } if isDefault { - err2 = gitrepo.SetDefaultBranch(ctx, repo, to) + // if default branch changed, we need to delete all schedules and cron jobs + if err := actions_model.DeleteScheduleTaskByRepo(ctx, repo.ID); err != nil { + log.Error("DeleteCronTaskByRepo: %v", err) + } + // cancel running cron jobs of this repository and delete old schedules + if err := actions_model.CancelRunningJobs( + ctx, + repo.ID, + from, + "", + webhook_module.HookEventSchedule, + ); err != nil { + log.Error("CancelRunningJobs: %v", err) + } + + err2 = gitRepo.SetDefaultBranch(ctx, repo, to) if err2 != nil { return err2 } @@ -496,3 +513,50 @@ func AddAllRepoBranchesToSyncQueue(ctx context.Context, doerID int64) error { } return nil } + +func SetRepoDefaultBranch(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, newBranchName string) error { + if repo.DefaultBranch == newBranchName { + return nil + } + + if !gitRepo.IsBranchExist(newBranchName) { + return git_model.ErrBranchNotExist{ + BranchName: newBranchName, + } + } + + oldDefaultBranchName := repo.DefaultBranch + repo.DefaultBranch = newBranchName + if err := db.WithTx(ctx, func(ctx context.Context) error { + if err := repo_model.UpdateDefaultBranch(ctx, repo); err != nil { + return err + } + + if err := actions_model.DeleteScheduleTaskByRepo(ctx, repo.ID); err != nil { + log.Error("DeleteCronTaskByRepo: %v", err) + } + // cancel running cron jobs of this repository and delete old schedules + if err := actions_model.CancelRunningJobs( + ctx, + repo.ID, + oldDefaultBranchName, + "", + webhook_module.HookEventSchedule, + ); err != nil { + log.Error("CancelRunningJobs: %v", err) + } + + if err := gitRepo.SetDefaultBranch(newBranchName); err != nil { + if !git.IsErrUnsupportedVersion(err) { + return err + } + } + return nil + }); err != nil { + return err + } + + notify_service.ChangeDefaultBranch(ctx, repo) + + return nil +} diff --git a/services/repository/setting.go b/services/repository/setting.go index 7dded5d6b..6496ac401 100644 --- a/services/repository/setting.go +++ b/services/repository/setting.go @@ -5,10 +5,13 @@ package repository import ( "context" + "slices" + actions_model "code.gitea.io/gitea/models/actions" "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" + "code.gitea.io/gitea/modules/log" ) // UpdateRepositoryUnits updates a repository's units @@ -24,6 +27,12 @@ func UpdateRepositoryUnits(ctx context.Context, repo *repo_model.Repository, uni deleteUnitTypes = append(deleteUnitTypes, u.Type) } + if slices.Contains(deleteUnitTypes, unit.TypeActions) { + if err := actions_model.CleanRepoScheduleTasks(ctx, repo); err != nil { + log.Error("CleanRepoScheduleTasks: %v", err) + } + } + if _, err = db.GetEngine(ctx).Where("repo_id = ?", repo.ID).In("type", deleteUnitTypes).Delete(new(repo_model.RepoUnit)); err != nil { return err } diff --git a/services/wiki/wiki.go b/services/wiki/wiki.go index 01c8cf987..f2505e82c 100644 --- a/services/wiki/wiki.go +++ b/services/wiki/wiki.go @@ -21,6 +21,7 @@ import ( repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/sync" asymkey_service "code.gitea.io/gitea/services/asymkey" + repo_service "code.gitea.io/gitea/services/repository" ) // TODO: use clustered lock (unique queue? or *abuse* cache) @@ -399,7 +400,7 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model // DeleteWiki removes the actual and local copy of repository wiki. func DeleteWiki(ctx context.Context, repo *repo_model.Repository) error { - if err := repo_model.UpdateRepositoryUnits(ctx, repo, nil, []unit.Type{unit.TypeWiki}); err != nil { + if err := repo_service.UpdateRepositoryUnits(ctx, repo, nil, []unit.Type{unit.TypeWiki}); err != nil { return err } From 3d19921dbeff341b9bfd6b22e4a6579913e0c8ae Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 22 Jan 2024 10:13:24 +0800 Subject: [PATCH 3/9] Fix schedule not trigger bug because matching full ref name with short ref name (#28874) Fix #28533 Caused by #28691 (cherry picked from commit 23efd9d2781c2ac22594a83afa75182d276b1571) --- services/actions/notifier_helper.go | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/services/actions/notifier_helper.go b/services/actions/notifier_helper.go index 7de2fb78e..81ae97d55 100644 --- a/services/actions/notifier_helper.go +++ b/services/actions/notifier_helper.go @@ -173,24 +173,28 @@ func notify(ctx context.Context, input *notifyInput) error { workflows, schedules, err := actions_module.DetectWorkflows(gitRepo, commit, input.Event, input.Payload, - input.Event == webhook_module.HookEventPush && input.Ref == input.Repo.DefaultBranch, + input.Event == webhook_module.HookEventPush && git.RefName(input.Ref).BranchName() == input.Repo.DefaultBranch, ) if err != nil { return fmt.Errorf("DetectWorkflows: %w", err) } - if len(workflows) == 0 { - log.Trace("repo %s with commit %s couldn't find workflows", input.Repo.RepoPath(), commit.ID) - } else { - for _, wf := range workflows { - if actionsConfig.IsWorkflowDisabled(wf.EntryName) { - log.Trace("repo %s has disable workflows %s", input.Repo.RepoPath(), wf.EntryName) - continue - } + log.Trace("repo %s with commit %s event %s find %d workflows and %d schedules", + input.Repo.RepoPath(), + commit.ID, + input.Event, + len(workflows), + len(schedules), + ) - if wf.TriggerEvent.Name != actions_module.GithubEventPullRequestTarget { - detectedWorkflows = append(detectedWorkflows, wf) - } + for _, wf := range workflows { + if actionsConfig.IsWorkflowDisabled(wf.EntryName) { + log.Trace("repo %s has disable workflows %s", input.Repo.RepoPath(), wf.EntryName) + continue + } + + if wf.TriggerEvent.Name != actions_module.GithubEventPullRequestTarget { + detectedWorkflows = append(detectedWorkflows, wf) } } From cf78141bddbcae73633cd342efcb624dba279623 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Wed, 31 Jan 2024 22:55:12 +0800 Subject: [PATCH 4/9] Fix an actions schedule bug (#28942) In #28691, schedule plans will be deleted when a repo's actions unit is disabled. But when the unit is enabled, the schedule plans won't be created again. This PR fixes the bug. The schedule plans will be created again when the actions unit is re-enabled (cherry picked from commit adc3598a755b43e3911266d7fa575c121e16613d) --- modules/actions/workflows.go | 35 +++++++++++++++++++++++++++++ routers/api/v1/repo/wiki.go | 4 ++-- services/actions/notifier_helper.go | 32 ++++++++++++++++++++++++++ services/actions/schedule_tasks.go | 12 ++++++++-- services/convert/wiki.go | 15 ------------- services/repository/setting.go | 10 +++++++++ services/wiki/wiki_path.go | 15 +++++++++++++ 7 files changed, 104 insertions(+), 19 deletions(-) diff --git a/modules/actions/workflows.go b/modules/actions/workflows.go index b0d53b83c..c103aa192 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -149,6 +149,41 @@ func DetectWorkflows( return workflows, schedules, nil } +func DetectScheduledWorkflows(gitRepo *git.Repository, commit *git.Commit) ([]*DetectedWorkflow, error) { + entries, err := ListWorkflows(commit) + if err != nil { + return nil, err + } + + wfs := make([]*DetectedWorkflow, 0, len(entries)) + for _, entry := range entries { + content, err := GetContentFromEntry(entry) + if err != nil { + return nil, err + } + + // one workflow may have multiple events + events, err := GetEventsFromContent(content) + if err != nil { + log.Warn("ignore invalid workflow %q: %v", entry.Name(), err) + continue + } + for _, evt := range events { + if evt.IsSchedule() { + log.Trace("detect scheduled workflow: %q", entry.Name()) + dwf := &DetectedWorkflow{ + EntryName: entry.Name(), + TriggerEvent: evt, + Content: content, + } + wfs = append(wfs, dwf) + } + } + } + + return wfs, nil +} + func detectMatched(gitRepo *git.Repository, commit *git.Commit, triggedEvent webhook_module.HookEventType, payload api.Payloader, evt *jobparser.Event) bool { if !canGithubEventMatch(evt.Name, triggedEvent) { return false diff --git a/routers/api/v1/repo/wiki.go b/routers/api/v1/repo/wiki.go index 347b7539b..f18ea087c 100644 --- a/routers/api/v1/repo/wiki.go +++ b/routers/api/v1/repo/wiki.go @@ -203,7 +203,7 @@ func getWikiPage(ctx *context.APIContext, wikiName wiki_service.WebPath) *api.Wi } return &api.WikiPage{ - WikiPageMetaData: convert.ToWikiPageMetaData(wikiName, lastCommit, ctx.Repo.Repository), + WikiPageMetaData: wiki_service.ToWikiPageMetaData(wikiName, lastCommit, ctx.Repo.Repository), ContentBase64: content, CommitCount: commitsCount, Sidebar: sidebarContent, @@ -333,7 +333,7 @@ func ListWikiPages(ctx *context.APIContext) { ctx.Error(http.StatusInternalServerError, "WikiFilenameToName", err) return } - pages = append(pages, convert.ToWikiPageMetaData(wikiName, c, ctx.Repo.Repository)) + pages = append(pages, wiki_service.ToWikiPageMetaData(wikiName, c, ctx.Repo.Repository)) } ctx.SetTotalCountHeader(int64(len(entries))) diff --git a/services/actions/notifier_helper.go b/services/actions/notifier_helper.go index 81ae97d55..33f8b00fd 100644 --- a/services/actions/notifier_helper.go +++ b/services/actions/notifier_helper.go @@ -530,3 +530,35 @@ func handleSchedules( return actions_model.CreateScheduleTask(ctx, crons) } + +// DetectAndHandleSchedules detects the schedule workflows on the default branch and create schedule tasks +func DetectAndHandleSchedules(ctx context.Context, repo *repo_model.Repository) error { + gitRepo, err := gitrepo.OpenRepository(context.Background(), repo) + if err != nil { + return fmt.Errorf("git.OpenRepository: %w", err) + } + defer gitRepo.Close() + + // Only detect schedule workflows on the default branch + commit, err := gitRepo.GetCommit(repo.DefaultBranch) + if err != nil { + return fmt.Errorf("gitRepo.GetCommit: %w", err) + } + scheduleWorkflows, err := actions_module.DetectScheduledWorkflows(gitRepo, commit) + if err != nil { + return fmt.Errorf("detect schedule workflows: %w", err) + } + if len(scheduleWorkflows) == 0 { + return nil + } + + // We need a notifyInput to call handleSchedules + // Here we use the commit author as the Doer of the notifyInput + commitUser, err := user_model.GetUserByEmail(ctx, commit.Author.Email) + if err != nil { + return fmt.Errorf("get user by email: %w", err) + } + notifyInput := newNotifyInput(repo, commitUser, webhook_module.HookEventSchedule) + + return handleSchedules(ctx, scheduleWorkflows, commit, notifyInput, repo.DefaultBranch) +} diff --git a/services/actions/schedule_tasks.go b/services/actions/schedule_tasks.go index e7aa4a39a..79dd84e0c 100644 --- a/services/actions/schedule_tasks.go +++ b/services/actions/schedule_tasks.go @@ -10,6 +10,7 @@ import ( actions_model "code.gitea.io/gitea/models/actions" "code.gitea.io/gitea/models/db" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/timeutil" @@ -65,8 +66,15 @@ func startTasks(ctx context.Context) error { } } - cfg := row.Repo.MustGetUnit(ctx, unit.TypeActions).ActionsConfig() - if cfg.IsWorkflowDisabled(row.Schedule.WorkflowID) { + cfg, err := row.Repo.GetUnit(ctx, unit.TypeActions) + if err != nil { + if repo_model.IsErrUnitTypeNotExist(err) { + // Skip the actions unit of this repo is disabled. + continue + } + return fmt.Errorf("GetUnit: %w", err) + } + if cfg.ActionsConfig().IsWorkflowDisabled(row.Schedule.WorkflowID) { continue } diff --git a/services/convert/wiki.go b/services/convert/wiki.go index 1f0484348..767bfdb88 100644 --- a/services/convert/wiki.go +++ b/services/convert/wiki.go @@ -6,11 +6,8 @@ package convert import ( "time" - repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/git" api "code.gitea.io/gitea/modules/structs" - "code.gitea.io/gitea/modules/util" - wiki_service "code.gitea.io/gitea/services/wiki" ) // ToWikiCommit convert a git commit into a WikiCommit @@ -46,15 +43,3 @@ func ToWikiCommitList(commits []*git.Commit, total int64) *api.WikiCommitList { Count: total, } } - -// ToWikiPageMetaData converts meta information to a WikiPageMetaData -func ToWikiPageMetaData(wikiName wiki_service.WebPath, lastCommit *git.Commit, repo *repo_model.Repository) *api.WikiPageMetaData { - subURL := string(wikiName) - _, title := wiki_service.WebPathToUserTitle(wikiName) - return &api.WikiPageMetaData{ - Title: title, - HTMLURL: util.URLJoin(repo.HTMLURL(), "wiki", subURL), - SubURL: subURL, - LastCommit: ToWikiCommit(lastCommit), - } -} diff --git a/services/repository/setting.go b/services/repository/setting.go index 6496ac401..b82f24271 100644 --- a/services/repository/setting.go +++ b/services/repository/setting.go @@ -12,6 +12,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/modules/log" + actions_service "code.gitea.io/gitea/services/actions" ) // UpdateRepositoryUnits updates a repository's units @@ -33,6 +34,15 @@ func UpdateRepositoryUnits(ctx context.Context, repo *repo_model.Repository, uni } } + for _, u := range units { + if u.Type == unit.TypeActions { + if err := actions_service.DetectAndHandleSchedules(ctx, repo); err != nil { + log.Error("DetectAndHandleSchedules: %v", err) + } + break + } + } + if _, err = db.GetEngine(ctx).Where("repo_id = ?", repo.ID).In("type", deleteUnitTypes).Delete(new(repo_model.RepoUnit)); err != nil { return err } diff --git a/services/wiki/wiki_path.go b/services/wiki/wiki_path.go index e51d6c630..74c706404 100644 --- a/services/wiki/wiki_path.go +++ b/services/wiki/wiki_path.go @@ -9,7 +9,10 @@ import ( "strings" repo_model "code.gitea.io/gitea/models/repo" + "code.gitea.io/gitea/modules/git" + api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" + "code.gitea.io/gitea/services/convert" ) // To define the wiki related concepts: @@ -155,3 +158,15 @@ func UserTitleToWebPath(base, title string) WebPath { } return WebPath(title) } + +// ToWikiPageMetaData converts meta information to a WikiPageMetaData +func ToWikiPageMetaData(wikiName WebPath, lastCommit *git.Commit, repo *repo_model.Repository) *api.WikiPageMetaData { + subURL := string(wikiName) + _, title := WebPathToUserTitle(wikiName) + return &api.WikiPageMetaData{ + Title: title, + HTMLURL: util.URLJoin(repo.HTMLURL(), "wiki", subURL), + SubURL: subURL, + LastCommit: convert.ToWikiCommit(lastCommit), + } +} From 07eeab863be65c5b7dedeee5d62e97eef3d8137c Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Thu, 8 Feb 2024 21:00:17 +0800 Subject: [PATCH 5/9] Only delete scheduled workflows when needed (#29091) Fix #29040 `handleSchedules` should be called only if `DetectWorkflows` should detect schedule workflows (cherry picked from commit e600c35f066c79b717dc0c416b07d5c34502d286) --- services/actions/notifier_helper.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/services/actions/notifier_helper.go b/services/actions/notifier_helper.go index 33f8b00fd..cc21d54f8 100644 --- a/services/actions/notifier_helper.go +++ b/services/actions/notifier_helper.go @@ -170,10 +170,11 @@ func notify(ctx context.Context, input *notifyInput) error { var detectedWorkflows []*actions_module.DetectedWorkflow actionsConfig := input.Repo.MustGetUnit(ctx, unit_model.TypeActions).ActionsConfig() + shouldDetectSchedules := input.Event == webhook_module.HookEventPush && git.RefName(input.Ref).BranchName() == input.Repo.DefaultBranch workflows, schedules, err := actions_module.DetectWorkflows(gitRepo, commit, input.Event, input.Payload, - input.Event == webhook_module.HookEventPush && git.RefName(input.Ref).BranchName() == input.Repo.DefaultBranch, + shouldDetectSchedules, ) if err != nil { return fmt.Errorf("DetectWorkflows: %w", err) @@ -226,8 +227,10 @@ func notify(ctx context.Context, input *notifyInput) error { } } - if err := handleSchedules(ctx, schedules, commit, input, ref); err != nil { - return err + if shouldDetectSchedules { + if err := handleSchedules(ctx, schedules, commit, input, ref); err != nil { + return err + } } return handleWorkflows(ctx, detectedWorkflows, commit, input, ref) From 869286595fe6b99ce882ece80e0795ab1f8b3a8d Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Tue, 5 Mar 2024 23:47:07 +0900 Subject: [PATCH 6/9] Add empty repo check in `DetectAndHandleSchedules` (#29606) ![image](https://github.com/go-gitea/gitea/assets/18380374/e6081301-bd3e-4cf6-ba4e-e574348dffb4) (cherry picked from commit 3f3335ae51d89520acce6573e870590423248c5c) --- services/actions/notifier_helper.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/services/actions/notifier_helper.go b/services/actions/notifier_helper.go index cc21d54f8..ff33c5a94 100644 --- a/services/actions/notifier_helper.go +++ b/services/actions/notifier_helper.go @@ -536,6 +536,10 @@ func handleSchedules( // DetectAndHandleSchedules detects the schedule workflows on the default branch and create schedule tasks func DetectAndHandleSchedules(ctx context.Context, repo *repo_model.Repository) error { + if repo.IsEmpty { + return nil + } + gitRepo, err := gitrepo.OpenRepository(context.Background(), repo) if err != nil { return fmt.Errorf("git.OpenRepository: %w", err) From 547d32c4928256a3e9b06bc854fb663622110955 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Thu, 21 Mar 2024 15:01:35 +0800 Subject: [PATCH 7/9] Cancel previous runs of the same PR automatically (#29961) Follow #25716. Also cancel previous runs for `pull_request_sync`. It's not a bug since it original PR said "if the event is push". The main change is https://github.com/go-gitea/gitea/pull/29961/files#diff-08adda3f8ae0360937f46abb1f4418603bd3518522baa356be11c6c7ac4abcc3. And also rename `CancelRunningJobs` to `CancelPreviousJobs` to make it more clear. (cherry picked from commit b150ff0bab3fc6c419edf1569a0271ebcb9734fa) --- models/actions/run.go | 9 +++++---- models/actions/schedule.go | 4 ++-- services/actions/notifier_helper.go | 10 +++++----- services/actions/schedule_tasks.go | 4 ++-- services/repository/branch.go | 8 ++++---- 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/models/actions/run.go b/models/actions/run.go index 5d4e3b74d..493bd0173 100644 --- a/models/actions/run.go +++ b/models/actions/run.go @@ -170,15 +170,16 @@ func updateRepoRunsNumbers(ctx context.Context, repo *repo_model.Repository) err return err } -// CancelRunningJobs cancels all running and waiting jobs associated with a specific workflow. -func CancelRunningJobs(ctx context.Context, repoID int64, ref, workflowID string, event webhook_module.HookEventType) error { - // Find all runs in the specified repository, reference, and workflow with statuses 'Running' or 'Waiting'. +// CancelPreviousJobs cancels all previous jobs of the same repository, reference, workflow, and event. +// It's useful when a new run is triggered, and all previous runs needn't be continued anymore. +func CancelPreviousJobs(ctx context.Context, repoID int64, ref, workflowID string, event webhook_module.HookEventType) error { + // Find all runs in the specified repository, reference, and workflow with non-final status runs, total, err := db.FindAndCount[ActionRun](ctx, FindRunOptions{ RepoID: repoID, Ref: ref, WorkflowID: workflowID, TriggerEvent: event, - Status: []Status{StatusRunning, StatusWaiting}, + Status: []Status{StatusRunning, StatusWaiting, StatusBlocked}, }) if err != nil { return err diff --git a/models/actions/schedule.go b/models/actions/schedule.go index d450e7aa0..3646a046a 100644 --- a/models/actions/schedule.go +++ b/models/actions/schedule.go @@ -127,14 +127,14 @@ func CleanRepoScheduleTasks(ctx context.Context, repo *repo_model.Repository) er return fmt.Errorf("DeleteCronTaskByRepo: %v", err) } // cancel running cron jobs of this repository and delete old schedules - if err := CancelRunningJobs( + if err := CancelPreviousJobs( ctx, repo.ID, repo.DefaultBranch, "", webhook_module.HookEventSchedule, ); err != nil { - return fmt.Errorf("CancelRunningJobs: %v", err) + return fmt.Errorf("CancelPreviousJobs: %v", err) } return nil } diff --git a/services/actions/notifier_helper.go b/services/actions/notifier_helper.go index ff33c5a94..5e01de2ca 100644 --- a/services/actions/notifier_helper.go +++ b/services/actions/notifier_helper.go @@ -348,17 +348,17 @@ func handleWorkflows( continue } - // cancel running jobs if the event is push - if run.Event == webhook_module.HookEventPush { - // cancel running jobs of the same workflow - if err := actions_model.CancelRunningJobs( + // cancel running jobs if the event is push or pull_request_sync + if run.Event == webhook_module.HookEventPush || + run.Event == webhook_module.HookEventPullRequestSync { + if err := actions_model.CancelPreviousJobs( ctx, run.RepoID, run.Ref, run.WorkflowID, run.Event, ); err != nil { - log.Error("CancelRunningJobs: %v", err) + log.Error("CancelPreviousJobs: %v", err) } } diff --git a/services/actions/schedule_tasks.go b/services/actions/schedule_tasks.go index 79dd84e0c..59862fd0d 100644 --- a/services/actions/schedule_tasks.go +++ b/services/actions/schedule_tasks.go @@ -55,14 +55,14 @@ func startTasks(ctx context.Context) error { // cancel running jobs if the event is push if row.Schedule.Event == webhook_module.HookEventPush { // cancel running jobs of the same workflow - if err := actions_model.CancelRunningJobs( + if err := actions_model.CancelPreviousJobs( ctx, row.RepoID, row.Schedule.Ref, row.Schedule.WorkflowID, webhook_module.HookEventSchedule, ); err != nil { - log.Error("CancelRunningJobs: %v", err) + log.Error("CancelPreviousJobs: %v", err) } } diff --git a/services/repository/branch.go b/services/repository/branch.go index cab1d5639..02ae80992 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -371,14 +371,14 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, doer *user_m log.Error("DeleteCronTaskByRepo: %v", err) } // cancel running cron jobs of this repository and delete old schedules - if err := actions_model.CancelRunningJobs( + if err := actions_model.CancelPreviousJobs( ctx, repo.ID, from, "", webhook_module.HookEventSchedule, ); err != nil { - log.Error("CancelRunningJobs: %v", err) + log.Error("CancelPreviousJobs: %v", err) } err2 = gitRepo.SetDefaultBranch(ctx, repo, to) @@ -536,14 +536,14 @@ func SetRepoDefaultBranch(ctx context.Context, repo *repo_model.Repository, gitR log.Error("DeleteCronTaskByRepo: %v", err) } // cancel running cron jobs of this repository and delete old schedules - if err := actions_model.CancelRunningJobs( + if err := actions_model.CancelPreviousJobs( ctx, repo.ID, oldDefaultBranchName, "", webhook_module.HookEventSchedule, ); err != nil { - log.Error("CancelRunningJobs: %v", err) + log.Error("CancelPreviousJobs: %v", err) } if err := gitRepo.SetDefaultBranch(newBranchName); err != nil { From 52e9d2db063bf87b7b2ea043d020115c23f032c2 Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Mon, 25 Mar 2024 16:54:22 +0100 Subject: [PATCH 8/9] [DEADCODE] update --- .deadcode-out | 1 - 1 file changed, 1 deletion(-) diff --git a/.deadcode-out b/.deadcode-out index c728ea81a..1b65e3203 100644 --- a/.deadcode-out +++ b/.deadcode-out @@ -329,7 +329,6 @@ package "code.gitea.io/gitea/services/pull" package "code.gitea.io/gitea/services/repository" func IsErrForkAlreadyExist - func UpdateRepositoryUnits package "code.gitea.io/gitea/services/repository/archiver" func ArchiveRepository From a117dfd2b1e37b36eb29b7d9cf1f59581697fd6b Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Mon, 25 Mar 2024 16:54:38 +0100 Subject: [PATCH 9/9] [ACTIONS] port scheduled actions from Gitea Refs: https://codeberg.org/forgejo/forgejo/issues/2798 --- routers/web/repo/setting/default_branch.go | 7 +++---- services/repository/branch.go | 4 ++-- tests/integration/integration_test.go | 2 +- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/routers/web/repo/setting/default_branch.go b/routers/web/repo/setting/default_branch.go index 12e1ab7d1..881d148af 100644 --- a/routers/web/repo/setting/default_branch.go +++ b/routers/web/repo/setting/default_branch.go @@ -6,12 +6,11 @@ package setting import ( "net/http" - repo_model "code.gitea.io/gitea/models/repo" - "code.gitea.io/gitea/modules/git" - "code.gitea.io/gitea/modules/gitrepo" + git_model "code.gitea.io/gitea/models/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/routers/web/repo" + "code.gitea.io/gitea/services/context" repo_service "code.gitea.io/gitea/services/repository" ) @@ -37,7 +36,7 @@ func SetDefaultBranchPost(ctx *context.Context) { branch := ctx.FormString("branch") if err := repo_service.SetRepoDefaultBranch(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, branch); err != nil { switch { - case ctx.Repo.GitRepo.IsErrBranchNotExist(err): + case git_model.IsErrBranchNotExist(err): ctx.Status(http.StatusNotFound) default: ctx.ServerError("SetDefaultBranch", err) diff --git a/services/repository/branch.go b/services/repository/branch.go index 02ae80992..9d7193410 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -381,7 +381,7 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, doer *user_m log.Error("CancelPreviousJobs: %v", err) } - err2 = gitRepo.SetDefaultBranch(ctx, repo, to) + err2 = gitrepo.SetDefaultBranch(ctx, repo, to) if err2 != nil { return err2 } @@ -546,7 +546,7 @@ func SetRepoDefaultBranch(ctx context.Context, repo *repo_model.Repository, gitR log.Error("CancelPreviousJobs: %v", err) } - if err := gitRepo.SetDefaultBranch(newBranchName); err != nil { + if err := gitrepo.SetDefaultBranch(ctx, repo, newBranchName); err != nil { if !git.IsErrUnsupportedVersion(err) { return err } diff --git a/tests/integration/integration_test.go b/tests/integration/integration_test.go index 76b36a734..e8f28105c 100644 --- a/tests/integration/integration_test.go +++ b/tests/integration/integration_test.go @@ -635,7 +635,7 @@ func CreateDeclarativeRepo(t *testing.T, owner *user_model.User, name string, en } } - err := repo_model.UpdateRepositoryUnits(db.DefaultContext, repo, units, disabledUnits) + err := repo_service.UpdateRepositoryUnits(db.DefaultContext, repo, units, disabledUnits) assert.NoError(t, err) }