From b2d8688eeef9de71ca797c8d455a74064f949d56 Mon Sep 17 00:00:00 2001 From: Aaron Naden Date: Thu, 14 Sep 2023 15:32:46 +0100 Subject: [PATCH 1/4] Added in Proxy URL settings for the alerting --- cli/setup/setup.go | 2 ++ services/tasks/alert.go | 30 ++++++++++++++++++++++++++++-- util/config.go | 5 +++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/cli/setup/setup.go b/cli/setup/setup.go index 3245d8b2e..8449e4e85 100644 --- a/cli/setup/setup.go +++ b/cli/setup/setup.go @@ -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) diff --git a/services/tasks/alert.go b/services/tasks/alert.go index 43206c68a..5884fd53c 100644 --- a/services/tasks/alert.go +++ b/services/tasks/alert.go @@ -2,12 +2,14 @@ package tasks import ( "bytes" - "github.com/ansible-semaphore/semaphore/db" - "github.com/ansible-semaphore/semaphore/util" "html/template" "net/http" + "net/url" "strconv" "strings" + + "github.com/ansible-semaphore/semaphore/db" + "github.com/ansible-semaphore/semaphore/util" ) const emailTemplate = "Subject: Task '{{ .Name }}' failed\r\n" + @@ -142,6 +144,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 { @@ -162,6 +176,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 diff --git a/util/config.go b/util/config.go index 82737f3ea..044a6921c 100644 --- a/util/config.go +++ b/util/config.go @@ -18,6 +18,10 @@ import ( "strings" "github.com/google/go-github/github" +<<<<<<< HEAD +======= + +>>>>>>> 954e168 (Added in Proxy URL settings for the alerting) "github.com/gorilla/securecookie" ) @@ -155,6 +159,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"` From bee18730f7a1d46d56d74977fc6aeb87b82ef6a8 Mon Sep 17 00:00:00 2001 From: Aaron Naden Date: Thu, 14 Sep 2023 21:51:55 +0100 Subject: [PATCH 2/4] Rebase commit --- util/config.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/util/config.go b/util/config.go index 044a6921c..838dc4dc1 100644 --- a/util/config.go +++ b/util/config.go @@ -18,10 +18,6 @@ import ( "strings" "github.com/google/go-github/github" -<<<<<<< HEAD -======= - ->>>>>>> 954e168 (Added in Proxy URL settings for the alerting) "github.com/gorilla/securecookie" ) From 5e3b1752e599e924ef7dd266e671e65d2577766e Mon Sep 17 00:00:00 2001 From: Aaron Naden Date: Fri, 20 Oct 2023 13:23:30 +0100 Subject: [PATCH 3/4] Fix upstream merge conflicts --- services/tasks/alert.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/services/tasks/alert.go b/services/tasks/alert.go index 08b5ef482..c75440a3a 100644 --- a/services/tasks/alert.go +++ b/services/tasks/alert.go @@ -1,20 +1,12 @@ package tasks - -import ( "bytes" -<<<<<<< HEAD -======= "github.com/ansible-semaphore/semaphore/lib" "github.com/ansible-semaphore/semaphore/util" ->>>>>>> upstream/develop "html/template" "net/http" "net/url" "strconv" "strings" - - "github.com/ansible-semaphore/semaphore/db" - "github.com/ansible-semaphore/semaphore/util" ) const emailTemplate = "Subject: Task '{{ .Name }}' failed\r\n" + From ece71f6f0fa8bf1db0e5e74cc8fc728d4bf81a2e Mon Sep 17 00:00:00 2001 From: Aaron Naden Date: Fri, 20 Oct 2023 13:24:35 +0100 Subject: [PATCH 4/4] Go linting on the import statement --- services/tasks/alert.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/services/tasks/alert.go b/services/tasks/alert.go index c75440a3a..21c242c27 100644 --- a/services/tasks/alert.go +++ b/services/tasks/alert.go @@ -1,12 +1,15 @@ 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" +