Add goimports as a check to the Github commit actions (#31432)

* Add goimports as a check to the Github commit actions

* check diff against main instead of last commit

* goimports should fix the problems

* fix up conditionals and wildcard matching

* specify origin/main in diff

* fetch main branch when checkout

* back to origin main
This commit is contained in:
Liam Cervante 2022-07-19 10:14:28 +01:00 committed by GitHub
parent 7b4a5513a9
commit afd273d636
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 2 deletions

View File

@ -141,6 +141,8 @@ jobs:
steps: steps:
- name: "Fetch source code" - name: "Fetch source code"
uses: actions/checkout@v2 uses: actions/checkout@v2
with:
fetch-depth: 0 # We need to do comparisons against the main branch.
- name: Determine Go version - name: Determine Go version
id: go id: go
@ -178,9 +180,13 @@ jobs:
restore-keys: | restore-keys: |
protobuf-tools- protobuf-tools-
- name: Install CI tooling
run: |
go install golang.org/x/tools/cmd/goimports@v0.1.11
- name: "Code consistency checks" - name: "Code consistency checks"
run: | run: |
make fmtcheck generate staticcheck exhaustive protobuf make fmtcheck importscheck generate staticcheck exhaustive protobuf
if [[ -n "$(git status --porcelain)" ]]; then if [[ -n "$(git status --porcelain)" ]]; then
echo >&2 "ERROR: Generated files are inconsistent. Run 'make generate' and 'make protobuf' locally and then commit the updated files." echo >&2 "ERROR: Generated files are inconsistent. Run 'make generate' and 'make protobuf' locally and then commit the updated files."
git >&2 status --porcelain git >&2 status --porcelain

View File

@ -35,6 +35,9 @@ protobuf:
fmtcheck: fmtcheck:
@sh -c "'$(CURDIR)/scripts/gofmtcheck.sh'" @sh -c "'$(CURDIR)/scripts/gofmtcheck.sh'"
importscheck:
@sh -c "'$(CURDIR)/scripts/goimportscheck.sh'"
staticcheck: staticcheck:
@sh -c "'$(CURDIR)/scripts/staticcheck.sh'" @sh -c "'$(CURDIR)/scripts/staticcheck.sh'"
@ -63,4 +66,4 @@ website/build-local:
# under parallel conditions. # under parallel conditions.
.NOTPARALLEL: .NOTPARALLEL:
.PHONY: fmtcheck generate protobuf website website-test staticcheck website/local website/build-local .PHONY: fmtcheck importscheck generate protobuf website website-test staticcheck website/local website/build-local

19
scripts/goimportscheck.sh Executable file
View File

@ -0,0 +1,19 @@
#!/usr/bin/env bash
# Check goimports
echo "==> Checking the code complies with goimports requirements..."
target_files=$(git diff --name-only origin/main | grep "\.go")
if [[ -z ${target_files} ]]; then
exit 0
fi
goimports_files=$(goimports -w -l -local github.com/hashicorp/terraform/ ${target_files})
if [[ -n ${goimports_files} ]]; then
echo 'goimports needs running on the following files:'
echo "${goimports_files}"
echo "You can use the command and flags \`goimports -w -l -local github.com/hashicorp/terraform/\` to reformat the code"
exit 1
fi
exit 0