diff --git a/services/actions/job_emitter.go b/services/actions/job_emitter.go index 1f859fcf70506..d0f4bfe31b6da 100644 --- a/services/actions/job_emitter.go +++ b/services/actions/job_emitter.go @@ -11,7 +11,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" @@ -70,6 +72,12 @@ 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 a27177e8f52ef..b154b93a9e263 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,4 +203,65 @@ func (m *mailNotifier) RepoPendingTransfer(ctx context.Context, doer, newOwner * if err := SendRepoTransferNotifyMail(ctx, doer, newOwner, repo); err != nil { log.Error("SendRepoTransferNotifyMail: %v", err) } +func (m *mailNotifier) ActionRunFinished(ctx context.Context, run *actions_model.ActionRun) { + if run.Status != actions_model.StatusSuccess && run.Status != actions_model.StatusFailure { + return + } + + if err := run.LoadAttributes(ctx); err != nil { + log.Error("LoadAttributes: %v", err) + return + } + + subject := fmt.Sprintf("[%s] Workflow run %s: %s", + run.Repo.FullName(), + run.WorkflowID, + run.Status, + ) + + commitSHA := run.CommitSHA + if len(commitSHA) > 7 { + commitSHA = commitSHA[:7] + } + + body := fmt.Sprintf(`Workflow "%s" run #%d has completed with status: %s +Repository: %s +Branch: %s +Commit: %s +Triggered by: %s +View the run details here: %s`, + run.WorkflowID, + run.Index, + run.Status, + run.Repo.FullName(), + run.PrettyRef(), + commitSHA, + run.TriggerUser.Name, + run.HTMLURL(), + ) + + // Send to repo owner + if run.Repo.Owner.Email != "" && + run.Repo.Owner.EmailNotificationsPreference != user_model.EmailNotificationsDisabled { + if err := mailer.SendAsyncEmail(ctx, mailer.Mail{ + To: []string{run.Repo.Owner.Email}, + Subject: subject, + Body: body, + }); err != nil { + log.Error("Failed to send email to repo owner %s: %v", run.Repo.Owner.Email, err) + } + } + + // Send to trigger user + if run.TriggerUser.ID != run.Repo.Owner.ID && + run.TriggerUser.Email != "" && + run.TriggerUser.EmailNotificationsPreference != user_model.EmailNotificationsDisabled { + if err := mailer.SendAsyncEmail(ctx, mailer.Mail{ + To: []string{run.TriggerUser.Email}, + Subject: subject, + Body: body, + }); err != nil { + log.Error("Failed to send email to trigger user %s: %v", run.TriggerUser.Email, err) + } + } } diff --git a/services/notify/notifier.go b/services/notify/notifier.go index 29bbb5702b3bd..ee713ad3eb611 100644 --- a/services/notify/notifier.go +++ b/services/notify/notifier.go @@ -5,7 +5,8 @@ package notify import ( "context" - + + actions_model "code.gitea.io/gitea/models/actions" git_model "code.gitea.io/gitea/models/git" issues_model "code.gitea.io/gitea/models/issues" packages_model "code.gitea.io/gitea/models/packages" @@ -76,5 +77,7 @@ type Notifier interface { ChangeDefaultBranch(ctx context.Context, repo *repo_model.Repository) + ActionRunFinished(ctx context.Context, run *actions_model.ActionRun) + CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, commit *repository.PushCommit, sender *user_model.User, status *git_model.CommitStatus) } diff --git a/services/notify/notify.go b/services/notify/notify.go index c97d0fcbaf0af..e9ab638b742c7 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" git_model "code.gitea.io/gitea/models/git" issues_model "code.gitea.io/gitea/models/issues" packages_model "code.gitea.io/gitea/models/packages" @@ -374,3 +375,10 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, commit notifier.CreateCommitStatus(ctx, repo, commit, sender, status) } } + +// 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 7354efd70157c..5500ab0e42889 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" git_model "code.gitea.io/gitea/models/git" issues_model "code.gitea.io/gitea/models/issues" packages_model "code.gitea.io/gitea/models/packages" @@ -212,3 +213,7 @@ func (*NullNotifier) ChangeDefaultBranch(ctx context.Context, repo *repo_model.R func (*NullNotifier) CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, commit *repository.PushCommit, sender *user_model.User, status *git_model.CommitStatus) { } + + +// ActionRunFinished represents action run finished +func (*NullNotifier) ActionRunFinished(ctx context.Context, run *actions_model.ActionRun) {}