Skip to content

Commit

Permalink
Release v0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
fleaz committed May 2, 2019
2 parents c4b081c + 0746558 commit 0842b9b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 15 deletions.
1 change: 1 addition & 0 deletions cpthook.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ modules:
gitlab:
enabled: true
default: "#defaultChannel"
commit_limit: 3
groups:
"myGitlabGroup":
- "#groupChannel"
Expand Down
22 changes: 18 additions & 4 deletions input/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
type GitlabModule struct {
channelMapping mapping
channel chan IRCMessage
commitLimit int
}

type mapping struct {
Expand All @@ -39,6 +40,19 @@ func (m *GitlabModule) Init(c *viper.Viper, channel *chan IRCMessage) {
log.Fatal("Failed to unmarshal channelmapping into struct")
}
m.channel = *channel

if c.IsSet("commit_limit") {
commitLimit := c.GetInt("commit_limit")
if 0 < commitLimit && commitLimit <= 20 {
m.commitLimit = commitLimit
} else {
log.Debug("commit_limit was set to an invalid value. Using default of 3")
m.commitLimit = 3
}
} else {
m.commitLimit = 3
}

}

func (m GitlabModule) sendMessage(message string, projectName string, namespace string) {
Expand Down Expand Up @@ -389,8 +403,8 @@ func (m GitlabModule) GetHandler() http.HandlerFunc {
m.sendMessage(buf.String(), pushEvent.Project.Name, pushEvent.Project.Namespace)

// Limit number of commit meessages to 3
if pushEvent.TotalCommits > 3 {
pushEvent.Commits = pushEvent.Commits[0:3]
if pushEvent.TotalCommits > m.commitLimit {
pushEvent.Commits = pushEvent.Commits[0:m.commitLimit]
}

for _, commit := range pushEvent.Commits {
Expand Down Expand Up @@ -422,8 +436,8 @@ func (m GitlabModule) GetHandler() http.HandlerFunc {
m.sendMessage(buf.String(), pushEvent.Project.Name, pushEvent.Project.Namespace)
}

if pushEvent.TotalCommits > 3 {
var message = fmt.Sprintf("and %d more commits.", pushEvent.TotalCommits-3)
if pushEvent.TotalCommits > m.commitLimit {
var message = fmt.Sprintf("and %d more commits.", pushEvent.TotalCommits-m.commitLimit)
m.sendMessage(message, pushEvent.Project.Name, pushEvent.Project.Namespace)
}
}
Expand Down
22 changes: 15 additions & 7 deletions input/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package input
import (
"bytes"
"encoding/json"
"net"
"net/http"
"regexp"
"strings"
Expand All @@ -17,7 +18,7 @@ import (
type PrometheusModule struct {
defaultChannel string
channel chan IRCMessage
hostnameFilter string
hostnameFilter *regexp.Regexp
}

type alert struct {
Expand Down Expand Up @@ -77,9 +78,12 @@ func getColorcode(status string) string {
}
}

func shortenInstanceName(name string, pattern string) string {
r := regexp.MustCompile(pattern)
match := r.FindStringSubmatch(name)
func shortenInstanceName(name string, pattern *regexp.Regexp) string {
if net.ParseIP(name) != nil {
// Don't try to shorten an IP address
return name
}
match := pattern.FindStringSubmatch(name)
if len(match) > 1 {
return match[1]
}
Expand All @@ -96,8 +100,12 @@ func (m PrometheusModule) GetChannelList() []string {

func (m *PrometheusModule) Init(c *viper.Viper, channel *chan IRCMessage) {
m.defaultChannel = c.GetString("channel")
pattern, err := regexp.Compile(c.GetString("hostname_filter"))
if err != nil {
log.Fatalf("Error while parsing hostname_filter: %s", err)
}
m.channel = *channel
m.hostnameFilter = c.GetString("hostname_filter")
m.hostnameFilter = pattern
}

func (m PrometheusModule) GetHandler() http.HandlerFunc {
Expand Down Expand Up @@ -197,9 +205,9 @@ func (m PrometheusModule) GetHandler() http.HandlerFunc {
// getNameFromLabels tries to determine a meaningful name for an alert
// If the alert has no 'instance' label, we use the 'alertname' which should always
// be present in an alert
func getNameFromLabels(alert *alert, filter string) string {
func getNameFromLabels(alert *alert, pattern *regexp.Regexp) string {
if instance, ok := alert.Labels["instance"]; ok {
return shortenInstanceName(instance.(string), filter)
return shortenInstanceName(instance.(string), pattern)
} else if alertName, ok := alert.Labels["alertname"]; ok {
return alertName.(string)
} else {
Expand Down
20 changes: 18 additions & 2 deletions irc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"strings"
"sync"
Expand All @@ -14,8 +15,10 @@ import (
"github.com/spf13/viper"
)

var client *girc.Client
var clientLock = &sync.RWMutex{}
var (
client *girc.Client
clientLock = &sync.RWMutex{}
)

func ircConnection(config *viper.Viper, channelList []string) {
clientConfig := girc.Config{
Expand Down Expand Up @@ -89,6 +92,19 @@ func ircConnection(config *viper.Viper, channelList []string) {
}
})

client.Handlers.Add(girc.PRIVMSG, func(c *girc.Client, e girc.Event) {
if e.IsFromUser() {
log.Debugf("Received a query: %v", e)
message := "Hi. I'm a CptHook bot."
if version == "dev" {
message += fmt.Sprintf(" I was compiled by hand at %v", date)
} else {
message += fmt.Sprintf(" I am running v%v (Commit: %v, Builddate: %v)", version, commit, date)
}
c.Cmd.ReplyTo(e, message)
}
})

// Start thread to process message queue
go channelReceiver()

Expand Down
10 changes: 8 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ import (
"net/http"
"path"
"strings"
"time"

log "github.com/sirupsen/logrus"

"github.com/fleaz/CptHook/input"
"github.com/spf13/viper"
)

var inputChannel = make(chan input.IRCMessage, 10)
var (
inputChannel = make(chan input.IRCMessage, 10)
version = "dev"
commit = "none"
date = time.Now().Format(time.RFC3339)
)

func configureLogLevel() {
if l := viper.GetString("logging.level"); l != "" {
Expand Down Expand Up @@ -89,7 +95,7 @@ func main() {
for moduleName := range configurtaion.Modules {
module, err := createModuleObject(moduleName)
if err != nil {
log.Error(err)
log.Warn(err)
continue
}
log.Infof("Loaded module %q", moduleName)
Expand Down

0 comments on commit 0842b9b

Please sign in to comment.