mirror of
https://github.com/go-gitea/gitea.git
synced 2025-07-21 09:31:19 +02:00
Send email on Workflow Run Success/Failure (#34982)
Closes #23725   /claim #23725 --------- Signed-off-by: NorthRealm <155140859+NorthRealm@users.noreply.github.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: ChristopherHX <christopher.homberger@web.de>
This commit is contained in:
@@ -16,7 +16,7 @@ import (
|
||||
|
||||
func MailPreviewRender(ctx *context.Context) {
|
||||
tmplName := ctx.PathParam("*")
|
||||
mockDataContent, err := templates.AssetFS().ReadFile("mail/" + tmplName + ".mock.yml")
|
||||
mockDataContent, err := templates.AssetFS().ReadFile("mail/" + tmplName + ".devtest.yml")
|
||||
mockData := map[string]any{}
|
||||
if err == nil {
|
||||
err = yaml.Unmarshal(mockDataContent, &mockData)
|
||||
|
@@ -4,11 +4,10 @@
|
||||
package setting
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"code.gitea.io/gitea/models/unit"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/optional"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/templates"
|
||||
@@ -29,6 +28,13 @@ func Notifications(ctx *context.Context) {
|
||||
ctx.Data["PageIsSettingsNotifications"] = true
|
||||
ctx.Data["EmailNotificationsPreference"] = ctx.Doer.EmailNotificationsPreference
|
||||
|
||||
actionsEmailPref, err := user_model.GetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyEmailNotificationGiteaActions, user_model.SettingEmailNotificationGiteaActionsFailureOnly)
|
||||
if err != nil {
|
||||
ctx.ServerError("GetUserSetting", err)
|
||||
return
|
||||
}
|
||||
ctx.Data["ActionsEmailNotificationsPreference"] = actionsEmailPref
|
||||
|
||||
ctx.HTML(http.StatusOK, tplSettingsNotifications)
|
||||
}
|
||||
|
||||
@@ -44,19 +50,40 @@ func NotificationsEmailPost(ctx *context.Context) {
|
||||
preference == user_model.EmailNotificationsOnMention ||
|
||||
preference == user_model.EmailNotificationsDisabled ||
|
||||
preference == user_model.EmailNotificationsAndYourOwn) {
|
||||
log.Error("Email notifications preference change returned unrecognized option %s: %s", preference, ctx.Doer.Name)
|
||||
ctx.ServerError("SetEmailPreference", errors.New("option unrecognized"))
|
||||
ctx.Flash.Error(ctx.Tr("invalid_data", preference))
|
||||
ctx.Redirect(setting.AppSubURL + "/user/settings/notifications")
|
||||
return
|
||||
}
|
||||
opts := &user.UpdateOptions{
|
||||
EmailNotificationsPreference: optional.Some(preference),
|
||||
}
|
||||
if err := user.UpdateUser(ctx, ctx.Doer, opts); err != nil {
|
||||
log.Error("Set Email Notifications failed: %v", err)
|
||||
ctx.ServerError("UpdateUser", err)
|
||||
return
|
||||
}
|
||||
log.Trace("Email notifications preference made %s: %s", preference, ctx.Doer.Name)
|
||||
ctx.Flash.Success(ctx.Tr("settings.email_preference_set_success"))
|
||||
ctx.Redirect(setting.AppSubURL + "/user/settings/notifications")
|
||||
}
|
||||
|
||||
// NotificationsActionsEmailPost set user's email notification preference on Gitea Actions
|
||||
func NotificationsActionsEmailPost(ctx *context.Context) {
|
||||
if !setting.Actions.Enabled || unit.TypeActions.UnitGlobalDisabled() {
|
||||
ctx.NotFound(nil)
|
||||
return
|
||||
}
|
||||
|
||||
preference := ctx.FormString("preference")
|
||||
if !(preference == user_model.SettingEmailNotificationGiteaActionsAll ||
|
||||
preference == user_model.SettingEmailNotificationGiteaActionsDisabled ||
|
||||
preference == user_model.SettingEmailNotificationGiteaActionsFailureOnly) {
|
||||
ctx.Flash.Error(ctx.Tr("invalid_data", preference))
|
||||
ctx.Redirect(setting.AppSubURL + "/user/settings/notifications")
|
||||
return
|
||||
}
|
||||
if err := user_model.SetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyEmailNotificationGiteaActions, preference); err != nil {
|
||||
ctx.ServerError("SetUserSetting", err)
|
||||
return
|
||||
}
|
||||
ctx.Flash.Success(ctx.Tr("settings.email_preference_set_success"))
|
||||
ctx.Redirect(setting.AppSubURL + "/user/settings/notifications")
|
||||
}
|
||||
|
@@ -598,6 +598,7 @@ func registerWebRoutes(m *web.Router) {
|
||||
m.Group("/notifications", func() {
|
||||
m.Get("", user_setting.Notifications)
|
||||
m.Post("/email", user_setting.NotificationsEmailPost)
|
||||
m.Post("/actions", user_setting.NotificationsActionsEmailPost)
|
||||
})
|
||||
m.Group("/security", func() {
|
||||
m.Get("", security.Security)
|
||||
|
Reference in New Issue
Block a user