Skip to content

Commit

Permalink
Refactor config (#7)
Browse files Browse the repository at this point in the history
* rename event to rule

* rename event to rule

* Test

* Trst

* Test

* Test

* Test

* Test

* Rename trigger

* Refactor ctx

* refactor config

* Rename processing templazes

* simplified

* new structure

* refactored

* implmented github comment

* compiling

* fix compile errors

* fix api

* added event logging

* refactored plugins

* fix actions

* added debug

* fix github hook

* update github action

* implemented github comments
  • Loading branch information
cheld committed Apr 24, 2022
1 parent 5d7ea0e commit e430b9e
Show file tree
Hide file tree
Showing 36 changed files with 513 additions and 998 deletions.
9 changes: 9 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This configuration file was automatically generated by Gitpod.
# Please adjust to your needs (see https://www.gitpod.io/docs/config-gitpod-file)
# and commit this file to your remote git repository to share the goodness with others.

tasks:
- init: go get && go build ./... && go test ./... && make
command: go run


14 changes: 7 additions & 7 deletions cmd/miniprow/miniprow.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/cheld/miniprow/pkg/common/info"
commonServer "github.com/cheld/miniprow/pkg/common/server"
"github.com/cheld/miniprow/pkg/common/util"
piperServer "github.com/cheld/miniprow/pkg/piper/server"
hookServer "github.com/cheld/miniprow/pkg/hook/server"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

Expand All @@ -50,7 +50,7 @@ chat-ops via /foo style commands and Slack notifications.`,
Run: func(cmd *cobra.Command, args []string) {

// read cli flags
piperFileName, _ := cmd.Flags().GetString("config-piper")
hookFileName, _ := cmd.Flags().GetString("config-hook")
boskosFileName, _ := cmd.Flags().GetString("config-boskos")
secret, _ := cmd.Flags().GetString("secret")
bindaddr, _ := cmd.Flags().GetString("bind-addr")
Expand All @@ -62,8 +62,8 @@ chat-ops via /foo style commands and Slack notifications.`,
util.Environment.Value("PORT").Update(&port)

// read config files
piperCfgFile := util.FindExistingFile(util.DefaultConfigLocations(piperFileName))
piperCfg, err := util.ReadConfiguration(piperCfgFile, "PIPER_CONFIG")
hookCfgFile := util.FindExistingFile(util.DefaultConfigLocations(hookFileName))
hookCfg, err := util.ReadConfiguration(hookCfgFile, "HOOK_CONFIG")
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand All @@ -80,10 +80,10 @@ chat-ops via /foo style commands and Slack notifications.`,

// Register http endpoints
mux := http.NewServeMux()
mux.Handle("/piper/", piperServer.NewHandler(piperCfg, secret))
mux.Handle("/hook/", hookServer.NewHandler(hookCfg, secret))
mux.Handle("/boskos/", boskosServer.NewHandler(boskosCfg))
mux.Handle("/common/", commonServer.NewHandler())
mux.Handle("/metrics", promhttp.Handler())
mux.Handle("/", commonServer.NewHandler())

// Start server
addr := fmt.Sprintf("%s:%d", bindaddr, port)
Expand All @@ -102,7 +102,7 @@ func command() *cobra.Command {
rootCmd.Flags().StringP("bind-addr", "", "0.0.0.0", "the bind addr of the server")
rootCmd.Flags().StringP("secret", "s", "", "Protect access to the webhook")
rootCmd.Flags().StringP("config-boskos", "", "boskos.yaml", "config file for boskos")
rootCmd.Flags().StringP("config-piper", "", "piper.yaml", "config file for piper")
rootCmd.Flags().StringP("config-hook", "", "hook.yaml", "config file for hook")
rootCmd.PersistentFlags().StringP("log-level", "l", "DEBUG", "set debug log level")
return rootCmd
}
Expand Down
File renamed without changes.
65 changes: 14 additions & 51 deletions examples/hello/piper.yaml → examples/hello/workflows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,17 @@ rules:
rules:
- on: github_comment
if-contains: /needs-performance-tests
execute:
action: github_comment_response
comment: Performance tests are scheduled
then:
- trigger: github_comment_response
with:
comment: Performance tests are scheduled
- trigger: jenkins-job
with:
host: localhost
port: 8080
user: admin
pwd: ${{.Environ.jenkins_secret}}
job: performance

rules:
- on: github_comment
Expand All @@ -84,52 +92,6 @@ rules:



events:
- source: github
type: comment
if_contains: /needs-performance-tests
trigger: Github_Comment
values:
number: ${{ .Payload.Issue.Number }}
comment: Performance tests are scheduled

- source: github
type: comment
if_contains: /needs-preview-environment
trigger: Github_Comment
values:
number: ${{ .Payload.Issue.Number }}
comment: This is your preview environment https://www.myapp.com

- source: gitlab
type: comment
if_true: ${{ eq .Payload.Branch "test" }}
trigger: jenkins_job
values:
target: preview

- source: http
type: get
if_contains: action
trigger: Debug
values:
message: '{{ .Payload }}'


- source: cli
type: stdin
if_contains: go
trigger: Debug
values:
message: 'Files: {{.Payload}}'

- source: cli
type: headers
if_true: true
trigger: github_comment
values:
comment: hello
number: 2


triggers:
Expand All @@ -146,8 +108,9 @@ triggers:
############################################################
- name: jenkins_job
type: http
spec:
url: http://admin:{{.Env.SECRET_JENKINS}}@localhost:8080/job/{{.Values.jobname}}/build
parms:
url: http://admin:{{.Env.SECRET_JENKINS}}@localhost:8080/job/{{.trigger.jenkins_job.input.jobname}}/build


- name: Test
type: http
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/cheld/miniprow
go 1.13

require (
github.com/go-playground/webhooks/v6 v6.0.0-rc.1 // indirect
github.com/go-test/deep v1.0.4
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/google/go-cmp v0.4.0
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ github.com/go-openapi/swag v0.19.7/go.mod h1:ao+8BpOPyKdpQz3AOJfbeEVpLmWAvlT1IfT
github.com/go-openapi/validate v0.18.0/go.mod h1:Uh4HdOzKt19xGIGm1qHf/ofbX1YQ4Y+MYsct2VUrAJ4=
github.com/go-openapi/validate v0.19.2/go.mod h1:1tRCw7m3jtI8eNWEEliiAqUIcBztB2KDnRCRMUi7GTA=
github.com/go-openapi/validate v0.19.5/go.mod h1:8DJv2CVJQ6kGNpFW6eV9N3JviE1C85nY1c2z52x1Gk4=
github.com/go-playground/webhooks/v6 v6.0.0-rc.1 h1:U78wIkdcCGzMw38BGw2eep542XRsUr3/kGDSjmFFELU=
github.com/go-playground/webhooks/v6 v6.0.0-rc.1/go.mod h1:GCocmfMtpJdkEOM1uG9p2nXzg1kY5X/LtvQgtPHUaaA=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
Expand All @@ -310,6 +312,7 @@ github.com/gobuffalo/envy v1.6.5/go.mod h1:N+GkhhZ/93bGZc6ZKhJLP6+m+tCNPKwgSpH9k
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/godbus/dbus v0.0.0-20190422162347-ade71ed3457e/go.mod h1:bBOAhwG1umN6/6ZUMtDFBMQR8jRg9O75tm9K00oMsK4=
github.com/gofrs/flock v0.7.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
github.com/gogits/go-gogs-client v0.0.0-20200905025246-8bb8a50cb355/go.mod h1:cY2AIrMgHm6oOHmR7jY+9TtjzSjQ3iG7tURJG3Y6XH0=
github.com/gogo/googleapis v1.1.0/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFGgqEef3s=
github.com/gogo/protobuf v1.0.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
Expand Down Expand Up @@ -729,6 +732,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/syndtr/gocapability v0.0.0-20170704070218-db04d3cc01c8/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
Expand Down Expand Up @@ -1089,6 +1094,7 @@ gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20190709130402-674ba3eaed22/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
Expand Down
10 changes: 10 additions & 0 deletions hook.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
rules:
- if:
trigger: github_comment
when:
contains: /cat
then:
action: github_comment
with:
comment: Grumpy cat

1 change: 1 addition & 0 deletions pkg/common/info/version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package info

//overwritten by CI
var Version = "0.1"
var Commit = "undefined"
4 changes: 2 additions & 2 deletions pkg/common/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func NewHandler() *CommonServer {
server := CommonServer{
mux: http.NewServeMux(),
}
server.mux.Handle("/common/health", handleHealth())
server.mux.Handle("/common/version", handleVersion())
server.mux.Handle("/health", handleHealth())
server.mux.Handle("/version", handleVersion())
return &server
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package util

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package util

import (
"testing"
Expand Down
55 changes: 55 additions & 0 deletions pkg/hook/model/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package model

import (
"fmt"

"github.com/cheld/miniprow/pkg/common/util"
"gopkg.in/yaml.v3"
)

type Configuration struct {
Rules []Rule
}

type Rule struct {
If Trigger
Then Action
}

type Trigger struct {
Trigger string
When map[string]string
}

type Action struct {
Action string
With map[string]interface{}
}

func (config *Configuration) Filter(event *Event) []Rule {
matchingRules := []Rule{}
for _, rule := range config.Rules {
if rule.If.Trigger == event.Type {
resolved, err := util.ProcessAllTemplates(rule, event)
if err != nil {
event.Err("Template for rule %v cannot be processed: %v", rule.If.Trigger, err)
return matchingRules
}
matchingRules = append(matchingRules, resolved.(Rule))
event.Log("Rule configuration %v found", rule.If.Trigger)
}
}
if len(matchingRules) == 0 {
event.Log("No matching rule found")
}
return matchingRules
}

func Load(cfg *[]byte) (Configuration, error) {
var yamlConfig Configuration
err := yaml.Unmarshal(*cfg, &yamlConfig)
if err != nil {
return Configuration{}, fmt.Errorf("Error parsing YAML file: %s", err)
}
return yamlConfig, nil
}
33 changes: 33 additions & 0 deletions pkg/hook/model/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package model

import (
"encoding/json"
"fmt"
)

type Event struct {
Type string
Data interface{}
status status
}

type status struct {
logs []string
err string
}

func (event *Event) Err(message string, params ...interface{}) {
txt := fmt.Sprintf("ERROR: "+message, params...)
event.status.err = txt
event.status.logs = append(event.status.logs, txt)
}

func (event *Event) Log(message string, params ...interface{}) {
txt := fmt.Sprintf(message, params...)
event.status.logs = append(event.status.logs, txt)
}

func (event *Event) Trail() string {
s, _ := json.MarshalIndent(event.status.logs, "", "\t")
return string(s)
}
7 changes: 7 additions & 0 deletions pkg/hook/model/tenant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package model


type Tenant struct {
Config Configuration
Environ map[string]string
}
16 changes: 16 additions & 0 deletions pkg/hook/plugins-imports/plugin-imports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package pluginimports

// We need to empty import all enabled plugins so that they will be linked into
// any hook binary.
import (

// triggers
_ "github.com/cheld/miniprow/pkg/hook/plugins/triggers/github" // Import all enabled plugins.
_ "github.com/cheld/miniprow/pkg/hook/plugins/triggers/http"

// actions
_ "github.com/cheld/miniprow/pkg/hook/plugins/actions/debug"
_ "github.com/cheld/miniprow/pkg/hook/plugins/actions/github"
_ "github.com/cheld/miniprow/pkg/hook/plugins/actions/http"
_ "github.com/cheld/miniprow/pkg/hook/plugins/actions/misc"
)
31 changes: 31 additions & 0 deletions pkg/hook/plugins/actions/actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package actions

import config "github.com/cheld/miniprow/pkg/hook/model"

var (
handlers = map[string]ActionHandler{}
)

// TriggerHandler defines the function contract for all triggers.
type ActionHandler func(map[string]interface{}, *config.Event)

func RegisterHandler(name string, fn ActionHandler) {
handlers[name] = fn
}

func GetHandler(name string) ActionHandler {
return handlers[name]
}

func Handle(triggeredRules []config.Rule, event *config.Event, tenant config.Tenant) {
for _, rule := range triggeredRules {
actionName := rule.Then.Action
event.Log("Action is %v", actionName)
handler := handlers[actionName]
if handler == nil {
event.Log("No action handler implementation for %v", rule.Then.Action)
return
}
handler(rule.Then.With, event)
}
}
18 changes: 18 additions & 0 deletions pkg/hook/plugins/actions/debug/debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package debug

import (
"github.com/cheld/miniprow/pkg/hook/model"
"github.com/cheld/miniprow/pkg/hook/plugins/actions"
)

const (
HANDLER_ID = "debug"
)

func init() {
actions.RegisterHandler(HANDLER_ID, handleAction)
}

func handleAction(params map[string]interface{}, event *model.Event) {
event.Log("Debug action executed")
}
Loading

0 comments on commit e430b9e

Please sign in to comment.