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

added custom port option for rocketchat #80

Merged
merged 3 commits into from
Nov 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"))
})
})
})
})