Skip to content

Commit

Permalink
Add license check functionality to castor
Browse files Browse the repository at this point in the history
A new function has been introduced in `castor.php` to check licenses and ensure compliance with defined acceptable licenses. This function has also been integrated into the CI process via the `integrate.yml` GitHub workflow file. It checks license compliance for each dependency before a pull request is merged.
  • Loading branch information
Spomky committed Apr 13, 2024
1 parent e7bebb9 commit 65eae0d
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/integrate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,32 @@ jobs:
- name: "Deptrac"
run: 'castor deptrac'

check_licenses:
name: "5️⃣ Check Licenses"
needs:
- "byte_level"
- "syntax_errors"
runs-on: "ubuntu-latest"
steps:
- name: "Set up PHP"
uses: "shivammathur/setup-php@v2"
with:
php-version: "8.3"
extensions: "ctype, dom, json, libxml, mbstring, openssl, phar, simplexml, tokenizer, xml, xmlwriter"
tools: castor

- name: "Checkout code"
uses: "actions/checkout@v3"

- name: "Install dependencies"
uses: "ramsey/composer-install@v3"
with:
dependency-versions: "highest"
composer-options: "--optimize-autoloader"

- name: "Execute license check"
run: "castor check-licenses"

rector_checkstyle:
name: "6️⃣ Rector Checkstyle"
needs:
Expand Down
58 changes: 58 additions & 0 deletions castor.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,64 @@ function validate(): void
run($command, environment: $environment);
}

/**
* @param array<string> $allowedLicenses
*/
#[AsTask(description: 'Check licenses')]
function checkLicenses(
array $allowedLicenses = ['Apache-2.0', 'BSD-2-Clause', 'BSD-3-Clause', 'ISC', 'MIT', 'MPL-2.0', 'OSL-3.0']
): void {
io()->title('Checking licenses');
$allowedExceptions = [];
$command = ['composer', 'licenses', '-f', 'json'];
$environment = [
'XDEBUG_MODE' => 'off',
];
$result = run($command, environment: $environment, quiet: true);
if (! $result->isSuccessful()) {
io()->error('Cannot determine licenses');
exit(1);
}
$licenses = json_decode($result->getOutput(), true);
$disallowed = array_filter(
$licenses['dependencies'],
static fn (array $info, $name) => ! in_array($name, $allowedExceptions, true)
&& count(array_diff($info['license'], $allowedLicenses)) === 1,
\ARRAY_FILTER_USE_BOTH
);
$allowed = array_filter(
$licenses['dependencies'],
static fn (array $info, $name) => in_array($name, $allowedExceptions, true)
|| count(array_diff($info['license'], $allowedLicenses)) === 0,
\ARRAY_FILTER_USE_BOTH
);
if (count($disallowed) > 0) {
io()
->table(
['Package', 'License'],
array_map(
static fn ($name, $info) => [$name, implode(', ', $info['license'])],
array_keys($disallowed),
$disallowed
)
);
io()
->error('Disallowed licenses found');
exit(1);
}
io()
->table(
['Package', 'License'],
array_map(
static fn ($name, $info) => [$name, implode(', ', $info['license'])],
array_keys($allowed),
$allowed
)
);
io()
->success('All licenses are allowed');
}

#[AsTask(description: 'Run Rector')]
function rector(bool $fix = false): void
{
Expand Down

0 comments on commit 65eae0d

Please sign in to comment.