Skip to content

Commit

Permalink
fix codacy issues
Browse files Browse the repository at this point in the history
  • Loading branch information
simskij committed May 18, 2019
1 parent 06d00b0 commit 860b6e8
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 21 deletions.
7 changes: 5 additions & 2 deletions pkg/plugins/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
maxlength = 2000
)

// Send a notification message to discord
func (plugin *DiscordPlugin) Send(url string, message string) error {
config, err := plugin.CreateConfigFromURL(url)
if err != nil {
Expand All @@ -27,20 +28,22 @@ func (plugin *DiscordPlugin) Send(url string, message string) error {
}
fmt.Println(string(payload))

apiUrl := CreateApiUrlFromConfig(config)
apiUrl := CreateApiURLFromConfig(config)
fmt.Println(apiUrl)

return doSend(payload, apiUrl)
}

func CreateApiUrlFromConfig(config DiscordConfig) string {
// CreateApiURLFromConfig takes a discord config object and creates a post url
func CreateApiURLFromConfig(config DiscordConfig) string {
return fmt.Sprintf(
"%s/%s/%s",
hookUrl,
config.Channel,
config.Token)
}

// CreateConfigFromURL creates a DiscordConfig struct given a valid discord notification url
func (plugin *DiscordPlugin) CreateConfigFromURL(url string) (DiscordConfig, error) {
args, err := plugins.ExtractArguments(url)
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions pkg/plugins/discord/discord_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ func TestDiscord(t *testing.T) {
}

var (
plugin *DiscordPlugin
envDiscordUrl string
plugin *DiscordPlugin
envDiscordURL string
)

var _ = Describe("the discord plugin", func() {
BeforeSuite(func() {
plugin = &DiscordPlugin{}
envDiscordUrl = os.Getenv("SHOUTRRR_DISCORD_URL")
envDiscordURL = os.Getenv("SHOUTRRR_DISCORD_URL")
})
When("running integration tests", func() {
It("should work without errors", func() {
if envDiscordUrl == "" {
if envDiscordURL == "" {
return
}
err := plugin.Send(envDiscordUrl, "this is an integration test")
err := plugin.Send(envDiscordURL, "this is an integration test")
Expect(err).NotTo(HaveOccurred())
})
})
Expand Down Expand Up @@ -72,4 +72,4 @@ var _ = Describe("the discord plugin", func() {
})
})
})
})
})
1 change: 1 addition & 0 deletions pkg/plugins/pushover/pushover.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (

type PushoverPlugin struct{}

// Send a notification message to Pushover
func (plugin *PushoverPlugin) Send(url string, message string) error {
config, _ := CreateConfigFromURL(url)
data := netUrl.Values{}
Expand Down
8 changes: 4 additions & 4 deletions pkg/plugins/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
)



// Send a notification message to Slack
func (slack *SlackPlugin) Send(url string, message string) error {
config, err := CreateConfigFromURL(url)
if err != nil {
Expand All @@ -33,8 +33,8 @@ func (slack *SlackPlugin) Send(url string, message string) error {
}

func (slack *SlackPlugin) doSend(config *SlackConfig, message string) error {
url := slack.getUrl(config)
json, _ := CreateJsonPayload(config, message)
url := slack.getURL(config)
json, _ := CreateJSONPayload(config, message)
res, err := http.Post(url, "application/json", bytes.NewReader(json))

if res.StatusCode != http.StatusOK {
Expand All @@ -43,7 +43,7 @@ func (slack *SlackPlugin) doSend(config *SlackConfig, message string) error {
return err
}

func (slack *SlackPlugin) getUrl(config *SlackConfig) string {
func (slack *SlackPlugin) getURL(config *SlackConfig) string {
return fmt.Sprintf(
"%s/%s/%s/%s",
url,
Expand Down
7 changes: 4 additions & 3 deletions pkg/plugins/slack/slack_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package slack

import "encoding/json"

type SlackJson struct {
type SlackJSON struct {
Text string `json:"text"`
Botname string `json:"username"`
}

func CreateJsonPayload(config *SlackConfig, message string) ([]byte, error) {
// CreateJSONPayload compatible with the slack webhook api
func CreateJSONPayload(config *SlackConfig, message string) ([]byte, error) {
return json.Marshal(
SlackJson {
SlackJSON{
Text: message,
Botname: config.Botname,
})
Expand Down
8 changes: 4 additions & 4 deletions pkg/plugins/slack/slack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ func TestShoutrrr(t *testing.T) {

var (
plugin *SlackPlugin
envSlackUrl string
envSlackURL string
)

var _ = Describe("the slack plugin", func() {

BeforeSuite(func() {
plugin = &SlackPlugin{}
envSlackUrl = os.Getenv("SHOUTRRR_SLACK_URL")
envSlackURL = os.Getenv("SHOUTRRR_SLACK_URL")

})

When("running integration tests", func() {
It("should not error out", func() {
if envSlackUrl == "" {
if envSlackURL == "" {
return
}
err := plugin.Send(envSlackUrl, "This is an integration test message")
err := plugin.Send(envSlackURL, "This is an integration test message")
Expect(err).NotTo(HaveOccurred())
})
})
Expand Down
5 changes: 3 additions & 2 deletions pkg/plugins/teams/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import (

type TeamsPlugin struct{}

// Send a notification message to Microsoft Teams
func (plugin *TeamsPlugin) Send(url string, message string) error {
config, err := plugin.CreateConfigFromURL(url)
if err != nil {
return err
}

postUrl := buildUrl(config)
postUrl := buildURL(config)
return plugin.doSend(postUrl, message)
}

Expand All @@ -41,7 +42,7 @@ func (plugin *TeamsPlugin) doSend(postUrl string, message string) error {
return nil
}

func buildUrl(config *TeamsConfig) string {
func buildURL(config *TeamsConfig) string {
var baseUrl = "https://outlook.office.com/webhook"
return fmt.Sprintf(
"%s/%s/IncomingWebhook/%s/%s",
Expand Down

0 comments on commit 860b6e8

Please sign in to comment.