1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
package hook
import (
"context"
"fmt"
"io"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
"gitlab.com/gitlab-org/gitaly/v15/internal/git"
"gitlab.com/gitlab-org/gitaly/v15/internal/helper"
"gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb"
)
//nolint:stylecheck // This is unintentionally missing documentation.
func (m *GitLabHookManager) UpdateHook(ctx context.Context, repo *gitalypb.Repository, ref, oldValue, newValue string, env []string, stdout, stderr io.Writer) error {
payload, err := git.HooksPayloadFromEnv(env)
if err != nil {
return helper.ErrInternalf("extracting hooks payload: %w", err)
}
if isPrimary(payload) {
if err := m.updateHook(ctx, payload, repo, ref, oldValue, newValue, env, stdout, stderr); err != nil {
ctxlogrus.Extract(ctx).WithError(err).Warn("stopping transaction because update hook failed")
// If the update hook declines the push, then we need
// to stop any secondaries voting on the transaction.
if err := m.stopTransaction(ctx, payload); err != nil {
ctxlogrus.Extract(ctx).WithError(err).Error("failed stopping transaction in update hook")
}
return err
}
}
return nil
}
func (m *GitLabHookManager) updateHook(ctx context.Context, payload git.HooksPayload, repo *gitalypb.Repository, ref, oldValue, newValue string, env []string, stdout, stderr io.Writer) error {
if ref == "" {
return helper.ErrInternalf("hook got no reference")
}
if err := git.ObjectHashSHA1.ValidateHex(oldValue); err != nil {
return helper.ErrInternalf("hook got invalid old value: %w", err)
}
if err := git.ObjectHashSHA1.ValidateHex(newValue); err != nil {
return helper.ErrInternalf("hook got invalid new value: %w", err)
}
if payload.UserDetails == nil {
return helper.ErrInternalf("payload has no receive hooks info")
}
executor, err := m.newCustomHooksExecutor(repo, "update")
if err != nil {
return helper.ErrInternal(err)
}
customEnv, err := m.customHooksEnv(ctx, payload, nil, env)
if err != nil {
return helper.ErrInternalf("constructing custom hook environment: %v", err)
}
if err = executor(
ctx,
[]string{ref, oldValue, newValue},
customEnv,
nil,
stdout,
stderr,
); err != nil {
return fmt.Errorf("executing custom hooks: %w", err)
}
return nil
}
|