From e7ef4fad5b95f2438cdd1dbb7c9c8b31434ed513 Mon Sep 17 00:00:00 2001 From: Ihar Kryvanos Date: Tue, 27 Aug 2024 19:00:40 +0200 Subject: [PATCH] Extract license check from integration tests (#473) --- .github/workflows/build-test.yaml | 15 ++++--- .pre-commit-config.yaml | 7 +++ scripts/check-license.sh | 71 +++++++++++++++++++++++++++++++ testing/integrate.sh | 21 --------- 4 files changed, 88 insertions(+), 26 deletions(-) create mode 100755 scripts/check-license.sh diff --git a/.github/workflows/build-test.yaml b/.github/workflows/build-test.yaml index 92356d91..f6beb12c 100644 --- a/.github/workflows/build-test.yaml +++ b/.github/workflows/build-test.yaml @@ -20,7 +20,15 @@ jobs: go-version-file: 'go.mod' - uses: actions/setup-python@v3 - uses: pre-commit/action@v3.0.0 - unit_test: + chack_license: + name: Check license + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v3 + - name: Check license in project files + run: ./scripts/check-license.sh + shell: bash + test: name: Unit tests runs-on: ubuntu-20.04 steps: @@ -37,11 +45,8 @@ jobs: - name: integration bash tests run: ./testing/integrate.sh shell: bash - - name: integration tests - run: go test -tags=integration ./... - shell: bash integration_test: - name: Unit tests + name: Integration tests runs-on: ubuntu-20.04 steps: - uses: actions/checkout@v3 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8b951495..b0ae802c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -14,6 +14,13 @@ repos: - id: go-fmt - id: go-build - id: go-mod-tidy +- repo: local + hooks: + - id: check-license + name: check-license + entry: scripts/check-license.sh + language: script + pass_filenames: false - repo: https://github.com/golangci/golangci-lint rev: v1.59.1 hooks: diff --git a/scripts/check-license.sh b/scripts/check-license.sh new file mode 100755 index 00000000..6166894d --- /dev/null +++ b/scripts/check-license.sh @@ -0,0 +1,71 @@ +#!/usr/bin/env bash + +# Copyright (c) 2019 Snowflake Inc. All rights reserved. +# +# 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. + +set -o nounset + +function create_tempfile() { + tempprefix=$(basename "$0") + mktemp /tmp/${tempprefix}.XXXXXX +} + + +# check_status takes 3 args: +# +# A code to compare against +# Logfile to print on error +# Any text to output on failure (all remaining args). +function check_status { + STATUS=$1 + shift + LOG=$1 + shift + FAIL=$* + + if [ "${STATUS}" != 0 ]; then + { + echo "FAIL ${FAIL}" + } >&2 + if [ "${LOG}" != "/dev/null" ]; then + ls "${LOGS}" + print_logs "${LOG}" "${FAIL}" + fi + exit 1 + fi +} + +license_go_files=$(create_tempfile) +license_proto_files=$(create_tempfile) + +# Check licensing +# For Go we can ignore generate protobuf files. +find . -type f -name \*.go ! -name \*.pb.go >"${license_go_files}" +find . -type f -name \*.proto >"${license_proto_files}" + +cat "${license_go_files}" "${license_proto_files}" | ( + broke="" + while read -r i; do + if ! grep -q "Licensed under the Apache License" ${i}; then + echo "${i} is missing required license." + broke=true + fi + done + + if [ "${broke}" == "true" ]; then + exit 1 + fi +) +check_status $? /dev/null Files missing license diff --git a/testing/integrate.sh b/testing/integrate.sh index 61a4eb4a..8a02a9a0 100755 --- a/testing/integrate.sh +++ b/testing/integrate.sh @@ -263,27 +263,6 @@ cd "${dir}/.." || { echo "package sansshell.authz" >${LOGS}/policy echo "default allow = true" >>${LOGS}/policy -# Check licensing -# For Go we can ignore generate protobuf files. -find . -type f -name \*.go ! -name \*.pb.go >${LOGS}/license-go -find . -type f -name \*.sh >${LOGS}/license-sh -find . -type f -name \*.proto >${LOGS}/license-proto - -cat "${LOGS}/license-go" "${LOGS}/license-proto" | ( - broke="" - while read -r i; do - if ! grep -q "Licensed under the Apache License" ${i}; then - echo "${i} is missing required license." - broke=true - fi - done - - if [ "${broke}" == "true" ]; then - exit 1 - fi -) -check_status $? /dev/null Files missing license - echo echo "Checking with go vet" echo