Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #23725: Actions - send Email on Success/Failure #33601

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions services/actions/job_emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
62 changes: 62 additions & 0 deletions services/mailer/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"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"
Expand Down Expand Up @@ -202,4 +203,65 @@
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) {

Check failure on line 206 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / backend

syntax error: unexpected name context in argument list; possibly missing comma or )

Check failure on line 206 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / test-pgsql

syntax error: unexpected name context in argument list; possibly missing comma or )

Check failure on line 206 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / test-sqlite

syntax error: unexpected name context in argument list; possibly missing comma or )

Check failure on line 206 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / test-mysql

syntax error: unexpected name context in argument list; possibly missing comma or )

Check failure on line 206 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / test-mssql

syntax error: unexpected name context in argument list; possibly missing comma or )

Check failure on line 206 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / test-unit

syntax error: unexpected name context in argument list; possibly missing comma or )

Check failure on line 206 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / checks-backend

missing ',' in argument list

Check failure on line 206 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / test-e2e

syntax error: unexpected name context in argument list; possibly missing comma or )

Check failure on line 206 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / lint-backend

syntax error: unexpected name context in argument list; possibly missing comma or )) (typecheck)

Check failure on line 206 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / lint-backend

syntax error: unexpected name context in argument list; possibly missing comma or )) (typecheck)

Check failure on line 206 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / lint-backend

syntax error: unexpected name context in argument list; possibly missing comma or )) (typecheck)

Check failure on line 206 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / lint-backend

syntax error: unexpected name context in argument list; possibly missing comma or )) (typecheck)

Check failure on line 206 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / lint-backend

syntax error: unexpected name context in argument list; possibly missing comma or )) (typecheck)

Check failure on line 206 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

syntax error: unexpected name context in argument list; possibly missing comma or )) (typecheck)

Check failure on line 206 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

syntax error: unexpected name context in argument list; possibly missing comma or )) (typecheck)

Check failure on line 206 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

syntax error: unexpected name context in argument list; possibly missing comma or )) (typecheck)

Check failure on line 206 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

syntax error: unexpected name context in argument list; possibly missing comma or )) (typecheck)

Check failure on line 206 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

syntax error: unexpected name context in argument list; possibly missing comma or )) (typecheck)

Check failure on line 206 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

syntax error: unexpected name context in argument list; possibly missing comma or )) (typecheck)

Check failure on line 206 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

syntax error: unexpected name context in argument list; possibly missing comma or )) (typecheck)

Check failure on line 206 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

syntax error: unexpected name context in argument list; possibly missing comma or )) (typecheck)

Check failure on line 206 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

syntax error: unexpected name context in argument list; possibly missing comma or )) (typecheck)

Check failure on line 206 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

syntax error: unexpected name context in argument list; possibly missing comma or )) (typecheck)
if run.Status != actions_model.StatusSuccess && run.Status != actions_model.StatusFailure {

Check failure on line 207 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / checks-backend

missing ',' in argument list
return

Check failure on line 208 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / checks-backend

expected operand, found 'return'
}

if err := run.LoadAttributes(ctx); err != nil {

Check failure on line 211 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / checks-backend

missing ',' in composite literal
log.Error("LoadAttributes: %v", err)

Check failure on line 212 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / checks-backend

missing ',' before newline in composite literal
return

Check failure on line 213 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / checks-backend

expected operand, found 'return'
}

subject := fmt.Sprintf("[%s] Workflow run %s: %s",
run.Repo.FullName(),
run.WorkflowID,
run.Status,
)

commitSHA := run.CommitSHA
if len(commitSHA) > 7 {

Check failure on line 223 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / checks-backend

missing ',' in composite literal
commitSHA = commitSHA[:7]

Check failure on line 224 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / checks-backend

expected '==', found '='
}

Check failure on line 225 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / checks-backend

missing ',' before newline in composite literal

body := fmt.Sprintf(`Workflow "%s" run #%d has completed with status: %s

Check failure on line 227 in services/mailer/notify.go

View workflow job for this annotation

GitHub Actions / checks-backend

missing ',' in composite literal
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)
}
}
}
5 changes: 4 additions & 1 deletion services/notify/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
8 changes: 8 additions & 0 deletions services/notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
}
5 changes: 5 additions & 0 deletions services/notify/null.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {}
Loading