Chore: Upgrade Jenkins-job-builder to 6.3.0
[releng/global-jjb.git] / shell / license-check.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2017 The Linux Foundation and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11 # Scans files for license header violations
12 #
13 # This script is meant to be called by a Jenkins job and was not designed to
14 # run standalone refer to the below Inputs for details on optional ENVVAR
15 # inputs.
16
17 echo "---> license-check.sh"
18
19 # --- Inputs
20
21 # Space separated list of file patterns to scan for license headers.
22 file_patterns=("${FILE_PATTERNS:-*.go *.groovy *.java *.py *.sh}")
23 # Version of the License Header Checker to install
24 lhc_version="${LHC_VERSION:-0.2.0}"
25 # Comma-separated list of paths to exclude from license checking
26 license_exclude_paths="${LICENSE_EXCLUDE_PATHS:-}"
27 # Comma-separated list of allowed licenses
28 licenses_allowed="${LICENSES_ALLOWED:-Apache-2.0,EPL-1.0,MIT}"
29
30 if [[ "${SPDX_DISABLE}" == "true" ]]; then
31     disable_spdx="--disable-spdx"
32 else
33     disable_spdx=""
34 fi
35
36 # --- Script start
37
38 # DO NOT enable -u because LICENSE_EXCLUDE_PATHS is unbound.
39 # Ensure we fail the job if any steps fail.
40 set -eux -o pipefail
41
42 if hash lhc 2>/dev/null; then
43     echo "License Header Checker is installed."
44     lhc --version
45 else
46     echo "License Header Checker is not installed. Installing..."
47     mkdir "$WORKSPACE/bin"
48     NEXUS_BASEURL="https://nexus.opendaylight.org/"
49     NEXUS_PATH="content/repositories/hosted_installers/org/linuxfoundation/"
50     NEXUS_LHC="lhc/${lhc_version}/lhc-${lhc_version}.tar.gz"
51     NEXUS_URL="${NEXUS_BASEURL}${NEXUS_PATH}${NEXUS_LHC}"
52     wget -nv -O "/tmp/lhc.tar.gz" "$NEXUS_URL"
53     tar -zxvf /tmp/lhc.tar.gz -C "$WORKSPACE/bin"
54     chmod +x "$WORKSPACE/bin/lhc"
55     export PATH="$WORKSPACE/bin:$PATH"
56     lhc --version
57 fi
58
59
60 set -f  # Disable globbing for $file_patterns to pass '*'
61 # Purposely disable SC2068 for $file_patterns
62 # shellcheck disable=SC2068
63 lhc --license "$licenses_allowed" "${disable_spdx}" \
64     --exclude "$license_exclude_paths" \
65     ${file_patterns[@]}
66 set +f