Skip to content

Commit

Permalink
Allow giving additional context on notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
julienduchesne committed Nov 16, 2021
1 parent 0a9b9f1 commit 6463e7c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
8 changes: 5 additions & 3 deletions pkg/handlers/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type launchPayload struct {
WaitForResults bool
SlackChannelsString string `json:"slack_channels"`
SlackChannels []string
NotificationContext string `json:"notification_context"`
} `json:"metadata"`
}

Expand Down Expand Up @@ -103,11 +104,12 @@ func (h *launchHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
return
}

slackContext := payload.Metadata.NotificationContext
// Find the Cloud URL from the k6 output
if waitErr := h.waitForOutputPath(&buf); waitErr != nil {
logError(req, resp, fmt.Sprintf("error while waiting for test to start: %v", waitErr), 400)
text := fmt.Sprintf(":red_circle: Load testing of `%s` in namespace `%s` didn't start successfully", payload.Name, payload.Namespace)
slackMessages, err := h.slackClient.SendMessages(payload.Metadata.SlackChannels, text, "")
slackMessages, err := h.slackClient.SendMessages(payload.Metadata.SlackChannels, text, slackContext)
if err != nil {
log.Error(err)
}
Expand All @@ -117,10 +119,10 @@ func (h *launchHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
return
}

url, slackContext := "", ""
url := ""
if payload.Metadata.UploadToCloud {
url = outputRegex.FindStringSubmatch(buf.String())[1]
slackContext = fmt.Sprintf("Cloud URL: <%s>", url)
slackContext += fmt.Sprintf("\nCloud URL: <%s>", url)
log.Infof("cloud run URL: %s", url)
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/handlers/launch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func TestLaunchAndWaitCloud(t *testing.T) {
slackClient.EXPECT().SendMessages(
[]string{"test", "test2"},
":warning: Load testing of `test-name` in namespace `test-space` has started",
"Cloud URL: <https://app.k6.io/runs/1157843>",
"extra context\nCloud URL: <https://app.k6.io/runs/1157843>",
).Return(channelMap, nil)

// * Wait for the command to finish
Expand All @@ -156,12 +156,12 @@ func TestLaunchAndWaitCloud(t *testing.T) {
slackClient.EXPECT().UpdateMessages(
channelMap,
":large_green_circle: Load testing of `test-name` in namespace `test-space` has succeeded",
"Cloud URL: <https://app.k6.io/runs/1157843>",
"extra context\nCloud URL: <https://app.k6.io/runs/1157843>",
).Return(nil)

// Make request
request := &http.Request{
Body: ioutil.NopCloser(strings.NewReader(`{"name": "test-name", "namespace": "test-space", "phase": "pre-rollout", "metadata": {"script": "my-script", "upload_to_cloud": "true", "slack_channels": "test,test2"}}`)),
Body: ioutil.NopCloser(strings.NewReader(`{"name": "test-name", "namespace": "test-space", "phase": "pre-rollout", "metadata": {"script": "my-script", "upload_to_cloud": "true", "slack_channels": "test,test2", "notification_context": "extra context"}}`)),
}
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, request)
Expand Down

0 comments on commit 6463e7c

Please sign in to comment.