add option for per-project telegram alert to different chats

This commit is contained in:
Anton Markelov 2017-05-03 14:27:58 +10:00
parent 39298cb0d2
commit bff8297145
9 changed files with 37 additions and 12 deletions

View File

@ -61,15 +61,16 @@ func MustBeAdmin(w http.ResponseWriter, r *http.Request) {
func UpdateProject(w http.ResponseWriter, r *http.Request) {
project := context.Get(r, "project").(db.Project)
var body struct {
Name string `json:"name"`
Alert bool `json:"alert"`
Name string `json:"name"`
Alert bool `json:"alert"`
AlertChat string `json:"alert_chat"`
}
if err := mulekick.Bind(w, r, &body); err != nil {
return
}
if _, err := db.Mysql.Exec("update project set name=?, alert=? where id=?", body.Name, body.Alert, project.ID); err != nil {
if _, err := db.Mysql.Exec("update project set name=?, alert=?, alert_chat=? where id=?", body.Name, body.Alert, body.AlertChat, project.ID); err != nil {
panic(err)
}

View File

@ -73,12 +73,17 @@ func (t *task) sendTelegramAlert() {
return
}
chat_id := util.Config.TelegramChat
if t.alert_chat != "" {
chat_id = t.alert_chat
}
var telegramBuffer bytes.Buffer
alert := Alert{
TaskID: strconv.Itoa(t.task.ID),
Alias: t.template.Alias,
TaskURL: util.Config.WebHost + "/project/" + strconv.Itoa(t.template.ProjectID),
ChatID: util.Config.TelegramChat,
ChatID: chat_id,
}
tpl := template.New("telegram body template")
tpl, err := tpl.Parse(telegramTemplate)

View File

@ -26,6 +26,7 @@ type task struct {
users []int
projectID int
alert bool
alert_chat string
}
func (t *task) fail() {
@ -146,10 +147,18 @@ func (t *task) populateDetails() error {
return err
}
type AlertSettings struct {
Alert bool `db:"alert"`
AlertChat string `db:"alert_chat"`
}
var project db.Project
// get project alert setting
if err := t.fetch("Alert setting not found!", &t.alert, "select alert from project where id=?", t.template.ProjectID); err != nil {
if err := t.fetch("Alert setting not found!", &project, "select alert, alert_chat from project where id=?", t.template.ProjectID); err != nil {
return err
}
t.alert = project.Alert
t.alert_chat = project.AlertChat
// get project users
var users []struct {

View File

@ -5,10 +5,11 @@ import (
)
type Project struct {
ID int `db:"id" json:"id"`
Name string `db:"name" json:"name" binding:"required"`
Created time.Time `db:"created" json:"created"`
Alert bool `db:"alert" json:"alert"`
ID int `db:"id" json:"id"`
Name string `db:"name" json:"name" binding:"required"`
Created time.Time `db:"created" json:"created"`
Alert bool `db:"alert" json:"alert"`
AlertChat string `db:"alert_chat" json:"alert_chat"`
}
func (project *Project) CreateProject() error {

1
db/migrations/v2.4.0.sql Normal file
View File

@ -0,0 +1 @@
ALTER TABLE project ADD alert_chat varchar(10) DEFAULT '' AFTER alert;

View File

@ -70,5 +70,6 @@ func init() {
{Major: 2, Minor: 3},
{Major: 2, Minor: 3, Patch: 1},
{Major: 2, Minor: 3, Patch: 2},
{Major: 2, Minor: 4},
}
}

View File

@ -11,9 +11,14 @@ form.form-horizontal
input(type="checkbox" title="Send email alerts about failed tasks" ng-model="alert")
| Allow alerts for this project
.form-group
label.control-label.col-sm-4 Chat ID
.col-sm-6
input.form-control(type="text" ng-model="alert_chat" placeholder="Telegram Chat ID for alerts")
.form-group
.col-sm-6.col-sm-offset-4
button.btn.btn-success(ng-click="save(projectName, alert)") Save
button.btn.btn-success(ng-click="save(projectName, alert, alert_chat)") Save
hr

View File

@ -2,9 +2,10 @@ define(function () {
app.registerController('ProjectEditCtrl', ['$scope', '$http', 'Project', '$state', function ($scope, $http, Project, $state) {
$scope.projectName = Project.name;
$scope.alert = Project.alert;
$scope.alert_chat = Project.alert_chat;
$scope.save = function (name, alert) {
$http.put(Project.getURL(), { name: name, alert: alert }).success(function () {
$scope.save = function (name, alert, alert_chat) {
$http.put(Project.getURL(), { name: name, alert: alert, alert_chat: alert_chat}).success(function () {
swal('Saved', 'Project settings saved.', 'success');
}).error(function () {
swal('Error', 'Project settings were not saved', 'error');

View File

@ -3,6 +3,7 @@ app.factory('ProjectFactory', ['$http', function ($http) {
this.id = project.id;
this.name = project.name;
this.alert = project.alert;
this.alert_chat = project.alert_chat;
}
Project.prototype.getURL = function () {