diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-11-26 12:37:22 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-12-01 17:12:07 +0300 |
commit | ff5805ed00db7003e9fcb06a8aee883616b090b6 (patch) | |
tree | a75d1386afc60c01b0693a1b31d435fff79e0423 /internal/gitaly/service/operations/merge.go | |
parent | a30207cce453b4d500ee7a393a9613bd127a152d (diff) |
operations: Move update with hook logic into own file
For some of our RPCs, we need to emulate how git would invoke hooks as
we effectively split up the operation across multiple calls to either
libgit2 or git itself. This is Ruby implements `update_ref_in_hooks`,
which has been ported to Go with commit f7bb67439 (operations: Port
UserMergeBranch from Ruby to Go, 2020-09-09).
The logic is quite involved and has since grown additional users next to
UserMergeBranch. As a consequence, its original location next to the
merge logic doesn't really make much sense anymore and is hard to find.
So let's move it into its own file instead.
Diffstat (limited to 'internal/gitaly/service/operations/merge.go')
-rw-r--r-- | internal/gitaly/service/operations/merge.go | 105 |
1 files changed, 0 insertions, 105 deletions
diff --git a/internal/gitaly/service/operations/merge.go b/internal/gitaly/service/operations/merge.go index 0439f4ec9..06993c833 100644 --- a/internal/gitaly/service/operations/merge.go +++ b/internal/gitaly/service/operations/merge.go @@ -1,7 +1,6 @@ package operations import ( - "bytes" "context" "errors" "fmt" @@ -9,34 +8,14 @@ import ( "gitlab.com/gitlab-org/gitaly/internal/command" "gitlab.com/gitlab-org/gitaly/internal/git" - "gitlab.com/gitlab-org/gitaly/internal/git/updateref" "gitlab.com/gitlab-org/gitaly/internal/git2go" - "gitlab.com/gitlab-org/gitaly/internal/gitaly/hook" "gitlab.com/gitlab-org/gitaly/internal/gitaly/rubyserver" - "gitlab.com/gitlab-org/gitaly/internal/gitlabshell" "gitlab.com/gitlab-org/gitaly/internal/helper" "gitlab.com/gitlab-org/gitaly/internal/helper/text" "gitlab.com/gitlab-org/gitaly/internal/metadata/featureflag" - "gitlab.com/gitlab-org/gitaly/internal/praefect/metadata" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" ) -type preReceiveError struct { - message string -} - -func (e preReceiveError) Error() string { - return e.message -} - -type updateRefError struct { - reference string -} - -func (e updateRefError) Error() string { - return fmt.Sprintf("Could not update %s. Please refresh and try again.", e.reference) -} - func (s *server) UserMergeBranch(bidi gitalypb.OperationService_UserMergeBranchServer) error { ctx := bidi.Context() @@ -116,90 +95,6 @@ func hookErrorFromStdoutAndStderr(sout string, serr string) string { return sout } -func (s *server) updateReferenceWithHooks(ctx context.Context, repo *gitalypb.Repository, user *gitalypb.User, reference, newrev, oldrev string) error { - gitlabshellEnv, err := gitlabshell.EnvFromConfig(s.cfg) - if err != nil { - return err - } - - env := append([]string{ - "GL_PROTOCOL=web", - fmt.Sprintf("GL_ID=%s", user.GetGlId()), - fmt.Sprintf("GL_USERNAME=%s", user.GetGlUsername()), - fmt.Sprintf("GL_REPOSITORY=%s", repo.GetGlRepository()), - fmt.Sprintf("GL_PROJECT_PATH=%s", repo.GetGlProjectPath()), - fmt.Sprintf("GITALY_SOCKET=" + s.cfg.GitalyInternalSocketPath()), - fmt.Sprintf("GITALY_REPO=%s", repo), - fmt.Sprintf("GITALY_TOKEN=%s", s.cfg.Auth.Token), - }, gitlabshellEnv...) - - transaction, err := metadata.TransactionFromContext(ctx) - if err != nil { - if err != metadata.ErrTransactionNotFound { - return err - } - } - - if err == nil { - praefect, err := metadata.PraefectFromContext(ctx) - if err != nil { - return err - } - - transactionEnv, err := transaction.Env() - if err != nil { - return err - } - - praefectEnv, err := praefect.Env() - if err != nil { - return err - } - - env = append(env, transactionEnv, praefectEnv) - } - - changes := fmt.Sprintf("%s %s %s\n", oldrev, newrev, reference) - var stdout, stderr bytes.Buffer - - if err := s.hookManager.PreReceiveHook(ctx, repo, env, strings.NewReader(changes), &stdout, &stderr); err != nil { - msg := hookErrorFromStdoutAndStderr(stdout.String(), stderr.String()) - return preReceiveError{message: msg} - } - if err := s.hookManager.UpdateHook(ctx, repo, reference, oldrev, newrev, env, &stdout, &stderr); err != nil { - msg := hookErrorFromStdoutAndStderr(stdout.String(), stderr.String()) - return preReceiveError{message: msg} - } - - // For backwards compatibility with Ruby, we need to only call the reference-transaction - // hook if the corresponding Ruby feature flag is set. - if featureflag.IsEnabled(ctx, featureflag.RubyReferenceTransactionHook) { - if err := s.hookManager.ReferenceTransactionHook(ctx, hook.ReferenceTransactionPrepared, env, strings.NewReader(changes)); err != nil { - msg := hookErrorFromStdoutAndStderr(stdout.String(), stderr.String()) - return preReceiveError{message: msg} - } - } - - updater, err := updateref.New(ctx, repo) - if err != nil { - return err - } - - if err := updater.Update(reference, newrev, oldrev); err != nil { - return err - } - - if err := updater.Wait(); err != nil { - return updateRefError{reference: reference} - } - - if err := s.hookManager.PostReceiveHook(ctx, repo, nil, env, strings.NewReader(changes), &stdout, &stderr); err != nil { - return err - } - - return nil -} - func (s *server) userMergeBranch(stream gitalypb.OperationService_UserMergeBranchServer) error { ctx := stream.Context() |