Skip to content

Commit

Permalink
Add slack message
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleimp committed Jul 31, 2020
1 parent ffaa7d8 commit 08cd721
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 101 deletions.
23 changes: 19 additions & 4 deletions cmd/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,31 @@ func Run(version string, args []string) error {
Usage: "checks the flags and notify",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "slack-channel",
Name: "channel",
Usage: "slack notification channel",
Required: true,
},
&cli.StringFlag{
Name: "silent",
Aliases: []string{"s"},
Usage: "silent mode doesn't print the flags on the console",
Name: "slack-token",
Usage: "slack token",
EnvVars: []string{"SLACK_TOKEN"},
},
&cli.StringFlag{
Name: "url",
Usage: "unleash api url",
Required: true,
},
&cli.IntFlag{
Name: "expires",
Aliases: []string{"e"},
Usage: "expires after days",
Value: 40,
},
&cli.StringFlag{
Name: "silent",
Aliases: []string{"s"},
Usage: "silent mode doesn't print the flags on the console",
},
},
Action: runNotify,
},
Expand Down
30 changes: 29 additions & 1 deletion cmd/cli/notify.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
package cli

import "github.com/urfave/cli/v2"
import (
"github.com/urfave/cli/v2"
"github.com/wesleimp/unleash-checkr/internal/flag"
"github.com/wesleimp/unleash-checkr/internal/notification/slack"
"github.com/wesleimp/unleash-checkr/pkg/config"
"github.com/wesleimp/unleash-checkr/pkg/context"
)

func runNotify(c *cli.Context) error {
token := c.String("slack-token")
channel := c.String("channel")
url := c.String("url")
expires := c.Int("expires")

ctx := context.New(&config.Config{
SlackChannel: channel,
SlackToken: token,
URL: url,
Expires: expires,
})

ff, err := flag.Get(ctx)
if err != nil {
return err
}

err = slack.Notify(ctx, ff)
if err != nil {
return err
}

return nil
}
64 changes: 22 additions & 42 deletions internal/notification/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,48 @@ import (

slackAPI "github.com/slack-go/slack"
"github.com/wesleimp/unleash-checkr/internal/flag"
"github.com/wesleimp/unleash-checkr/internal/tmpl"
"github.com/wesleimp/unleash-checkr/pkg/context"
)

var template = `Here are some flags that have passed their expiration and can now be removed.
{{- with .Flags }}
*Flags*
{{ range $element := .}}
*{{ .Name }}*{{ if .Description }} - {{ .Description }}{{ end }}
Created at: {{ .CreatedAt }}
{{ .Location }}
{{ end -}}
{{- end -}}
`
var (
message = "Here are some flags that have passed their expiration and can now be removed. :rocket:"
color = "#36a64f"
)

// Notify some slack channel
func Notify(ctx *context.Context, flags []flag.Flag) error {
api := slackAPI.New(ctx.Config.SlackToken)

text, err := describeMessageBody(ctx, flags)
if err != nil {
return err
}
att := createAttachments(ctx, flags)

var iconURL = "https://raw.githubusercontent.com/Unleash/unleash-frontend/master/public/logo.png"
_, _, _, err = api.SendMessage(ctx.Config.SlackChannel,
slackAPI.MsgOptionAsUser(true),
slackAPI.MsgOptionText(text, true),
_, _, _, err := api.SendMessage(ctx.Config.SlackChannel,
slackAPI.MsgOptionText(message, true),
slackAPI.MsgOptionIconURL(iconURL),
slackAPI.MsgOptionUsername("unleash-checkr"),
slackAPI.MsgOptionAttachments(att...),
)

return err
}

func describeMessageBody(ctx *context.Context, flags []flag.Flag) (string, error) {
var ff = parseToTemplate(ctx, flags)

text, err := tmpl.New(ctx).
WithExtraFields(tmpl.Fields{
"Flags": ff,
}).
Apply(template)
if err != nil {
return "", err
}

return text, nil
}
func createAttachments(ctx *context.Context, flags []flag.Flag) []slackAPI.Attachment {
var aa []slackAPI.Attachment

func parseToTemplate(ctx *context.Context, flags []flag.Flag) []map[string]string {
var ff []map[string]string
for _, f := range flags {
ff = append(ff, map[string]string{
"Name": f.Name,
"Description": f.Description,
"CreatedAt": f.CreatedAt.Format("01/02/2006 3:04 PM"),
"Location": fmt.Sprintf("%s/#/features/strategies/%s", ctx.Config.URL, f.Name),
aa = append(aa, slackAPI.Attachment{
Color: color,
Title: f.Name,
TitleLink: fmt.Sprintf("%s/#/features/strategies/%s", ctx.Config.URL, f.Name),
Text: f.Description,
Fields: []slackAPI.AttachmentField{
{
Title: "Created at",
Value: f.CreatedAt.Format("01/02/2006 3:04 PM"),
},
},
})
}

return ff
return aa
}
54 changes: 0 additions & 54 deletions internal/notification/slack/slack_test.go
Original file line number Diff line number Diff line change
@@ -1,55 +1 @@
package slack

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/wesleimp/unleash-checkr/internal/flag"
"github.com/wesleimp/unleash-checkr/pkg/config"
"github.com/wesleimp/unleash-checkr/pkg/context"
)

func TestWithEmptyFlags(t *testing.T) {
ctx := context.New(&config.Config{})
var flags []flag.Flag
text, err := describeMessageBody(ctx, flags)
assert.NoError(t, err)
assert.Equal(t, text, "Here are some flags that have passed their expiration and can now be removed.")
}

func TestWithOneFlag(t *testing.T) {
ctx := context.New(&config.Config{
URL: "http://test.com",
})

date, err := time.Parse(time.RFC3339, "2020-01-01T15:04:05+07:00")
assert.NoError(t, err)

var flags = []flag.Flag{
{
Name: "First",
Description: "",
CreatedAt: date,
},
{
Name: "Second",
Description: "Second flag",
CreatedAt: date,
},
}
text, err := describeMessageBody(ctx, flags)
assert.NoError(t, err)
assert.Equal(t, text, `Here are some flags that have passed their expiration and can now be removed.
*Flags*
*First*
Created at: 01/01/2020 3:04 PM
http://test.com/#/features/strategies/First
*Second* - Second flag
Created at: 01/01/2020 3:04 PM
http://test.com/#/features/strategies/Second
`)
}

0 comments on commit 08cd721

Please sign in to comment.