From ca24321b4f006e8a5a68ae82e08f678f06d763c4 Mon Sep 17 00:00:00 2001 From: 1riatsila1 Date: Wed, 14 Aug 2024 09:41:37 +0200 Subject: [PATCH 1/7] feat: linting --- .github/workflows/lint-test-code.yaml | 45 ++++++ .../.gitignore => .gitignore | 2 + Justfile | 26 +++- .../.openapi-generator-ignore | 3 +- pkg/config-api-client/api_health.go | 62 ++++---- pkg/config-api-client/client.go | 145 +++++++++--------- pkg/config-api-client/configuration.go | 17 +- pkg/config-api-client/model_error_response.go | 14 +- .../model_liveness_response.go | 10 +- .../model_readiness_response.go | 14 +- .../model_status_response.go | 12 +- pkg/config-api-client/utils.go | 2 +- 12 files changed, 208 insertions(+), 144 deletions(-) create mode 100644 .github/workflows/lint-test-code.yaml rename pkg/config-api-client/.gitignore => .gitignore (96%) diff --git a/.github/workflows/lint-test-code.yaml b/.github/workflows/lint-test-code.yaml new file mode 100644 index 00000000..ecf87961 --- /dev/null +++ b/.github/workflows/lint-test-code.yaml @@ -0,0 +1,45 @@ +name: Lint & Test Code + +on: + push: + branches: + - "**" + - "!main" + - "!master" + +env: + go_version: 1.22 + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + lint-test-code: + name: Lint & Test Code + runs-on: ubuntu-latest + steps: + - name: Checkout Code + uses: actions/checkout@v4 + + - name: Setup Go + uses: actions/setup-go@v2 + with: + go-version: "${{ env.go_version }}" + + - name: Setup Just + uses: extractions/setup-just@v2 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Run Code Linting Test + run: just lint >> "$GITHUB_STEP_SUMMARY" + + - name: Run Tests + run: | + just test + + - name: Upload Code Coverage Report + uses: codecov/codecov-action@v4 + with: + token: ${{ secrets.CODECOV_TOKEN }} diff --git a/pkg/config-api-client/.gitignore b/.gitignore similarity index 96% rename from pkg/config-api-client/.gitignore rename to .gitignore index daf913b1..e2240335 100644 --- a/pkg/config-api-client/.gitignore +++ b/.gitignore @@ -22,3 +22,5 @@ _testmain.go *.exe *.test *.prof + +.coverage diff --git a/Justfile b/Justfile index 15b37752..b020ebbf 100644 --- a/Justfile +++ b/Justfile @@ -18,6 +18,30 @@ generate-config-api-client: retrieve-config-api-openapi-spec --git-user-id aruba-uxi \ --git-repo-id configuration-api-terraform-provider/{{ CONFIG_API_DIR }} \ cd {{ CONFIG_API_DIR }} && go mod tidy + just fmt-client test-client: - cd {{ CONFIG_API_DIR }} && go test -v ./... + cd {{ CONFIG_API_DIR }} && go test -v ./... -race -covermode=atomic -coverprofile=.coverage + +fmt-client: + gofmt -w pkg/config-api-client + +lint-client: + #!/usr/bin/env bash + + if [ -n "$(gofmt -d pkg/config-api-client)" ]; then + echo "Error: (gofmt) formatting required" >&2 + exit 1 + fi + +test: + just test-client + +lint: + just lint-client + +fmt: + just fmt-client + +clean: + find . -name ".coverage" -type f -delete diff --git a/pkg/config-api-client/.openapi-generator-ignore b/pkg/config-api-client/.openapi-generator-ignore index 080aeef2..e57e6ea6 100644 --- a/pkg/config-api-client/.openapi-generator-ignore +++ b/pkg/config-api-client/.openapi-generator-ignore @@ -22,5 +22,6 @@ # Then explicitly reverse the ignore rule for a single file: #!docs/README.md -git_push.sh +.gitignore .travis.yml +git_push.sh diff --git a/pkg/config-api-client/api_health.go b/pkg/config-api-client/api_health.go index 57b07621..c2f30a3f 100644 --- a/pkg/config-api-client/api_health.go +++ b/pkg/config-api-client/api_health.go @@ -18,12 +18,11 @@ import ( "net/url" ) - // HealthAPIService HealthAPI service type HealthAPIService service type ApiGetLivezHealthLivezGetRequest struct { - ctx context.Context + ctx context.Context ApiService *HealthAPIService } @@ -36,24 +35,25 @@ GetLivezHealthLivezGet Live health check Check the health of the service. - @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background(). - @return ApiGetLivezHealthLivezGetRequest + @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background(). + @return ApiGetLivezHealthLivezGetRequest */ func (a *HealthAPIService) GetLivezHealthLivezGet(ctx context.Context) ApiGetLivezHealthLivezGetRequest { return ApiGetLivezHealthLivezGetRequest{ ApiService: a, - ctx: ctx, + ctx: ctx, } } // Execute executes the request -// @return LivenessResponse +// +// @return LivenessResponse func (a *HealthAPIService) GetLivezHealthLivezGetExecute(r ApiGetLivezHealthLivezGetRequest) (*LivenessResponse, *http.Response, error) { var ( - localVarHTTPMethod = http.MethodGet - localVarPostBody interface{} - formFiles []formFile - localVarReturnValue *LivenessResponse + localVarHTTPMethod = http.MethodGet + localVarPostBody interface{} + formFiles []formFile + localVarReturnValue *LivenessResponse ) localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "HealthAPIService.GetLivezHealthLivezGet") @@ -122,7 +122,7 @@ func (a *HealthAPIService) GetLivezHealthLivezGetExecute(r ApiGetLivezHealthLive } type ApiGetReadyzHealthReadyzGetRequest struct { - ctx context.Context + ctx context.Context ApiService *HealthAPIService } @@ -135,24 +135,25 @@ GetReadyzHealthReadyzGet Ready health check Check if the service is ready to process requests. - @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background(). - @return ApiGetReadyzHealthReadyzGetRequest + @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background(). + @return ApiGetReadyzHealthReadyzGetRequest */ func (a *HealthAPIService) GetReadyzHealthReadyzGet(ctx context.Context) ApiGetReadyzHealthReadyzGetRequest { return ApiGetReadyzHealthReadyzGetRequest{ ApiService: a, - ctx: ctx, + ctx: ctx, } } // Execute executes the request -// @return ReadinessResponse +// +// @return ReadinessResponse func (a *HealthAPIService) GetReadyzHealthReadyzGetExecute(r ApiGetReadyzHealthReadyzGetRequest) (*ReadinessResponse, *http.Response, error) { var ( - localVarHTTPMethod = http.MethodGet - localVarPostBody interface{} - formFiles []formFile - localVarReturnValue *ReadinessResponse + localVarHTTPMethod = http.MethodGet + localVarPostBody interface{} + formFiles []formFile + localVarReturnValue *ReadinessResponse ) localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "HealthAPIService.GetReadyzHealthReadyzGet") @@ -212,8 +213,8 @@ func (a *HealthAPIService) GetReadyzHealthReadyzGetExecute(r ApiGetReadyzHealthR newErr.error = err.Error() return localVarReturnValue, localVarHTTPResponse, newErr } - newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) - newErr.model = v + newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v) + newErr.model = v } return localVarReturnValue, localVarHTTPResponse, newErr } @@ -231,7 +232,7 @@ func (a *HealthAPIService) GetReadyzHealthReadyzGetExecute(r ApiGetReadyzHealthR } type ApiGetStatusHealthStatusGetRequest struct { - ctx context.Context + ctx context.Context ApiService *HealthAPIService } @@ -244,24 +245,25 @@ GetStatusHealthStatusGet Service stats endpoint Check basic service details. - @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background(). - @return ApiGetStatusHealthStatusGetRequest + @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background(). + @return ApiGetStatusHealthStatusGetRequest */ func (a *HealthAPIService) GetStatusHealthStatusGet(ctx context.Context) ApiGetStatusHealthStatusGetRequest { return ApiGetStatusHealthStatusGetRequest{ ApiService: a, - ctx: ctx, + ctx: ctx, } } // Execute executes the request -// @return StatusResponse +// +// @return StatusResponse func (a *HealthAPIService) GetStatusHealthStatusGetExecute(r ApiGetStatusHealthStatusGetRequest) (*StatusResponse, *http.Response, error) { var ( - localVarHTTPMethod = http.MethodGet - localVarPostBody interface{} - formFiles []formFile - localVarReturnValue *StatusResponse + localVarHTTPMethod = http.MethodGet + localVarPostBody interface{} + formFiles []formFile + localVarReturnValue *StatusResponse ) localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "HealthAPIService.GetStatusHealthStatusGet") diff --git a/pkg/config-api-client/client.go b/pkg/config-api-client/client.go index 58cf7944..d9d8962d 100644 --- a/pkg/config-api-client/client.go +++ b/pkg/config-api-client/client.go @@ -31,14 +31,13 @@ import ( "strings" "time" "unicode/utf8" - ) var ( JsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:[^;]+\+)?json)`) XmlCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:[^;]+\+)?xml)`) queryParamSplit = regexp.MustCompile(`(^|&)([^&]+)`) - queryDescape = strings.NewReplacer( "%5B", "[", "%5D", "]" ) + queryDescape = strings.NewReplacer("%5B", "[", "%5D", "]") ) // APIClient manages communication with the FastAPI API v1.0.0 @@ -125,15 +124,15 @@ func typeCheckParameter(obj interface{}, expected string, name string) error { return nil } -func parameterValueToString( obj interface{}, key string ) string { +func parameterValueToString(obj interface{}, key string) string { if reflect.TypeOf(obj).Kind() != reflect.Ptr { return fmt.Sprintf("%v", obj) } - var param,ok = obj.(MappedNullable) + var param, ok = obj.(MappedNullable) if !ok { return "" } - dataMap,err := param.ToMap() + dataMap, err := param.ToMap() if err != nil { return "" } @@ -149,81 +148,81 @@ func parameterAddToHeaderOrQuery(headerOrQueryParams interface{}, keyPrefix stri value = "null" } else { switch v.Kind() { - case reflect.Invalid: - value = "invalid" + case reflect.Invalid: + value = "invalid" - case reflect.Struct: - if t,ok := obj.(MappedNullable); ok { - dataMap,err := t.ToMap() - if err != nil { - return - } - parameterAddToHeaderOrQuery(headerOrQueryParams, keyPrefix, dataMap, collectionType) - return - } - if t, ok := obj.(time.Time); ok { - parameterAddToHeaderOrQuery(headerOrQueryParams, keyPrefix, t.Format(time.RFC3339Nano), collectionType) - return - } - value = v.Type().String() + " value" - case reflect.Slice: - var indValue = reflect.ValueOf(obj) - if indValue == reflect.ValueOf(nil) { + case reflect.Struct: + if t, ok := obj.(MappedNullable); ok { + dataMap, err := t.ToMap() + if err != nil { return } - var lenIndValue = indValue.Len() - for i:=0;i Date: Wed, 14 Aug 2024 10:18:05 +0200 Subject: [PATCH 2/7] PR template --- .github/pull_request_template.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .github/pull_request_template.md diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 00000000..dbb3e94e --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,7 @@ +## Description + +Any further details ... + +## Related Issue(s) + +- From 881b24bedbb14a968969f6a69a73c9f15442a55b Mon Sep 17 00:00:00 2001 From: 1riatsila1 Date: Wed, 14 Aug 2024 11:08:09 +0200 Subject: [PATCH 3/7] add golangci-lint --- .github/workflows/lint-test-code.yaml | 3 +++ Justfile | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/.github/workflows/lint-test-code.yaml b/.github/workflows/lint-test-code.yaml index ecf87961..2ed13d11 100644 --- a/.github/workflows/lint-test-code.yaml +++ b/.github/workflows/lint-test-code.yaml @@ -32,6 +32,9 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Install Dependencies + run: just setup-dev + - name: Run Code Linting Test run: just lint >> "$GITHUB_STEP_SUMMARY" diff --git a/Justfile b/Justfile index b020ebbf..781927fc 100644 --- a/Justfile +++ b/Justfile @@ -20,6 +20,9 @@ generate-config-api-client: retrieve-config-api-openapi-spec cd {{ CONFIG_API_DIR }} && go mod tidy just fmt-client +setup-dev: + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.60.1 + test-client: cd {{ CONFIG_API_DIR }} && go test -v ./... -race -covermode=atomic -coverprofile=.coverage @@ -39,6 +42,7 @@ test: lint: just lint-client + golangci-lint run fmt: just fmt-client From 296c0b9a24155864c071e569a6d00f27ac197199 Mon Sep 17 00:00:00 2001 From: 1riatsila1 Date: Wed, 14 Aug 2024 11:18:48 +0200 Subject: [PATCH 4/7] hmm dirs are hard --- Justfile | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Justfile b/Justfile index 781927fc..9bdae89c 100644 --- a/Justfile +++ b/Justfile @@ -32,17 +32,20 @@ fmt-client: lint-client: #!/usr/bin/env bash - if [ -n "$(gofmt -d pkg/config-api-client)" ]; then + cd pkg/config-api-client + + if [ -n "$(gofmt -d .)" ]; then echo "Error: (gofmt) formatting required" >&2 exit 1 fi + golangci-lint run + test: just test-client lint: just lint-client - golangci-lint run fmt: just fmt-client From 4640ab599fcd39908dfecd3db815872e72439089 Mon Sep 17 00:00:00 2001 From: 1riatsila1 Date: Wed, 14 Aug 2024 11:24:36 +0200 Subject: [PATCH 5/7] test if linting fails on CI --- pkg/config-api-client/model_liveness_response.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/config-api-client/model_liveness_response.go b/pkg/config-api-client/model_liveness_response.go index 05212499..4065889a 100644 --- a/pkg/config-api-client/model_liveness_response.go +++ b/pkg/config-api-client/model_liveness_response.go @@ -21,7 +21,7 @@ var _ MappedNullable = &LivenessResponse{} // LivenessResponse struct for LivenessResponse type LivenessResponse struct { - Status string `json:"status"` + status string `json:"status"` } type _LivenessResponse LivenessResponse From ff293b74a9f5c270639ae575fb8a3c6b299126ed Mon Sep 17 00:00:00 2001 From: 1riatsila1 Date: Wed, 14 Aug 2024 11:25:44 +0200 Subject: [PATCH 6/7] revert --- pkg/config-api-client/model_liveness_response.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/config-api-client/model_liveness_response.go b/pkg/config-api-client/model_liveness_response.go index 4065889a..05212499 100644 --- a/pkg/config-api-client/model_liveness_response.go +++ b/pkg/config-api-client/model_liveness_response.go @@ -21,7 +21,7 @@ var _ MappedNullable = &LivenessResponse{} // LivenessResponse struct for LivenessResponse type LivenessResponse struct { - status string `json:"status"` + Status string `json:"status"` } type _LivenessResponse LivenessResponse From 836c12143ab0b8c04251955cd6ca133332ef5b14 Mon Sep 17 00:00:00 2001 From: 1riatsila1 Date: Wed, 14 Aug 2024 11:26:52 +0200 Subject: [PATCH 7/7] updated template --- .github/pull_request_template.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index dbb3e94e..93f893e4 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -4,4 +4,4 @@ Any further details ... ## Related Issue(s) -- +-