Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Steinhardt <psteinhardt@gitlab.com>2020-09-16 09:20:39 +0300
committerPatrick Steinhardt <psteinhardt@gitlab.com>2020-09-16 09:20:39 +0300
commitb44de3009cd851681a21565a37d4072474aaf2cd (patch)
treee43c5a16b92d118c2ae91c0da6f53ac2473ebb69
parentb81ed87e4e8330295d0e12cd70a04e96c41e6992 (diff)
hooks: Call reference-transaction hook from Ruby HooksService
When modifying Git references, we want to perform voting on the change that's about to be made to enable strong consistency. While we're currently emulating this behaviour mostly via the pre-receive hook, eventually we'll want to fully migrate to the new reference-transaction hook. This buys us more granularity, allows us to capture reference updates across all commands and also allows us to abort any update. To reach this goal, there's two steps we need to do: first we need to always invoke Git with appropriate environment variables to make our reference-transaction hook implementation aware of the ongoing transaction. And second we'll need to adjust places where we manually call out to Git hooks. This commit addresses the second part for our Ruby sidecar, invoking the reference-transaction hook in the hooks service. As the hook implementation is currently hidden behind a feature flag, this commit also sets up a separate feature flag for Ruby. If set, it knows to set up the correct environment variables required by our hook implementation to execute the actual logic.
-rw-r--r--changelogs/unreleased/pks-ruby-reftx-hook.yml5
-rw-r--r--internal/gitaly/service/operations/branches_test.go23
-rw-r--r--internal/metadata/featureflag/feature_flags.go4
-rw-r--r--ruby/lib/gitlab/git/hook.rb3
-rw-r--r--ruby/lib/gitlab/git/hooks_service.rb2
5 files changed, 34 insertions, 3 deletions
diff --git a/changelogs/unreleased/pks-ruby-reftx-hook.yml b/changelogs/unreleased/pks-ruby-reftx-hook.yml
new file mode 100644
index 000000000..a39abc55e
--- /dev/null
+++ b/changelogs/unreleased/pks-ruby-reftx-hook.yml
@@ -0,0 +1,5 @@
+---
+title: 'hooks: Call reference-transaction hook from Ruby HooksService'
+merge_request: 2566
+author:
+type: added
diff --git a/internal/gitaly/service/operations/branches_test.go b/internal/gitaly/service/operations/branches_test.go
index db772db06..8a93f4abf 100644
--- a/internal/gitaly/service/operations/branches_test.go
+++ b/internal/gitaly/service/operations/branches_test.go
@@ -5,6 +5,7 @@ import (
"fmt"
"net"
"os/exec"
+ "strconv"
"testing"
"github.com/stretchr/testify/require"
@@ -109,6 +110,15 @@ func TestSuccessfulGitHooksForUserCreateBranchRequest(t *testing.T) {
}
func TestUserCreateBranchWithTransaction(t *testing.T) {
+ t.Run("with reftx hook", func(t *testing.T) {
+ testUserCreateBranchWithTransaction(t, true)
+ })
+ t.Run("without reftx hook ", func(t *testing.T) {
+ testUserCreateBranchWithTransaction(t, false)
+ })
+}
+
+func testUserCreateBranchWithTransaction(t *testing.T, withRefTxHook bool) {
testRepo, testRepoPath, cleanup := testhelper.NewTestRepo(t)
defer cleanup()
@@ -177,6 +187,12 @@ func TestUserCreateBranchWithTransaction(t *testing.T) {
require.NoError(t, err)
ctx = helper.IncomingToOutgoing(ctx)
+ ctx = featureflag.OutgoingCtxWithFeatureFlagValue(
+ ctx,
+ featureflag.RubyReferenceTransactionHook,
+ strconv.FormatBool(withRefTxHook),
+ )
+
request := &gitalypb.UserCreateBranchRequest{
Repository: testRepo,
BranchName: []byte("new-branch"),
@@ -188,7 +204,12 @@ func TestUserCreateBranchWithTransaction(t *testing.T) {
response, err := client.UserCreateBranch(ctx, request)
require.NoError(t, err)
require.Empty(t, response.PreReceiveError)
- require.Equal(t, 1, transactionServer.called)
+
+ if withRefTxHook {
+ require.Equal(t, 2, transactionServer.called)
+ } else {
+ require.Equal(t, 1, transactionServer.called)
+ }
})
}
}
diff --git a/internal/metadata/featureflag/feature_flags.go b/internal/metadata/featureflag/feature_flags.go
index e4309db49..836db2201 100644
--- a/internal/metadata/featureflag/feature_flags.go
+++ b/internal/metadata/featureflag/feature_flags.go
@@ -29,6 +29,9 @@ var (
// ReferenceTransactionHook will enable the reference-transaction hook
// introduced with Git v2.28.0 for voting on transactions
ReferenceTransactionHook = FeatureFlag{Name: "reference_transaction_hook", OnByDefault: true}
+ // RubyReferenceTransactionHook will enable the reference-transaction hook
+ // introduced with Git v2.28.0 for voting on transactions in the Ruby sidecar.
+ RubyReferenceTransactionHook = FeatureFlag{Name: "ruby_reference_transaction_hook", OnByDefault: false}
// LogCommandStats will log additional rusage stats for commands
LogCommandStats = FeatureFlag{Name: "log_command_stats", OnByDefault: false}
// GoUserMergeBranch enables the Go implementation of UserMergeBranch
@@ -46,6 +49,7 @@ var All = []FeatureFlag{
ReferenceTransactionsSSHService,
ReferenceTransactionsPrimaryWins,
ReferenceTransactionHook,
+ RubyReferenceTransactionHook,
GoUserMergeBranch,
}
diff --git a/ruby/lib/gitlab/git/hook.rb b/ruby/lib/gitlab/git/hook.rb
index 8a514da2c..4a6ee936f 100644
--- a/ruby/lib/gitlab/git/hook.rb
+++ b/ruby/lib/gitlab/git/hook.rb
@@ -133,7 +133,8 @@ module Gitlab
'GIT_DIR' => repo_path,
'GITALY_REPO' => repository.gitaly_repository.to_json,
'GITALY_SOCKET' => Gitlab.config.gitaly.internal_socket,
- 'GITALY_GO_POSTRECEIVE' => repository.feature_enabled?('go-postreceive-hook').to_s
+ 'GITALY_GO_POSTRECEIVE' => repository.feature_enabled?('go-postreceive-hook').to_s,
+ 'GITALY_REFERENCE_TRANSACTION_HOOK' => repository.feature_enabled?('reference-transaction-hook').to_s
}
end
end
diff --git a/ruby/lib/gitlab/git/hooks_service.rb b/ruby/lib/gitlab/git/hooks_service.rb
index 5ad8e43b7..67ac4c3fb 100644
--- a/ruby/lib/gitlab/git/hooks_service.rb
+++ b/ruby/lib/gitlab/git/hooks_service.rb
@@ -13,7 +13,7 @@ module Gitlab
@push_options = push_options
@transaction = transaction
- %w[pre-receive update].each do |hook_name|
+ %w[pre-receive update reference-transaction].each do |hook_name|
status, message = run_hook(hook_name)
raise PreReceiveError, message unless status