Skip to content

Commit

Permalink
use buf
Browse files Browse the repository at this point in the history
  • Loading branch information
ryantxu committed May 8, 2024
2 parents 25cb6da + a502fa5 commit 4a05e56
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 58 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/validate-protos.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Validate protobuf files for incompatible changes.

name: Validate protobuf files

on:
pull_request:
paths:
- 'proto/backend.proto'
branches:
- 'main'

jobs:
main:
runs-on: ubuntu-latest
steps:
# Run `git checkout`
- uses: actions/checkout@v2
# Install the `buf` CLI
- uses: bufbuild/buf-setup-action@v1
# Perform breaking change detection against the `proto` directory in the `main` branch
- uses: bufbuild/buf-breaking-action@v1
with:
input: 'proto'
against: 'https://github.com/grafana/grafana-plugin-sdk-go.git#branch=main,subdir=proto'
21 changes: 15 additions & 6 deletions Magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package main

import (
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)

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

// Protobuf generates protobuf files.
func Protobuf() error {
if err := sh.RunV("./scripts/protobuf-check.sh"); err != nil {
return err
}
// Protobuf protobuf related commands.
type Protobuf mg.Namespace

// Generate generates protobuf files.
func (Protobuf) Generate() error {
return sh.RunV("buf", "generate", "proto", "--template", "./proto/buf.gen.yaml")
}

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

// Test runs the test suite.
Expand Down Expand Up @@ -59,4 +64,8 @@ func Drone() error {
return nil
}

var Aliases = map[string]interface{}{
"protobuf": Protobuf.Generate,
}

var Default = Build
2 changes: 1 addition & 1 deletion backend/httpclient/tracing_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func TestTracingMiddleware(t *testing.T) {
require.False(t, span.EndTime().IsZero())
require.Equal(t, codes.Error, span.Status().Code)
require.Equal(t, "error with HTTP status code 400", span.Status().Description)
require.Equal(t, []attribute.KeyValue{
require.ElementsMatch(t, []attribute.KeyValue{
attribute.String("l1", "v1"),
attribute.String("l2", "v2"),
semconv.HTTPURL("http://test.com/query"),
Expand Down
11 changes: 8 additions & 3 deletions contribute/developer-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ mage lint

### Generate Go code for Protobuf definitions

A prerequisite is to have [protoc](http://google.github.io/proto-lens/installing-protoc.html) installed and available in your path.

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.
A prerequisite is to have [Buf CLI](https://buf.build/docs/installation) installed and available in your path.

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

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

```shell
mage protobuf
# or
mage protobuf:generate
```

To verify no breaking changes Protobuf definitions compared with latest commit in main:
```shell
mage protobuf:validate
```

### Changing `generic_*.go` files in the `data` package
Expand Down
2 changes: 1 addition & 1 deletion genproto/pluginv2/backend.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion genproto/pluginv2/backend_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions proto/buf.gen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: v1
plugins:
- plugin: go
out: genproto/pluginv2
protoc_path: protoc
- plugin: go-grpc
out: genproto/pluginv2
opt:
- require_unimplemented_servers=false
7 changes: 7 additions & 0 deletions proto/buf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: v1
breaking:
use:
- FILE
lint:
use:
- DEFAULT
19 changes: 0 additions & 19 deletions proto/generate.sh

This file was deleted.

27 changes: 0 additions & 27 deletions scripts/protobuf-check.sh

This file was deleted.

0 comments on commit 4a05e56

Please sign in to comment.