Skip to content

Commit 095f69e

Browse files
authored
chore: add license-checker for license validation (#30)
1 parent 041e858 commit 095f69e

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

.github/workflows/license-check.yml

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: License Check
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- develop
8+
paths:
9+
- 'composer.lock'
10+
- 'composer.json'
11+
pull_request:
12+
paths:
13+
- 'composer.lock'
14+
- 'composer.json'
15+
16+
jobs:
17+
license-check:
18+
runs-on: ubuntu-latest
19+
20+
strategy:
21+
matrix:
22+
operating-system: [ 'ubuntu-latest' ]
23+
php-versions: [ '8.4' ]
24+
dependency-stability: [ 'prefer-none' ]
25+
26+
name: P${{ matrix.php-versions }} - ${{ matrix.operating-system}}
27+
28+
steps:
29+
- uses: actions/checkout@v4
30+
- name: Install PHP versions
31+
uses: shivammathur/setup-php@v2
32+
with:
33+
php-version: ${{ matrix.php-versions }}
34+
tools: "composer:v2"
35+
36+
- name: Install dependencies
37+
run: composer install --prefer-dist --no-interaction
38+
39+
- name: Run license check
40+
run: composer run license-check

composer.json

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
"phpunit/phpunit": "^11.5",
4040
"thecodingmachine/phpstan-safe-rule": "^1.4"
4141
},
42+
"scripts": {
43+
"license-check": "php license-checker.php"
44+
},
4245
"config": {
4346
"sort-packages": true,
4447
"allow-plugins": {

license-checker.php

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Simple license checker for Composer dependencies
7+
*
8+
* Usage: php license-checker.php
9+
*/
10+
11+
// Configure allowed licenses
12+
$allowedLicenses = [
13+
'MIT',
14+
'BSD-3-Clause',
15+
'Apache-2.0',
16+
];
17+
18+
// Optional: Configure packages to exclude from checking
19+
$excludedPackages = [
20+
// For example: 'vendor/package-name'
21+
];
22+
23+
$output = shell_exec('composer licenses -f json');
24+
if (!$output) {
25+
echo "Failed to retrieve license information.\n";
26+
exit(1);
27+
}
28+
29+
$licensesData = json_decode($output, true);
30+
if (!isset($licensesData['dependencies']) || !is_array($licensesData['dependencies'])) {
31+
echo "Invalid license data format.\n";
32+
exit(1);
33+
}
34+
35+
echo "Checking licenses against allowed list: " . implode(', ', $allowedLicenses) . "\n\n";
36+
37+
$violations = [];
38+
$checkedCount = 0;
39+
40+
foreach ($licensesData['dependencies'] as $package => $info) {
41+
if (in_array($package, $excludedPackages, true)) {
42+
echo "⏩ Skipping excluded package: {$package}\n";
43+
continue;
44+
}
45+
46+
$checkedCount++;
47+
$packageLicenses = $info['license'] ?? [];
48+
$version = $info['version'] ?? 'unknown';
49+
50+
$hasAllowedLicense = false;
51+
foreach ($packageLicenses as $license) {
52+
if (in_array($license, $allowedLicenses, true)) {
53+
$hasAllowedLicense = true;
54+
break;
55+
}
56+
}
57+
58+
if (!$hasAllowedLicense) {
59+
$violations[] = [
60+
'package' => $package,
61+
'version' => $version,
62+
'licenses' => $packageLicenses,
63+
];
64+
echo "❌ License violation: {$package} ({$version}) uses " . implode(', ', $packageLicenses) . "\n";
65+
} else {
66+
echo "{$package} ({$version}) uses " . implode(', ', $packageLicenses) . "\n";
67+
}
68+
}
69+
70+
echo "\n";
71+
echo "Summary:\n";
72+
echo "- Packages checked: {$checkedCount}\n";
73+
echo "- Violations found: " . count($violations) . "\n";
74+
75+
if (count($violations) > 0) {
76+
echo "\nLicense violations detected. Please review the dependencies above.\n";
77+
exit(1);
78+
} else {
79+
echo "\nAll dependencies comply with the allowed licenses.\n";
80+
exit(0);
81+
}

0 commit comments

Comments
 (0)