mirror of
https://github.com/adnanh/webhook.git
synced 2025-08-01 23:40:28 +00:00
Fix error on unrecognized configuration keys
- Error while loading configuration with unrecognized keys - Do not start if failed to load configuration from file
This commit is contained in:
parent
dab29e7267
commit
d7367bcb8b
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()
|
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
|
// Append appends hooks unless the new hooks contain a hook with an ID that already exists
|
||||||
|
|
|
@ -424,6 +424,7 @@ var hooksLoadFromFileTests = []struct {
|
||||||
{"", false, true},
|
{"", false, true},
|
||||||
// failures
|
// failures
|
||||||
{"missing.json", false, false},
|
{"missing.json", false, false},
|
||||||
|
{"testdata/unrecognized.yaml", false, false},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHooksLoadFromFile(t *testing.T) {
|
func TestHooksLoadFromFile(t *testing.T) {
|
||||||
|
@ -433,6 +434,7 @@ func TestHooksLoadFromFile(t *testing.T) {
|
||||||
for _, tt := range hooksLoadFromFileTests {
|
for _, tt := range hooksLoadFromFileTests {
|
||||||
h := &Hooks{}
|
h := &Hooks{}
|
||||||
err := h.LoadFromFile(tt.path, tt.asTemplate)
|
err := h.LoadFromFile(tt.path, tt.asTemplate)
|
||||||
|
t.Log(err)
|
||||||
if (err == nil) != tt.ok {
|
if (err == nil) != tt.ok {
|
||||||
t.Errorf(err.Error())
|
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)
|
err := newHooks.LoadFromFile(hooksFilePath, *asTemplate)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("couldn't load hooks from file! %+v\n", err)
|
log.Fatalf("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.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]
|
newHooksFiles := hooksFiles[:0]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue