diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-09-17 11:34:12 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-10-14 10:28:46 +0300 |
commit | fb2bbfc2dd9f0cbfcb843b3b5fc31d7c9c075806 (patch) | |
tree | 2c0cea762f19739a7055ea49c1b0c3b7778f2a09 | |
parent | 30a162442e3fbf3d451460de6e080b925b0ae3ae (diff) |
hook: Wire through state from service to manager
As the hook.ReferenceTransaction RPC now has info available about a
given transaction's state, we can wire it through to the hook manager.
-rw-r--r-- | internal/gitaly/hook/manager.go | 18 | ||||
-rw-r--r-- | internal/gitaly/hook/referencetransaction.go | 2 | ||||
-rw-r--r-- | internal/gitaly/service/hook/reference_transaction.go | 14 |
3 files changed, 32 insertions, 2 deletions
diff --git a/internal/gitaly/hook/manager.go b/internal/gitaly/hook/manager.go index 09cb82df1..092816468 100644 --- a/internal/gitaly/hook/manager.go +++ b/internal/gitaly/hook/manager.go @@ -10,6 +10,22 @@ import ( "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" ) +// ReferenceTransactionState is the state of the Git reference transaction. It reflects the first +// parameter of the reference-transaction hook. See githooks(1) for more information. +type ReferenceTransactionState int + +const ( + // ReferenceTransactionPrepared indicates all reference updates have been queued to the + // transaction and were locked on disk. + ReferenceTransactionPrepared = ReferenceTransactionState(iota) + // ReferenceTransactionCommitted indicates the reference transaction was committed and all + // references now have their respective new value. + ReferenceTransactionCommitted + // ReferenceTransactionAborted indicates the transaction was aborted, no changes were + // performed and the reference locks have been released. + ReferenceTransactionAborted +) + // Manager is an interface providing the ability to execute Git hooks. type Manager interface { // PreReceiveHook executes the pre-receive Git hook and any installed custom hooks. stdin @@ -26,7 +42,7 @@ type Manager interface { // ReferenceTransactionHook executes the reference-transaction Git hook. stdin must contain // all references to be updated and match the format specified in githooks(5). - ReferenceTransactionHook(ctx context.Context, env []string, stdin io.Reader) error + ReferenceTransactionHook(ctx context.Context, state ReferenceTransactionState, env []string, stdin io.Reader) error } // GitLabHookManager is a hook manager containing Git hook business logic. It diff --git a/internal/gitaly/hook/referencetransaction.go b/internal/gitaly/hook/referencetransaction.go index d16cce80a..7128a2e11 100644 --- a/internal/gitaly/hook/referencetransaction.go +++ b/internal/gitaly/hook/referencetransaction.go @@ -9,7 +9,7 @@ import ( "gitlab.com/gitlab-org/gitaly/internal/helper" ) -func (m *GitLabHookManager) ReferenceTransactionHook(ctx context.Context, env []string, stdin io.Reader) error { +func (m *GitLabHookManager) ReferenceTransactionHook(ctx context.Context, state ReferenceTransactionState, env []string, stdin io.Reader) error { changes, err := ioutil.ReadAll(stdin) if err != nil { return helper.ErrInternalf("reading stdin from request: %w", err) diff --git a/internal/gitaly/service/hook/reference_transaction.go b/internal/gitaly/service/hook/reference_transaction.go index 2118ceb02..92122ba23 100644 --- a/internal/gitaly/service/hook/reference_transaction.go +++ b/internal/gitaly/service/hook/reference_transaction.go @@ -3,6 +3,7 @@ package hook import ( "errors" + "gitlab.com/gitlab-org/gitaly/internal/gitaly/hook" "gitlab.com/gitlab-org/gitaly/internal/helper" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "gitlab.com/gitlab-org/gitaly/streamio" @@ -26,6 +27,18 @@ func (s *server) ReferenceTransactionHook(stream gitalypb.HookService_ReferenceT return helper.ErrInvalidArgument(err) } + var state hook.ReferenceTransactionState + switch request.State { + case gitalypb.ReferenceTransactionHookRequest_PREPARED: + state = hook.ReferenceTransactionPrepared + case gitalypb.ReferenceTransactionHookRequest_COMMITTED: + state = hook.ReferenceTransactionCommitted + case gitalypb.ReferenceTransactionHookRequest_ABORTED: + state = hook.ReferenceTransactionAborted + default: + return helper.ErrInvalidArgument(errors.New("invalid hook state")) + } + stdin := streamio.NewReader(func() ([]byte, error) { req, err := stream.Recv() return req.GetStdin(), err @@ -33,6 +46,7 @@ func (s *server) ReferenceTransactionHook(stream gitalypb.HookService_ReferenceT if err := s.manager.ReferenceTransactionHook( stream.Context(), + state, request.GetEnvironmentVariables(), stdin, ); err != nil { |