Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a license check for npm modules #6552

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
- stage: test
script:
- npm install || exit 1
- ./bin/check-npm-licenses.sh || exit 1
- npm run ci || exit 1

- stage: test
Expand Down
41 changes: 41 additions & 0 deletions bin/check-npm-licenses.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash

# Include useful functions
. "$(dirname "$0")/includes.sh"

ALLOWED_LICENSES=(
"GPL-2.0-or-later"
"GPL-2.0+"
"LGPL-2.1"
"(GPL-2.0 OR MIT)"
"MIT"
"ISC"
"BSD"
"BSD-2-Clause"
"BSD-3-Clause"
"CC0-1.0"
)

FOUND_INCOMPATIBLE_MODULE=false


for MODULE_DIR in $(npm ls --production --parseable); do
PACKAGE_JSON="${MODULE_DIR}/package.json"

PACKAGE_NAME=$(jq --raw-output '.name' $PACKAGE_JSON)
PACKAGE_LICENSE=$(jq --raw-output '( .license // .licenses[0].type )' $PACKAGE_JSON)

if ! in_array "${PACKAGE_LICENSE}" "${ALLOWED_LICENSES[@]}"; then
if ! $FOUND_INCOMPATIBLE_MODULE; then
FOUND_INCOMPATIBLE_MODULE=true;
echo "These modules have incompatible licences:"
fi
echo "${PACKAGE_NAME}: ${PACKAGE_LICENSE}"
fi
done

if ! $FOUND_INCOMPATIBLE_MODULE; then
echo "All module licenses are compatible."
else
exit 1;
fi
24 changes: 24 additions & 0 deletions bin/includes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,27 @@ action_format() {
command_exists() {
type -t "$1" >/dev/null 2>&1
}

##
# Checks if an array countains a particular value.
#
# @param {mixed} needle The value to search for.
# @param {array} haystack The array to search.
#
# @return bool Whether the haystack contains the needle or not.
##
in_array() {
local needle="$1";
shift;
local haystack=("$@");

local item;

for item in "${haystack[@]}"; do
if [ "$item" == "${needle}" ]; then
return 0;
fi
done

return 1;
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
"test": "npm run lint && npm run test-unit",
"test-php": "npm run lint-php && npm run test-unit-php",
"ci": "concurrently \"npm run lint && npm run build\" \"npm run test-unit:coverage-ci\"",
"check-licenses": "./bin/check-npm-licenses.sh",
"fixtures:clean": "rimraf \"core-blocks/test/fixtures/*.+(json|serialized.html)\"",
"fixtures:server-registered": "docker-compose run -w /var/www/html/wp-content/plugins/gutenberg --rm wordpress ./bin/get-server-blocks.php > core-blocks/test/server-registered.json",
"fixtures:generate": "npm run fixtures:server-registered && cross-env GENERATE_MISSING_FIXTURES=y npm run test-unit",
Expand Down