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

Add in Proxy URL for Alert for #1484 #1487

Open
wants to merge 5 commits into
base: develop
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions cli/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ func InteractiveSetup(conf *util.ConfigType) {
if conf.TelegramAlert {
askValue("Telegram bot token (you can get it from @BotFather)", "", &conf.TelegramToken)
askValue("Telegram chat ID", "", &conf.TelegramChat)
askValue("Telegram Proxy URL", "", &conf.AlertUrlProxy)
}

askConfirmation("Enable slack alerts?", false, &conf.SlackAlert)
if conf.SlackAlert {
askValue("Slack Webhook URL", "", &conf.SlackUrl)
askValue("Slack Proxy URL", "", &conf.AlertUrlProxy)
}

askConfirmation("Enable LDAP authentication?", false, &conf.LdapEnable)
Expand Down
30 changes: 28 additions & 2 deletions services/tasks/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package tasks

import (
"bytes"
"github.com/ansible-semaphore/semaphore/lib"
"github.com/ansible-semaphore/semaphore/util"
"html/template"
"net/http"
"net/url"
"strconv"
"strings"

"github.com/ansible-semaphore/semaphore/lib"
"github.com/ansible-semaphore/semaphore/util"
)

const emailTemplate = "Subject: Task '{{ .Name }}' failed\r\n" +
Expand Down Expand Up @@ -153,6 +155,18 @@ func (t *TaskRunner) sendTelegramAlert() {
panic(err)
}

httpTransport := &http.Transport{}
if len(util.Config.AlertUrlProxy) != 0 { // Set the proxy only if the proxy param is specified
alertUrlProxy, proxyErr := url.Parse(util.Config.AlertUrlProxy)
if proxyErr == nil {
httpTransport.Proxy = http.ProxyURL(alertUrlProxy)
}
if proxyErr != nil {
t.Log("Can't send slack alert! Error: " + proxyErr.Error())
}
}
http := http.Client{Transport: httpTransport}

resp, err := http.Post("https://api.telegram.org/bot"+util.Config.TelegramToken+"/sendMessage", "application/json", &telegramBuffer)

if err != nil {
Expand All @@ -173,6 +187,18 @@ func (t *TaskRunner) sendSlackAlert() {

slackUrl := util.Config.SlackUrl

httpTransport := &http.Transport{}
if len(util.Config.AlertUrlProxy) != 0 { // Set the proxy only if the proxy param is specified
alertUrlProxy, proxyErr := url.Parse(util.Config.AlertUrlProxy)
if proxyErr == nil {
httpTransport.Proxy = http.ProxyURL(alertUrlProxy)
}
if proxyErr != nil {
t.Log("Can't send slack alert! Error: " + proxyErr.Error())
}
}
http := http.Client{Transport: httpTransport}

var slackBuffer bytes.Buffer

var version string
Expand Down
1 change: 1 addition & 0 deletions util/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ type ConfigType struct {
LdapNeedTLS bool `json:"ldap_needtls" env:"SEMAPHORE_LDAP_NEEDTLS"`

// telegram and slack alerting
AlertUrlProxy string `json:"alert_url_proxy" env:"SEMAPHORE_ALERT_PROXY_URL"`
TelegramAlert bool `json:"telegram_alert" env:"SEMAPHORE_TELEGRAM_ALERT"`
TelegramChat string `json:"telegram_chat" env:"SEMAPHORE_TELEGRAM_CHAT"`
TelegramToken string `json:"telegram_token" env:"SEMAPHORE_TELEGRAM_TOKEN"`
Expand Down