Skip to content

Commit

Permalink
Merge pull request #41 from clly/workflows
Browse files Browse the repository at this point in the history
Create GitHub Workflows
  • Loading branch information
clly authored Jul 11, 2023
2 parents 374fdea + c6907b3 commit 345d5f3
Show file tree
Hide file tree
Showing 13 changed files with 236 additions and 33 deletions.
11 changes: 11 additions & 0 deletions .github/in-to-json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env python3

from sys import stdin
import json

lines = []
for line in stdin:
lines.append(line.strip())


print(json.dumps(lines))
21 changes: 21 additions & 0 deletions .github/mods.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash

set -euf pipefail

WORK="go.work"
if [[ -e $WORK ]]; then
S=1
while IFS= read -r line; do
if [[ $line == ')' ]]; then
S=1
fi
# echo "$S $line"
if [ $S -eq 0 ]; then
echo -e "${line}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'
fi

if [[ $line == 'use (' ]]; then
S=0
fi
done < $WORK
fi
89 changes: 89 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
name: "Go Checks"
on:
pull_request:
push:
branches:
# It's important that the action also runs on merge to main
- main

jobs:
mods:
runs-on: ubuntu-latest
outputs:
go-mods: ${{ steps.get-go-mods.outputs.go-mods }}
steps:
- uses: actions/checkout@v3
- id: get-go-mods
run: |
f=$(./.github/mods.sh | ./.github/in-to-json.py)
echo "go-mods=${f}" >> $GITHUB_OUTPUT
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Go environment
uses: actions/setup-go@v4.0.1
- name: test
run: |
make test
lint:
runs-on: ubuntu-latest
needs: [mods]
strategy:
matrix:
mod: ${{ fromJson(needs.mods.outputs.go-mods)}}
steps:
- uses: actions/checkout@v3
- name: Setup Go environment
uses: actions/setup-go@v4.0.1
- name: lint
uses: golangci/golangci-lint-action@v3
with:
# Require: The version of golangci-lint to use.
# When `install-mode` is `binary` (default) the value can be v1.2 or v1.2.3 or `latest` to use the latest version.
# When `install-mode` is `goinstall` the value can be v1.2.3, `latest`, or the hash of a commit.
version: v1.53
skip-pkg-cache: true
working-directory: ${{ matrix.mod }}
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Go environment
uses: actions/setup-go@v4.0.1
- name: build
run: |
make build
coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
# default fetch-depth is insufficent to find previous coverage notes
fetch-depth: 10

- uses: gwatts/go-coverage-action@v1
id: coverage
with:
# Optional coverage threshold
# use fail-coverage to determine what should happen below this threshold
coverage-threshold: 20

# collect coverage for all packages beyond the one under test
cover-pkg: ./...

# Ignore code-generated files when calculating coverage totals
ignore-pattern: |
\.pb\.go$
\_string\.go$
# A url that the html report will be accessible at, once your
# workflow uploads it. Used in the pull request comment.
#report-url: https://artifacts.example.com/go-coverage/${{ github.ref_name}}.html

#- name: Upload coverage to s3
# ensure this runs regardless of whether the threshold is met using always()
#if: always() && steps.coverage.outputs.report-pathname != ''
#run: |
# aws s3 cp ${{ steps.coverage.outputs.report-pathname }} s3://artifacts.example.com-bucket/go-coverage/${{ github.ref_name}}.html
37 changes: 37 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: goreleaser

on:
push:
# run only against tags
tags:
- '*'

permissions:
contents: write
packages: write
# issues: write

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- run: git fetch --force --tags
- uses: actions/setup-go@v4
with:
go-version: stable
# More assembly might be required: Docker logins, GPG, etc. It all depends
# on your needs.
- uses: goreleaser/goreleaser-action@v4
with:
# either 'goreleaser' (default) or 'goreleaser-pro':
distribution: goreleaser
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Your GoReleaser Pro key, if you are using the 'goreleaser-pro'
# distribution:
# GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ examples/example-otel/server

examples/example-oc/client
examples/example-oc/server
protoc-gen-go-telemetry

dist/
3 changes: 1 addition & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ linters:
# - tenv
# - thelper # detects golang test helpers without t.Helper() call and checks the consistency of test helpers
# - unconvert # removes unnecessary type conversions
# - unparam # reports unused function parameters
# - unused
- unused
# - usestdlibvars # detects the possibility to use variables/constants from the Go standard library
# - wastedassign # finds wasted assignment statements
# - whitespace # detects leading and trailing whitespace
Expand Down
46 changes: 46 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com
before:
hooks:
# You may remove this if you don't use go modules.
- go mod tidy
# you may remove this if you don't need go generate
- go generate ./...
builds:
- main: ./cmd/protoc-gen-go-telemetry
env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin

archives:
- format: tar.gz
# this name template makes the OS and Arch compatible with the results of uname.
name_template: >-
{{ .ProjectName }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
# use zip for windows archives
format_overrides:
- goos: windows
format: zip
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ incpatch .Version }}-next"
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'

