Skip to content

chore: add license-checker for license validation #30

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

Merged
merged 1 commit into from
Apr 11, 2025
Merged
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
40 changes: 40 additions & 0 deletions .github/workflows/license-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: License Check

on:
push:
branches:
- main
- develop
paths:
- 'composer.lock'
- 'composer.json'
pull_request:
paths:
- 'composer.lock'
- 'composer.json'

jobs:
license-check:
runs-on: ubuntu-latest

strategy:
matrix:
operating-system: [ 'ubuntu-latest' ]
php-versions: [ '8.4' ]
dependency-stability: [ 'prefer-none' ]

name: P${{ matrix.php-versions }} - ${{ matrix.operating-system}}

steps:
- uses: actions/checkout@v4
- name: Install PHP versions
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
tools: "composer:v2"

- name: Install dependencies
run: composer install --prefer-dist --no-interaction

- name: Run license check
run: composer run license-check
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
"phpunit/phpunit": "^11.5",
"thecodingmachine/phpstan-safe-rule": "^1.4"
},
"scripts": {
"license-check": "php license-checker.php"
},
"config": {
"sort-packages": true,
"allow-plugins": {
Expand Down
81 changes: 81 additions & 0 deletions license-checker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

/**
* Simple license checker for Composer dependencies
*
* Usage: php license-checker.php
*/

// Configure allowed licenses
$allowedLicenses = [
'MIT',
'BSD-3-Clause',
'Apache-2.0',
];

// Optional: Configure packages to exclude from checking
$excludedPackages = [
// For example: 'vendor/package-name'
];

$output = shell_exec('composer licenses -f json');
if (!$output) {
echo "Failed to retrieve license information.\n";
exit(1);
}

$licensesData = json_decode($output, true);
if (!isset($licensesData['dependencies']) || !is_array($licensesData['dependencies'])) {
echo "Invalid license data format.\n";
exit(1);
}

echo "Checking licenses against allowed list: " . implode(', ', $allowedLicenses) . "\n\n";

$violations = [];
$checkedCount = 0;

foreach ($licensesData['dependencies'] as $package => $info) {
if (in_array($package, $excludedPackages, true)) {
echo "⏩ Skipping excluded package: {$package}\n";
continue;
}

$checkedCount++;
$packageLicenses = $info['license'] ?? [];
$version = $info['version'] ?? 'unknown';

$hasAllowedLicense = false;
foreach ($packageLicenses as $license) {
if (in_array($license, $allowedLicenses, true)) {
$hasAllowedLicense = true;
break;
}
}

if (!$hasAllowedLicense) {
$violations[] = [
'package' => $package,
'version' => $version,
'licenses' => $packageLicenses,
];
echo "❌ License violation: {$package} ({$version}) uses " . implode(', ', $packageLicenses) . "\n";
} else {
echo "✅ {$package} ({$version}) uses " . implode(', ', $packageLicenses) . "\n";
}
}

echo "\n";
echo "Summary:\n";
echo "- Packages checked: {$checkedCount}\n";
echo "- Violations found: " . count($violations) . "\n";

if (count($violations) > 0) {
echo "\nLicense violations detected. Please review the dependencies above.\n";
exit(1);
} else {
echo "\nAll dependencies comply with the allowed licenses.\n";
exit(0);
}