Skip to content

Commit 147c95d

Browse files
authored
Merge pull request #158 from adnanh/development
2.6.5
2 parents c7ec25f + cfd138c commit 147c95d

File tree

6 files changed

+171
-52
lines changed

6 files changed

+171
-52
lines changed

hook/hook.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"regexp"
1616
"strconv"
1717
"strings"
18+
19+
"github.com/ghodss/yaml"
1820
)
1921

2022
// Constants used to specify the parameter source
@@ -503,7 +505,7 @@ func (h *Hooks) LoadFromFile(path string) error {
503505
return e
504506
}
505507

506-
e = json.Unmarshal(file, h)
508+
e = yaml.Unmarshal(file, h)
507509
return e
508510
}
509511

hook/hook_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ var hooksLoadFromFileTests = []struct {
233233
ok bool
234234
}{
235235
{"../hooks.json.example", true},
236+
{"../hooks.yaml.example", true},
236237
{"", true},
237238
// failures
238239
{"missing.json", false},

hooks.yaml.example

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
- id: webhook
2+
execute-command: /home/adnan/redeploy-go-webhook.sh
3+
command-working-directory: /home/adnan/go
4+
response-message: I got the payload!
5+
response-headers:
6+
- name: Access-Control-Allow-Origin
7+
value: '*'
8+
pass-arguments-to-command:
9+
- source: payload
10+
name: head_commit.id
11+
- source: payload
12+
name: pusher.name
13+
- source: payload
14+
name: pusher.email
15+
trigger-rule:
16+
and:
17+
- match:
18+
type: payload-hash-sha1
19+
secret: mysecret
20+
parameter:
21+
source: header
22+
name: X-Hub-Signature
23+
- match:
24+
type: value
25+
value: refs/heads/master
26+
parameter:
27+
source: payload
28+
name: ref

test/hooks.yaml.tmpl

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
- trigger-rule:
2+
and:
3+
- match:
4+
parameter:
5+
source: header
6+
name: X-Hub-Signature
7+
secret: mysecret
8+
type: payload-hash-sha1
9+
- match:
10+
parameter:
11+
source: payload
12+
name: ref
13+
type: value
14+
value: refs/heads/master
15+
include-command-output-in-response: true
16+
trigger-rule-mismatch-http-response-code: 400
17+
execute-command: '{{ .Hookecho }}'
18+
pass-arguments-to-command:
19+
- source: payload
20+
name: head_commit.id
21+
- source: payload
22+
name: head_commit.author.email
23+
pass-environment-to-command:
24+
- source: payload
25+
name: head_commit.timestamp
26+
id: github
27+
command-working-directory: /
28+
- trigger-rule:
29+
and:
30+
- match:
31+
parameter:
32+
source: payload
33+
name: payload.canon_url
34+
type: value
35+
value: https://bitbucket.org
36+
- match:
37+
parameter:
38+
source: payload
39+
name: payload.repository.absolute_url
40+
type: value
41+
value: /webhook/testing/
42+
- match:
43+
parameter:
44+
source: payload
45+
name: payload.commits.0.branch
46+
type: value
47+
value: master
48+
parse-parameters-as-json:
49+
- source: payload
50+
name: payload
51+
trigger-rule-mismatch-http-response-code: 999
52+
execute-command: '{{ .Hookecho }}'
53+
response-message: success
54+
include-command-output-in-response: false
55+
id: bitbucket
56+
command-working-directory: /
57+
- trigger-rule:
58+
match:
59+
parameter:
60+
source: payload
61+
name: ref
62+
type: value
63+
value: refs/heads/master
64+
pass-arguments-to-command:
65+
- source: payload
66+
name: commits.0.id
67+
- source: payload
68+
name: user_name
69+
- source: payload
70+
name: user_email
71+
execute-command: '{{ .Hookecho }}'
72+
response-message: success
73+
include-command-output-in-response: true
74+
id: gitlab
75+
command-working-directory: /

webhook.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
)
2222

2323
const (
24-
version = "2.6.4"
24+
version = "2.6.5"
2525
)
2626

