Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: repository tidiness checks on CI #107

Merged
merged 2 commits into from
Jan 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ jobs:
path: .cache/bazel-repo
key: bazel-repo
- name: Validate changed files
if: github.event_name == 'pull_request'
if: github.event_name == 'pull_request' && !contains(github.event.pull_request.labels.*.name, 'allow sdk change')
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