From b610bfedfd24f3561d255c67d89716da117e9e52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20J=C3=B8rgensen?= Date: Wed, 17 Jul 2019 11:18:23 +0200 Subject: [PATCH] Tag @team within working hours --- webhook/function.go | 33 +++++++++++++++++++++++++++++++++ webhook/go.mod | 1 + webhook/go.sum | 2 ++ 3 files changed, 36 insertions(+) diff --git a/webhook/function.go b/webhook/function.go index e04307d..ee48aac 100644 --- a/webhook/function.go +++ b/webhook/function.go @@ -5,7 +5,9 @@ import ( "log" "net/http" "os" + "time" + "github.com/rickar/cal" "github.com/wm/go-flowdock/flowdock" "gopkg.in/go-playground/webhooks.v5/github" ) @@ -88,6 +90,10 @@ func Handle(w http.ResponseWriter, r *http.Request) { pullRequest.PullRequest.User.Login, ) + if withinWorkingHours() { + str = fmt.Sprintf("@team, %s", str) + } + err = flowpost(str) if err != nil { @@ -143,3 +149,30 @@ func flowpost(msg string) error { return err } + +func withinWorkingHours() bool { + c := workCalendar() + now := time.Now() + + if !c.IsWorkday(now) { + return false + } + + if (now.Hour() < 8) || (now.Hour() > 17) { + return false + } + + return true +} + +func workCalendar() *cal.Calendar { + c := cal.NewCalendar() + + cal.AddDanishHolidays(c) + c.AddHoliday( + cal.DKJuleaften, + cal.DKNytaarsaften, + ) + + return c +} diff --git a/webhook/go.mod b/webhook/go.mod index efc1cd2..786a9db 100644 --- a/webhook/go.mod +++ b/webhook/go.mod @@ -5,6 +5,7 @@ go 1.11 require ( github.com/bernerdschaefer/eventsource v0.0.0-20130606115634-220e99a79763 // indirect github.com/google/go-querystring v1.0.0 // indirect + github.com/rickar/cal v1.0.1 github.com/stretchr/testify v1.3.0 // indirect github.com/wm/go-flowdock v0.0.0-20171019142228-93c0a78fc14f gopkg.in/go-playground/webhooks.v5 v5.12.0 diff --git a/webhook/go.sum b/webhook/go.sum index 2ad6f03..50318e8 100644 --- a/webhook/go.sum +++ b/webhook/go.sum @@ -6,6 +6,8 @@ github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASu github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rickar/cal v1.0.1 h1:Tyjkk4sBvVC3gcXCgLowEM53R2eVfFcoi1gtQuocrmk= +github.com/rickar/cal v1.0.1/go.mod h1:3GBx8OBrvh4/y/JTxM0e1bUUIHMnqILl1rMANHWExxQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=