2727
var (
@@ -152,7 +152,16 @@ func main() {
152152
}
153153

154154
l := negroni.NewLogger()
155-
l.ALogger = log.New(os.Stderr, "[webhook] ", log.Ldate|log.Ltime)
155+
156+
l.SetFormat("{{.Status}} | {{.Duration}} | {{.Hostname}} | {{.Method}} {{.Path}} \n")
157+
158+
standardLogger := log.New(os.Stdout, "[webhook] ", log.Ldate|log.Ltime)
159+
160+
if !*verbose {
161+
standardLogger.SetOutput(ioutil.Discard)
162+
}
163+
164+
l.ALogger = standardLogger
156165

157166
negroniRecovery := &negroni.Recovery{
158167
Logger: l.ALogger,
@@ -326,7 +335,7 @@ func handleHook(h *hook.Hook, headers, query, payload *map[string]interface{}, b
326335

327336
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)
328337

329-
out, err := cmd.Output()
338+
out, err := cmd.CombinedOutput()
330339

331340
log.Printf("command output: %s\n", out)
332341

webhook_test.go

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -21,60 +21,62 @@ func TestWebhook(t *testing.T) {
2121
hookecho, cleanupHookecho := buildHookecho(t)
2222
defer cleanupHookecho()
2323

24-
config, cleanupConfig := genConfig(t, hookecho)
25-
defer cleanupConfig()
26-
27-
webhook, cleanupWebhook := buildWebhook(t)
28-
defer cleanupWebhook()
29-
30-
ip, port := serverAddress(t)
31-
args := []string{fmt.Sprintf("-hooks=%s", config), fmt.Sprintf("-ip=%s", ip), fmt.Sprintf("-port=%s", port), "-verbose"}
32-
33-
cmd := exec.Command(webhook, args...)
34-
//cmd.Stderr = os.Stderr // uncomment to see verbose output
35-
cmd.Env = webhookEnv()
36-
cmd.Args[0] = "webhook"
37-
if err := cmd.Start(); err != nil {
38-
t.Fatalf("failed to start webhook: %s", err)
39-
}
40-
defer killAndWait(cmd)
24+
for _, hookTmpl := range []string{"test/hooks.json.tmpl", "test/hooks.yaml.tmpl"} {
25+
config, cleanupConfig := genConfig(t, hookecho, hookTmpl)
26+
defer cleanupConfig()
27+
28+
webhook, cleanupWebhook := buildWebhook(t)
29+
defer cleanupWebhook()
30+
31+
ip, port := serverAddress(t)
32+
args := []string{fmt.Sprintf("-hooks=%s", config), fmt.Sprintf("-ip=%s", ip), fmt.Sprintf("-port=%s", port), "-verbose"}
33+
34+
cmd := exec.Command(webhook, args...)
35+
//cmd.Stderr = os.Stderr // uncomment to see verbose output
36+
cmd.Env = webhookEnv()
37+
cmd.Args[0] = "webhook"
38+
if err := cmd.Start(); err != nil {
39+
t.Fatalf("failed to start webhook: %s", err)
40+
}
41+
defer killAndWait(cmd)
4142

42-
waitForServerReady(t, ip, port)
43+
waitForServerReady(t, ip, port)
4344

44-
for _, tt := range hookHandlerTests {
45-
url := fmt.Sprintf("http://%s:%s/hooks/%s", ip, port, tt.id)
45+
for _, tt := range hookHandlerTests {
46+
url := fmt.Sprintf("http://%s:%s/hooks/%s", ip, port, tt.id)
4647

47-
req, err := http.NewRequest("POST", url, ioutil.NopCloser(strings.NewReader(tt.body)))
48-
if err != nil {
49-
t.Errorf("New request failed: %s", err)
50-
}
48+
req, err := http.NewRequest("POST", url, ioutil.NopCloser(strings.NewReader(tt.body)))
49+
if err != nil {
50+
t.Errorf("New request failed: %s", err)
51+
}
5152

52-
for k, v := range tt.headers {
53-
req.Header.Add(k, v)
54-
}
53+
for k, v := range tt.headers {
54+
req.Header.Add(k, v)
55+
}
5556

56-
var res *http.Response
57+
var res *http.Response
5758

58-
if tt.urlencoded == true {
59-
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
60-
} else {
61-
req.Header.Add("Content-Type", "application/json")
62-
}
59+
if tt.urlencoded == true {
60+
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
61+
} else {
62+
req.Header.Add("Content-Type", "application/json")
63+
}
6364

64-
client := &http.Client{}
65-
res, err = client.Do(req)
66-
if err != nil {
67-
t.Errorf("client.Do failed: %s", err)
68-
}
65+
client := &http.Client{}
66+
res, err = client.Do(req)
67+
if err != nil {
68+
t.Errorf("client.Do failed: %s", err)
69+
}
6970

70-
body, err := ioutil.ReadAll(res.Body)
71-
res.Body.Close()
72-
if err != nil {
73-
t.Errorf("POST %q: failed to ready body: %s", tt.desc, err)
74-
}
71+
body, err := ioutil.ReadAll(res.Body)
72+
res.Body.Close()
73+
if err != nil {
74+
t.Errorf("POST %q: failed to ready body: %s", tt.desc, err)
75+
}
7576

76-
if res.StatusCode != tt.respStatus || string(body) != tt.respBody {
77-
t.Errorf("failed %q (id: %s):\nexpected status: %#v, response: %s\ngot status: %#v, response: %s", tt.desc, tt.id, tt.respStatus, tt.respBody, res.StatusCode, body)
77+
if res.StatusCode != tt.respStatus || string(body) != tt.respBody {
78+
t.Errorf("failed %q (id: %s):\nexpected status: %#v, response: %s\ngot status: %#v, response: %s", tt.desc, tt.id, tt.respStatus, tt.respBody, res.StatusCode, body)
79+
}
7880
}
7981
}
8082
}
@@ -103,8 +105,8 @@ func buildHookecho(t *testing.T) (bin string, cleanup func()) {
103105
return bin, func() { os.RemoveAll(tmp) }
104106
}
105107

106-
func genConfig(t *testing.T, bin string) (config string, cleanup func()) {
107-
tmpl := template.Must(template.ParseFiles("test/hooks.json.tmpl"))
108+
func genConfig(t *testing.T, bin string, hookTemplate string) (config string, cleanup func()) {
109+
tmpl := template.Must(template.ParseFiles(hookTemplate))
108110

109111
tmp, err := ioutil.TempDir("", "webhook-config-")
110112
if err != nil {
@@ -116,7 +118,9 @@ func genConfig(t *testing.T, bin string) (config string, cleanup func()) {
116118
}
117119
}()
118120

119-
path := filepath.Join(tmp, "hooks.json")
121+
outputBaseName := filepath.Ext(filepath.Ext(hookTemplate))
122+
123+
path := filepath.Join(tmp, outputBaseName)
120124
file, err := os.Create(path)
121125
if err != nil {
122126
t.Fatalf("Creating config template: %v", err)

0 commit comments

Comments
 (0)