Skip to content

Commit

Permalink
Merge pull request #13 from insidieux/bug-out-of-range-fix
Browse files Browse the repository at this point in the history
Bugfix for out of range
  • Loading branch information
insidieux authored Nov 27, 2018
2 parents 9ec7a5e + 69db9bd commit 44d4467
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 32 deletions.
2 changes: 0 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
*

!.docker/docker-entrypoint
!slack-duty-bot
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
*.out
config.yaml
slack-duty-bot
!/slack-duty-bot
!./**/slack-duty-bot
.kubernetes/deploy.yaml
20 changes: 3 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
all: build

.PHONY : all package
.PHONY: all

# prevent run if docker not found
ifeq (, $(shell which docker))
Expand All @@ -18,17 +18,6 @@ ifeq ($(MOD_NAME),)
override MOD_NAME=slack-duty-bot
endif

# vendor variables
FORCE_INIT=0
ifeq (,$(wildcard ./go.mod))
FORCE_INIT=1
endif

FORCE_VENDOR=0
ifeq (,$(wildcard ./vendor/.*))
FORCE_VENDOR=1
endif

# build go binary variables
GO_VERSION=1.11
GOOS?=$(shell go env GOOS || echo linux)
Expand All @@ -43,24 +32,21 @@ DOCKER_USER?=
DOCKER_PASSWORD?=

init:
ifeq ($(FORCE_INIT), 1)
docker run --rm \
-v ${ROOT_DIR}:/project \
-w /project \
-e GO111MODULE=on \
golang:${GO_VERSION} \
go mod init ${MOD_NAME}
endif
go mod init ${MOD_NAME} || true

vendor: init
ifeq ($(FORCE_VENDOR), 1)
rm -r vendor || true
docker run --rm \
-v ${ROOT_DIR}:/project \
-w /project \
-e GO111MODULE=on \
golang:${GO_VERSION} \
go mod vendor
endif

test: vendor
docker run --rm \
Expand Down
2 changes: 1 addition & 1 deletion ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ROADMAP
* HTTP rest API for edit duties configuration
* Persistent storage for duties configuration
* Multiple instance mode with ex-locks
* HELM support
* ~~HELM support~~
* Healtchecks
* Kubernetes livenessprobe/readinessprobe
* Support prometheus metrics
62 changes: 51 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,44 @@ func validateArguments() error {
if len(viper.GetStringSlice("slack.keyword")) == 0 {
return fmt.Errorf("parameter slack.keyword is required")
}
var (
duties = parseDutiesList()
index = getCurrentDayIndex()
cfgGroup = viper.GetString("slack.group.id") != "" && viper.GetString("slack.group.name") != ""
length = len(duties)
)
switch length > 0 {
case true:
if length < 7 {
return fmt.Errorf("duties list is not empty, but indexes count %d is less than 7 (weekdays)", length)
}
if len(duties[index]) == 0 && !cfgGroup {
return fmt.Errorf("empty duties list for current day (%d) index and slack.group info does not exist in config", index)
}
break
case false:
if !cfgGroup {
return fmt.Errorf("empty duties list in config and slack.group info does not exist in config")
}
break
}
return nil
}

func parseDutiesList() [][]string {
var (
config = struct {
Duties [][]string // we need this hack cause viper cannot resolve slice of slice
}{}
)
viper.Unmarshal(&config)
return config.Duties
}

func getCurrentDayIndex() int {
return int(time.Now().Weekday())
}

func handleMessageEvent(rtm *slack.RTM, event *slack.MessageEvent) error {
if err := checkMessageEvent(event); err != nil {
logrus.Debugf("Incoming message check error: %+v", err)
Expand All @@ -158,23 +193,27 @@ func handleMessageEvent(rtm *slack.RTM, event *slack.MessageEvent) error {
}
}
var (
config = struct {
Duties [][]string // we need this hack cause viper cannot resolve slice of slice
}{}
duties []string
cfg = parseDutiesList()
index = getCurrentDayIndex()
)
viper.Unmarshal(&config)
for _, username := range config.Duties[int(time.Now().Weekday())] {
userId, ok := userIds[username]
if !ok {
logrus.Errorf("Failed to get user id by username %s", username)
if len(cfg) > index {
for _, username := range cfg[index] {
userId, ok := userIds[username]
if !ok {
logrus.Errorf("Failed to get user id by username %s", username)
}
duties = append(duties, fmt.Sprintf("<@%s|%s>", userId, username))
}
duties = append(duties, fmt.Sprintf("<@%s|%s>", userId, username))
}
if len(duties) == 0 && viper.GetString("slack.group.id") != "" && viper.GetString("slack.group.name") != "" {
duties = append(duties, fmt.Sprintf("<!subteam^%s|@%s>", viper.GetString("slack.group.id"), viper.GetString("slack.group.name")))
}
// send message
if len(duties) == 0 {
return fmt.Errorf("failed to collect duties list for incoming message")
}
logrus.Debugf("Final duties list for call: %+v", duties)
//send message
var outgoing = rtm.NewOutgoingMessage(strings.Join(duties, ", "), event.Channel)
if viper.GetBool("slack.threads") == true {
outgoing.ThreadTimestamp = event.Timestamp
Expand All @@ -185,8 +224,9 @@ func handleMessageEvent(rtm *slack.RTM, event *slack.MessageEvent) error {
}

func checkMessageEvent(event *slack.MessageEvent) error {
// skip topic messages
if event.Topic != "" {
return fmt.Errorf("inocming message about topic change")
return fmt.Errorf("the incoming message about topic change")
}
// check text
if event.Text == "" {
Expand Down

0 comments on commit 44d4467

Please sign in to comment.