From dbc0f951ab0939eb4f3b03685883987bf6cfd905 Mon Sep 17 00:00:00 2001 From: "Nelo-T. Wallus" Date: Thu, 27 Apr 2017 16:25:49 +0200 Subject: [PATCH 1/2] Use break instead of fallthrough to properly exit the switch --- hook_payload.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/hook_payload.go b/hook_payload.go index 4f516f7..8a66ebc 100644 --- a/hook_payload.go +++ b/hook_payload.go @@ -92,14 +92,16 @@ func ParseHook(payload []byte) (*HookPayload, error) { if len(hp.After) == 0 { return nil, fmt.Errorf("Invalid hook received, commit hash not found.") } + break case hp.ObjectKind == "pipeline": - fallthrough + break case hp.ObjectKind == "issue": - fallthrough + break case hp.ObjectKind == "merge_request": if hp.ObjectAttributes == nil { return nil, fmt.Errorf("Invalid hook received, attributes not found.") } + break default: return nil, fmt.Errorf("Invalid hook received, payload format not recognized.") } From 89d58ae60af2b4e6a8817592087461a7011c8874 Mon Sep 17 00:00:00 2001 From: "Nelo-T. Wallus" Date: Thu, 27 Apr 2017 16:26:17 +0200 Subject: [PATCH 2/2] Use hp.ObjectKind as expression statement in payload switch I haven't noticed it before, but at least now, with 9.1.0, all payloads contain the `object_kind` member. --- hook_payload.go | 14 +++++--------- stubs/hooks/push.json | 1 + 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/hook_payload.go b/hook_payload.go index 8a66ebc..88a3016 100644 --- a/hook_payload.go +++ b/hook_payload.go @@ -86,18 +86,14 @@ func ParseHook(payload []byte) (*HookPayload, error) { } // Basic sanity check - switch { - case len(hp.ObjectKind) == 0: - // Assume this is a post-receive within repository - if len(hp.After) == 0 { - return nil, fmt.Errorf("Invalid hook received, commit hash not found.") - } + switch hp.ObjectKind { + case "push": break - case hp.ObjectKind == "pipeline": + case "pipeline": break - case hp.ObjectKind == "issue": + case "issue": break - case hp.ObjectKind == "merge_request": + case "merge_request": if hp.ObjectAttributes == nil { return nil, fmt.Errorf("Invalid hook received, attributes not found.") } diff --git a/stubs/hooks/push.json b/stubs/hooks/push.json index 2c35b68..143b238 100644 --- a/stubs/hooks/push.json +++ b/stubs/hooks/push.json @@ -1,4 +1,5 @@ { + "object_kind": "push", "before": "95790bf891e76fee5e1747ab589903a6a1f80f22", "after": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7", "ref": "refs/heads/master",