# The lines beneath this are called `modelines`. See `:help modeline`
# Feel free to remove those if you don't want/use them.
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ SHELL = bash
BENCHFLAGS = -gcflags '-l' -benchmem -bench=. -benchtime 5s

GO_MODULE_DIRS ?= $(shell go list -m -f "{{ .Dir }}" | grep -v mod-vendor)
GOLANGCI_CONFIG_DIR ?= $(CURDIR)
mkfile_path := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
GOLANGCI_CONFIG_DIR ?= $(mkfile_path)
TIMEOUT ?= 10s
GOFILES = $(shell find -type f -name '*.go' ! -name '*.pb.go')

Expand All @@ -12,8 +13,11 @@ GOFILES = $(shell find -type f -name '*.go' ! -name '*.pb.go')
dev:
./run-dev.sh

.PHONY: tests
tests:
build:
go build ./cmd/protoc-gen-go-telemetry

.PHONY: test
test:
go test ./...

.PHONY: lint
Expand All @@ -30,7 +34,7 @@ fmt: $(GOFILES)
.PHONY: $(GO_MODULE_DIRS)
$(GO_MODULE_DIRS):
@echo -e "Running $(TARGET) for $(@)\n"
make -f $(CURDIR)/Makefile -C $@ $(TARGET)
make -k -f $(CURDIR)/Makefile -C $@ $(TARGET)

.PHONY: go/test/mod go/test
go/test/mod: TARGET=go/test
Expand Down
5 changes: 1 addition & 4 deletions cmd/pkg/generators/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package generators
import (
"crypto/rand"
"encoding/hex"
"fmt"
"testing"

"github.com/shoenig/test/must"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/compiler/protogen"
)
Expand Down Expand Up @@ -64,7 +62,6 @@ func Test_Message(t *testing.T) {

func testMessageGenerator(t *testing.T, numMsg int, children int) []*protogen.Message {
msgs := make([]*protogen.Message, 0, numMsg*children)
fmt.Println(children)
for i := 0; i < numMsg; i++ {
msg := genMsg(t)
if children > 0 {
Expand All @@ -79,7 +76,7 @@ func testMessageGenerator(t *testing.T, numMsg int, children int) []*protogen.Me
func genMsg(t *testing.T) *protogen.Message {
b := make([]byte, 128)
_, err := rand.Read(b)
must.NoError(t, err)
require.NoError(t, err)
name := hex.EncodeToString(b)

return &protogen.Message{
Expand Down
3 changes: 2 additions & 1 deletion examples/example-oc/cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/clly/proto-telemetry/examples/example-oc/tracing"
"go.opentelemetry.io/otel"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

func main() {
Expand All @@ -19,7 +20,7 @@ func main() {

func run() error {
connectTo := "127.0.0.1:8080"
conn, err := grpc.Dial(connectTo, grpc.WithBlock(), grpc.WithInsecure())
conn, err := grpc.Dial(connectTo, grpc.WithBlock(), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return fmt.Errorf("failed to connect to ocecho service at %s: %w", connectTo, err)
}
Expand Down
3 changes: 2 additions & 1 deletion examples/example-otel/cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/otel"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

otechov1 "github.com/clly/proto-telemetry/examples/example-otel/gen/proto/go/otecho/v1"
"github.com/clly/proto-telemetry/examples/example-otel/tracing"
Expand All @@ -22,7 +23,7 @@ func main() {

func run() error {
connectTo := "127.0.0.1:8080"
conn, err := grpc.Dial(connectTo, grpc.WithBlock(), grpc.WithInsecure(),
conn, err := grpc.Dial(connectTo, grpc.WithBlock(), grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithChainUnaryInterceptor(otelgrpc.UnaryClientInterceptor(), messagemarker.UnaryClientInterceptor()))
if err != nil {
return fmt.Errorf("failed to connect to ocecho service at %s: %w", connectTo, err)
Expand Down
6 changes: 3 additions & 3 deletions interceptor/grpc/messagemarker/unary_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func Test_UnaryServerInterceptor(t *testing.T) {
pingsvr := &ping.PingServer{}
pingv1.RegisterPingServiceServer(s, pingsvr)
go func() {
s.Serve(l)
must.NoError(t, s.Serve(l))
}()

conn, err := grpc.Dial(fmt.Sprintf("localhost:%d", port), grpc.WithTransportCredentials(insecure.NewCredentials()))
Expand Down Expand Up @@ -177,7 +177,7 @@ func TestUnaryClientInterceptor(t *testing.T) {
pingsvr := &ping.PingServer{}
pingv1.RegisterPingServiceServer(s, pingsvr)
go func() {
s.Serve(l)
must.NoError(t, s.Serve(l))
}()

conn, err := grpc.Dial(fmt.Sprintf("localhost:%d", port),
Expand Down Expand Up @@ -227,7 +227,7 @@ func TestUnaryClientInterceptor(t *testing.T) {
}

closer()
l.Close()
must.NoError(t, l.Close())
})
}
}
Expand Down
Loading

0 comments on commit 345d5f3

Please sign in to comment.