-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Email alerts #287
Email alerts #287
Changes from all commits
31599e0
a9c994c
c8cad51
4bbedac
0993a00
8ada2d0
ff31d42
a436e97
2a66610
1e95790
f20252e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package tasks | ||
|
||
import ( | ||
"bytes" | ||
"html/template" | ||
"strconv" | ||
|
||
"github.com/ansible-semaphore/semaphore/models" | ||
"github.com/ansible-semaphore/semaphore/util" | ||
) | ||
|
||
const emailTemplate = `Subject: Task '{{ .Alias }}' failed | ||
|
||
Task {{ .TaskId }} with template '{{ .Alias }}' has failed! | ||
Task log: <a href='{{ .TaskUrl }}'>{{ .TaskUrl }}</a>` | ||
|
||
type Alert struct { | ||
TaskId string | ||
Alias string | ||
TaskUrl string | ||
} | ||
|
||
func (t *task) sendMailAlert() { | ||
|
||
if util.Config.EmailAlert != true { | ||
return | ||
} | ||
|
||
if t.alert != true { | ||
return | ||
} | ||
|
||
mailHost := util.Config.EmailHost + ":" + util.Config.EmailPort | ||
|
||
var mailBuffer bytes.Buffer | ||
alert := Alert{TaskId: strconv.Itoa(t.task.ID), Alias: t.template.Alias, TaskUrl: util.Config.WebHost + "/project/" + strconv.Itoa(t.template.ProjectID)} | ||
tpl := template.New("mail body template") | ||
tpl, err := tpl.Parse(emailTemplate) | ||
err = tpl.Execute(&mailBuffer, alert) | ||
|
||
if err != nil { | ||
t.log("Can't generate alert template!") | ||
panic(err) | ||
} | ||
|
||
for _, user := range t.users { | ||
|
||
userObj, err := models.FetchUser(user) | ||
|
||
if userObj.Alert != true { | ||
return | ||
} | ||
|
||
if err != nil { | ||
t.log("Can't find user Email!") | ||
panic(err) | ||
} | ||
|
||
t.log("Sending email to " + userObj.Email + " from " + util.Config.EmailSender) | ||
err = util.SendMail(mailHost, util.Config.EmailSender, userObj.Email, mailBuffer) | ||
if err != nil { | ||
t.log("Can't send email!") | ||
t.log("Error: " + err.Error()) | ||
panic(err) | ||
} | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ALTER TABLE user ADD alert BOOLEAN NOT NULL AFTER password; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
ALTER TABLE project ADD alert BOOLEAN NOT NULL AFTER name; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,5 +66,6 @@ func init() { | |
{Major: 1, Minor: 7}, | ||
{Major: 1, Minor: 8}, | ||
{Major: 1, Minor: 9}, | ||
{Major: 2, Minor: 3}, | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this code block (not specific to Go) could be rewritten to be more readable
Example:
Could be rewritten to be more expressive as:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed