mirror of
https://github.com/adnanh/webhook.git
synced 2025-08-01 15:30:30 +00:00
Merge pull request #1 from nopcoder/fix/load-unrecognized-keys
Fix error on unrecognized configuration keys
This commit is contained in:
commit
f90fc7ce77
4 changed files with 49 additions and 13 deletions
|
@ -774,7 +774,13 @@ func (h *Hooks) LoadFromFile(path string, asTemplate bool) error {
|
|||
file = buf.Bytes()
|
||||
}
|
||||
|
||||
return yaml.Unmarshal(file, h)
|
||||
toJSON, err := yaml.YAMLToJSON(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
jsonDecoder := json.NewDecoder(bytes.NewBuffer(toJSON))
|
||||
jsonDecoder.DisallowUnknownFields()
|
||||
return jsonDecoder.Decode(h)
|
||||
}
|
||||
|
||||
// Append appends hooks unless the new hooks contain a hook with an ID that already exists
|
||||
|
|
|
@ -425,6 +425,7 @@ var hooksLoadFromFileTests = []struct {
|
|||
{"", false, true},
|
||||
// failures
|
||||
{"missing.json", false, false},
|
||||
{"testdata/unrecognized.yaml", false, false},
|
||||
}
|
||||
|
||||
func TestHooksLoadFromFile(t *testing.T) {
|
||||
|
@ -434,6 +435,7 @@ func TestHooksLoadFromFile(t *testing.T) {
|
|||
for _, tt := range hooksLoadFromFileTests {
|
||||
h := &Hooks{}
|
||||
err := h.LoadFromFile(tt.path, tt.asTemplate)
|
||||
t.Log(err)
|
||||
if (err == nil) != tt.ok {
|
||||
t.Errorf(err.Error())
|
||||
}
|
||||
|
|
28
internal/hook/testdata/unrecognized.yaml
vendored
Normal file
28
internal/hook/testdata/unrecognized.yaml
vendored
Normal file
|
@ -0,0 +1,28 @@
|
|||
- id: webhook
|
||||
unrecognized-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-hmac-sha1
|
||||
secret: mysecret
|
||||
parameter:
|
||||
source: header
|
||||
name: X-Hub-Signature
|
||||
- match:
|
||||
type: value
|
||||
value: refs/heads/master
|
||||
parameter:
|
||||
source: payload
|
||||
name: ref
|
24
webhook.go
24
webhook.go
|
@ -194,19 +194,19 @@ func main() {
|
|||
err := newHooks.LoadFromFile(hooksFilePath, *asTemplate)
|
||||
|
||||
if err != nil {
|
||||
log.Printf("couldn't load hooks from file! %+v\n", err)
|
||||
} else {
|
||||
log.Printf("found %d hook(s) in file\n", len(newHooks))
|
||||
|
||||
for _, hook := range newHooks {
|
||||
if matchLoadedHook(hook.ID) != nil {
|
||||
log.Fatalf("error: hook with the id %s has already been loaded!\nplease check your hooks file for duplicate hooks ids!\n", hook.ID)
|
||||
}
|
||||
log.Printf("\tloaded: %s\n", hook.ID)
|
||||
}
|
||||
|
||||
loadedHooksFromFiles[hooksFilePath] = newHooks
|
||||
log.Fatalf("couldn't load hooks from file! %+v\n", err)
|
||||
}
|
||||
|
||||
log.Printf("found %d hook(s) in file\n", len(newHooks))
|
||||
|
||||
for _, hook := range newHooks {
|
||||
if matchLoadedHook(hook.ID) != nil {
|
||||
log.Fatalf("error: hook with the id %s has already been loaded!\nplease check your hooks file for duplicate hooks ids!\n", hook.ID)
|
||||
}
|
||||
log.Printf("\tloaded: %s\n", hook.ID)
|
||||
}
|
||||
|
||||
loadedHooksFromFiles[hooksFilePath] = newHooks
|
||||
}
|
||||
|
||||
newHooksFiles := hooksFiles[:0]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue