diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-12-15 12:34:59 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-12-17 19:29:53 +0300 |
commit | e4241ec92a02f2430dc6f249d8732ee11a5738e7 (patch) | |
tree | 389102467c829d375ac0af1413e2248e21a9fd5a | |
parent | 8e8146470716158e12cf06d7997c47dcb2b96bb9 (diff) |
hooks: Move `GitPushOptions` into the hook manager's code
The `GitPushOptions` function is used to convert a slice of push options
into a set of environment variables like git would export them. Except
for tests, this code is really only used in the hooks manager's code, so
let's move it there in order to have computation of all custom hook
environment variables in a single place.
-rw-r--r-- | internal/git/hooks/hooks.go | 17 | ||||
-rw-r--r-- | internal/git/hooks/hooks_test.go | 35 | ||||
-rw-r--r-- | internal/gitaly/hook/custom.go | 16 | ||||
-rw-r--r-- | internal/gitaly/hook/custom_test.go | 35 | ||||
-rw-r--r-- | internal/gitaly/hook/postreceive.go | 3 | ||||
-rw-r--r-- | internal/gitaly/hook/prereceive.go | 3 |
6 files changed, 53 insertions, 56 deletions
diff --git a/internal/git/hooks/hooks.go b/internal/git/hooks/hooks.go index 72b3a0df1..0c64d3836 100644 --- a/internal/git/hooks/hooks.go +++ b/internal/git/hooks/hooks.go @@ -1,7 +1,6 @@ package hooks import ( - "fmt" "path/filepath" "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" @@ -25,19 +24,3 @@ func Path(cfg config.Cfg) string { return filepath.Join(cfg.Ruby.Dir, "git-hooks") } - -// GitPushOptions turns a slice of git push option values into a GIT_PUSH_OPTION_COUNT and individual -// GIT_PUSH_OPTION_0, GIT_PUSH_OPTION_1 etc. -func GitPushOptions(options []string) []string { - if len(options) == 0 { - return []string{} - } - - envVars := []string{fmt.Sprintf("GIT_PUSH_OPTION_COUNT=%d", len(options))} - - for i, pushOption := range options { - envVars = append(envVars, fmt.Sprintf("GIT_PUSH_OPTION_%d=%s", i, pushOption)) - } - - return envVars -} diff --git a/internal/git/hooks/hooks_test.go b/internal/git/hooks/hooks_test.go index a55989288..9e86b1247 100644 --- a/internal/git/hooks/hooks_test.go +++ b/internal/git/hooks/hooks_test.go @@ -34,38 +34,3 @@ func TestPath(t *testing.T) { require.Equal(t, "/var/empty", Path(config.Config)) }) } - -func TestGitPushOptions(t *testing.T) { - testCases := []struct { - desc string - input []string - expected []string - }{ - { - desc: "empty input", - input: []string{}, - expected: []string{}, - }, - { - desc: "nil input", - input: nil, - expected: []string{}, - }, - { - desc: "one option", - input: []string{"option1"}, - expected: []string{"GIT_PUSH_OPTION_COUNT=1", "GIT_PUSH_OPTION_0=option1"}, - }, - { - desc: "multiple options", - input: []string{"option1", "option2"}, - expected: []string{"GIT_PUSH_OPTION_COUNT=2", "GIT_PUSH_OPTION_0=option1", "GIT_PUSH_OPTION_1=option2"}, - }, - } - - for _, tc := range testCases { - t.Run(tc.desc, func(t *testing.T) { - require.Equal(t, tc.expected, GitPushOptions(tc.input)) - }) - } -} diff --git a/internal/gitaly/hook/custom.go b/internal/gitaly/hook/custom.go index 2687f926c..90217b3a3 100644 --- a/internal/gitaly/hook/custom.go +++ b/internal/gitaly/hook/custom.go @@ -144,3 +144,19 @@ func customHooksEnv(payload git.HooksPayload) []string { "GL_PROTOCOL=" + payload.ReceiveHooksPayload.Protocol, } } + +// pushOptionsEnv turns a slice of git push option values into a GIT_PUSH_OPTION_COUNT and individual +// GIT_PUSH_OPTION_0, GIT_PUSH_OPTION_1 etc. +func pushOptionsEnv(options []string) []string { + if len(options) == 0 { + return []string{} + } + + envVars := []string{fmt.Sprintf("GIT_PUSH_OPTION_COUNT=%d", len(options))} + + for i, pushOption := range options { + envVars = append(envVars, fmt.Sprintf("GIT_PUSH_OPTION_%d=%s", i, pushOption)) + } + + return envVars +} diff --git a/internal/gitaly/hook/custom_test.go b/internal/gitaly/hook/custom_test.go index 482a78a23..069daecea 100644 --- a/internal/gitaly/hook/custom_test.go +++ b/internal/gitaly/hook/custom_test.go @@ -439,3 +439,38 @@ func writeCustomHook(t *testing.T, hookName, dir string, content []byte) func() os.RemoveAll(dir) } } + +func TestPushOptionsEnv(t *testing.T) { + testCases := []struct { + desc string + input []string + expected []string + }{ + { + desc: "empty input", + input: []string{}, + expected: []string{}, + }, + { + desc: "nil input", + input: nil, + expected: []string{}, + }, + { + desc: "one option", + input: []string{"option1"}, + expected: []string{"GIT_PUSH_OPTION_COUNT=1", "GIT_PUSH_OPTION_0=option1"}, + }, + { + desc: "multiple options", + input: []string{"option1", "option2"}, + expected: []string{"GIT_PUSH_OPTION_COUNT=2", "GIT_PUSH_OPTION_0=option1", "GIT_PUSH_OPTION_1=option2"}, + }, + } + + for _, tc := range testCases { + t.Run(tc.desc, func(t *testing.T) { + require.Equal(t, tc.expected, pushOptionsEnv(tc.input)) + }) + } +} diff --git a/internal/gitaly/hook/postreceive.go b/internal/gitaly/hook/postreceive.go index da763cd32..703011f20 100644 --- a/internal/gitaly/hook/postreceive.go +++ b/internal/gitaly/hook/postreceive.go @@ -11,7 +11,6 @@ import ( "strings" "gitlab.com/gitlab-org/gitaly/internal/git" - "gitlab.com/gitlab-org/gitaly/internal/git/hooks" "gitlab.com/gitlab-org/gitaly/internal/helper" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" ) @@ -169,7 +168,7 @@ func (m *GitLabHookManager) PostReceiveHook(ctx context.Context, repo *gitalypb. } customHooksEnv := append(env, customHooksEnv(payload)...) - customHooksEnv = append(customHooksEnv, hooks.GitPushOptions(pushOptions)...) + customHooksEnv = append(customHooksEnv, pushOptionsEnv(pushOptions)...) if err = executor( ctx, diff --git a/internal/gitaly/hook/prereceive.go b/internal/gitaly/hook/prereceive.go index 56a49fd56..0706b9e94 100644 --- a/internal/gitaly/hook/prereceive.go +++ b/internal/gitaly/hook/prereceive.go @@ -11,7 +11,6 @@ import ( "strings" "gitlab.com/gitlab-org/gitaly/internal/git" - "gitlab.com/gitlab-org/gitaly/internal/git/hooks" "gitlab.com/gitlab-org/gitaly/internal/helper" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" ) @@ -126,7 +125,7 @@ func (m *GitLabHookManager) preReceiveHook(ctx context.Context, payload git.Hook } customHooksEnv := append(env, customHooksEnv(payload)...) - customHooksEnv = append(customHooksEnv, hooks.GitPushOptions(pushOptions)...) + customHooksEnv = append(customHooksEnv, pushOptionsEnv(pushOptions)...) if err = executor( ctx, |