8000 minor #40700 [CI] Make sure packages contain all metafiles (Nyholm) · symfony/symfony@790e2a8 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 790e2a8

Browse files
committed
minor #40700 [CI] Make sure packages contain all metafiles (Nyholm)
This PR was squashed before being merged into the 5.3-dev branch. Discussion ---------- [CI] Make sure packages contain all metafiles | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | This PR introduce a new CI job to just do some checks on a package. It first looks at the git diff to figure out what packages have changed. Then for each changed package it verifies: - .gitattributes exists - .gitignore exists - CHANGELOG.md exists - LICENSE exists - phpunit.xml.dist exists - README.md exists - The LICENSE file includes "Fabien Potencier", the correct year and has same content as /LICENSE If the package is new (license file changed), we also make sure that the package is present in composer.json's replace. I got this idea after seeing PRs like #40696. And we do add one or two package every month. Commits ------- 18658a2 [CI] Make sure packages contain all metafiles
2 parents 9966e98 + 18658a2 commit 790e2a8

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed

.github/get-modified-packages.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* Given a list of all packages, find the package that have been modified.
5+
*/
6+
7+
if (3 > $_SERVER['argc']) {
8+
echo "Usage: app-packages modified-files\n";
9+
exit(1);
10+
}
11+
12+
$allPackages = json_decode($_SERVER['argv'][1], true, 512, \JSON_THROW_ON_ERROR);
13+
$modifiedFiles = json_decode($_SERVER['argv'][2], true, 512, \JSON_THROW_ON_ERROR);
14+
15+
function isComponentBridge(string $packageDir): bool
16+
{
17+
return 0 < preg_match('@Symfony/Component/.*/Bridge/@', $packageDir);
18+
}
19+
20+
$newPackage = [];
21+
$modifiedPackages = [];
22+
foreach ($modifiedFiles as $file) {
23+
foreach ($allPackages as $package) {
24+
if (0 === strpos($file, $package)) {
25+
$modifiedPackages[$package] = true;
26+
if ('LICENSE' === substr($file, -7)) {
27+
/*
28+
* There is never a reason to modify the LICENSE file, this diff
29+
* must be adding a new package
30+
*/
31+
$newPackage[$package] = true;
32+
}
33+
break;
34+
}
35+
}
36+
}
37+
38+
$output = [];
39+
foreach ($modifiedPackages as $directory => $bool) {
40+
$name = json_decode(file_get_contents($directory.'/composer.json'), true)['name'] ?? 'unknown';
41+
$output[] = ['name' => $name, 'directory' => $directory, 'new' => $newPackage[$directory] ?? false, 'component_bridge' => isComponentBridge($directory)];
42+
}
43+
44+
echo json_encode($output);

.github/workflows/package-tests.yml

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
name: Package
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- src/**
7+
8+
jobs:
9+
verify:
10+
name: Verify
11+
runs-on: Ubuntu-20.04
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v2
15+
16+
- name: Fetch branch from where the PR started
17+
run: git fetch --no-tags --prune --depth=1 origin +refs/heads/*:refs/remotes/origin/*
18+
19+
- name: Find packages
20+
id: find-packages
21+
run: echo "::set-output name=packages::$(php .github/get-modified-packages.php $(find src/Symfony -mindepth 2 -type f -name composer.json -printf '%h\n' | jq -R -s -c 'split("\n")[:-1]') $(git diff --name-only origin/${{ github.base_ref }} HEAD | grep src/ | jq -R -s -c 'split("\n")[:-1]'))"
22+
23+
- name: Verify meta files are correct
24+
run: |
25+
ok=0
26+
27+
_file_exist() {
28+
if [ ! -f "${1}" ]; then
29+
echo "File ${1} does not exist"
30+
return 1
31+
fi
32+
}
33+
34+
_file_not_exist() {
35+
if [ -f "${1}" ]; then
36+
echo "File ${1} should not be here"
37+
return 1
38+
fi
39+
}
40+
41+
_correct_license_file() {
42+
FIRST_LINE="Copyright (c) $(date +"%Y") Fabien Potencier"
43+
PACKAGE_FIRST_LINE=$(head -1 ${1})
44+
if [[ "$FIRST_LINE" != "$PACKAGE_FIRST_LINE" ]]; then
45+
echo "First line of the license file is wrong. Maybe it is the wrong year?"
46+
return 1
47+
fi
48+
49+
TEMPLATE=$(tail -n +2 LICENSE)
50+
PACKAGE_LICENSE=$(tail -n +2 ${1})
51+
if [[ "$TEMPLATE" != "$PACKAGE_LICENSE" ]]; then
52+
echo "Wrong content in license file"
53+
return 1
54+
fi
55+
}
56+
57+
json='${{ steps.find-packages.outputs.packages }}'
58+
for package in $(echo "${json}" | jq -r '.[] | @base64'); do
59+
_jq() {
60+
echo ${package} | base64 --decode | jq -r ${1}
61+
}
62+
63+
DIR=$(_jq '.directory')
64+
NAME=$(_jq '.name')
65+
echo "::group::$NAME"
66+
localExit=0
67+
68+
_file_exist $DIR/.gitattributes || localExit=1
69+
_file_exist $DIR/.gitignore || localExit=1
70+
_file_exist $DIR/CHANGELOG.md || localExit=1
71+
_file_exist $DIR/LICENSE || localExit=1
72+
_file_exist $DIR/phpunit.xml.dist || localExit=1
73+
_file_exist $DIR/README.md || localExit=1
74+
_file_not_exist $DIR/phpunit.xml || localExit=1
75+
76+
if [ $(_jq '.new') == true ]; then
77+
echo "Verifying new package"
78+
_correct_license_file $DIR/LICENSE || localExit=1
79+
80+
if [ $(_jq '.component_bridge') == false ]; then
81+
if [ ! $(cat composer.json | jq -e ".replace.\"$NAME\"|test(\"self.version\")") ]; then
82+
echo "Composer.json's replace section needs to contain $NAME"
83+
localExit=1
84+
fi
85+
fi
86+
fi
87+
88+
ok=$(( $localExit || $ok ))
89+
echo ::endgroup::
90+
if [ $localExit -ne 0 ]; then
91+
echo "::error::$NAME failed"
92+
fi
93+
done
94+
95+
exit $ok

0 commit comments

Comments
 (0)
0