Skip to content

Commit

Permalink
feat: repository tidiness checks on CI
Browse files Browse the repository at this point in the history
Signed-off-by: Thulio Ferraz Assis <3149049+f0rmiga@users.noreply.github.com>
  • Loading branch information
f0rmiga committed Jan 8, 2022
1 parent da3a1e1 commit 1182018
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ jobs:
- name: Validate changed files
if: github.event_name == 'pull_request'
run: ./.github/workflows/validate_changed_files.sh ${{ github.head_ref }} ${{ github.base_ref }}
- name: Tidy up repository
run: ./.github/workflows/tidy_up_repository.sh
- name: bazel test //...
env:
XDG_CACHE_HOME: .cache/bazel-repo
Expand Down
26 changes: 26 additions & 0 deletions .github/workflows/tidy_up_repository.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash

set -o errexit -o nounset -o pipefail

# First verify if the repository is clean.
if ! git diff --exit-code; then
>&2 echo "ERROR: The repository is not clean - please verify the changes and fix them."
exit 1
fi

# Then, run a series of commands that could produce changes to the source tree.
# For each command, we check if the repository is still clean and proceed.
commands=(
"bazel run @go_sdk//:bin/go -- fmt \$(go list ./... | grep -v /bazel-/)"
"bazel run @go_sdk//:bin/go -- mod tidy"
"bazel run //:update_go_deps"
"bazel run //:gazelle"
)

for cmd in "${commands[@]}"; do
/bin/bash -c "${cmd}"
if ! git diff --exit-code; then
>&2 echo "ERROR: Please run '${cmd}' and commit the changes."
exit 1
fi
done
8 changes: 8 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
load("@bazel_gazelle//:def.bzl", "gazelle")
load("@io_bazel_rules_go//go:def.bzl", "TOOLS_NOGO", "nogo")

nogo(
name = "nogo",
config = "nogo_config.json",
visibility = ["//visibility:public"],
deps = TOOLS_NOGO,
)

# TODO: follow https://sagikazarmark.hu/blog/vanity-import-paths-in-go/
# so we have savvy go imports for users
Expand Down
5 changes: 4 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ go_rules_dependencies()

go_embed_data_dependencies()

go_register_toolchains(version = "1.17.5")
go_register_toolchains(
version = "1.17.5",
nogo = "@//:nogo",
)

gazelle_dependencies()

Expand Down
38 changes: 38 additions & 0 deletions nogo_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"composites": {
"exclude_files": {
"(third_party|vendor|external)/.*": "only run for first-party code"
}
},
"copylocks": {
"exclude_files": {
"(third_party|vendor|external)/.*": "only run for first-party code"
}
},
"nilness": {
"exclude_files": {
"(third_party|vendor|external)/.*": "only run for first-party code"
}
},
"shadow": {
"exclude_files": {
"(third_party|vendor|external)/.*": "only run for first-party code",
"pkg/bazel/bazelisk.go": "copied third-party code"
}
},
"stdmethods": {
"exclude_files": {
"(third_party|vendor|external)/.*": "only run for first-party code"
}
},
"unreachable": {
"exclude_files": {
"(third_party|vendor|external)/.*": "only run for first-party code"
}
},
"unsafeptr": {
"exclude_files": {
"(third_party|vendor|external)/.*": "only run for first-party code"
}
}
}
4 changes: 2 additions & 2 deletions pkg/interceptors/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ func TestWorkspaceRootInterceptor(t *testing.T) {
cmd := &cobra.Command{Use: "fake"}
args := []string{"foo", "bar"}
next := func(_ctx context.Context, _cmd *cobra.Command, _args []string) error {
ctx := context.WithValue(ctx, WorkspaceRootKey, expectedWorkspaceRoot)
g.Expect(_ctx).To(Equal(ctx))
ctxWithWorkspace := context.WithValue(ctx, WorkspaceRootKey, expectedWorkspaceRoot)
g.Expect(_ctx).To(Equal(ctxWithWorkspace))
g.Expect(_cmd).To(Equal(cmd))
g.Expect(_args).To(Equal(args))
return nil
Expand Down
2 changes: 2 additions & 0 deletions pkg/plugin/sdk/v1alpha1/proto/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
load("@rules_proto//proto:defs.bzl", "proto_library")

# gazelle:exclude dummy.go

proto_library(
name = "proto_proto",
srcs = ["plugin.proto"],
Expand Down
3 changes: 0 additions & 3 deletions pkg/plugin/sdk/v1alpha1/proto/dummy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//go:build dummy
// +build dummy

// This file exists to make the go tooling happy. This package is generated by
// bazel.
package proto
2 changes: 2 additions & 0 deletions pkg/plugin/sdk/v1alpha2/proto/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
load("@rules_proto//proto:defs.bzl", "proto_library")

# gazelle:exclude dummy.go

proto_library(
name = "proto_proto",
srcs = ["plugin.proto"],
Expand Down
3 changes: 0 additions & 3 deletions pkg/plugin/sdk/v1alpha2/proto/dummy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//go:build dummy
// +build dummy

// This file exists to make the go tooling happy. This package is generated by
// bazel.
package proto

0 comments on commit 1182018

Please sign in to comment.