From cad2441716ddd092df4deec783154761d720c334 Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Thu, 14 Feb 2019 01:32:27 +0100 Subject: [PATCH] [TASK] add gitlab-ci --- .ci/check-gofmt | 8 ++++++++ .ci/check-testfiles | 25 +++++++++++++++++++++++++ .gitlab-ci.yml | 35 +++++++++++++++++++++++++++++++++++ README.md | 6 +++++- circle.yml | 35 ----------------------------------- circleci/main_test.go | 1 + config_example.conf | 1 + deploy.sh | 14 -------------- grafana/main.go | 8 ++++---- grafana/main_test.go | 1 + main.go | 3 ++- runtime/config.go | 5 ++++- runtime/xmpp_test.go | 1 + 13 files changed, 87 insertions(+), 56 deletions(-) create mode 100755 .ci/check-gofmt create mode 100755 .ci/check-testfiles create mode 100644 .gitlab-ci.yml delete mode 100644 circle.yml create mode 100644 circleci/main_test.go delete mode 100755 deploy.sh create mode 100644 grafana/main_test.go create mode 100644 runtime/xmpp_test.go diff --git a/.ci/check-gofmt b/.ci/check-gofmt new file mode 100755 index 0000000..4a1c0b2 --- /dev/null +++ b/.ci/check-gofmt @@ -0,0 +1,8 @@ +#!/bin/bash + +result="$(gofmt -s -l . | grep -v '^vendor/' )" +if [ -n "$result" ]; then + echo "Go code is not formatted, run 'gofmt -s -w .'" >&2 + echo "$result" + exit 1 +fi diff --git a/.ci/check-testfiles b/.ci/check-testfiles new file mode 100755 index 0000000..132ff73 --- /dev/null +++ b/.ci/check-testfiles @@ -0,0 +1,25 @@ +#!/usr/bin/env python +# checks if every desired package has test files + +import os +import re +import sys + +source_re = re.compile(".*\.go") +test_re = re.compile(".*_test\.go") +missing = False + +for root, dirs, files in os.walk("."): + # ignore some paths + if root == "." or root.startswith("./vendor") or root.startswith("./."): + continue + + # source files but not test files? + if len(filter(source_re.match, files)) > 0 and len(filter(test_re.match, files)) == 0: + print("no test files for {}".format(root)) + missing = True + +if missing: + sys.exit(1) +else: + print("every package has test files") diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..07f1c0d --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,35 @@ +image: golang:latest +stages: + - build + - test + +before_script: + - mkdir -p /go/src/dev.sum7.eu/$CI_PROJECT_NAMESPACE/ + - cp -R /builds/$CI_PROJECT_PATH /go/src/dev.sum7.eu/$CI_PROJECT_NAMESPACE/ + - cd /go/src/dev.sum7.eu/$CI_PROJECT_PATH + - go get -d -t ./... + +build-my-project: + stage: build + script: + - go install dev.sum7.eu/$CI_PROJECT_PATH + artifacts: + paths: + - /go/bin/$CI_PROJECT_NAME + +test-my-project: + stage: test + script: + - go get github.com/client9/misspell/cmd/misspell + - misspell -error . + - ./.ci/check-gofmt + - ./.ci/check-testfiles + - go test $(go list ./... | grep -v /vendor/) -v -coverprofile .testCoverage.txt + artifacts: + paths: + - .testCoverage.txt + +test-race-my-project: + stage: test + script: + - go test -race ./... diff --git a/README.md b/README.md index 0223ff6..d0802f4 100644 --- a/README.md +++ b/README.md @@ -1 +1,5 @@ -# hook2xmpp [![CircleCI](https://circleci.com/gh/genofire/hook2xmpp/tree/master.svg?style=shield)](https://circleci.com/gh/genofire/hook2xmpp/tree/master) [![Coverage Status](https://coveralls.io/repos/github/genofire/hook2xmpp/badge.svg?branch=master)](https://coveralls.io/github/genofire/hook2xmpp?branch=master) [![Go Report Card](https://goreportcard.com/badge/github.com/genofire/hook2xmpp)](https://goreportcard.com/report/github.com/genofire/hook2xmpp) [![GoDoc](https://godoc.org/github.com/genofire/hook2xmpp?status.svg)](https://godoc.org/github.com/genofire/hook2xmpp) +# hook2xmpp +[![Build Status](https://dev.sum7.eu/genofire/hook2xmpp/badges/master/build.svg)](https://dev.sum7.eu/genofire/hook2xmpp/pipelines) +[![Go Report Card](https://goreportcard.com/badge/dev.sum7.eu/genofire/hook2xmpp)](https://goreportcard.com/report/dev.sum7.eu/genofire/hook2xmpp) +[![GoDoc](https://godoc.org/dev.sum7.eu/genofire/hook2xmpp?status.svg)](https://godoc.org/dev.sum7.eu/genofire/hook2xmpp) + diff --git a/circle.yml b/circle.yml deleted file mode 100644 index 58adc9a..0000000 --- a/circle.yml +++ /dev/null @@ -1,35 +0,0 @@ -notify: - webhooks: - - url: https://hook2xmpp.pub.warehost.de/circleci - -machine: - environment: - GOROOT: "" - PATH: "/usr/local/go/bin:/usr/local/go_workspace/bin:~/.go_workspace/bin:${PATH}" - GOPATH: "${HOME}/.go_workspace" - -dependencies: - override: - - mkdir -p ~/.go_workspace/src/github.com/${CIRCLE_PROJECT_USERNAME} - - ln -s ${HOME}/${CIRCLE_PROJECT_REPONAME} ${HOME}/.go_workspace/src/github.com/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME} - - go get -t -d -v ./... - - go install github.com/genofire/hook2xmpp/cmd/hook2xmpp - post: - - cp ~/.go_workspace/bin/hook2xmpp hook2xmpp.bin - - tar -cvzf hook2xmpp-builded.tar.gz hook2xmpp.bin config_example.conf - - mv hook2xmpp-builded.tar.gz $CIRCLE_ARTIFACTS - - - -test: - pre: - - go get github.com/mattn/goveralls - - go get golang.org/x/tools/cmd/cover - override: - - ./.test-coverage circle-ci - -deployment: - staging: - branch: master - commands: - - ./deploy.sh $HOST_FOR_STAGING $PORT_FOR_STAGING diff --git a/circleci/main_test.go b/circleci/main_test.go new file mode 100644 index 0000000..f569327 --- /dev/null +++ b/circleci/main_test.go @@ -0,0 +1 @@ +package circleci diff --git a/config_example.conf b/config_example.conf index 7f6b2b8..82fd3b7 100644 --- a/config_example.conf +++ b/config_example.conf @@ -1,3 +1,4 @@ +log_level = 50 webserver_bind = ":8080" [xmpp] diff --git a/deploy.sh b/deploy.sh deleted file mode 100755 index d49bad2..0000000 --- a/deploy.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash -host=$1 -port=$2 -remote="circleci@${host}" -echo "deploying..." -ssh -p $port $remote sudo systemctl stop hook2xmpp; -RETVAL=$? -[ $RETVAL -ne 0 ] && exit 1 -scp -q -P $port ~/.go_workspace/bin/hook2xmpp $remote:~/bin/hook2xmpp; -RETVAL=$? -ssh -p $port $remote sudo systemctl start hook2xmpp; -[ $RETVAL -eq 0 ] && RETVAL=$? -[ $RETVAL -ne 0 ] && exit 1 -echo "deployed" diff --git a/grafana/main.go b/grafana/main.go index 527a295..b674001 100644 --- a/grafana/main.go +++ b/grafana/main.go @@ -1,4 +1,4 @@ -package circleci +package grafana import ( "fmt" @@ -56,8 +56,8 @@ func init() { return } logger = logger.WithFields(map[string]interface{}{ - "url": request.RuleURL, - "msg": request.String(), + "url": request.RuleURL, + "msg": request.String(), "image": request.ImageURL, }) @@ -77,7 +77,7 @@ func init() { runtime.Notify(client, hook, request.String()) if request.ImageURL != "" { runtime.NotifyImage(client, hook, request.ImageURL, request.String()) - }else{ + } else { } logger.Infof("run hook") ok = true diff --git a/grafana/main_test.go b/grafana/main_test.go new file mode 100644 index 0000000..8965472 --- /dev/null +++ b/grafana/main_test.go @@ -0,0 +1 @@ +package grafana diff --git a/main.go b/main.go index b8f57d4..4e56767 100644 --- a/main.go +++ b/main.go @@ -28,6 +28,8 @@ func main() { log.Panicf("error on read config: %s", err) } + log.SetLevel(config.LogLevel) + // load config options := xmpp.Options{ Host: config.XMPP.Host, @@ -90,7 +92,6 @@ func main() { log.Infof("started hock2xmpp with %s", client.JID()) notify("startup of hock2xmpp") - // Wait for system signal sigs := make(chan os.Signal, 1) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) diff --git a/runtime/config.go b/runtime/config.go index ac6fee8..0715be6 100644 --- a/runtime/config.go +++ b/runtime/config.go @@ -1,7 +1,10 @@ package runtime +import "github.com/bdlm/std/logger" + type Config struct { - WebserverBind string `toml:"webserver_bind"` + LogLevel logger.Level `toml:"log_level"` + WebserverBind string `toml:"webserver_bind"` XMPP struct { Host string `toml:"host"` diff --git a/runtime/xmpp_test.go b/runtime/xmpp_test.go new file mode 100644 index 0000000..7ccdf5f --- /dev/null +++ b/runtime/xmpp_test.go @@ -0,0 +1 @@ +package runtime