Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
fleaz committed Nov 25, 2018
2 parents 49377c8 + 2b84cd4 commit a04922c
Show file tree
Hide file tree
Showing 19 changed files with 231 additions and 187 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ cpthook.yml
.idea/
*.crt
vendor/
.vendor-new/

# Binarys
CptHook
Expand Down
3 changes: 3 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ archive:
replacements:
linux: Linux
amd64: x86_64
files:
- LICENSE
- cpthook.yml.example

checksum:
name_template: 'checksums.txt'
Expand Down
18 changes: 8 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ before_install:

script:
- cp cpthook.yml.example cpthook.yml
- go test
- go test ./...
- CGO_ENABLED="0" GOARCH="amd64" GOOS="linux" go build -a -installsuffix cgo -o CptHook
- docker build -f Dockerfile -t $IMAGE .

Expand All @@ -28,6 +28,13 @@ deploy:
script: bash scripts/deploy.sh stable
on:
branch: master
# Build tagged Docker container
- provider: script
skip_cleanup: true
script: bash scripts/deploy.sh $TRAVIS_TAG
on:
tags: true
branch: master
# Build development Docker container
- provider: script
skip_cleanup: true
Expand All @@ -43,13 +50,4 @@ deploy:
condition: $TRAVIS_OS_NAME = linux

notifications:
webhooks:
urls:
- http://webhooks.rainbownerds.de:12345
on_success: always # default: always
on_failure: always # default: always
on_start: always # default: never
on_cancel: always # default: always
on_error: always # default: always

email: false
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ FROM alpine
LABEL maintainer mail@fleaz.me

RUN apk add --no-cache ca-certificates
COPY templates/ /templates
COPY CptHook /
EXPOSE 8086
CMD ["/CptHook"]
80 changes: 54 additions & 26 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions cpthook.yml.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
http:
listen: ":8086"

logging:
level: "INFO"

irc:
host: "irc.hackint.eu"
port: 6667
Expand Down
30 changes: 17 additions & 13 deletions gitlab.go → input/gitlab.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package main
package input

import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"text/template"

log "github.com/sirupsen/logrus"

"github.com/spf13/viper"
)

type GitlabModule struct {
channelMapping mapping
channel chan IRCMessage
}

type mapping struct {
Expand All @@ -31,6 +33,14 @@ func contains(mapping map[string][]string, entry string) bool {
return false
}

func (m *GitlabModule) Init(c *viper.Viper, channel *chan IRCMessage) {
err := c.Unmarshal(&m.channelMapping)
if err != nil {
log.Fatal("Failed to unmarshal channelmapping into struct")
}
m.channel = *channel
}

func (m GitlabModule) sendMessage(message string, projectName string, namespace string) {
var channelNames []string
var fullProjectName = namespace + "/" + projectName
Expand All @@ -51,19 +61,12 @@ func (m GitlabModule) sendMessage(message string, projectName string, namespace
var event IRCMessage
event.Messages = append(event.Messages, message)
event.Channel = channelName
messageChannel <- event
m.channel <- event
}

}

func (m *GitlabModule) init(c *viper.Viper) {
err := c.Unmarshal(&m.channelMapping)
if err != nil {
log.Fatal("Failed to unmarshal channelmapping into struct")
}
}

func (m GitlabModule) getChannelList() []string {
func (m GitlabModule) GetChannelList() []string {
var all []string

for _, v := range m.channelMapping.ExplicitMappings {
Expand All @@ -81,11 +84,11 @@ func (m GitlabModule) getChannelList() []string {
return all
}

func (m GitlabModule) getEndpoint() string {
func (m GitlabModule) GetEndpoint() string {
return "/gitlab"
}

func (m GitlabModule) getHandler() http.HandlerFunc {
func (m GitlabModule) GetHandler() http.HandlerFunc {

const pushCompareString = "[\x0312{{ .Project.Name }}\x03] {{ .UserName }} pushed {{ .TotalCommits }} commits to \x0305{{ .Branch }}\x03 {{ .Project.WebURL }}/compare/{{ .BeforeCommit }}...{{ .AfterCommit }}"
const pushCommitLogString = "[\x0312{{ .Project.Name }}\x03] {{ .UserName }} pushed {{ .TotalCommits }} commits to \x0305{{ .Branch }}\x03 {{ .Project.WebURL }}/commits/{{ .Branch }}"
Expand Down Expand Up @@ -167,6 +170,7 @@ func (m GitlabModule) getHandler() http.HandlerFunc {
}

return func(wr http.ResponseWriter, req *http.Request) {
log.Debug("Got a request for the GitlabModule")
defer req.Body.Close()
decoder := json.NewDecoder(req.Body)

Expand Down
19 changes: 10 additions & 9 deletions gitlab_test.go → input/gitlab_test.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
package main
package input

import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"

log "github.com/sirupsen/logrus"

"github.com/spf13/viper"
)

func TestGitlabHandler(t *testing.T) {
viper.SetConfigName("cpthook")
viper.AddConfigPath(".")
viper.AddConfigPath("../")
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("Fatal error config file: %s", err))
log.Panic(err)
}

file, e := os.Open("./tests/gitlab.json")
file, e := os.Open("./test_data/gitlab.json")
if e != nil {
fmt.Printf("File error: %v\n", e)
os.Exit(1)
log.Fatal(e)
}

req, err := http.NewRequest("POST", "/", file)
Expand All @@ -33,8 +33,9 @@ func TestGitlabHandler(t *testing.T) {

rr := httptest.NewRecorder()
var gitlabModule Module = &GitlabModule{}
gitlabModule.init(viper.Sub("modules.gitlab"))
handler := http.HandlerFunc(gitlabModule.getHandler())
c := make(chan IRCMessage, 10)
gitlabModule.Init(viper.Sub("modules.gitlab"), &c)
handler := http.HandlerFunc(gitlabModule.GetHandler())

handler.ServeHTTP(rr, req)

Expand Down
21 changes: 21 additions & 0 deletions input/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package input

import (
"net/http"

"github.com/spf13/viper"
)

// Module defines a common interface for all CptHook modules
type Module interface {
Init(c *viper.Viper, channel *chan IRCMessage)
GetChannelList() []string
GetEndpoint() string
GetHandler() http.HandlerFunc
}

// IRCMessage are send over the inputChannel from the different modules
type IRCMessage struct {
Messages []string
Channel string
}
Loading

0 comments on commit a04922c

Please sign in to comment.