Skip to content

Commit

Permalink
Adds check for oss license in source files (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
ubiratansoares authored Nov 4, 2023
1 parent 563965b commit 3ef3afb
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
19 changes: 19 additions & 0 deletions checks/licenses/action.yaml
Original file line number Diff line number Diff line change
@@ -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 }}
59 changes: 59 additions & 0 deletions src/license-enforcer.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3ef3afb

Please sign in to comment.