Skip to content

Commit ef92f56

Browse files
committed
add DetectAndHandleSchedules
1 parent 0dc1361 commit ef92f56

File tree

3 files changed

+66
-5
lines changed

3 files changed

+66
-5
lines changed

modules/actions/workflows.go

+35
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,41 @@ func DetectWorkflows(
146146
return workflows, schedules, nil
147147
}
148148

149+
func DetectScheduledWorkflows(gitRepo *git.Repository, commit *git.Commit) ([]*DetectedWorkflow, error) {
150+
entries, err := ListWorkflows(commit)
151+
if err != nil {
152+
return nil, err
153+
}
154+
155+
wfs := make([]*DetectedWorkflow, 0, len(entries))
156+
for _, entry := range entries {
157+
content, err := GetContentFromEntry(entry)
158+
if err != nil {
159+
return nil, err
160+
}
161+
162+
// one workflow may have multiple events
163+
events, err := GetEventsFromContent(content)
164+
if err != nil {
165+
log.Warn("ignore invalid workflow %q: %v", entry.Name(), err)
166+
continue
167+
}
168+
for _, evt := range events {
169+
log.Trace("detect scheduled workflow: %q", entry.Name())
170+
if evt.IsSchedule() {
171+
dwf := &DetectedWorkflow{
172+
EntryName: entry.Name(),
173+
TriggerEvent: evt,
174+
Content: content,
175+
}
176+
wfs = append(wfs, dwf)
177+
}
178+
}
179+
}
180+
181+
return wfs, nil
182+
}
183+
149184
func detectMatched(gitRepo *git.Repository, commit *git.Commit, triggedEvent webhook_module.HookEventType, payload api.Payloader, evt *jobparser.Event) bool {
150185
if !canGithubEventMatch(evt.Name, triggedEvent) {
151186
return false

services/actions/notifier_helper.go

+30-3
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,34 @@ func handleSchedules(
475475
return actions_model.CreateScheduleTask(ctx, crons)
476476
}
477477

478-
func DetactAndHandleSchedules(ctx context.Context, repo *repo_model.Repository) error {
479-
// TODO
480-
return nil
478+
// DetectAndHandleSchedules detects the schedule workflows on the default branch and create schedule tasks
479+
func DetectAndHandleSchedules(ctx context.Context, repo *repo_model.Repository) error {
480+
gitRepo, err := gitrepo.OpenRepository(context.Background(), repo)
481+
if err != nil {
482+
return fmt.Errorf("git.OpenRepository: %w", err)
483+
}
484+
defer gitRepo.Close()
485+
486+
// Only detect schedule workflows on the default branch
487+
commit, err := gitRepo.GetCommit(repo.DefaultBranch)
488+
if err != nil {
489+
return fmt.Errorf("gitRepo.GetCommit: %w", err)
490+
}
491+
scheduleWorkflows, err := actions_module.DetectScheduledWorkflows(gitRepo, commit)
492+
if err != nil {
493+
return fmt.Errorf("detect schedule workflows: %w", err)
494+
}
495+
if len(scheduleWorkflows) == 0 {
496+
return nil
497+
}
498+
499+
// We need a notifyInput to call handleSchedules
500+
// Here we use the commit author as the Doer of the notifyInput
501+
commitUser, err := user_model.GetUserByEmail(ctx, commit.Author.Email)
502+
if err != nil {
503+
return fmt.Errorf("get user by email: %w", err)
504+
}
505+
notifyInput := newNotifyInput(repo, commitUser, webhook_module.HookEventSchedule)
506+
507+
return handleSchedules(ctx, scheduleWorkflows, commit, notifyInput, repo.DefaultBranch)
481508
}

services/repository/setting.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package repository
55

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

1110
actions_model "code.gitea.io/gitea/models/actions"
@@ -38,7 +37,7 @@ func UpdateRepositoryUnits(ctx context.Context, repo *repo_model.Repository, uni
3837
for _, u := range units {
3938
if u.Type == unit.TypeActions {
4039
if err := actions_service.DetectAndHandleSchedules(ctx, repo); err != nil {
41-
return fmt.Errorf("detect and handle schedule workflows: %w", err)
40+
log.Error("DetectAndHandleSchedules: %v", err)
4241
}
4342
break
4443
}

0 commit comments

Comments
 (0)