From 4d4a7a1a83c9d9e4c588187a9dd2d92ab7784fe5 Mon Sep 17 00:00:00 2001 From: Martin Zibricky Date: Wed, 1 Nov 2023 14:14:18 +0100 Subject: [PATCH 1/8] [CCXDEV-12103] Add Github Actions workflows --- .github/workflows/bdd.yaml | 30 ++++++++++++++++ .github/workflows/gh-pages.yml | 53 +++++++++++++++++++++++++++++ .github/workflows/golangci-lint.yml | 18 ++-------- .github/workflows/gotests.yaml | 47 +++++++++++++++++++++++++ .github/workflows/linters.yaml | 29 ++++++++++++++++ .github/workflows/shellcheck.yaml | 15 ++++++++ 6 files changed, 177 insertions(+), 15 deletions(-) create mode 100644 .github/workflows/bdd.yaml create mode 100644 .github/workflows/gh-pages.yml create mode 100644 .github/workflows/gotests.yaml create mode 100644 .github/workflows/linters.yaml create mode 100644 .github/workflows/shellcheck.yaml diff --git a/.github/workflows/bdd.yaml b/.github/workflows/bdd.yaml new file mode 100644 index 00000000..0cba005b --- /dev/null +++ b/.github/workflows/bdd.yaml @@ -0,0 +1,30 @@ +name: BDD tests + +on: + push: + branches: + - master + pull_request: + +jobs: + bdd: + runs-on: ubuntu-20.04 + strategy: + matrix: + go-version: + - "1.18" + name: BDD for Go ${{ matrix.go-version}} + steps: + - uses: actions/checkout@v4 + - name: Setup Go + uses: actions/setup-go@v4 + with: + go-version: ${{ matrix.go-version }} + - name: Build project + run: make build + - name: Retrieve Docker compose file + run: wget -O docker-compose.yml https://raw.githubusercontent.com/RedHatInsights/insights-behavioral-spec/main/docker-compose.yml + - name: Retrieve BDD runner + run: wget -O bdd_runner.sh https://raw.githubusercontent.com/RedHatInsights/insights-behavioral-spec/main/run_in_docker.sh && chmod +x bdd_runner.sh + - name: Run BDD + run: ./bdd_runner.sh aggregator-tests . diff --git a/.github/workflows/gh-pages.yml b/.github/workflows/gh-pages.yml new file mode 100644 index 00000000..2ee395ff --- /dev/null +++ b/.github/workflows/gh-pages.yml @@ -0,0 +1,53 @@ +# Copyright 2023 Red Hat, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +name: Build and deploy Jekyll site to GitHub Pages + +on: + push: + branches: ["master"] + +permissions: + contents: read + pages: write + id-token: write + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Setup Go + uses: actions/setup-go@v4 + with: + go-version: '1.18' + - name: Generate docgo and literate + run: make godoc + - name: Build with Jekyll + uses: actions/jekyll-build-pages@v1 + with: + source: "./docs/" + - name: Upload artifact + uses: actions/upload-pages-artifact@v1 + deploy: + runs-on: ubuntu-latest + needs: build + steps: + - name: Deploy to GitHub Pages + id: deployment + uses: actions/deploy-pages@v2 + environment: + name: github-pages + url: ${{ steps.deployment.outputs.page_url }} diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index ba54e37a..dc37fa79 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -1,17 +1,3 @@ -# Copyright 2022 Red Hat, Inc -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - name: golangci-lint on: push: @@ -42,7 +28,9 @@ jobs: # Optional: golangci-lint command line arguments. # args: --issues-exit-code=0 - args: --timeout=3m + args: > + --enable=goimports,gosimple,nilerr,prealloc,revive,staticcheck,unconvert,unused,whitespace,zerologlint + --timeout=3m # Optional: show only new issues if it's a pull request. The default value is `false`. # only-new-issues: true diff --git a/.github/workflows/gotests.yaml b/.github/workflows/gotests.yaml new file mode 100644 index 00000000..4ee85ab5 --- /dev/null +++ b/.github/workflows/gotests.yaml @@ -0,0 +1,47 @@ +name: Go tests + +on: + push: + branches: + - master + pull_request: + +jobs: + gotests: + runs-on: ubuntu-20.04 + strategy: + matrix: + go-version: + - "1.18" + - "1.19" + - "1.20" + - "1.21" + name: Tests for Go ${{ matrix.go-version}} + services: + postgres: + image: postgres + env: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + POSTGRES_DB: postgres + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + ports: + - 5432:5432 + steps: + - uses: actions/checkout@v4 + - name: Setup Go + uses: actions/setup-go@v4 + with: + go-version: ${{ matrix.go-version }} + - name: Print env vars + run: env + - name: Unit tests + run: make test + - name: Check code coverage + run: ./check_coverage.sh + - name: Display code coverage + run: make coverage diff --git a/.github/workflows/linters.yaml b/.github/workflows/linters.yaml new file mode 100644 index 00000000..03229c0d --- /dev/null +++ b/.github/workflows/linters.yaml @@ -0,0 +1,29 @@ +name: Go linters + +on: + push: + branches: + - master + pull_request: + +jobs: + golint: + runs-on: ubuntu-20.04 + strategy: + matrix: + go-version: + - "1.18" + - "1.19" + - "1.20" + - "1.21" + name: Linters for Go ${{ matrix.go-version}} + steps: + - uses: actions/checkout@v4 + - name: Setup Go + uses: actions/setup-go@v4 + with: + go-version: ${{ matrix.go-version }} + - name: Build project + run: go build + - name: Style linters + run: make style diff --git a/.github/workflows/shellcheck.yaml b/.github/workflows/shellcheck.yaml new file mode 100644 index 00000000..f57217c2 --- /dev/null +++ b/.github/workflows/shellcheck.yaml @@ -0,0 +1,15 @@ +name: Shell check + +on: + push: + branches: + - master + pull_request: + +jobs: + shellcheck: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v3 + - name: Shell check + run: ./shellcheck.sh From aa43336edb4039d5d829d5d0b475d10f13e87ad1 Mon Sep 17 00:00:00 2001 From: Martin Zibricky Date: Wed, 1 Nov 2023 16:30:29 +0100 Subject: [PATCH 2/8] [CCXDEV-12106] Update branch name in GH Actions --- .github/workflows/bdd.yaml | 30 ----------------------------- .github/workflows/gh-pages.yml | 2 +- .github/workflows/golangci-lint.yml | 3 +-- .github/workflows/linters.yaml | 2 +- .github/workflows/shellcheck.yaml | 2 +- 5 files changed, 4 insertions(+), 35 deletions(-) delete mode 100644 .github/workflows/bdd.yaml diff --git a/.github/workflows/bdd.yaml b/.github/workflows/bdd.yaml deleted file mode 100644 index 0cba005b..00000000 --- a/.github/workflows/bdd.yaml +++ /dev/null @@ -1,30 +0,0 @@ -name: BDD tests - -on: - push: - branches: - - master - pull_request: - -jobs: - bdd: - runs-on: ubuntu-20.04 - strategy: - matrix: - go-version: - - "1.18" - name: BDD for Go ${{ matrix.go-version}} - steps: - - uses: actions/checkout@v4 - - name: Setup Go - uses: actions/setup-go@v4 - with: - go-version: ${{ matrix.go-version }} - - name: Build project - run: make build - - name: Retrieve Docker compose file - run: wget -O docker-compose.yml https://raw.githubusercontent.com/RedHatInsights/insights-behavioral-spec/main/docker-compose.yml - - name: Retrieve BDD runner - run: wget -O bdd_runner.sh https://raw.githubusercontent.com/RedHatInsights/insights-behavioral-spec/main/run_in_docker.sh && chmod +x bdd_runner.sh - - name: Run BDD - run: ./bdd_runner.sh aggregator-tests . diff --git a/.github/workflows/gh-pages.yml b/.github/workflows/gh-pages.yml index 2ee395ff..e5e93012 100644 --- a/.github/workflows/gh-pages.yml +++ b/.github/workflows/gh-pages.yml @@ -16,7 +16,7 @@ name: Build and deploy Jekyll site to GitHub Pages on: push: - branches: ["master"] + branches: ["main"] permissions: contents: read diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index dc37fa79..016dd43c 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -4,7 +4,6 @@ on: tags: - v* branches: - - master - main pull_request: permissions: @@ -31,7 +30,7 @@ jobs: args: > --enable=goimports,gosimple,nilerr,prealloc,revive,staticcheck,unconvert,unused,whitespace,zerologlint --timeout=3m - + # Optional: show only new issues if it's a pull request. The default value is `false`. # only-new-issues: true diff --git a/.github/workflows/linters.yaml b/.github/workflows/linters.yaml index 03229c0d..ba3f15cf 100644 --- a/.github/workflows/linters.yaml +++ b/.github/workflows/linters.yaml @@ -3,7 +3,7 @@ name: Go linters on: push: branches: - - master + - main pull_request: jobs: diff --git a/.github/workflows/shellcheck.yaml b/.github/workflows/shellcheck.yaml index f57217c2..70e62a3a 100644 --- a/.github/workflows/shellcheck.yaml +++ b/.github/workflows/shellcheck.yaml @@ -3,7 +3,7 @@ name: Shell check on: push: branches: - - master + - main pull_request: jobs: From 2b05dcf3a4fbec259ef191d58d79715b725a4b5f Mon Sep 17 00:00:00 2001 From: Martin Zibricky Date: Wed, 1 Nov 2023 17:08:59 +0100 Subject: [PATCH 3/8] [CCXDEV-12106] Fix go tests --- .github/workflows/gh-pages.yml | 53 ---------------------------------- .github/workflows/gotests.yaml | 18 +++--------- 2 files changed, 4 insertions(+), 67 deletions(-) delete mode 100644 .github/workflows/gh-pages.yml diff --git a/.github/workflows/gh-pages.yml b/.github/workflows/gh-pages.yml deleted file mode 100644 index e5e93012..00000000 --- a/.github/workflows/gh-pages.yml +++ /dev/null @@ -1,53 +0,0 @@ -# Copyright 2023 Red Hat, Inc -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -name: Build and deploy Jekyll site to GitHub Pages - -on: - push: - branches: ["main"] - -permissions: - contents: read - pages: write - id-token: write - -jobs: - build: - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v3 - - name: Setup Go - uses: actions/setup-go@v4 - with: - go-version: '1.18' - - name: Generate docgo and literate - run: make godoc - - name: Build with Jekyll - uses: actions/jekyll-build-pages@v1 - with: - source: "./docs/" - - name: Upload artifact - uses: actions/upload-pages-artifact@v1 - deploy: - runs-on: ubuntu-latest - needs: build - steps: - - name: Deploy to GitHub Pages - id: deployment - uses: actions/deploy-pages@v2 - environment: - name: github-pages - url: ${{ steps.deployment.outputs.page_url }} diff --git a/.github/workflows/gotests.yaml b/.github/workflows/gotests.yaml index 4ee85ab5..844faada 100644 --- a/.github/workflows/gotests.yaml +++ b/.github/workflows/gotests.yaml @@ -17,20 +17,6 @@ jobs: - "1.20" - "1.21" name: Tests for Go ${{ matrix.go-version}} - services: - postgres: - image: postgres - env: - POSTGRES_USER: postgres - POSTGRES_PASSWORD: postgres - POSTGRES_DB: postgres - options: >- - --health-cmd pg_isready - --health-interval 10s - --health-timeout 5s - --health-retries 5 - ports: - - 5432:5432 steps: - uses: actions/checkout@v4 - name: Setup Go @@ -45,3 +31,7 @@ jobs: run: ./check_coverage.sh - name: Display code coverage run: make coverage + - name: OpenAPI Check + run: make openapi_check + - name: REST API Tests + run: make integration_tests From bbb59a3123006bb81b87efd6b135f878d623431d Mon Sep 17 00:00:00 2001 From: Martin Zibricky Date: Wed, 1 Nov 2023 17:11:16 +0100 Subject: [PATCH 4/8] [CCXDEV-12106] Typo in GH actions gotest --- .github/workflows/gotests.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/gotests.yaml b/.github/workflows/gotests.yaml index 844faada..5ed51bdf 100644 --- a/.github/workflows/gotests.yaml +++ b/.github/workflows/gotests.yaml @@ -32,6 +32,6 @@ jobs: - name: Display code coverage run: make coverage - name: OpenAPI Check - run: make openapi_check + run: make openapi-check - name: REST API Tests run: make integration_tests From 4c7a2fe1053e3e0e16cb3274c35b927d62ac94a1 Mon Sep 17 00:00:00 2001 From: Martin Zibricky Date: Wed, 1 Nov 2023 17:59:45 +0100 Subject: [PATCH 5/8] [CCXDEV-12103] Fix linting issues in Go code --- internal/config/config_test.go | 1 - internal/server/auth.go | 4 ++-- internal/server/auth_test.go | 2 +- internal/server/server_test.go | 1 - internal/service/endpoints_test.go | 6 +++--- internal/service/service_test.go | 2 -- 6 files changed, 6 insertions(+), 10 deletions(-) diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 551f6ce5..91c81ee7 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -147,7 +147,6 @@ func TestLoadConfiguration(t *testing.T) { } }) } - } func TestEnvVarsOverride(t *testing.T) { diff --git a/internal/server/auth.go b/internal/server/auth.go index 50a530c2..21a821f6 100644 --- a/internal/server/auth.go +++ b/internal/server/auth.go @@ -112,7 +112,7 @@ func (server *Server) Authentication(next http.Handler, noAuthURLs []string) htt // if we took JWT token, it has different structure then x-rh-identity if server.AuthConfig.Type == "jwt" { jwtPayload := &JWTPayload{} - err = json.Unmarshal([]byte(decoded), jwtPayload) + err = json.Unmarshal(decoded, jwtPayload) if err != nil { // malformed token, returns with HTTP code 403 as usual log.Error().Err(err).Msg(malformedTokenMessage) @@ -127,7 +127,7 @@ func (server *Server) Authentication(next http.Handler, noAuthURLs []string) htt }, } } else { - err = json.Unmarshal([]byte(decoded), tk) + err = json.Unmarshal(decoded, tk) if err != nil { // malformed token, returns with HTTP code 403 as usual diff --git a/internal/server/auth_test.go b/internal/server/auth_test.go index 91653c2f..b63eaefa 100644 --- a/internal/server/auth_test.go +++ b/internal/server/auth_test.go @@ -179,7 +179,7 @@ var configAuth2 = server.AuthConfig{ } // dummy HTTP request handler -func dummyHandler(w http.ResponseWriter, r *http.Request) { +func dummyHandler(_ http.ResponseWriter, r *http.Request) { } // start new HTTP server, perform request, check response, and stop HTTP server diff --git a/internal/server/server_test.go b/internal/server/server_test.go index 64f9f129..37f1079c 100644 --- a/internal/server/server_test.go +++ b/internal/server/server_test.go @@ -84,7 +84,6 @@ func TestServer(t *testing.T) { } for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { testServer := server.New(tc.config, tc.authConfig, mux.NewRouter()) go func() { diff --git a/internal/service/endpoints_test.go b/internal/service/endpoints_test.go index 13e907c8..3d32aac3 100644 --- a/internal/service/endpoints_test.go +++ b/internal/service/endpoints_test.go @@ -36,11 +36,11 @@ func (mw mockedWriter) Header() http.Header { return make(http.Header) } -func (mw mockedWriter) Write(buf []byte) (int, error) { - return 0, errors.New("Write error!") +func (mw mockedWriter) Write(_ []byte) (int, error) { + return 0, errors.New("write error") } -func (mw mockedWriter) WriteString(str string) (int, error) { +func (mw mockedWriter) WriteString(_ string) (int, error) { return 0, nil } diff --git a/internal/service/service_test.go b/internal/service/service_test.go index 09198b31..83010222 100644 --- a/internal/service/service_test.go +++ b/internal/service/service_test.go @@ -56,7 +56,6 @@ func TestService(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { for _, version := range versions { - store := mockStorage{ mockData: tc.mockData, } @@ -91,5 +90,4 @@ func TestService(t *testing.T) { } }) } - } From b86e76bc3835410618a8e23a9f47a5fc165bbdc2 Mon Sep 17 00:00:00 2001 From: Martin Zibricky Date: Wed, 1 Nov 2023 18:00:32 +0100 Subject: [PATCH 6/8] [CCXDEV-12103] remove travis file --- .travis.yml | 60 ----------------------------------------------------- 1 file changed, 60 deletions(-) delete mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index ca9e08cb..00000000 --- a/.travis.yml +++ /dev/null @@ -1,60 +0,0 @@ -# Copyright 2022 Red Hat, Inc -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -language: go -go: -- "1.20" - -jobs: - include: - - stage: build - script: - - make build - - stage: style - script: - - ./gofmt.sh - - ./govet.sh - - ./golint.sh - - ./gocyclo.sh - - ./goerrcheck.sh - - ./goconst.sh - - ./gosec.sh - - ./abcgo.sh - - ./ineffassign.sh - - stage: unit tests - before_script: make build - script: - - make test - after_success: - - make coverage - - bash <(curl -s https://codecov.io/bash) - - stage: openapi-checks - language: node_js - node_js: - - 17 - - node - script: - - npm install -g @openapitools/openapi-generator-cli - - openapi-generator-cli validate -i openapi.json - - stage: REST API tests - before_script: make build - script: - - ./test.sh - -stages: - - build - - style - - unit tests - - openapi-checks - - REST API tests From 60474b49918fe5ae5cad9d0f65dccab702df8cfa Mon Sep 17 00:00:00 2001 From: Martin Zibricky Date: Wed, 1 Nov 2023 18:02:51 +0100 Subject: [PATCH 7/8] [CCXDEV-12103] More linting fixes --- internal/server/auth.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/server/auth.go b/internal/server/auth.go index 21a821f6..76c8326d 100644 --- a/internal/server/auth.go +++ b/internal/server/auth.go @@ -72,7 +72,6 @@ type JWTPayload struct { // Authentication middleware for checking auth rights func (server *Server) Authentication(next http.Handler, noAuthURLs []string) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - // for specific URLs it is ok to not use auth. mechanisms at all // this is specific to OpenAPI JSON response and for all OPTION HTTP methods if collections.StringInSlice(r.RequestURI, noAuthURLs) || r.Method == "OPTIONS" { @@ -179,7 +178,7 @@ func (server *Server) GetAuthToken(request *http.Request) (*Identity, error) { return &identity, nil } -func (server *Server) getAuthTokenHeader(w http.ResponseWriter, r *http.Request) (string, error) { +func (server *Server) getAuthTokenHeader(_ http.ResponseWriter, r *http.Request) (string, error) { var tokenHeader string // In case of testing on local machine we don't take x-rh-identity // header, but instead Authorization with JWT token in it From 0895983bbd0e787d9d819cb00ebb4516506e01f8 Mon Sep 17 00:00:00 2001 From: Martin Zibricky Date: Wed, 1 Nov 2023 18:04:18 +0100 Subject: [PATCH 8/8] [CCXDEV-12103] More linting fixes --- internal/server/auth_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/server/auth_test.go b/internal/server/auth_test.go index b63eaefa..002828fb 100644 --- a/internal/server/auth_test.go +++ b/internal/server/auth_test.go @@ -179,7 +179,7 @@ var configAuth2 = server.AuthConfig{ } // dummy HTTP request handler -func dummyHandler(_ http.ResponseWriter, r *http.Request) { +func dummyHandler(_ http.ResponseWriter, _ *http.Request) { } // start new HTTP server, perform request, check response, and stop HTTP server