Skip to content

Commit a502fa5

Browse files
authored
Chore: Migrate to use buf for protobuf generation (#979)
1 parent 21f2303 commit a502fa5

File tree

10 files changed

+66
-58
lines changed

10 files changed

+66
-58
lines changed

.github/workflows/validate-protos.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Validate protobuf files for incompatible changes.
2+
3+
name: Validate protobuf files
4+
5+
on:
6+
pull_request:
7+
paths:
8+
- 'proto/backend.proto'
9+
branches:
10+
- 'main'
11+
12+
jobs:
13+
main:
14+
runs-on: ubuntu-latest
15+
steps:
16+
# Run `git checkout`
17+
- uses: actions/checkout@v2
18+
# Install the `buf` CLI
19+
- uses: bufbuild/buf-setup-action@v1
20+
# Perform breaking change detection against the `proto` directory in the `main` branch
21+
- uses: bufbuild/buf-breaking-action@v1
22+
with:
23+
input: 'proto'
24+
against: 'https://github.com/grafana/grafana-plugin-sdk-go.git#branch=main,subdir=proto'

Magefile.go

+15-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package main
55

66
import (
7+
"github.com/magefile/mage/mg"
78
"github.com/magefile/mage/sh"
89
)
910

@@ -17,13 +18,17 @@ func DataGenerate() error {
1718
return sh.Run("go", "generate", "./data")
1819
}
1920

20-
// Protobuf generates protobuf files.
21-
func Protobuf() error {
22-
if err := sh.RunV("./scripts/protobuf-check.sh"); err != nil {
23-
return err
24-
}
21+
// Protobuf protobuf related commands.
22+
type Protobuf mg.Namespace
23+
24+
// Generate generates protobuf files.
25+
func (Protobuf) Generate() error {
26+
return sh.RunV("buf", "generate", "proto", "--template", "./proto/buf.gen.yaml")
27+
}
2528

26-
return sh.RunV("./proto/generate.sh")
29+
// Validate validate breaking changes in protobuf files.
30+
func (Protobuf) Validate() error {
31+
return sh.RunV("buf", "breaking", "proto", "--against", "https://github.com/grafana/grafana-plugin-sdk-go.git#branch=main,subdir=proto")
2732
}
2833

2934
// Test runs the test suite.
@@ -59,4 +64,8 @@ func Drone() error {
5964
return nil
6065
}
6166

67+
var Aliases = map[string]interface{}{
68+
"protobuf": Protobuf.Generate,
69+
}
70+
6271
var Default = Build

backend/httpclient/tracing_middleware_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func TestTracingMiddleware(t *testing.T) {
125125
require.False(t, span.EndTime().IsZero())
126126
require.Equal(t, codes.Error, span.Status().Code)
127127
require.Equal(t, "error with HTTP status code 400", span.Status().Description)
128-
require.Equal(t, []attribute.KeyValue{
128+
require.ElementsMatch(t, []attribute.KeyValue{
129129
attribute.String("l1", "v1"),
130130
attribute.String("l2", "v2"),
131131
semconv.HTTPURL("http://test.com/query"),

contribute/developer-guide.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ mage lint
4242

4343
### Generate Go code for Protobuf definitions
4444

45-
A prerequisite is to have [protoc](http://google.github.io/proto-lens/installing-protoc.html) installed and available in your path.
46-
47-
Next, you need to have [protoc-gen-go](https://github.com/protocolbuffers/protobuf-go/tree/master/cmd/protoc-gen-go) and [protoc-gen-grpc-go](https://pkg.go.dev/google.golang.org/grpc/cmd/protoc-gen-go-grpc) installed and available in your path. It's very important that you match the `protoc-gen-go` version specified of `google.golang.org/protobuf` in [go.mod](../go.mod), which at the time of writing is v1.33.0.
45+
A prerequisite is to have [Buf CLI](https://buf.build/docs/installation) installed and available in your path.
4846

4947
To install protoc-gen-go (version should automatically be taken from the go.mod):
5048

@@ -61,6 +59,13 @@ To compile the protobuf:
6159

6260
```shell
6361
mage protobuf
62+
# or
63+
mage protobuf:generate
64+
```
65+
66+
To verify no breaking changes Protobuf definitions compared with latest commit in main:
67+
```shell
68+
mage protobuf:validate
6469
```
6570

6671
### Changing `generic_*.go` files in the `data` package

genproto/pluginv2/backend.pb.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

genproto/pluginv2/backend_grpc.pb.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto/buf.gen.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version: v1
2+
plugins:
3+
- plugin: go
4+
out: genproto/pluginv2
5+
protoc_path: protoc
6+
- plugin: go-grpc
7+
out: genproto/pluginv2
8+
opt:
9+
- require_unimplemented_servers=false

proto/buf.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
version: v1
2+
breaking:
3+
use:
4+
- FILE
5+
lint:
6+
use:
7+
- DEFAULT

proto/generate.sh

-19
This file was deleted.

scripts/protobuf-check.sh

-27
This file was deleted.

0 commit comments

Comments
 (0)