-
-
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 8 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,71 @@ | ||
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 }}' was 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 { | ||
|
||
if t.alert == true { | ||
|
||
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 { | ||
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) | ||
} | ||
|
||
} else { | ||
t.log("Alerts disabled for user " + userObj.Name + ", nothing to do.") | ||
} | ||
} | ||
} else { | ||
t.log("Alerts disabled for this project, nothing to do.") | ||
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. if alerts are disabled, there's no need to log them :) 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. fixed |
||
} | ||
} else { | ||
t.log("Alerts globally disabled, nothing to do.") | ||
} | ||
|
||
} |
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}, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,9 +6,13 @@ form.form-horizontal | |
.col-sm-6 | ||
input.form-control(type="text" ng-model="projectName" placeholder="Project Name") | ||
|
||
.form-group | ||
label.control-label.col-sm-4 Allow alerts for this project | ||
.col-sm-1: input.form-control(type="checkbox" title="Send email alerts about failed tasks" ng-model="alert") | ||
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. See the docs for checkboxes in bootstrap: 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. made it a bit less ugly |
||
|
||
.form-group | ||
.col-sm-6.col-sm-offset-4 | ||
button.btn.btn-success(ng-click="save(projectName)") Save | ||
button.btn.btn-success(ng-click="save(projectName, alert)") Save | ||
|
||
hr | ||
|
||
|
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