Skip to content

Commit

Permalink
Merge pull request #166 from SomtochiAma/event-notifier
Browse files Browse the repository at this point in the history
Use deepcopy method for event
  • Loading branch information
stefanprodan authored Mar 22, 2021
2 parents e0e4204 + a99a0bc commit 7cbca8b
Show file tree
Hide file tree
Showing 20 changed files with 59 additions and 90 deletions.
6 changes: 3 additions & 3 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log/zap"

"github.com/fluxcd/pkg/apis/meta"
"github.com/fluxcd/pkg/recorder"
"github.com/fluxcd/pkg/runtime/events"

notifyv1 "github.com/fluxcd/notification-controller/api/v1beta1"
"github.com/fluxcd/notification-controller/internal/server"
Expand Down Expand Up @@ -146,7 +146,7 @@ var _ = Describe("Event handlers", func() {

var (
alert notifyv1.Alert
event recorder.Event
event events.Event
)

JustBeforeEach(func() {
Expand Down Expand Up @@ -204,7 +204,7 @@ var _ = Describe("Event handlers", func() {
},
},
}
event = recorder.Event{
event = events.Event{
InvolvedObject: corev1.ObjectReference{
Kind: "Bucket",
Name: "hyacinth",
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ replace github.com/fluxcd/notification-controller/api => ./api
require (
github.com/fluxcd/notification-controller/api v0.10.0
github.com/fluxcd/pkg/apis/meta v0.8.0
github.com/fluxcd/pkg/recorder v0.0.6
github.com/fluxcd/pkg/runtime v0.8.5
github.com/fluxcd/pkg/runtime v0.9.0
github.com/go-logr/logr v0.3.0
github.com/google/go-github/v32 v32.1.0
github.com/hashicorp/go-retryablehttp v0.6.8
Expand Down
35 changes: 2 additions & 33 deletions go.sum

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions internal/notifier/azure_devops.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import (
"context"
"errors"
"fmt"
"github.com/fluxcd/pkg/runtime/events"
"strings"
"time"

"github.com/fluxcd/pkg/recorder"
"github.com/microsoft/azure-devops-go-api/azuredevops"
"github.com/microsoft/azure-devops-go-api/azuredevops/git"
)
Expand Down Expand Up @@ -70,7 +70,7 @@ func NewAzureDevOps(addr string, token string) (*AzureDevOps, error) {
}

// Post Azure DevOps commit status
func (a AzureDevOps) Post(event recorder.Event) error {
func (a AzureDevOps) Post(event events.Event) error {
// Skip progressing events
if event.Reason == "Progressing" {
return nil
Expand Down Expand Up @@ -131,9 +131,9 @@ func (a AzureDevOps) Post(event recorder.Event) error {

func toAzureDevOpsState(severity string) (git.GitStatusState, error) {
switch severity {
case recorder.EventSeverityInfo:
case events.EventSeverityInfo:
return git.GitStatusStateValues.Succeeded, nil
case recorder.EventSeverityError:
case events.EventSeverityError:
return git.GitStatusStateValues.Error, nil
default:
return "", errors.New("can't convert to azure devops state")
Expand Down
8 changes: 4 additions & 4 deletions internal/notifier/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"fmt"
"strings"

"github.com/fluxcd/pkg/recorder"
"github.com/fluxcd/pkg/runtime/events"
"github.com/ktrysmt/go-bitbucket"
)

Expand Down Expand Up @@ -65,7 +65,7 @@ func NewBitbucket(addr string, token string) (*Bitbucket, error) {
}

// Post Bitbucket commit status
func (b Bitbucket) Post(event recorder.Event) error {
func (b Bitbucket) Post(event events.Event) error {
// Skip progressing events
if event.Reason == "Progressing" {
return nil
Expand Down Expand Up @@ -108,9 +108,9 @@ func (b Bitbucket) Post(event recorder.Event) error {

func toBitbucketState(severity string) (string, error) {
switch severity {
case recorder.EventSeverityInfo:
case events.EventSeverityInfo:
return "SUCCESSFUL", nil
case recorder.EventSeverityError:
case events.EventSeverityError:
return "FAILED", nil
default:
return "", errors.New("can't convert to bitbucket state")
Expand Down
6 changes: 3 additions & 3 deletions internal/notifier/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"net/http/httptest"
"testing"

"github.com/fluxcd/pkg/recorder"
"github.com/fluxcd/pkg/runtime/events"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

Expand All @@ -46,8 +46,8 @@ func Test_postMessage(t *testing.T) {
require.NoError(t, err)
}

func testEvent() recorder.Event {
return recorder.Event{
func testEvent() events.Event {
return events.Event{
InvolvedObject: corev1.ObjectReference{
Kind: "GitRepository",
Namespace: "gitops-system",
Expand Down
6 changes: 3 additions & 3 deletions internal/notifier/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"path"
"strings"

"github.com/fluxcd/pkg/recorder"
"github.com/fluxcd/pkg/runtime/events"
)

// Discord holds the hook URL
Expand Down Expand Up @@ -65,7 +65,7 @@ func NewDiscord(hookURL string, proxyURL string, username string, channel string
}

// Post Discord message
func (s *Discord) Post(event recorder.Event) error {
func (s *Discord) Post(event events.Event) error {
// Skip any update events
if isCommitStatus(event.Metadata, "update") {
return nil
Expand All @@ -80,7 +80,7 @@ func (s *Discord) Post(event recorder.Event) error {
}

color := "good"
if event.Severity == recorder.EventSeverityError {
if event.Severity == events.EventSeverityError {
color = "danger"
}

Expand Down
4 changes: 2 additions & 2 deletions internal/notifier/forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ package notifier

import (
"fmt"
"github.com/fluxcd/pkg/runtime/events"
"net/url"

"github.com/fluxcd/pkg/recorder"
"github.com/hashicorp/go-retryablehttp"
)

Expand All @@ -46,7 +46,7 @@ func NewForwarder(hookURL string, proxyURL string) (*Forwarder, error) {
}, nil
}

func (f *Forwarder) Post(event recorder.Event) error {
func (f *Forwarder) Post(event events.Event) error {
err := postMessage(f.URL, f.ProxyURL, event, func(req *retryablehttp.Request) {
req.Header.Set(NotificationHeader, event.ReportingController)
})
Expand Down
5 changes: 2 additions & 3 deletions internal/notifier/forwarder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ package notifier

import (
"encoding/json"
"github.com/fluxcd/pkg/runtime/events"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"

"github.com/fluxcd/pkg/recorder"

"github.com/stretchr/testify/require"
)

Expand All @@ -34,7 +33,7 @@ func TestForwarder_Post(t *testing.T) {
require.NoError(t, err)

require.Equal(t, "source-controller", r.Header.Get("gotk-component"))
var payload = recorder.Event{}
var payload = events.Event{}
err = json.Unmarshal(b, &payload)
require.NoError(t, err)
require.Equal(t, "webapp", payload.InvolvedObject.Name)
Expand Down
8 changes: 4 additions & 4 deletions internal/notifier/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import (
"context"
"errors"
"fmt"
"github.com/fluxcd/pkg/runtime/events"
"net/url"
"strings"
"time"

"github.com/fluxcd/pkg/recorder"
"github.com/google/go-github/v32/github"
"golang.org/x/oauth2"
)
Expand Down Expand Up @@ -73,7 +73,7 @@ func NewGitHub(addr string, token string) (*GitHub, error) {
}

// Post Github commit status
func (g *GitHub) Post(event recorder.Event) error {
func (g *GitHub) Post(event events.Event) error {
// Skip progressing events
if event.Reason == "Progressing" {
return nil
Expand Down Expand Up @@ -121,9 +121,9 @@ func (g *GitHub) Post(event recorder.Event) error {

func toGitHubState(severity string) (string, error) {
switch severity {
case recorder.EventSeverityInfo:
case events.EventSeverityInfo:
return "success", nil
case recorder.EventSeverityError:
case events.EventSeverityError:
return "failure", nil
default:
return "", errors.New("can't convert to github state")
Expand Down
8 changes: 4 additions & 4 deletions internal/notifier/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package notifier
import (
"errors"

"github.com/fluxcd/pkg/recorder"
"github.com/fluxcd/pkg/runtime/events"
"github.com/xanzy/go-gitlab"
)

Expand Down Expand Up @@ -53,7 +53,7 @@ func NewGitLab(addr string, token string) (*GitLab, error) {
}

// Post GitLab commit status
func (g *GitLab) Post(event recorder.Event) error {
func (g *GitLab) Post(event events.Event) error {
// Skip progressing events
if event.Reason == "Progressing" {
return nil
Expand Down Expand Up @@ -89,9 +89,9 @@ func (g *GitLab) Post(event recorder.Event) error {

func toGitLabState(severity string) (gitlab.BuildStateValue, error) {
switch severity {
case recorder.EventSeverityInfo:
case events.EventSeverityInfo:
return gitlab.Success, nil
case recorder.EventSeverityError:
case events.EventSeverityError:
return gitlab.Failed, nil
default:
return "", errors.New("can't convert to gitlab state")
Expand Down
6 changes: 3 additions & 3 deletions internal/notifier/google_chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"net/url"
"strings"

"github.com/fluxcd/pkg/recorder"
"github.com/fluxcd/pkg/runtime/events"
)

// Slack holds the hook URL
Expand Down Expand Up @@ -85,7 +85,7 @@ func NewGoogleChat(hookURL string, proxyURL string) (*GoogleChat, error) {
}

// Post Google Chat message
func (s *GoogleChat) Post(event recorder.Event) error {
func (s *GoogleChat) Post(event events.Event) error {
// Skip any update events
if isCommitStatus(event.Metadata, "update") {
return nil
Expand All @@ -108,7 +108,7 @@ func (s *GoogleChat) Post(event recorder.Event) error {

// Message
messageText := event.Message
if event.Severity == recorder.EventSeverityError {
if event.Severity == events.EventSeverityError {
messageText = fmt.Sprintf("<font color=\"#ff0000\">%s</font>", event.Message)
}
sections = append(sections, GoogleChatCardSection{
Expand Down
4 changes: 2 additions & 2 deletions internal/notifier/nop.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ limitations under the License.

package notifier

import "github.com/fluxcd/pkg/recorder"
import "github.com/fluxcd/pkg/runtime/events"

type NopNotifier struct{}

func (n *NopNotifier) Post(event recorder.Event) error {
func (n *NopNotifier) Post(event events.Event) error {
return nil
}
6 changes: 4 additions & 2 deletions internal/notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ limitations under the License.

package notifier

import "github.com/fluxcd/pkg/recorder"
import (
"github.com/fluxcd/pkg/runtime/events"
)

type Interface interface {
Post(event recorder.Event) error
Post(event events.Event) error
}
6 changes: 3 additions & 3 deletions internal/notifier/rocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"net/url"
"strings"

"github.com/fluxcd/pkg/recorder"
"github.com/fluxcd/pkg/runtime/events"
)

// Rocket holds the hook URL
Expand Down Expand Up @@ -57,7 +57,7 @@ func NewRocket(hookURL string, proxyURL string, username string, channel string)
}

// Post Rocket message
func (s *Rocket) Post(event recorder.Event) error {
func (s *Rocket) Post(event events.Event) error {
// Skip any update events
if isCommitStatus(event.Metadata, "update") {
return nil
Expand All @@ -69,7 +69,7 @@ func (s *Rocket) Post(event recorder.Event) error {
}

color := "#0076D7"
if event.Severity == recorder.EventSeverityError {
if event.Severity == events.EventSeverityError {
color = "#FF0000"
}

Expand Down
6 changes: 3 additions & 3 deletions internal/notifier/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"net/url"
"strings"

"github.com/fluxcd/pkg/recorder"
"github.com/fluxcd/pkg/runtime/events"
)

// Slack holds the hook URL
Expand Down Expand Up @@ -78,7 +78,7 @@ func NewSlack(hookURL string, proxyURL string, username string, channel string)
}

// Post Slack message
func (s *Slack) Post(event recorder.Event) error {
func (s *Slack) Post(event events.Event) error {
// Skip any update events
if isCommitStatus(event.Metadata, "update") {
return nil
Expand All @@ -93,7 +93,7 @@ func (s *Slack) Post(event recorder.Event) error {
}

color := "good"
if event.Severity == recorder.EventSeverityError {
if event.Severity == events.EventSeverityError {
color = "danger"
}

Expand Down
6 changes: 3 additions & 3 deletions internal/notifier/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"net/url"
"strings"

"github.com/fluxcd/pkg/recorder"
"github.com/fluxcd/pkg/runtime/events"
)

// MS Teams holds the incoming webhook URL
Expand Down Expand Up @@ -65,7 +65,7 @@ func NewMSTeams(hookURL string, proxyURL string) (*MSTeams, error) {
}

// Post MS Teams message
func (s *MSTeams) Post(event recorder.Event) error {
func (s *MSTeams) Post(event events.Event) error {
// Skip any update events
if isCommitStatus(event.Metadata, "update") {
return nil
Expand Down Expand Up @@ -94,7 +94,7 @@ func (s *MSTeams) Post(event recorder.Event) error {
},
}

if event.Severity == recorder.EventSeverityError {
if event.Severity == events.EventSeverityError {
payload.ThemeColor = "FF0000"
}

Expand Down
Loading

0 comments on commit 7cbca8b

Please sign in to comment.