From 8f033c825a2702291a9cd6e5927a1aed7ed338b3 Mon Sep 17 00:00:00 2001 From: andreaangiolillo Date: Thu, 7 Mar 2024 18:10:54 +0000 Subject: [PATCH 01/15] CLOUDP-236398: Adding golangci and code-health action --- .github/actionlint-matcher.json | 17 +++ .github/workflows/code-health-tools.yml | 68 ++++++++++++ tools/cli/.golangci.yml | 135 ++++++++++++++++++++++++ tools/cli/Makefile | 15 ++- tools/cli/internal/cli/merge/merge.go | 12 +-- 5 files changed, 235 insertions(+), 12 deletions(-) create mode 100644 .github/actionlint-matcher.json create mode 100644 .github/workflows/code-health-tools.yml create mode 100644 tools/cli/.golangci.yml diff --git a/.github/actionlint-matcher.json b/.github/actionlint-matcher.json new file mode 100644 index 0000000..4613e16 --- /dev/null +++ b/.github/actionlint-matcher.json @@ -0,0 +1,17 @@ +{ + "problemMatcher": [ + { + "owner": "actionlint", + "pattern": [ + { + "regexp": "^(?:\\x1b\\[\\d+m)?(.+?)(?:\\x1b\\[\\d+m)*:(?:\\x1b\\[\\d+m)*(\\d+)(?:\\x1b\\[\\d+m)*:(?:\\x1b\\[\\d+m)*(\\d+)(?:\\x1b\\[\\d+m)*: (?:\\x1b\\[\\d+m)*(.+?)(?:\\x1b\\[\\d+m)* \\[(.+?)\\]$", + "file": 1, + "line": 2, + "column": 3, + "message": 4, + "code": 5 + } + ] + } + ] +} diff --git a/.github/workflows/code-health-tools.yml b/.github/workflows/code-health-tools.yml new file mode 100644 index 0000000..8b12050 --- /dev/null +++ b/.github/workflows/code-health-tools.yml @@ -0,0 +1,68 @@ +name: 'Code Health Tools' + +# Set of validations run on each pull request and merged commits to master. +on: + push: + branches: + - main + paths: + - 'tools/**' + paths-ignore: # https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#example-excluding-paths + - '*.md' + pull_request: {} + workflow_dispatch: {} + +permissions: + contents: read + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout CLI + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 + with: + path: tools/cli + - name: Install Go + uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 + with: + go-version-file: 'tools/cli/go.mod' + - name: Build CLI + run: | + pushd tools/cli + make build + lint: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 + with: + sparse-checkout: | + .github + tools + - name: Install Go + uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 + with: + go-version-file: 'tools/cli/go.mod' + cache: false # see https://github.com/golangci/golangci-lint-action/issues/807 + - name: golangci-lint + uses: golangci/golangci-lint-action@3cfe3a4abbb849e10058ce4af15d205b6da42804 + with: + version: v1.56.2 + working-directory: tools/cli + - name: Download actionlint + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 + with: + sparse-checkout: | + .github + - name: Download actionlint + id: get_actionlint + run: bash <(curl https://raw.githubusercontent.com/rhysd/actionlint/main/scripts/download-actionlint.bash) + shell: bash + - name: Check workflow files + run: | + echo "::add-matcher::.github/actionlint-matcher.json" + ${{ steps.get_actionlint.outputs.executable }} -color + shell: bash + + diff --git a/tools/cli/.golangci.yml b/tools/cli/.golangci.yml new file mode 100644 index 0000000..35775df --- /dev/null +++ b/tools/cli/.golangci.yml @@ -0,0 +1,135 @@ +linters-settings: + gocritic: + enabled-tags: + - diagnostic + - experimental + - opinionated + - performance + - style + govet: + check-shadowing: true + enable: + - fieldalignment + + revive: + # see https://github.com/mgechev/revive#available-rules for details. + ignore-generated-header: true + severity: warning + rules: + - name: blank-imports + - name: context-as-argument + - name: context-keys-type + - name: dot-imports + - name: error-return + - name: error-strings + - name: error-naming + - name: errorf + - name: exported + - name: indent-error-flow + - name: if-return + - name: increment-decrement + - name: var-naming + - name: var-declaration + - name: package-comments + - name: range + - name: receiver-naming + - name: time-naming + - name: unexported-return + - name: indent-error-flow + - name: errorf + - name: empty-block + - name: superfluous-else + - name: struct-tag + # Too many unusued parameters, skipping this check for now + #- name: unused-parameter + - name: unreachable-code + - name: redefines-builtin-id + maligned: + suggest-new: true + misspell: + locale: US + ignore-words: + - cancelled + lll: + # Default is 120. '\t' is counted as 1 character. + # set our project to 500, as we are adding open api field description in the schema. + # also, for anyone using vscode, use the following configs: + # "rewrap.wrappingColumn": 500 ... requires the rewrap plugin + # "editor.rulers": [500] + line-length: 500 + nestif: + # minimal complexity of if statements to report, 5 by default + min-complexity: 7 + gomnd: + settings: + mnd: + # don't include the "operation" and "assign" + checks: [case, return] + funlen: + lines: 360 + statements: 120 +linters: + disable-all: true + enable: + - dogsled + - errcheck + - funlen + - gocritic + - gofmt + - goimports + - revive + - gomnd + - goprintffuncname + - gosec + - gosimple + - govet + - ineffassign + - lll + - misspell + - nakedret + - nolintlint + - rowserrcheck + - exportloopref + - staticcheck + - stylecheck + - typecheck + - unconvert + - unused + - whitespace + - thelper + - testifylint + - exhaustive + - makezero + - noctx + - tenv + + # don't enable: + # - deadcode + # - varcheck + # - structcheck + # - depguard # Go linter that checks if package imports are in a list of acceptable packages [fast: true, auto-fix: false] + # - gocyclo # we already have funlen lint + # - dupl # we have a lot of duplicate test cases + # - gochecknoinits # we need the init function for the provider + # - gochecknoglobals # we need some global variables + # - unparam # Forces to create global variables when one variable is repeated in different functions + # - goerr113 # It does not allow you to return an error, you need to save the error in a variable to do it + # - goconst + # - gocognit +issues: + exclude: + - declaration of ".*" shadows declaration at line .* + exclude-rules: + - linters: + - staticcheck + text: "SA1019:" # d.GetOkExists is deprecated: usage is discouraged due to undefined behaviors and may be removed in a future version of the SDK + - linters: + - gocritic + text: "^hugeParam: req is heavy" + +run: + timeout: 10m + tests: true + build-tags: + - integration + modules-download-mode: readonly diff --git a/tools/cli/Makefile b/tools/cli/Makefile index c1e8608..8dc6f04 100644 --- a/tools/cli/Makefile +++ b/tools/cli/Makefile @@ -1,5 +1,6 @@ # A Self-Documenting Makefile: http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html +GOLANGCI_VERSION=v1.56.2 SOURCE_FILES?=./cmd BINARY_NAME=openapicli VERSION=v0.0.1 @@ -20,8 +21,13 @@ deps: ## Download go module dependencies go mod download go mod tidy +.PHONY: devtools +devtools: ## Install dev tools + @echo "==> Installing dev tools..." + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell go env GOPATH)/bin $(GOLANGCI_VERSION) + .PHONY: setup -setup: deps ## Set up dev env +setup: deps devtools ## Set up dev env .PHONY: fmt fmt: ### Format all go files with goimports and gofmt @@ -39,6 +45,13 @@ build-debug: @echo "==> Building openapicli binary for debugging" go build -gcflags="$(DEBUG_FLAGS)" -ldflags "$(LINKER_FLAGS)" -o $(DESTINATION) $(SOURCE_FILES) +.PHONY: lint +lint: ## Run linter + golangci-lint run + +.PHONY: fix-lint +fix-lint: ## Fix linting errors + golangci-lint run --fix .PHONY: list list: ## List all make targets diff --git a/tools/cli/internal/cli/merge/merge.go b/tools/cli/internal/cli/merge/merge.go index 249bcde..1ad79f2 100644 --- a/tools/cli/internal/cli/merge/merge.go +++ b/tools/cli/internal/cli/merge/merge.go @@ -20,8 +20,7 @@ import ( ) type Opts struct { - Base *load.SpecInfo - outputPath string + Base *load.SpecInfo } func (o *Opts) Run(args []string) error { @@ -29,15 +28,6 @@ func (o *Opts) Run(args []string) error { return nil } -func (o *Opts) removeExternalReferences(paths []string, federated *load.SpecInfo) ([]byte, error) { - // To add in follow up PR: CLOUDP-225849 - return nil, nil - -} -func (o *Opts) saveFile(data []byte) error { - return nil -} - func (o *Opts) PreRunE(args []string) error { // To Add in follow up PR: CLOUDP-225849 return nil From be23a04f1043090c58052f8d94e16835796ba66a Mon Sep 17 00:00:00 2001 From: andreaangiolillo Date: Thu, 7 Mar 2024 18:12:19 +0000 Subject: [PATCH 02/15] Update code-health-tools.yml --- .github/workflows/code-health-tools.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/code-health-tools.yml b/.github/workflows/code-health-tools.yml index 8b12050..138f0eb 100644 --- a/.github/workflows/code-health-tools.yml +++ b/.github/workflows/code-health-tools.yml @@ -1,6 +1,4 @@ name: 'Code Health Tools' - -# Set of validations run on each pull request and merged commits to master. on: push: branches: From c889a3f51b56478d8e2e0cc9466f706319a00e95 Mon Sep 17 00:00:00 2001 From: andreaangiolillo Date: Thu, 7 Mar 2024 18:12:42 +0000 Subject: [PATCH 03/15] Update code-health-tools.yml --- .github/workflows/code-health-tools.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/code-health-tools.yml b/.github/workflows/code-health-tools.yml index 138f0eb..9d9fe9c 100644 --- a/.github/workflows/code-health-tools.yml +++ b/.github/workflows/code-health-tools.yml @@ -62,5 +62,3 @@ jobs: echo "::add-matcher::.github/actionlint-matcher.json" ${{ steps.get_actionlint.outputs.executable }} -color shell: bash - - From e615bd42392259cdf8deecd4da7f1c2a80e7af2d Mon Sep 17 00:00:00 2001 From: andreaangiolillo Date: Thu, 7 Mar 2024 18:13:42 +0000 Subject: [PATCH 04/15] fixes --- .github/workflows/code-health-tools.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/code-health-tools.yml b/.github/workflows/code-health-tools.yml index 9d9fe9c..e786329 100644 --- a/.github/workflows/code-health-tools.yml +++ b/.github/workflows/code-health-tools.yml @@ -5,8 +5,6 @@ on: - main paths: - 'tools/**' - paths-ignore: # https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#example-excluding-paths - - '*.md' pull_request: {} workflow_dispatch: {} @@ -48,7 +46,7 @@ jobs: with: version: v1.56.2 working-directory: tools/cli - - name: Download actionlint + - name: Checkout GitHub actions uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 with: sparse-checkout: | From bf9cc06fa6ceea2ea8d738c509be77993041a933 Mon Sep 17 00:00:00 2001 From: andreaangiolillo Date: Thu, 7 Mar 2024 18:15:11 +0000 Subject: [PATCH 05/15] Update code-health-tools.yml --- .github/workflows/code-health-tools.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/code-health-tools.yml b/.github/workflows/code-health-tools.yml index e786329..1568250 100644 --- a/.github/workflows/code-health-tools.yml +++ b/.github/workflows/code-health-tools.yml @@ -22,11 +22,9 @@ jobs: - name: Install Go uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 with: - go-version-file: 'tools/cli/go.mod' + go-version-file: 'go.mod' - name: Build CLI - run: | - pushd tools/cli - make build + run: make build lint: runs-on: ubuntu-latest steps: From c271a3b97072975cba84b55614e19027a54da12a Mon Sep 17 00:00:00 2001 From: andreaangiolillo Date: Thu, 7 Mar 2024 18:20:49 +0000 Subject: [PATCH 06/15] Update code-health-tools.yml --- .github/workflows/code-health-tools.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/code-health-tools.yml b/.github/workflows/code-health-tools.yml index 1568250..5dc5309 100644 --- a/.github/workflows/code-health-tools.yml +++ b/.github/workflows/code-health-tools.yml @@ -22,7 +22,7 @@ jobs: - name: Install Go uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 with: - go-version-file: 'go.mod' + go-version-file: '/tools/cli/go.mod' - name: Build CLI run: make build lint: From 6bc0b8c6a0c31c88a3105b700b36c90a41d67df6 Mon Sep 17 00:00:00 2001 From: andreaangiolillo Date: Thu, 7 Mar 2024 18:21:24 +0000 Subject: [PATCH 07/15] Update code-health-tools.yml --- .github/workflows/code-health-tools.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/code-health-tools.yml b/.github/workflows/code-health-tools.yml index 5dc5309..67ce2c0 100644 --- a/.github/workflows/code-health-tools.yml +++ b/.github/workflows/code-health-tools.yml @@ -22,7 +22,7 @@ jobs: - name: Install Go uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 with: - go-version-file: '/tools/cli/go.mod' + go-version-file: 'tools/cli/go.mod' - name: Build CLI run: make build lint: From e7999168122bc3bd29d87b1e0863dcbb15ef386e Mon Sep 17 00:00:00 2001 From: andreaangiolillo Date: Thu, 7 Mar 2024 18:22:06 +0000 Subject: [PATCH 08/15] Update code-health-tools.yml --- .github/workflows/code-health-tools.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/code-health-tools.yml b/.github/workflows/code-health-tools.yml index 67ce2c0..3dd8a21 100644 --- a/.github/workflows/code-health-tools.yml +++ b/.github/workflows/code-health-tools.yml @@ -22,7 +22,7 @@ jobs: - name: Install Go uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 with: - go-version-file: 'tools/cli/go.mod' + go-version-file: 'cli/go.mod' - name: Build CLI run: make build lint: From 9fc79a29906a5786bc63cc533a2253c52b426824 Mon Sep 17 00:00:00 2001 From: andreaangiolillo Date: Thu, 7 Mar 2024 18:22:53 +0000 Subject: [PATCH 09/15] Update code-health-tools.yml --- .github/workflows/code-health-tools.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/code-health-tools.yml b/.github/workflows/code-health-tools.yml index 3dd8a21..4e6b9ec 100644 --- a/.github/workflows/code-health-tools.yml +++ b/.github/workflows/code-health-tools.yml @@ -19,10 +19,12 @@ jobs: uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 with: path: tools/cli + - name: TEST + run: ls -al - name: Install Go uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 with: - go-version-file: 'cli/go.mod' + go-version-file: 'go.mod' - name: Build CLI run: make build lint: From 58191ee801835213d3a6497204337a0a2be0df1c Mon Sep 17 00:00:00 2001 From: andreaangiolillo Date: Thu, 7 Mar 2024 18:23:59 +0000 Subject: [PATCH 10/15] Update code-health-tools.yml --- .github/workflows/code-health-tools.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/code-health-tools.yml b/.github/workflows/code-health-tools.yml index 4e6b9ec..1aaca5d 100644 --- a/.github/workflows/code-health-tools.yml +++ b/.github/workflows/code-health-tools.yml @@ -20,11 +20,14 @@ jobs: with: path: tools/cli - name: TEST - run: ls -al + run: | + ls -al + ls -al tools + ls -al tools/cli - name: Install Go uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 with: - go-version-file: 'go.mod' + go-version-file: 'tools/cli/go.mod' - name: Build CLI run: make build lint: From abcbfee42ef87d015f679fc18156de003c4b3b5b Mon Sep 17 00:00:00 2001 From: andreaangiolillo Date: Thu, 7 Mar 2024 18:24:51 +0000 Subject: [PATCH 11/15] Update code-health-tools.yml --- .github/workflows/code-health-tools.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/code-health-tools.yml b/.github/workflows/code-health-tools.yml index 1aaca5d..57a2bc8 100644 --- a/.github/workflows/code-health-tools.yml +++ b/.github/workflows/code-health-tools.yml @@ -17,8 +17,6 @@ jobs: steps: - name: Checkout CLI uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 - with: - path: tools/cli - name: TEST run: | ls -al From e750d4ce8518137366e83f0ab4916d1c0d78b77c Mon Sep 17 00:00:00 2001 From: andreaangiolillo Date: Thu, 7 Mar 2024 18:25:53 +0000 Subject: [PATCH 12/15] Update code-health-tools.yml --- .github/workflows/code-health-tools.yml | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/.github/workflows/code-health-tools.yml b/.github/workflows/code-health-tools.yml index 57a2bc8..14450fc 100644 --- a/.github/workflows/code-health-tools.yml +++ b/.github/workflows/code-health-tools.yml @@ -17,17 +17,14 @@ jobs: steps: - name: Checkout CLI uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 - - name: TEST - run: | - ls -al - ls -al tools - ls -al tools/cli - name: Install Go uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 with: go-version-file: 'tools/cli/go.mod' - name: Build CLI - run: make build + run: | + pushd tools/cli + make build lint: runs-on: ubuntu-latest steps: From b4e879acfcfe0029520049f2fd017ea3c50e1cf3 Mon Sep 17 00:00:00 2001 From: andreaangiolillo Date: Fri, 8 Mar 2024 09:07:35 +0000 Subject: [PATCH 13/15] Update .golangci.yml --- tools/cli/.golangci.yml | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/tools/cli/.golangci.yml b/tools/cli/.golangci.yml index 35775df..f146890 100644 --- a/tools/cli/.golangci.yml +++ b/tools/cli/.golangci.yml @@ -48,15 +48,8 @@ linters-settings: suggest-new: true misspell: locale: US - ignore-words: - - cancelled lll: - # Default is 120. '\t' is counted as 1 character. - # set our project to 500, as we are adding open api field description in the schema. - # also, for anyone using vscode, use the following configs: - # "rewrap.wrappingColumn": 500 ... requires the rewrap plugin - # "editor.rulers": [500] - line-length: 500 + line-length: 120 nestif: # minimal complexity of if statements to report, 5 by default min-complexity: 7 From 0802a5f5e75d1b990c3889a215854b4d539b243a Mon Sep 17 00:00:00 2001 From: andreaangiolillo Date: Fri, 8 Mar 2024 10:05:12 +0000 Subject: [PATCH 14/15] Addressed PR comments --- tools/cli/.golangci.yml | 18 ++++-------------- tools/cli/internal/cli/merge/merge.go | 8 ++++---- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/tools/cli/.golangci.yml b/tools/cli/.golangci.yml index f146890..2dce35d 100644 --- a/tools/cli/.golangci.yml +++ b/tools/cli/.golangci.yml @@ -40,12 +40,9 @@ linters-settings: - name: empty-block - name: superfluous-else - name: struct-tag - # Too many unusued parameters, skipping this check for now - #- name: unused-parameter + - name: unused-parameter - name: unreachable-code - name: redefines-builtin-id - maligned: - suggest-new: true misspell: locale: US lll: @@ -95,6 +92,9 @@ linters: - makezero - noctx - tenv + - prealloc + - predeclared + - whitespace # don't enable: # - deadcode @@ -109,16 +109,6 @@ linters: # - goerr113 # It does not allow you to return an error, you need to save the error in a variable to do it # - goconst # - gocognit -issues: - exclude: - - declaration of ".*" shadows declaration at line .* - exclude-rules: - - linters: - - staticcheck - text: "SA1019:" # d.GetOkExists is deprecated: usage is discouraged due to undefined behaviors and may be removed in a future version of the SDK - - linters: - - gocritic - text: "^hugeParam: req is heavy" run: timeout: 10m diff --git a/tools/cli/internal/cli/merge/merge.go b/tools/cli/internal/cli/merge/merge.go index 1ad79f2..1aee19b 100644 --- a/tools/cli/internal/cli/merge/merge.go +++ b/tools/cli/internal/cli/merge/merge.go @@ -23,12 +23,12 @@ type Opts struct { Base *load.SpecInfo } -func (o *Opts) Run(args []string) error { +func (o *Opts) Run(_ []string) error { // To add in follow up PR: CLOUDP-225849 return nil } -func (o *Opts) PreRunE(args []string) error { +func (o *Opts) PreRunE(_ []string) error { // To Add in follow up PR: CLOUDP-225849 return nil } @@ -40,10 +40,10 @@ func Builder() *cobra.Command { Use: "merge [base-spec] [spec-1] [spec-2] [spec-3] ... [spec-n]", Short: "Merge Open API specifications into a base spec.", Args: cobra.MinimumNArgs(2), - PreRunE: func(cmd *cobra.Command, args []string) error { + PreRunE: func(_ *cobra.Command, args []string) error { return opts.PreRunE(args) }, - RunE: func(cmd *cobra.Command, args []string) error { + RunE: func(_ *cobra.Command, args []string) error { return opts.Run(args) }, } From 7825694282aee5b2dc08d77e42fdf342a50db4de Mon Sep 17 00:00:00 2001 From: andreaangiolillo Date: Fri, 8 Mar 2024 10:05:46 +0000 Subject: [PATCH 15/15] Update .golangci.yml --- tools/cli/.golangci.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/tools/cli/.golangci.yml b/tools/cli/.golangci.yml index 2dce35d..59f2104 100644 --- a/tools/cli/.golangci.yml +++ b/tools/cli/.golangci.yml @@ -113,6 +113,4 @@ linters: run: timeout: 10m tests: true - build-tags: - - integration modules-download-mode: readonly