From c07ed745c772efe462771cefa420033396a33b23 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 4 Apr 2024 22:59:04 +0800 Subject: [PATCH] Add a new event for actions finished and send email to related users --- services/actions/job_emitter.go | 10 ++++++++++ services/mailer/notify.go | 5 +++++ services/notify/notifier.go | 3 +++ services/notify/notify.go | 8 ++++++++ services/notify/null.go | 4 ++++ 5 files changed, 30 insertions(+) diff --git a/services/actions/job_emitter.go b/services/actions/job_emitter.go index d2bbbd9a7c..0784eba0d4 100644 --- a/services/actions/job_emitter.go +++ b/services/actions/job_emitter.go @@ -12,7 +12,9 @@ import ( actions_model "code.gitea.io/gitea/models/actions" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/graceful" + "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/queue" + notify_service "code.gitea.io/gitea/services/notify" "github.com/nektos/act/pkg/jobparser" "xorm.io/builder" @@ -71,6 +73,14 @@ func checkJobsOfRun(ctx context.Context, runID int64) error { }); err != nil { return err } + + run, _, err := db.GetByID[actions_model.ActionRun](ctx, runID) + if err != nil { + log.Error("GetByID failed: %v", err) + } else if run.Status == actions_model.StatusSuccess || run.Status == actions_model.StatusFailure { + notify_service.ActionRunFinished(ctx, run) + } + CreateCommitStatus(ctx, jobs...) return nil } diff --git a/services/mailer/notify.go b/services/mailer/notify.go index e48b5d399d..fa92c99c25 100644 --- a/services/mailer/notify.go +++ b/services/mailer/notify.go @@ -7,6 +7,7 @@ import ( "context" "fmt" + actions_model "code.gitea.io/gitea/models/actions" activities_model "code.gitea.io/gitea/models/activities" issues_model "code.gitea.io/gitea/models/issues" repo_model "code.gitea.io/gitea/models/repo" @@ -202,3 +203,7 @@ func (m *mailNotifier) RepoPendingTransfer(ctx context.Context, doer, newOwner * log.Error("SendRepoTransferNotifyMail: %v", err) } } + +func (m *mailNotifier) ActionRunFinished(ctx context.Context, run *actions_model.ActionRun) { + // TODO: send email to related users +} diff --git a/services/notify/notifier.go b/services/notify/notifier.go index ed053a812a..76934f7728 100644 --- a/services/notify/notifier.go +++ b/services/notify/notifier.go @@ -6,6 +6,7 @@ package notify import ( "context" + actions_model "code.gitea.io/gitea/models/actions" issues_model "code.gitea.io/gitea/models/issues" packages_model "code.gitea.io/gitea/models/packages" repo_model "code.gitea.io/gitea/models/repo" @@ -74,4 +75,6 @@ type Notifier interface { PackageDelete(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor) ChangeDefaultBranch(ctx context.Context, repo *repo_model.Repository) + + ActionRunFinished(ctx context.Context, run *actions_model.ActionRun) } diff --git a/services/notify/notify.go b/services/notify/notify.go index 16fbb6325d..6341ccb204 100644 --- a/services/notify/notify.go +++ b/services/notify/notify.go @@ -6,6 +6,7 @@ package notify import ( "context" + actions_model "code.gitea.io/gitea/models/actions" issues_model "code.gitea.io/gitea/models/issues" packages_model "code.gitea.io/gitea/models/packages" repo_model "code.gitea.io/gitea/models/repo" @@ -367,3 +368,10 @@ func ChangeDefaultBranch(ctx context.Context, repo *repo_model.Repository) { notifier.ChangeDefaultBranch(ctx, repo) } } + +// ActionRunFinished represents action run finished +func ActionRunFinished(ctx context.Context, run *actions_model.ActionRun) { + for _, notifier := range notifiers { + notifier.ActionRunFinished(ctx, run) + } +} diff --git a/services/notify/null.go b/services/notify/null.go index dddd421bef..1ac4deb507 100644 --- a/services/notify/null.go +++ b/services/notify/null.go @@ -6,6 +6,7 @@ package notify import ( "context" + actions_model "code.gitea.io/gitea/models/actions" issues_model "code.gitea.io/gitea/models/issues" packages_model "code.gitea.io/gitea/models/packages" repo_model "code.gitea.io/gitea/models/repo" @@ -208,3 +209,6 @@ func (*NullNotifier) PackageDelete(ctx context.Context, doer *user_model.User, p // ChangeDefaultBranch places a place holder function func (*NullNotifier) ChangeDefaultBranch(ctx context.Context, repo *repo_model.Repository) { } + +// ActionRunFinished represents action run finished +func (*NullNotifier) ActionRunFinished(ctx context.Context, run *actions_model.ActionRun) {}