Skip to content

Commit fe8622d

Browse files
committed
Revert "Fix schedule tasks bugs (go-gitea#28691)"
This reverts commit 97292da. (cherry picked from commit 83e5eba0311dc601518fb1a07a7e8538e573a837) (cherry picked from commit f6ef8f3819b5990858b0997cac72af000a3f3e3a) Conflicts: services/repository/setting.go
1 parent 0fd9d08 commit fe8622d

File tree

19 files changed

+88
-204
lines changed

19 files changed

+88
-204
lines changed

models/actions/run.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,13 @@ func updateRepoRunsNumbers(ctx context.Context, repo *repo_model.Repository) err
171171
}
172172

173173
// CancelRunningJobs cancels all running and waiting jobs associated with a specific workflow.
174-
func CancelRunningJobs(ctx context.Context, repoID int64, ref, workflowID string, event webhook_module.HookEventType) error {
174+
func CancelRunningJobs(ctx context.Context, repoID int64, ref, workflowID string) error {
175175
// Find all runs in the specified repository, reference, and workflow with statuses 'Running' or 'Waiting'.
176176
runs, total, err := db.FindAndCount[ActionRun](ctx, FindRunOptions{
177-
RepoID: repoID,
178-
Ref: ref,
179-
WorkflowID: workflowID,
180-
TriggerEvent: event,
181-
Status: []Status{StatusRunning, StatusWaiting},
177+
RepoID: repoID,
178+
Ref: ref,
179+
WorkflowID: workflowID,
180+
Status: []Status{StatusRunning, StatusWaiting},
182181
})
183182
if err != nil {
184183
return err

models/actions/run_list.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
repo_model "code.gitea.io/gitea/models/repo"
1111
user_model "code.gitea.io/gitea/models/user"
1212
"code.gitea.io/gitea/modules/container"
13-
webhook_module "code.gitea.io/gitea/modules/webhook"
1413

1514
"xorm.io/builder"
1615
)
@@ -72,7 +71,6 @@ type FindRunOptions struct {
7271
WorkflowID string
7372
Ref string // the commit/tag/… that caused this workflow
7473
TriggerUserID int64
75-
TriggerEvent webhook_module.HookEventType
7674
Approved bool // not util.OptionalBool, it works only when it's true
7775
Status []Status
7876
}
@@ -100,9 +98,6 @@ func (opts FindRunOptions) ToConds() builder.Cond {
10098
if opts.Ref != "" {
10199
cond = cond.And(builder.Eq{"ref": opts.Ref})
102100
}
103-
if opts.TriggerEvent != "" {
104-
cond = cond.And(builder.Eq{"trigger_event": opts.TriggerEvent})
105-
}
106101
return cond
107102
}
108103

models/actions/schedule.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package actions
55

66
import (
77
"context"
8-
"fmt"
98
"time"
109

1110
"code.gitea.io/gitea/models/db"
@@ -119,22 +118,3 @@ func DeleteScheduleTaskByRepo(ctx context.Context, id int64) error {
119118

120119
return committer.Commit()
121120
}
122-
123-
func CleanRepoScheduleTasks(ctx context.Context, repo *repo_model.Repository) error {
124-
// If actions disabled when there is schedule task, this will remove the outdated schedule tasks
125-
// There is no other place we can do this because the app.ini will be changed manually
126-
if err := DeleteScheduleTaskByRepo(ctx, repo.ID); err != nil {
127-
return fmt.Errorf("DeleteCronTaskByRepo: %v", err)
128-
}
129-
// cancel running cron jobs of this repository and delete old schedules
130-
if err := CancelRunningJobs(
131-
ctx,
132-
repo.ID,
133-
repo.DefaultBranch,
134-
"",
135-
webhook_module.HookEventSchedule,
136-
); err != nil {
137-
return fmt.Errorf("CancelRunningJobs: %v", err)
138-
}
139-
return nil
140-
}

models/git/branch.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ func FindRenamedBranch(ctx context.Context, repoID int64, from string) (branch *
287287
}
288288

289289
// RenameBranch rename a branch
290-
func RenameBranch(ctx context.Context, repo *repo_model.Repository, from, to string, gitAction func(ctx context.Context, isDefault bool) error) (err error) {
290+
func RenameBranch(ctx context.Context, repo *repo_model.Repository, from, to string, gitAction func(isDefault bool) error) (err error) {
291291
ctx, committer, err := db.TxContext(ctx)
292292
if err != nil {
293293
return err
@@ -362,7 +362,7 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, from, to str
362362
}
363363

364364
// 5. do git action
365-
if err = gitAction(ctx, isDefault); err != nil {
365+
if err = gitAction(isDefault); err != nil {
366366
return err
367367
}
368368

models/git/branch_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package git_test
55

66
import (
7-
"context"
87
"testing"
98

109
"code.gitea.io/gitea/models/db"
@@ -133,7 +132,7 @@ func TestRenameBranch(t *testing.T) {
133132
}, git_model.WhitelistOptions{}))
134133
assert.NoError(t, committer.Commit())
135134

136-
assert.NoError(t, git_model.RenameBranch(db.DefaultContext, repo1, "master", "main", func(ctx context.Context, isDefault bool) error {
135+
assert.NoError(t, git_model.RenameBranch(db.DefaultContext, repo1, "master", "main", func(isDefault bool) error {
137136
_isDefault = isDefault
138137
return nil
139138
}))

models/repo/repo_unit.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,29 @@ func UpdateRepoUnit(ctx context.Context, unit *RepoUnit) error {
314314
_, err := db.GetEngine(ctx).ID(unit.ID).Update(unit)
315315
return err
316316
}
317+
318+
// UpdateRepositoryUnits updates a repository's units
319+
func UpdateRepositoryUnits(ctx context.Context, repo *Repository, units []RepoUnit, deleteUnitTypes []unit.Type) (err error) {
320+
ctx, committer, err := db.TxContext(ctx)
321+
if err != nil {
322+
return err
323+
}
324+
defer committer.Close()
325+
326+
// Delete existing settings of units before adding again
327+
for _, u := range units {
328+
deleteUnitTypes = append(deleteUnitTypes, u.Type)
329+
}
330+
331+
if _, err = db.GetEngine(ctx).Where("repo_id = ?", repo.ID).In("type", deleteUnitTypes).Delete(new(RepoUnit)); err != nil {
332+
return err
333+
}
334+
335+
if len(units) > 0 {
336+
if err = db.Insert(ctx, units); err != nil {
337+
return err
338+
}
339+
}
340+
341+
return committer.Commit()
342+
}

modules/actions/github.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ const (
2222
GithubEventRelease = "release"
2323
GithubEventPullRequestComment = "pull_request_comment"
2424
GithubEventGollum = "gollum"
25-
GithubEventSchedule = "schedule"
2625
)
2726

2827
// canGithubEventMatch check if the input Github event can match any Gitea event.
@@ -70,9 +69,6 @@ func canGithubEventMatch(eventName string, triggedEvent webhook_module.HookEvent
7069
return false
7170
}
7271

73-
case GithubEventSchedule:
74-
return triggedEvent == webhook_module.HookEventSchedule
75-
7672
default:
7773
return eventName == string(triggedEvent)
7874
}

modules/actions/workflows.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222

2323
type DetectedWorkflow struct {
2424
EntryName string
25-
TriggerEvent *jobparser.Event
25+
TriggerEvent string
2626
Content []byte
2727
}
2828

@@ -103,7 +103,6 @@ func DetectWorkflows(
103103
commit *git.Commit,
104104
triggedEvent webhook_module.HookEventType,
105105
payload api.Payloader,
106-
detectSchedule bool,
107106
) ([]*DetectedWorkflow, []*DetectedWorkflow, error) {
108107
entries, err := ListWorkflows(commit)
109108
if err != nil {
@@ -118,7 +117,6 @@ func DetectWorkflows(
118117
return nil, nil, err
119118
}
120119

121-
// one workflow may have multiple events
122120
events, err := GetEventsFromContent(content)
123121
if err != nil {
124122
log.Warn("ignore invalid workflow %q: %v", entry.Name(), err)
@@ -127,18 +125,17 @@ func DetectWorkflows(
127125
for _, evt := range events {
128126
log.Trace("detect workflow %q for event %#v matching %q", entry.Name(), evt, triggedEvent)
129127
if evt.IsSchedule() {
130-
if detectSchedule {
131-
dwf := &DetectedWorkflow{
132-
EntryName: entry.Name(),
133-
TriggerEvent: evt,
134-
Content: content,
135-
}
136-
schedules = append(schedules, dwf)
128+
dwf := &DetectedWorkflow{
129+
EntryName: entry.Name(),
130+
TriggerEvent: evt.Name,
131+
Content: content,
137132
}
138-
} else if detectMatched(gitRepo, commit, triggedEvent, payload, evt) {
133+
schedules = append(schedules, dwf)
134+
}
135+
if detectMatched(gitRepo, commit, triggedEvent, payload, evt) {
139136
dwf := &DetectedWorkflow{
140137
EntryName: entry.Name(),
141-
TriggerEvent: evt,
138+
TriggerEvent: evt.Name,
142139
Content: content,
143140
}
144141
workflows = append(workflows, dwf)
@@ -159,8 +156,7 @@ func detectMatched(gitRepo *git.Repository, commit *git.Commit, triggedEvent web
159156
webhook_module.HookEventCreate,
160157
webhook_module.HookEventDelete,
161158
webhook_module.HookEventFork,
162-
webhook_module.HookEventWiki,
163-
webhook_module.HookEventSchedule:
159+
webhook_module.HookEventWiki:
164160
if len(evt.Acts()) != 0 {
165161
log.Warn("Ignore unsupported %s event arguments %v", triggedEvent, evt.Acts())
166162
}

modules/actions/workflows_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,6 @@ func TestDetectMatched(t *testing.T) {
118118
yamlOn: "on: gollum",
119119
expected: true,
120120
},
121-
{
122-
desc: "HookEventSchedue(schedule) matches GithubEventSchedule(schedule)",
123-
triggedEvent: webhook_module.HookEventSchedule,
124-
payload: nil,
125-
yamlOn: "on: schedule",
126-
expected: true,
127-
},
128121
}
129122

130123
for _, tc := range testCases {

modules/webhook/type.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ const (
3131
HookEventRepository HookEventType = "repository"
3232
HookEventRelease HookEventType = "release"
3333
HookEventPackage HookEventType = "package"
34-
HookEventSchedule HookEventType = "schedule"
3534
)
3635

3736
// Event returns the HookEventType as an event string

routers/api/v1/repo/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
984984
}
985985

986986
if len(units)+len(deleteUnitTypes) > 0 {
987-
if err := repo_service.UpdateRepositoryUnits(ctx, repo, units, deleteUnitTypes); err != nil {
987+
if err := repo_model.UpdateRepositoryUnits(ctx, repo, units, deleteUnitTypes); err != nil {
988988
ctx.Error(http.StatusInternalServerError, "UpdateRepositoryUnits", err)
989989
return err
990990
}

routers/web/repo/setting/default_branch.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ package setting
66
import (
77
"net/http"
88

9-
git_model "code.gitea.io/gitea/models/git"
9+
repo_model "code.gitea.io/gitea/models/repo"
1010
"code.gitea.io/gitea/modules/context"
11+
"code.gitea.io/gitea/modules/git"
1112
"code.gitea.io/gitea/modules/log"
1213
"code.gitea.io/gitea/modules/setting"
1314
"code.gitea.io/gitea/routers/web/repo"
14-
repo_service "code.gitea.io/gitea/services/repository"
15+
notify_service "code.gitea.io/gitea/services/notify"
1516
)
1617

1718
// SetDefaultBranchPost set default branch
@@ -34,14 +35,23 @@ func SetDefaultBranchPost(ctx *context.Context) {
3435
}
3536

3637
branch := ctx.FormString("branch")
37-
if err := repo_service.SetRepoDefaultBranch(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, branch); err != nil {
38-
switch {
39-
case git_model.IsErrBranchNotExist(err):
40-
ctx.Status(http.StatusNotFound)
41-
default:
38+
if !ctx.Repo.GitRepo.IsBranchExist(branch) {
39+
ctx.Status(http.StatusNotFound)
40+
return
41+
} else if repo.DefaultBranch != branch {
42+
repo.DefaultBranch = branch
43+
if err := ctx.Repo.GitRepo.SetDefaultBranch(branch); err != nil {
44+
if !git.IsErrUnsupportedVersion(err) {
45+
ctx.ServerError("SetDefaultBranch", err)
46+
return
47+
}
48+
}
49+
if err := repo_model.UpdateDefaultBranch(ctx, repo); err != nil {
4250
ctx.ServerError("SetDefaultBranch", err)
51+
return
4352
}
44-
return
53+
54+
notify_service.ChangeDefaultBranch(ctx, repo)
4555
}
4656

4757
log.Trace("Repository basic settings updated: %s/%s", ctx.Repo.Owner.Name, repo.Name)

routers/web/repo/setting/setting.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ func SettingsPost(ctx *context.Context) {
601601
return
602602
}
603603

604-
if err := repo_service.UpdateRepositoryUnits(ctx, repo, units, deleteUnitTypes); err != nil {
604+
if err := repo_model.UpdateRepositoryUnits(ctx, repo, units, deleteUnitTypes); err != nil {
605605
ctx.ServerError("UpdateRepositoryUnits", err)
606606
return
607607
}

0 commit comments

Comments
 (0)