diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 9670784..5c6d32e 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -17,3 +17,7 @@ jobs: - name: Component tests for Bash Linter uses: ./checks/bash + - name: Component tests for license enforcer + uses: ./checks/licenses + with: + file-patterns: "*.sh" diff --git a/checks/licenses/action.yaml b/checks/licenses/action.yaml new file mode 100644 index 0000000..90dd0c1 --- /dev/null +++ b/checks/licenses/action.yaml @@ -0,0 +1,19 @@ +name: "Lint documentation and find typos" +description: "Standardize documentation and prose with different linters" + +inputs: + file-patterns: + description: “Patterns to look in source files, comma separated” + required: true + + license: + description: “Open-source license to enforce” + default: "mit" + required: true + +runs: + using: "composite" + steps: + - name: Check licenses on source files + shell: bash + run: ./src/license-enforcer.sh $GITHUB_WORKSPACE ${{ inputs.file-patterns }} ${{ inputs.license }} diff --git a/src/license-enforcer.sh b/src/license-enforcer.sh new file mode 100755 index 0000000..14bcae7 --- /dev/null +++ b/src/license-enforcer.sh @@ -0,0 +1,59 @@ +#!/usr/bin/env bash +# Copyright 2023 Dotanuki Labs +# SPDX-License-Identifier: MIT + +# shellcheck disable=SC2046 + +set -eo pipefail + +# https://github.com/google/addlicense +readonly image="ghcr.io/google/addlicense" + +readonly target_folder="$1" +readonly sources="$2" +readonly license="$3" + +require_docker() { + if (! docker stats --no-stream >/dev/null); then + echo "Docker is required for this check" + echo + exit 1 + fi +} + +check_license_on_files() { + local extension="$1" + + echo "→ Checking licenses for $files files" + + docker run --rm -v "${target_folder}:/src" "$image" \ + -c "Dotanuki Labs" \ + -l "$license" \ + -check $(find . -type f -name "$extension") +} + +report_missing_licenses() { + echo + echo "There are files with missing ($license) license" + echo + exit 1 +} + +enforce_license_for_file_patterns() { + IFS=" " read -r -a files <<<"$(echo "$sources" | sed "s/,/[:space:]/g" | xargs)" + + for type in "${files[@]}"; do + check_license_on_files "$type" || report_missing_licenses + done +} + +echo +echo "🔥 Enforcing open-source license on source files" +echo + +require_docker +enforce_license_for_file_patterns + +echo +echo "✅ All good!" +echo