mirror of
https://github.com/adnanh/webhook.git
synced 2025-06-04 19:52:29 +00:00
commit
147c95dd8b
6 changed files with 170 additions and 51 deletions
|
@ -15,6 +15,8 @@ import (
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/ghodss/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Constants used to specify the parameter source
|
// Constants used to specify the parameter source
|
||||||
|
@ -503,7 +505,7 @@ func (h *Hooks) LoadFromFile(path string) error {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
e = json.Unmarshal(file, h)
|
e = yaml.Unmarshal(file, h)
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -233,6 +233,7 @@ var hooksLoadFromFileTests = []struct {
|
||||||
ok bool
|
ok bool
|
||||||
}{
|
}{
|
||||||
{"../hooks.json.example", true},
|
{"../hooks.json.example", true},
|
||||||
|
{"../hooks.yaml.example", true},
|
||||||
{"", true},
|
{"", true},
|
||||||
// failures
|
// failures
|
||||||
{"missing.json", false},
|
{"missing.json", false},
|
||||||
|
|
28
hooks.yaml.example
Normal file
28
hooks.yaml.example
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
- id: webhook
|
||||||
|
execute-command: /home/adnan/redeploy-go-webhook.sh
|
||||||
|
command-working-directory: /home/adnan/go
|
||||||
|
response-message: I got the payload!
|
||||||
|
response-headers:
|
||||||
|
- name: Access-Control-Allow-Origin
|
||||||
|
value: '*'
|
||||||
|
pass-arguments-to-command:
|
||||||
|
- source: payload
|
||||||
|
name: head_commit.id
|
||||||
|
- source: payload
|
||||||
|
name: pusher.name
|
||||||
|
- source: payload
|
||||||
|
name: pusher.email
|
||||||
|
trigger-rule:
|
||||||
|
and:
|
||||||
|
- match:
|
||||||
|
type: payload-hash-sha1
|
||||||
|
secret: mysecret
|
||||||
|
parameter:
|
||||||
|
source: header
|
||||||
|
name: X-Hub-Signature
|
||||||
|
- match:
|
||||||
|
type: value
|
||||||
|
value: refs/heads/master
|
||||||
|
parameter:
|
||||||
|
source: payload
|
||||||
|
name: ref
|
75
test/hooks.yaml.tmpl
Normal file
75
test/hooks.yaml.tmpl
Normal file
|
@ -0,0 +1,75 @@
|
||||||
|
- trigger-rule:
|
||||||
|
and:
|
||||||
|
- match:
|
||||||
|
parameter:
|
||||||
|
source: header
|
||||||
|
name: X-Hub-Signature
|
||||||
|
secret: mysecret
|
||||||
|
type: payload-hash-sha1
|
||||||
|
- match:
|
||||||
|
parameter:
|
||||||
|
source: payload
|
||||||
|
name: ref
|
||||||
|
type: value
|
||||||
|
value: refs/heads/master
|
||||||
|
include-command-output-in-response: true
|
||||||
|
trigger-rule-mismatch-http-response-code: 400
|
||||||
|
execute-command: '{{ .Hookecho }}'
|
||||||
|
pass-arguments-to-command:
|
||||||
|
- source: payload
|
||||||
|
name: head_commit.id
|
||||||
|
- source: payload
|
||||||
|
name: head_commit.author.email
|
||||||
|
pass-environment-to-command:
|
||||||
|
- source: payload
|
||||||
|
name: head_commit.timestamp
|
||||||
|
id: github
|
||||||
|
command-working-directory: /
|
||||||
|
- trigger-rule:
|
||||||
|
and:
|
||||||
|
- match:
|
||||||
|
parameter:
|
||||||
|
source: payload
|
||||||
|
name: payload.canon_url
|
||||||
|
type: value
|
||||||
|
value: https://bitbucket.org
|
||||||
|
- match:
|
||||||
|
parameter:
|
||||||
|
source: payload
|
||||||
|
name: payload.repository.absolute_url
|
||||||
|
type: value
|
||||||
|
value: /webhook/testing/
|
||||||
|
- match:
|
||||||
|
parameter:
|
||||||
|
source: payload
|
||||||
|
name: payload.commits.0.branch
|
||||||
|
type: value
|
||||||
|
value: master
|
||||||
|
parse-parameters-as-json:
|
||||||
|
- source: payload
|
||||||
|
name: payload
|
||||||
|
trigger-rule-mismatch-http-response-code: 999
|
||||||
|
execute-command: '{{ .Hookecho }}'
|
||||||
|
response-message: success
|
||||||
|
include-command-output-in-response: false
|
||||||
|
id: bitbucket
|
||||||
|
command-working-directory: /
|
||||||
|
- trigger-rule:
|
||||||
|
match:
|
||||||
|
parameter:
|
||||||
|
source: payload
|
||||||
|
name: ref
|
||||||
|
type: value
|
||||||
|
value: refs/heads/master
|
||||||
|
pass-arguments-to-command:
|
||||||
|
- source: payload
|
||||||
|
name: commits.0.id
|
||||||
|
- source: payload
|
||||||
|
name: user_name
|
||||||
|
- source: payload
|
||||||
|
name: user_email
|
||||||
|
execute-command: '{{ .Hookecho }}'
|
||||||
|
response-message: success
|
||||||
|
include-command-output-in-response: true
|
||||||
|
id: gitlab
|
||||||
|
command-working-directory: /
|
15
webhook.go
15
webhook.go
|
@ -21,7 +21,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
version = "2.6.4"
|
version = "2.6.5"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -152,7 +152,16 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
l := negroni.NewLogger()
|
l := negroni.NewLogger()
|
||||||
l.ALogger = log.New(os.Stderr, "[webhook] ", log.Ldate|log.Ltime)
|
|
||||||
|
l.SetFormat("{{.Status}} | {{.Duration}} | {{.Hostname}} | {{.Method}} {{.Path}} \n")
|
||||||
|
|
||||||
|
standardLogger := log.New(os.Stdout, "[webhook] ", log.Ldate|log.Ltime)
|
||||||
|
|
||||||
|
if !*verbose {
|
||||||
|
standardLogger.SetOutput(ioutil.Discard)
|
||||||
|
}
|
||||||
|
|
||||||
|
l.ALogger = standardLogger
|
||||||
|
|
||||||
negroniRecovery := &negroni.Recovery{
|
negroniRecovery := &negroni.Recovery{
|
||||||
Logger: l.ALogger,
|
Logger: l.ALogger,
|
||||||
|
@ -326,7 +335,7 @@ func handleHook(h *hook.Hook, headers, query, payload *map[string]interface{}, b
|
||||||
|
|
||||||
log.Printf("executing %s (%s) with arguments %q and environment %s using %s as cwd\n", h.ExecuteCommand, cmd.Path, cmd.Args, envs, cmd.Dir)
|
log.Printf("executing %s (%s) with arguments %q and environment %s using %s as cwd\n", h.ExecuteCommand, cmd.Path, cmd.Args, envs, cmd.Dir)
|
||||||
|
|
||||||
out, err := cmd.Output()
|
out, err := cmd.CombinedOutput()
|
||||||
|
|
||||||
log.Printf("command output: %s\n", out)
|
log.Printf("command output: %s\n", out)
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,8 @@ func TestWebhook(t *testing.T) {
|
||||||
hookecho, cleanupHookecho := buildHookecho(t)
|
hookecho, cleanupHookecho := buildHookecho(t)
|
||||||
defer cleanupHookecho()
|
defer cleanupHookecho()
|
||||||
|
|
||||||
config, cleanupConfig := genConfig(t, hookecho)
|
for _, hookTmpl := range []string{"test/hooks.json.tmpl", "test/hooks.yaml.tmpl"} {
|
||||||
|
config, cleanupConfig := genConfig(t, hookecho, hookTmpl)
|
||||||
defer cleanupConfig()
|
defer cleanupConfig()
|
||||||
|
|
||||||
webhook, cleanupWebhook := buildWebhook(t)
|
webhook, cleanupWebhook := buildWebhook(t)
|
||||||
|
@ -78,6 +79,7 @@ func TestWebhook(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func buildHookecho(t *testing.T) (bin string, cleanup func()) {
|
func buildHookecho(t *testing.T) (bin string, cleanup func()) {
|
||||||
tmp, err := ioutil.TempDir("", "hookecho-test-")
|
tmp, err := ioutil.TempDir("", "hookecho-test-")
|
||||||
|
@ -103,8 +105,8 @@ func buildHookecho(t *testing.T) (bin string, cleanup func()) {
|
||||||
return bin, func() { os.RemoveAll(tmp) }
|
return bin, func() { os.RemoveAll(tmp) }
|
||||||
}
|
}
|
||||||
|
|
||||||
func genConfig(t *testing.T, bin string) (config string, cleanup func()) {
|
func genConfig(t *testing.T, bin string, hookTemplate string) (config string, cleanup func()) {
|
||||||
tmpl := template.Must(template.ParseFiles("test/hooks.json.tmpl"))
|
tmpl := template.Must(template.ParseFiles(hookTemplate))
|
||||||
|
|
||||||
tmp, err := ioutil.TempDir("", "webhook-config-")
|
tmp, err := ioutil.TempDir("", "webhook-config-")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -116,7 +118,9 @@ func genConfig(t *testing.T, bin string) (config string, cleanup func()) {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
path := filepath.Join(tmp, "hooks.json")
|
outputBaseName := filepath.Ext(filepath.Ext(hookTemplate))
|
||||||
|
|
||||||
|
path := filepath.Join(tmp, outputBaseName)
|
||||||
file, err := os.Create(path)
|
file, err := os.Create(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Creating config template: %v", err)
|
t.Fatalf("Creating config template: %v", err)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue