Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
added custom port option for rocketchat (containrrr#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexei Tighineanu authored and JonasPf committed Dec 10, 2020
1 parent c78b769 commit b67f9b1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
14 changes: 10 additions & 4 deletions pkg/services/rocketchat/rocketchat.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"log"
"net/http"
"net/url"

"github.com/containrrr/shoutrrr/pkg/services/standard"
"github.com/containrrr/shoutrrr/pkg/types"
)
Expand All @@ -30,12 +29,14 @@ 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", err)
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)
Expand All @@ -44,5 +45,10 @@ 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 != "" {
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)
}
}

2 changes: 2 additions & 0 deletions pkg/services/rocketchat/rocketchat_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Config struct {
standard.EnumlessConfig
UserName string
Host string
Port string
TokenA string
Channel string
TokenB string
Expand Down Expand Up @@ -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]
Expand Down
9 changes: 9 additions & 0 deletions pkg/services/rocketchat/rocketchat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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://[email protected]: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"))
})
})
})
})

0 comments on commit b67f9b1

Please sign in to comment.