Skip to content

Commit

Permalink
Merge pull request #7 from Crazybus/color_me_impressed
Browse files Browse the repository at this point in the history
Fixup linting and testing for new blacklisting feature
  • Loading branch information
Crazybus authored Sep 18, 2018
2 parents cd93994 + 4f9350c commit 7674d3c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 38 deletions.
26 changes: 14 additions & 12 deletions moonsla.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"strings"
"time"

"github.com/nlopes/slack"
"github.com/logrusorgru/aurora"
"github.com/nlopes/slack"
)

func getChannels(api *slack.Client) (channels map[string]string) {
Expand Down Expand Up @@ -105,7 +105,7 @@ func formatAttachments(attachments []slack.Attachment) string {

func filterChannel(name string, channels map[string]string, whitelist []string, blacklist []string) (whitelisted bool, cName string) {
whitelisted = false
var blacklisted bool = false
blacklisted := false

cName, ok := channels[name]
if ok {
Expand All @@ -128,23 +128,25 @@ func filterChannel(name string, channels map[string]string, whitelist []string,
whitelisted = true
}

if len(blacklist) == 1 && blacklist[0] == "" {
blacklisted = false
}

if blacklisted {
return false, cName
} else {
return whitelisted, cName
}
return whitelisted, cName
}

func min_int(a int, b int) int {
func minInt(a int, b int) int {
if a < b {
return a
} else {
return b
}
return b
}

func takeN(text []string, n int) []string {
return text[:min_int(n, len(text))]
return text[:minInt(n, len(text))]
}

func trim(text string) string {
Expand Down Expand Up @@ -193,7 +195,7 @@ func main() {
case *slack.MessageEvent:

whitelisted, cName := filterChannel(ev.Channel, channels, whitelist, blacklist)
var is_dm bool = false
isDm := false

// Map the users ID to a username if it exists
uName, ok := users[ev.User]
Expand All @@ -208,11 +210,11 @@ func main() {
dmName, present := dms[ev.Channel]
if present {
cName = dmName
is_dm = true
isDm = true
}

t, err := getTimeStamp(ev.EventTimestamp)
var timeStamp string = "00:00:00"
timeStamp := "00:00:00"
if err == nil {
timeStamp = fmt.Sprintf("%02d:%02d:%02d", t.Hour(), t.Minute(), t.Second())
}
Expand All @@ -236,7 +238,7 @@ func main() {
msg = trim(msg)

msgC := aurora.Gray(msg)
if is_dm {
if isDm {
msgC = aurora.Red(msg)
}

Expand Down
68 changes: 42 additions & 26 deletions moonsla_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestGetTimeStamp(t *testing.T) {
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
ts := getTimeStamp(test.timeStamp)
ts, _ := getTimeStamp(test.timeStamp)
got := ts.Unix()

want := test.want
Expand All @@ -37,65 +37,81 @@ func TestFilterChannel(t *testing.T) {
description string
id string
channels map[string]string
blacklist []string
whitelist []string
name string
whitelisted bool
}{
{
"Channel that is whitelisted",
"12345",
map[string]string{
description: "Channel that is whitelisted",
id: "12345",
channels: map[string]string{
"12345": "channel-name",
},
[]string{
blacklist: []string{""},
whitelist: []string{
"channel-name",
},
"channel-name",
true,
name: "channel-name",
whitelisted: true,
},
{
"Channel that is not whitelisted",
"12344",
map[string]string{
description: "Channel that is blacklisted",
id: "12345",
channels: map[string]string{
"12345": "channel-name",
},
blacklist: []string{"channel-name"},
whitelist: []string{""},
name: "channel-name",
whitelisted: false,
},
{
description: "Channel that is not whitelisted",
id: "12344",
channels: map[string]string{
"12345": "channel-name",
"12344": "spam-channel",
},
[]string{
blacklist: []string{""},
whitelist: []string{
"channel-name",
},
"spam-channel",
false,
name: "spam-channel",
whitelisted: false,
},
{
"Channel that is not in the channels list",
"123",
map[string]string{
description: "Channel that is not in the channels list",
id: "123",
channels: map[string]string{
"12345": "channel-name",
"12344": "spam-channel",
},
[]string{
blacklist: []string{""},
whitelist: []string{
"channel-name",
},
"123",
true,
name: "123",
whitelisted: true,
},
{
"Empty whitelist matches all channels",
"12345",
map[string]string{
description: "Empty whitelist matches all channels",
id: "12345",
channels: map[string]string{
"12345": "channel-name",
"12344": "spam-channel",
},
[]string{
blacklist: []string{""},
whitelist: []string{
"",
},
"channel-name",
true,
name: "channel-name",
whitelisted: true,
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
whitelisted, name := filterChannel(test.id, test.channels, test.whitelist)
whitelisted, name := filterChannel(test.id, test.channels, test.whitelist, test.blacklist)

if name != test.name {
t.Errorf("got '%s' want '%s'", name, test.name)
Expand Down

0 comments on commit 7674d3c

Please sign in to comment.