Skip to content

Commit

Permalink
SlackWebhookURL should be a string for compatibility reason (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaspoignant authored Jul 7, 2022
1 parent 8c9f100 commit 96db56d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
11 changes: 8 additions & 3 deletions notifier/slacknotifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const (
)

type Notifier struct {
SlackWebhookURL url.URL
SlackWebhookURL string

httpClient internal.HTTPClient
init sync.Once
Expand All @@ -35,7 +35,7 @@ type Notifier struct {
func (c *Notifier) Notify(diff notifier.DiffCache, wg *sync.WaitGroup) error {
defer wg.Done()

if c.SlackWebhookURL.String() == "" {
if c.SlackWebhookURL == "" {
return fmt.Errorf("error: (Slack Notifier) invalid notifier configuration, no " +
"SlackWebhookURL provided for the slack notifier")
}
Expand All @@ -47,14 +47,19 @@ func (c *Notifier) Notify(diff notifier.DiffCache, wg *sync.WaitGroup) error {
}
})

slackURL, err := url.Parse(c.SlackWebhookURL)
if err != nil {
return fmt.Errorf("error: (Slack Notifier) invalid SlackWebhookURL: %v", c.SlackWebhookURL)
}

reqBody := convertToSlackMessage(diff)
payload, err := json.Marshal(reqBody)
if err != nil {
return fmt.Errorf("error: (Slack Notifier) impossible to read differences; %v", err)
}
request := http.Request{
Method: http.MethodPost,
URL: &c.SlackWebhookURL,
URL: slackURL,
Body: ioutil.NopCloser(bytes.NewReader(payload)),
Header: map[string][]string{"Content-type": {"application/json"}},
}
Expand Down
17 changes: 13 additions & 4 deletions notifier/slacknotifier/notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package slacknotifier
import (
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"sync"
Expand Down Expand Up @@ -132,17 +131,27 @@ func TestSlackNotifier_Notify(t *testing.T) {
args: args{
statusCode: http.StatusOK,
diff: notifier.DiffCache{},
forceError: true,
},
},
{
name: "invalid slack url",
expected: expected{
err: true,
errMsg: "error: (Slack Notifier) invalid SlackWebhookURL: https://{}hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX",
},
args: args{
url: "https://{}hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX",
statusCode: http.StatusOK,
diff: notifier.DiffCache{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockHTTPClient := &testutils.HTTPClientMock{StatusCode: tt.args.statusCode, ForceError: tt.args.forceError}

slackURL, _ := url.Parse(tt.args.url)
c := Notifier{
SlackWebhookURL: *slackURL,
SlackWebhookURL: tt.args.url,
httpClient: mockHTTPClient,
}

Expand Down

0 comments on commit 96db56d

Please sign in to comment.