Skip to content

Commit

Permalink
Merge pull request #158 from adnanh/development
Browse files Browse the repository at this point in the history
2.6.5
  • Loading branch information
adnanh committed Aug 9, 2017
2 parents c7ec25f + cfd138c commit 147c95d
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 52 deletions.
4 changes: 3 additions & 1 deletion hook/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"regexp"
"strconv"
"strings"

"github.com/ghodss/yaml"
)

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

e = json.Unmarshal(file, h)
e = yaml.Unmarshal(file, h)
return e
}

Expand Down
1 change: 1 addition & 0 deletions hook/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ var hooksLoadFromFileTests = []struct {
ok bool
}{
{"../hooks.json.example", true},
{"../hooks.yaml.example", true},
{"", true},
// failures
{"missing.json", false},
Expand Down
28 changes: 28 additions & 0 deletions hooks.yaml.example
Original file line number Diff line number Diff line change
@@ -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 changes: 75 additions & 0 deletions test/hooks.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -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 changes: 12 additions & 3 deletions webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

const (
version = "2.6.4"
version = "2.6.5"
)

var (
Expand Down Expand Up @@ -152,7 +152,16 @@ func main() {
}

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{
Logger: l.ALogger,
Expand Down Expand Up @@ -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)

out, err := cmd.Output()
out, err := cmd.CombinedOutput()

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

Expand Down
100 changes: 52 additions & 48 deletions webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,60 +21,62 @@ func TestWebhook(t *testing.T) {
hookecho, cleanupHookecho := buildHookecho(t)
defer cleanupHookecho()

config, cleanupConfig := genConfig(t, hookecho)
defer cleanupConfig()

webhook, cleanupWebhook := buildWebhook(t)
defer cleanupWebhook()

ip, port := serverAddress(t)
args := []string{fmt.Sprintf("-hooks=%s", config), fmt.Sprintf("-ip=%s", ip), fmt.Sprintf("-port=%s", port), "-verbose"}

cmd := exec.Command(webhook, args...)
//cmd.Stderr = os.Stderr // uncomment to see verbose output
cmd.Env = webhookEnv()
cmd.Args[0] = "webhook"
if err := cmd.Start(); err != nil {
t.Fatalf("failed to start webhook: %s", err)
}
defer killAndWait(cmd)
for _, hookTmpl := range []string{"test/hooks.json.tmpl", "test/hooks.yaml.tmpl"} {
config, cleanupConfig := genConfig(t, hookecho, hookTmpl)
defer cleanupConfig()

webhook, cleanupWebhook := buildWebhook(t)
defer cleanupWebhook()

ip, port := serverAddress(t)
args := []string{fmt.Sprintf("-hooks=%s", config), fmt.Sprintf("-ip=%s", ip), fmt.Sprintf("-port=%s", port), "-verbose"}

cmd := exec.Command(webhook, args...)
//cmd.Stderr = os.Stderr // uncomment to see verbose output
cmd.Env = webhookEnv()
cmd.Args[0] = "webhook"
if err := cmd.Start(); err != nil {
t.Fatalf("failed to start webhook: %s", err)
}
defer killAndWait(cmd)

waitForServerReady(t, ip, port)
waitForServerReady(t, ip, port)

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

req, err := http.NewRequest("POST", url, ioutil.NopCloser(strings.NewReader(tt.body)))
if err != nil {
t.Errorf("New request failed: %s", err)
}
req, err := http.NewRequest("POST", url, ioutil.NopCloser(strings.NewReader(tt.body)))
if err != nil {
t.Errorf("New request failed: %s", err)
}

for k, v := range tt.headers {
req.Header.Add(k, v)
}
for k, v := range tt.headers {
req.Header.Add(k, v)
}

var res *http.Response
var res *http.Response

if tt.urlencoded == true {
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
} else {
req.Header.Add("Content-Type", "application/json")
}
if tt.urlencoded == true {
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
} else {
req.Header.Add("Content-Type", "application/json")
}

client := &http.Client{}
res, err = client.Do(req)
if err != nil {
t.Errorf("client.Do failed: %s", err)
}
client := &http.Client{}
res, err = client.Do(req)
if err != nil {
t.Errorf("client.Do failed: %s", err)
}

body, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Errorf("POST %q: failed to ready body: %s", tt.desc, err)
}
body, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Errorf("POST %q: failed to ready body: %s", tt.desc, err)
}

if res.StatusCode != tt.respStatus || string(body) != tt.respBody {
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)
if res.StatusCode != tt.respStatus || string(body) != tt.respBody {
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)
}
}
}
}
Expand Down Expand Up @@ -103,8 +105,8 @@ func buildHookecho(t *testing.T) (bin string, cleanup func()) {
return bin, func() { os.RemoveAll(tmp) }
}

func genConfig(t *testing.T, bin string) (config string, cleanup func()) {
tmpl := template.Must(template.ParseFiles("test/hooks.json.tmpl"))
func genConfig(t *testing.T, bin string, hookTemplate string) (config string, cleanup func()) {
tmpl := template.Must(template.ParseFiles(hookTemplate))

tmp, err := ioutil.TempDir("", "webhook-config-")
if err != nil {
Expand All @@ -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)
if err != nil {
t.Fatalf("Creating config template: %v", err)
Expand Down

0 comments on commit 147c95d

Please sign in to comment.