diff --git a/internal/gitaly/hook/postreceive.go b/internal/gitaly/hook/postreceive.go index 2063b1adf992392541c0fa55ac5b1e379bc48db3..36faa08d13f2ccfd3752436ae03f98188f5fa54f 100644 --- a/internal/gitaly/hook/postreceive.go +++ b/internal/gitaly/hook/postreceive.go @@ -34,6 +34,9 @@ const ( func getEnvVar(key string, vars []string) string { for _, varPair := range vars { kv := strings.SplitN(varPair, "=", 2) + if len(kv) < 2 { + continue + } if kv[0] == key { return kv[1] } diff --git a/internal/gitaly/hook/postreceive_test.go b/internal/gitaly/hook/postreceive_test.go index 5fb125ad044013482c26889b9648595a06914136..e9abedb5385d44ea64c30835d1e610d878993d60 100644 --- a/internal/gitaly/hook/postreceive_test.go +++ b/internal/gitaly/hook/postreceive_test.go @@ -410,3 +410,57 @@ func TestPostReceive_quarantine(t *testing.T) { }) } } + +func Test_getEnvVar(t *testing.T) { + type args struct { + key string + vars []string + } + tests := []struct { + desc string + args args + want string + }{ + { + desc: "success found key from key-value pairs", + args: args{ + key: "key", + vars: []string{ + "key1=value1", + "key=value", + "key2=value2", + }, + }, + want: "value", + }, + { + desc: "invalid key-value format, only key name", + args: args{ + key: "key", + vars: []string{ + "key", + "key=value", + "key2=value2", + }, + }, + want: "value", + }, + { + desc: "key not found", + args: args{ + key: "key", + vars: []string{ + "key", + "key1=value1", + "key2=value2", + }, + }, + want: "", + }, + } + for _, tt := range tests { + t.Run(tt.desc, func(t *testing.T) { + assert.Equalf(t, tt.want, getEnvVar(tt.args.key, tt.args.vars), "getEnvVar(%v, %v)", tt.args.key, tt.args.vars) + }) + } +}