From 7303a6f48599757bc0e77d8fa4ec01c07bf0991c Mon Sep 17 00:00:00 2001 From: atighineanu Date: Sun, 15 Nov 2020 05:38:24 +0100 Subject: [PATCH 1/2] added custom port option for rocketchat --- go.mod | 6 +++--- pkg/services/rocketchat/rocketchat.go | 15 ++++++++++++--- pkg/services/rocketchat/rocketchat_config.go | 2 ++ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index c1893d58..9200f582 100644 --- a/go.mod +++ b/go.mod @@ -9,9 +9,6 @@ require ( github.com/mitchellh/mapstructure v1.2.2 // indirect github.com/onsi/ginkgo v1.8.0 github.com/onsi/gomega v1.5.0 - golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 - golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d - golang.org/x/sync v0.0.0-20190423024810-112230192c58 // indirect github.com/pelletier/go-toml v1.7.0 // indirect github.com/sirupsen/logrus v1.2.0 github.com/spf13/afero v1.2.2 // indirect @@ -20,6 +17,9 @@ require ( github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/viper v1.6.3 + golang.org/x/net v0.0.0-20190522155817-f3200d17e092 + golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d + golang.org/x/sync v0.0.0-20190423024810-112230192c58 // indirect golang.org/x/sys v0.0.0-20200409092240-59c9f1ba88fa // indirect golang.org/x/text v0.3.2 // indirect golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect diff --git a/pkg/services/rocketchat/rocketchat.go b/pkg/services/rocketchat/rocketchat.go index 88e3edc7..b40e0dd3 100644 --- a/pkg/services/rocketchat/rocketchat.go +++ b/pkg/services/rocketchat/rocketchat.go @@ -6,7 +6,6 @@ import ( "log" "net/http" "net/url" - "github.com/containrrr/shoutrrr/pkg/services/standard" "github.com/containrrr/shoutrrr/pkg/types" ) @@ -30,11 +29,16 @@ func (service *Service) Initialize(configURL *url.URL, logger *log.Logger) error // Send a notification message to Rocket.chat func (service *Service) Send(message string, params *types.Params) error { + var res *http.Response + var err error config := service.config apiURL := buildURL(config) json, _ := CreateJSONPayload(config, message, params) - res, err := http.Post(apiURL, "application/json", bytes.NewReader(json)) + res, err = http.Post(apiURL, "application/json", bytes.NewReader(json)) + if err != nil { + return fmt.Errorf("Error while posting to URL: %v\nHOST: %s\nPORT: %s\n", err, config.Host, config.Port) + } if res.StatusCode != http.StatusOK { return fmt.Errorf("failed to send notification to service, response status code %s", res.Status) } @@ -42,6 +46,11 @@ func (service *Service) Send(message string, params *types.Params) error { } func buildURL(config *Config) string { - return fmt.Sprintf("https://%s/hooks/%s/%s", config.Host, config.TokenA, config.TokenB) + if config.Port != "443" && config.Port != "" { + return fmt.Sprintf("https://%s:%s/hooks/%s/%s", config.Host, config.Port, config.TokenA, config.TokenB) + } else { + return fmt.Sprintf("https://%s/hooks/%s/%s", config.Host, config.TokenA, config.TokenB) + } + return "" } diff --git a/pkg/services/rocketchat/rocketchat_config.go b/pkg/services/rocketchat/rocketchat_config.go index d767d41c..d6fbfd5b 100644 --- a/pkg/services/rocketchat/rocketchat_config.go +++ b/pkg/services/rocketchat/rocketchat_config.go @@ -14,6 +14,7 @@ type Config struct { standard.EnumlessConfig UserName string Host string + Port string TokenA string Channel string TokenB string @@ -41,6 +42,7 @@ func (config *Config) SetURL(serviceURL *url.URL) error { return errors.New(NotEnoughArguments) } + config.Port = serviceURL.Port() config.UserName = UserName config.Host = host config.TokenA = path[1] From 65a6fd1a3c36471699672e76d67f492d9db0a042 Mon Sep 17 00:00:00 2001 From: atighineanu Date: Sun, 15 Nov 2020 20:06:39 +0100 Subject: [PATCH 2/2] added testcase; corrected buildURL func --- pkg/services/rocketchat/rocketchat.go | 6 ++---- pkg/services/rocketchat/rocketchat_test.go | 9 +++++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/pkg/services/rocketchat/rocketchat.go b/pkg/services/rocketchat/rocketchat.go index b40e0dd3..f05928ec 100644 --- a/pkg/services/rocketchat/rocketchat.go +++ b/pkg/services/rocketchat/rocketchat.go @@ -46,11 +46,9 @@ func (service *Service) Send(message string, params *types.Params) error { } func buildURL(config *Config) string { - if config.Port != "443" && config.Port != "" { + if config.Port != "" { return fmt.Sprintf("https://%s:%s/hooks/%s/%s", config.Host, config.Port, config.TokenA, config.TokenB) } else { return fmt.Sprintf("https://%s/hooks/%s/%s", config.Host, config.TokenA, config.TokenB) } - return "" -} - +} \ No newline at end of file diff --git a/pkg/services/rocketchat/rocketchat_test.go b/pkg/services/rocketchat/rocketchat_test.go index 50761afd..1b4cbb4f 100644 --- a/pkg/services/rocketchat/rocketchat_test.go +++ b/pkg/services/rocketchat/rocketchat_test.go @@ -162,5 +162,14 @@ var _ = Describe("the rocketchat service", func() { Expect(string(json)).To(Equal("{\"text\":\"this is a message\",\"username\":\"overwriteUserName\",\"channel\":\"overwriteChannel\"}")) }) }) + When("sending to an URL which contains HOST:PORT", func() { + rocketchatURL, _ := url.Parse("rocketchat://testUserName@rocketchat.my-domain.com:5055/tokenA/tokenB/testChannel") + config := &Config{} + config.SetURL(rocketchatURL) + It("should generate a correct hook URL https://HOST:PORT", func() { + hookURL := buildURL(config) + Expect(hookURL).To(ContainSubstring("my-domain.com:5055")) + }) + }) }) })