Skip to content

Commit

Permalink
Gitlab: Make commit_limit configurable. Fixes #46
Browse files Browse the repository at this point in the history
  • Loading branch information
fleaz committed Apr 23, 2019
1 parent 8ca6ff6 commit 846c1aa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 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

0 comments on commit 846c1aa

Please sign in to comment.