Merge pull request #1 from nopcoder/fix/load-unrecognized-keys

Fix error on unrecognized configuration keys
This commit is contained in:
Su Yang 2023-01-09 22:10:03 +08:00 committed by GitHub
commit f90fc7ce77
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 13 deletions

View file

@ -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

View file

@ -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())
}

View 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

View file

@ -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]