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

lint.go « protoc-gen-gitaly-lint « tools - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 28dcd54adb9273a47e6b68e5dcb083de43580667 (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main

import (
	"errors"
	"fmt"

	"gitlab.com/gitlab-org/gitaly/v15/internal/protoutil"
	"gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb"
	"google.golang.org/protobuf/reflect/protoregistry"
	"google.golang.org/protobuf/types/descriptorpb"
	"google.golang.org/protobuf/types/pluginpb"
)

// ensureMethodOpType will ensure that method includes the op_type option.
// See proto example below:
//
//	rpc ExampleMethod(ExampleMethodRequest) returns (ExampleMethodResponse) {
//	   option (op_type).op = ACCESSOR;
//	 }
func ensureMethodOpType(fileDesc *descriptorpb.FileDescriptorProto, m *descriptorpb.MethodDescriptorProto, req *pluginpb.CodeGeneratorRequest) error {
	opMsg, err := protoutil.GetOpExtension(m)
	if err != nil {
		if errors.Is(err, protoregistry.NotFound) {
			return fmt.Errorf("missing op_type extension")
		}

		return err
	}

	ml := methodLinter{
		req:        req,
		fileDesc:   fileDesc,
		methodDesc: m,
		opMsg:      opMsg,
	}

	switch opCode := opMsg.GetOp(); opCode {

	case gitalypb.OperationMsg_ACCESSOR:
		return ml.validateAccessor()

	case gitalypb.OperationMsg_MUTATOR:
		// if mutator, we need to make sure we specify scope or target repo
		return ml.validateMutator()

	case gitalypb.OperationMsg_MAINTENANCE:
		return ml.validateMaintenance()

	case gitalypb.OperationMsg_UNKNOWN:
		return errors.New("op set to UNKNOWN")

	default:
		return fmt.Errorf("invalid operation class with int32 value of %d", opCode)
	}
}

func validateMethod(file *descriptorpb.FileDescriptorProto, service *descriptorpb.ServiceDescriptorProto, method *descriptorpb.MethodDescriptorProto, req *pluginpb.CodeGeneratorRequest) error {
	if intercepted, err := protoutil.IsInterceptedMethod(service, method); err != nil {
		return fmt.Errorf("is intercepted method: %w", err)
	} else if intercepted {
		if _, err := protoutil.GetOpExtension(method); err != nil {
			if errors.Is(err, protoregistry.NotFound) {
				return nil
			}

			return err
		}

		return fmt.Errorf("operation type defined on an intercepted method")
	}

	return ensureMethodOpType(file, method, req)
}

// LintFile ensures the file described meets Gitaly required processes.
// Currently, this is limited to validating if request messages contain
// a mandatory operation code.
func LintFile(file *descriptorpb.FileDescriptorProto, req *pluginpb.CodeGeneratorRequest) []error {
	var errs []error

	for _, service := range file.GetService() {
		for _, method := range service.GetMethod() {
			if err := validateMethod(file, service, method, req); err != nil {
				errs = append(errs, formatError(file.GetName(), service.GetName(), method.GetName(), err))
			}
		}
	}

	return errs
}

func formatError(file, service, method string, err error) error {
	return fmt.Errorf("%s: service %q: method: %q: %w", file, service, method, err)
}