-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding support for webex as an alert provider
- Loading branch information
Showing
6 changed files
with
143 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
Copyright 2020 The Flux authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package notifier | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/fluxcd/pkg/runtime/events" | ||
) | ||
|
||
// Webex holds the hook URL | ||
type Webex struct { | ||
URL string | ||
ProxyURL string | ||
} | ||
|
||
// WebexPayload holds the message text | ||
type WebexPayload struct { | ||
Text string `json:"text,omitempty"` | ||
Markdown string `json:"markdown,omitempty"` | ||
} | ||
|
||
// NewWebex validates the Webex URL and returns a Webex object | ||
func NewWebex(hookURL, proxyURL string) (*Webex, error) { | ||
_, err := url.ParseRequestURI(hookURL) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid Webex hook URL %s", hookURL) | ||
} | ||
|
||
return &Webex{ | ||
URL: hookURL, | ||
ProxyURL: proxyURL, | ||
}, nil | ||
} | ||
|
||
// Post Webex message | ||
func (s *Webex) Post(event events.Event) error { | ||
// Skip any update events | ||
if isCommitStatus(event.Metadata, "update") { | ||
return nil | ||
} | ||
|
||
objName := fmt.Sprintf("%s/%s.%s", strings.ToLower(event.InvolvedObject.Kind), event.InvolvedObject.Name, event.InvolvedObject.Namespace) | ||
markdown := fmt.Sprintf("> **NAME** = %s | **MESSAGE** = %s", objName, event.Message) | ||
|
||
if len(event.Metadata) > 0 { | ||
markdown += " | **METADATA** =" | ||
for k, v := range event.Metadata { | ||
markdown += fmt.Sprintf(" **%s**: %s", k, v) | ||
} | ||
} | ||
|
||
payload := WebexPayload{ | ||
Text: "", | ||
Markdown: markdown, | ||
} | ||
|
||
if err := postMessage(s.URL, s.ProxyURL, payload); err != nil { | ||
return fmt.Errorf("postMessage failed: %w", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
Copyright 2020 The Flux authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package notifier | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestWebex_Post(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
b, err := ioutil.ReadAll(r.Body) | ||
require.NoError(t, err) | ||
|
||
var payload = WebexPayload{} | ||
err = json.Unmarshal(b, &payload) | ||
require.NoError(t, err) | ||
require.Empty(t, payload.Text) | ||
require.Equal(t, "> **NAME** = gitrepository/webapp.gitops-system | **MESSAGE** = message | **METADATA** = **test**: metadata", payload.Markdown) | ||
})) | ||
defer ts.Close() | ||
|
||
webex, err := NewWebex(ts.URL, "") | ||
require.NoError(t, err) | ||
|
||
err = webex.Post(testEvent()) | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestWebex_PostUpdate(t *testing.T) { | ||
webex, err := NewWebex("http://localhost", "") | ||
require.NoError(t, err) | ||
|
||
event := testEvent() | ||
event.Metadata["commit_status"] = "update" | ||
err = webex.Post(event) | ||
require.NoError(t, err) | ||
} |