diff --git a/.golangci.yml b/.golangci.yml index d1926205b46..1525db660ce 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -470,3 +470,6 @@ formatters: - gofmt - gofumpt - goimports + settings: + gofumpt: + extra-rules: true diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5d49b7be1ad..bc1892e9842 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -235,7 +235,7 @@ Its subcommands are: The `script` directory has shell scripts that help with common development tasks. -**script/fmt.sh** formats all go code in the repository. +**script/fmt.sh** formats all Go code in the repository. **script/generate.sh** runs code generators and `go mod tidy` on all modules. With `--check` it checks that the generated files are current. diff --git a/github/enterprise_codesecurity_configurations_test.go b/github/enterprise_codesecurity_configurations_test.go index 34c6fe052cf..02d2d1fc612 100644 --- a/github/enterprise_codesecurity_configurations_test.go +++ b/github/enterprise_codesecurity_configurations_test.go @@ -253,7 +253,7 @@ func TestEnterpriseService_UpdateCodeSecurityConfiguration(t *testing.T) { const methodName = "UpdateCodeSecurityConfiguration" testBadOptions(t, methodName, func() (err error) { _, _, err = client.Enterprise.UpdateCodeSecurityConfiguration(ctx, "\n", -1, input) - return + return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { @@ -288,7 +288,7 @@ func TestEnterpriseService_DeleteCodeSecurityConfiguration(t *testing.T) { const methodName = "DeleteCodeSecurityConfiguration" testBadOptions(t, methodName, func() (err error) { _, err = client.Enterprise.DeleteCodeSecurityConfiguration(ctx, "\n", -1) - return + return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { @@ -328,7 +328,7 @@ func TestEnterpriseService_AttachCodeSecurityConfigurationToRepositories(t *test const methodName = "AttachCodeSecurityConfigurationToRepositories" testBadOptions(t, methodName, func() (err error) { _, err = client.Enterprise.AttachCodeSecurityConfigurationToRepositories(ctx, "\n", -1, "") - return + return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { @@ -377,7 +377,7 @@ func TestEnterpriseService_SetDefaultCodeSecurityConfiguration(t *testing.T) { const methodName = "SetDefaultCodeSecurityConfiguration" testBadOptions(t, methodName, func() (err error) { _, _, err = client.Enterprise.SetDefaultCodeSecurityConfiguration(ctx, "\n", -1, "") - return + return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { @@ -431,7 +431,7 @@ func TestEnterpriseService_ListCodeSecurityConfigurationRepositories(t *testing. const methodName = "ListCodeSecurityConfigurationRepositories" testBadOptions(t, methodName, func() (err error) { _, _, err = client.Enterprise.ListCodeSecurityConfigurationRepositories(ctx, "\n", -1, opts) - return + return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { diff --git a/github/orgs_codesecurity_configurations_test.go b/github/orgs_codesecurity_configurations_test.go index 23a3972108f..0ee45657694 100644 --- a/github/orgs_codesecurity_configurations_test.go +++ b/github/orgs_codesecurity_configurations_test.go @@ -391,7 +391,7 @@ func TestOrganizationsService_UpdateCodeSecurityConfiguration(t *testing.T) { const methodName = "UpdateCodeSecurityConfiguration" testBadOptions(t, methodName, func() (err error) { _, _, err = client.Organizations.UpdateCodeSecurityConfiguration(ctx, "\n", -1, input) - return + return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { @@ -426,7 +426,7 @@ func TestOrganizationsService_DeleteCodeSecurityConfiguration(t *testing.T) { const methodName = "DeleteCodeSecurityConfiguration" testBadOptions(t, methodName, func() (err error) { _, err = client.Organizations.DeleteCodeSecurityConfiguration(ctx, "\n", -1) - return + return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { @@ -470,7 +470,7 @@ func TestOrganizationsService_AttachCodeSecurityConfigurationToRepositories(t *t const methodName = "AttachCodeSecurityConfigurationToRepositories" testBadOptions(t, methodName, func() (err error) { _, err = client.Organizations.AttachCodeSecurityConfigurationToRepositories(ctx, "\n", -1, "", nil) - return + return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { @@ -519,7 +519,7 @@ func TestOrganizationsService_SetDefaultCodeSecurityConfiguration(t *testing.T) const methodName = "SetDefaultCodeSecurityConfiguration" testBadOptions(t, methodName, func() (err error) { _, _, err = client.Organizations.SetDefaultCodeSecurityConfiguration(ctx, "\n", -1, "") - return + return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { @@ -573,7 +573,7 @@ func TestOrganizationsService_ListCodeSecurityConfigurationRepositories(t *testi const methodName = "ListCodeSecurityConfigurationRepositories" testBadOptions(t, methodName, func() (err error) { _, _, err = client.Organizations.ListCodeSecurityConfigurationRepositories(ctx, "\n", -1, opts) - return + return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { @@ -619,7 +619,7 @@ func TestOrganizationsService_GetCodeSecurityConfigurationForRepository(t *testi const methodName = "GetCodeSecurityConfigurationForRepository" testBadOptions(t, methodName, func() (err error) { _, _, err = client.Organizations.GetCodeSecurityConfigurationForRepository(ctx, "\n", "\n") - return + return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { diff --git a/github/timestamp.go b/github/timestamp.go index 71660193bb3..a26077da255 100644 --- a/github/timestamp.go +++ b/github/timestamp.go @@ -43,7 +43,7 @@ func (t *Timestamp) UnmarshalJSON(data []byte) (err error) { } else { t.Time, err = time.Parse(`"`+time.RFC3339+`"`, str) } - return + return err } // Equal reports whether t and u are equal based on time.Equal. diff --git a/script/fmt.sh b/script/fmt.sh index 20ff6e69254..13400c08569 100755 --- a/script/fmt.sh +++ b/script/fmt.sh @@ -1,8 +1,11 @@ #!/bin/sh -#/ script/fmt.sh runs go fmt on all go files in the project. +#/ script/fmt.sh runs formatting on all Go files in the project. +#/ It uses custom golangci-lint to format the code. set -e +CUSTOM_GCL="$(script/setup-custom-gcl.sh)" + CDPATH="" cd -- "$(dirname -- "$0")/.." MOD_DIRS="$(git ls-files '*go.mod' | xargs dirname | sort)" @@ -10,6 +13,6 @@ MOD_DIRS="$(git ls-files '*go.mod' | xargs dirname | sort)" for dir in $MOD_DIRS; do ( cd "$dir" - go fmt ./... + "$CUSTOM_GCL" fmt ) done diff --git a/script/lint.sh b/script/lint.sh index ba9fd89a648..ae3878b9f40 100755 --- a/script/lint.sh +++ b/script/lint.sh @@ -5,13 +5,11 @@ set -e -GOLANGCI_LINT_VERSION="2.7.0" +CUSTOM_GCL="$(script/setup-custom-gcl.sh)" CDPATH="" cd -- "$(dirname -- "$0")/.." BIN="$(pwd -P)"/bin -mkdir -p "$BIN" - EXIT_CODE=0 fail() { @@ -19,12 +17,6 @@ fail() { EXIT_CODE=1 } -# install golangci-lint and custom-gcl in ./bin if they don't exist with the correct version -if ! "$BIN"/custom-gcl --version 2> /dev/null | grep -q "$GOLANGCI_LINT_VERSION"; then - GOBIN="$BIN" go install "github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v$GOLANGCI_LINT_VERSION" - "$BIN"/golangci-lint custom --name custom-gcl --destination "$BIN" -fi - MOD_DIRS="$(git ls-files '*go.mod' | xargs dirname | sort)" for dir in $MOD_DIRS; do @@ -32,7 +24,7 @@ for dir in $MOD_DIRS; do echo linting "$dir" ( cd "$dir" - "$BIN"/custom-gcl run + "$CUSTOM_GCL" run ) || fail "failed linting $dir" done diff --git a/script/setup-custom-gcl.sh b/script/setup-custom-gcl.sh new file mode 100755 index 00000000000..e4f1cc81725 --- /dev/null +++ b/script/setup-custom-gcl.sh @@ -0,0 +1,20 @@ +#!/bin/sh +#/ script/setup-custom-gcl.sh ensures custom golangci-lint is installed. +#/ It returns the path to the custom-gcl binary. + +set -e + +GOLANGCI_LINT_VERSION="2.7.0" + +# should in sync with fmt.sh and lint.sh +BIN="$(pwd -P)"/bin + +mkdir -p "$BIN" + +# install golangci-lint and custom-gcl in ./bin if they don't exist with the correct version +if ! "$BIN"/custom-gcl --version 2> /dev/null | grep -q "$GOLANGCI_LINT_VERSION"; then + GOBIN="$BIN" go install "github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v$GOLANGCI_LINT_VERSION" + "$BIN"/golangci-lint custom --name custom-gcl --destination "$BIN" +fi + +echo "$BIN/custom-gcl"