From 236f7ab5dec4d238472a996ec5bddfecf863745b Mon Sep 17 00:00:00 2001 From: Anatolii Bazko Date: Fri, 9 Apr 2021 09:55:33 +0300 Subject: [PATCH] Add GitHub action to validate source code (#762) * Add GitHub action to validate source code Signed-off-by: Anatolii Bazko --- .github/workflows/build.yml | 37 -------- .../check-nightly-olm-bundle-version.yaml | 9 +- .../workflows/check-nightly-olm-bundle.yaml | 12 ++- .github/workflows/pr-check.yml | 87 +++++++++++++++++++ mocks/permission_checker_mock.go | 3 +- .../oauth_initial_htpasswd_provider_mock.go | 3 +- mocks/pkg/util/process_mock.go | 3 +- pkg/controller/add_che.go | 6 +- pkg/controller/che/che_controller_test.go | 8 +- pkg/controller/controller.go | 7 +- pkg/deploy/dev-workspace/dev_workspace.go | 2 + pkg/deploy/sync.go | 11 +++ pkg/util/k8s_helpers_test.go | 1 - pkg/util/util.go | 8 +- pkg/util/util_test.go | 2 - 15 files changed, 130 insertions(+), 69 deletions(-) delete mode 100644 .github/workflows/build.yml create mode 100644 .github/workflows/pr-check.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index 192ddaf37..000000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,37 +0,0 @@ -# -# Copyright (c) 2012-2020 Red Hat, Inc. -# This program and the accompanying materials are made -# available under the terms of the Eclipse Public License 2.0 -# which is available at https://www.eclipse.org/legal/epl-2.0/ -# -# SPDX-License-Identifier: EPL-2.0 -# -# Contributors: -# Red Hat, Inc. - initial API and implementation -# -name: Eclipse Che operator image -on: [pull_request, push] -jobs: - build: - name: Build - runs-on: ubuntu-20.04 - steps: - - uses: actions/checkout@v1 - - name: Build the Docker image - run: docker build . - buildx: - name: Multi-platform build - runs-on: ubuntu-20.04 - steps: - - name: Checkout source code - uses: actions/checkout@v1 - - name: Set up QEMU - uses: docker/setup-qemu-action@v1 - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 - - name: Build images - uses: docker/build-push-action@v2 - with: - file: Dockerfile - platforms: linux/amd64,linux/ppc64le - tags: quay.io/eclipse/che-operator:nightly diff --git a/.github/workflows/check-nightly-olm-bundle-version.yaml b/.github/workflows/check-nightly-olm-bundle-version.yaml index 981f43b2c..ca9d8ca93 100644 --- a/.github/workflows/check-nightly-olm-bundle-version.yaml +++ b/.github/workflows/check-nightly-olm-bundle-version.yaml @@ -19,8 +19,9 @@ jobs: - uses: actions/checkout@v2 - name: Install yq run: sudo pip install yq + - name: Set up Go 1.13 + uses: actions/setup-go@v2 + with: + go-version: 1.13 - name: Check nightly OLM bundle version - run: | - go version - export GOROOT=/opt/hostedtoolcache/go/1.15.10/x64/ - ${GITHUB_WORKSPACE}/.github/bin/check-nightly-olm-bundle-version.sh + run: ${GITHUB_WORKSPACE}/.github/bin/check-nightly-olm-bundle-version.sh diff --git a/.github/workflows/check-nightly-olm-bundle.yaml b/.github/workflows/check-nightly-olm-bundle.yaml index 102e0b3c5..802d54525 100644 --- a/.github/workflows/check-nightly-olm-bundle.yaml +++ b/.github/workflows/check-nightly-olm-bundle.yaml @@ -20,13 +20,11 @@ jobs: runs-on: ubuntu-20.04 steps: - uses: actions/checkout@v2 - - name: Install yq run: sudo pip install yq - + - name: Set up Go 1.13 + uses: actions/setup-go@v2 + with: + go-version: 1.13 - name: Che operator code check. - run: >- - env && - go version && - export GOROOT=/opt/hostedtoolcache/go/1.15.10/x64/ && - ${GITHUB_WORKSPACE}/.github/bin/check-nightly-olm-bundle.sh + run: ${GITHUB_WORKSPACE}/.github/bin/check-nightly-olm-bundle.sh diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml new file mode 100644 index 000000000..c72e2ea3e --- /dev/null +++ b/.github/workflows/pr-check.yml @@ -0,0 +1,87 @@ +# +# Copyright (c) 2012-2020 Red Hat, Inc. +# This program and the accompanying materials are made +# available under the terms of the Eclipse Public License 2.0 +# which is available at https://www.eclipse.org/legal/epl-2.0/ +# +# SPDX-License-Identifier: EPL-2.0 +# +# Contributors: +# Red Hat, Inc. - initial API and implementation +# +name: PR validation +on: pull_request +jobs: + build: + name: Image build + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v1 + - name: Build the Docker image + run: docker build . + multiplatform-build: + name: Multi-platform image build + runs-on: ubuntu-20.04 + steps: + - name: Checkout source code + uses: actions/checkout@v1 + - name: Set up QEMU + uses: docker/setup-qemu-action@v1 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + - name: Build images + uses: docker/build-push-action@v2 + with: + file: Dockerfile + platforms: linux/amd64,linux/ppc64le + tags: quay.io/eclipse/che-operator:nightly + sources: + name: Source code validation + runs-on: ubuntu-20.04 + steps: + - name: Set up Go 1.13 + uses: actions/setup-go@v2 + with: + go-version: 1.13 + - name: Checkout source code + uses: actions/checkout@v1 + - name: Cache go modules + id: cache-mod + uses: actions/cache@v2 + with: + path: ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + - name: Download dependencies + run: go mod download + if: steps.cache-mod.outputs.cache-hit != 'true' + - name: Check go mod status + run: | + go mod tidy + if [[ ! -z $(git status -s) ]] + then + echo "Go mod state is not clean:" + git --no-pager diff + exit 1 + fi + - name: Check format + run: | + git reset HEAD --hard + go fmt -x ./... + if [[ ! -z $(git status -s) ]] + then + echo "not well formatted sources are found:" + git --no-pager diff + exit 1 + fi + - name: Check imports + run: | + go get -u golang.org/x/tools/cmd/goimports + git reset HEAD --hard + if [[ $(find . -not -path "./vendor/*" -name '*.go' -exec goimports -l {} \;) != "" ]] + then + echo "not well formatted sources are found:" + find . -not -path "./vendor/*" -name '*.go' -exec goimports -l {} \; + exit 1 + fi diff --git a/mocks/permission_checker_mock.go b/mocks/permission_checker_mock.go index 390a11d5e..fe12700d1 100644 --- a/mocks/permission_checker_mock.go +++ b/mocks/permission_checker_mock.go @@ -5,9 +5,10 @@ package mock_che import ( + reflect "reflect" + gomock "github.com/golang/mock/gomock" v1 "k8s.io/api/rbac/v1" - reflect "reflect" ) // MockPermissionChecker is a mock of PermissionChecker interface diff --git a/mocks/pkg/controller/che/oauth_initial_htpasswd_provider_mock.go b/mocks/pkg/controller/che/oauth_initial_htpasswd_provider_mock.go index 6a535ac50..fb4cd9759 100644 --- a/mocks/pkg/controller/che/oauth_initial_htpasswd_provider_mock.go +++ b/mocks/pkg/controller/che/oauth_initial_htpasswd_provider_mock.go @@ -5,10 +5,11 @@ package mock_che import ( + reflect "reflect" + deploy "github.com/eclipse-che/che-operator/pkg/deploy" gomock "github.com/golang/mock/gomock" v1 "github.com/openshift/api/config/v1" - reflect "reflect" ) // MockOpenShiftOAuthUserHandler is a mock of OpenShiftOAuthUserHandler interface diff --git a/mocks/pkg/util/process_mock.go b/mocks/pkg/util/process_mock.go index 484faa52c..a668487c5 100644 --- a/mocks/pkg/util/process_mock.go +++ b/mocks/pkg/util/process_mock.go @@ -5,8 +5,9 @@ package mock_util import ( - gomock "github.com/golang/mock/gomock" reflect "reflect" + + gomock "github.com/golang/mock/gomock" ) // MockRunnable is a mock of Runnable interface diff --git a/pkg/controller/add_che.go b/pkg/controller/add_che.go index d67a5f314..b06e39f5e 100644 --- a/pkg/controller/add_che.go +++ b/pkg/controller/add_che.go @@ -1,13 +1,13 @@ // -// Copyright (c) 2012-2019 Red Hat, Inc. +// Copyright (c) 2019-2021 Red Hat, Inc. // This program and the accompanying materials are made // available under the terms of the Eclipse Public License 2.0 // which is available at https://www.eclipse.org/legal/epl-2.0/ // // SPDX-License-Identifier: EPL-2.0 // -// Contributors: // Contributors: -// Red Hat, Inc. - initial API and implementation // Red Hat, Inc. - initial API and implementation +// Contributors: +// Red Hat, Inc. - initial API and implementation // package controller diff --git a/pkg/controller/che/che_controller_test.go b/pkg/controller/che/che_controller_test.go index ea8d1a876..23a5fb792 100644 --- a/pkg/controller/che/che_controller_test.go +++ b/pkg/controller/che/che_controller_test.go @@ -426,13 +426,13 @@ func TestCaseAutoDetectOAuth(t *testing.T) { func TestEnsureServerExposureStrategy(t *testing.T) { type testCase struct { - name string - expectedCr *orgv1.CheCluster + name string + expectedCr *orgv1.CheCluster devWorkspaceEnabled bool - initObjects []runtime.Object + initObjects []runtime.Object } - testCases:= []testCase{ + testCases := []testCase{ { name: "Single Host should be enabled if devWorkspace is enabled", expectedCr: &orgv1.CheCluster{ diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index d037c5e4f..966939fe7 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -1,15 +1,14 @@ // -// Copyright (c) 2012-2019 Red Hat, Inc. +// Copyright (c) 2019-2021 Red Hat, Inc. // This program and the accompanying materials are made // available under the terms of the Eclipse Public License 2.0 // which is available at https://www.eclipse.org/legal/epl-2.0/ // // SPDX-License-Identifier: EPL-2.0 // -// Contributors: // Contributors: -// Red Hat, Inc. - initial API and implementation // Red Hat, Inc. - initial API and implementation +// Contributors: +// Red Hat, Inc. - initial API and implementation // - package controller import ( diff --git a/pkg/deploy/dev-workspace/dev_workspace.go b/pkg/deploy/dev-workspace/dev_workspace.go index 248482d97..4e81ebebf 100644 --- a/pkg/deploy/dev-workspace/dev_workspace.go +++ b/pkg/deploy/dev-workspace/dev_workspace.go @@ -9,11 +9,13 @@ // Contributors: // Red Hat, Inc. - initial API and implementation // + package devworkspace import ( "context" "errors" + "github.com/eclipse-che/che-operator/pkg/deploy" "github.com/eclipse-che/che-operator/pkg/util" operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1" diff --git a/pkg/deploy/sync.go b/pkg/deploy/sync.go index 941bca7d7..2149c0491 100644 --- a/pkg/deploy/sync.go +++ b/pkg/deploy/sync.go @@ -1,3 +1,14 @@ +// +// Copyright (c) 2021 Red Hat, Inc. +// This program and the accompanying materials are made +// available under the terms of the Eclipse Public License 2.0 +// which is available at https://www.eclipse.org/legal/epl-2.0/ +// +// SPDX-License-Identifier: EPL-2.0 +// +// Contributors: +// Red Hat, Inc. - initial API and implementation +// package deploy import ( diff --git a/pkg/util/k8s_helpers_test.go b/pkg/util/k8s_helpers_test.go index 1b3e0d32c..82b11ff31 100644 --- a/pkg/util/k8s_helpers_test.go +++ b/pkg/util/k8s_helpers_test.go @@ -59,4 +59,3 @@ func TestGetDeploymentPod(t *testing.T) { } logrus.Infof("Test passed. Pod %s found", pod) } - diff --git a/pkg/util/util.go b/pkg/util/util.go index 4badc59ac..437957f18 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -204,13 +204,13 @@ func GetServerExposureStrategy(c *orgv1.CheCluster) string { strategy := c.Spec.Server.ServerExposureStrategy if strategy != "" { return strategy - } else if c.Spec.DevWorkspace.Enable { + } else if c.Spec.DevWorkspace.Enable { return "single-host" - } else if IsOpenShift { + } else if IsOpenShift { return "multi-host" - } else { + } else { return GetValue(c.Spec.K8s.IngressStrategy, "multi-host") - } + } } func IsTestMode() (isTesting bool) { diff --git a/pkg/util/util_test.go b/pkg/util/util_test.go index f67baac8c..ca9eac54f 100644 --- a/pkg/util/util_test.go +++ b/pkg/util/util_test.go @@ -1,4 +1,3 @@ - // // Copyright (c) 2012-2019 Red Hat, Inc. // This program and the accompanying materials are made @@ -38,7 +37,6 @@ func TestGeneratePasswd(t *testing.T) { } } - func TestGetValue(t *testing.T) { key := "myvalue" defaultValue := "myDefaultValue"