Skip to content

Add github workflow to verify docs #103

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

Closed
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ APP_SECRET=5dd8ffca252d95e8b4fb5b2d15310e92

SYMFONY_DOCS_SECRET=''
SYMFONY_SECRET=''
BOT_USERNAME='carsonbot-test'
BOT_USERNAME='carsonbot'
###> knplabs/github-api ###
#GITHUB_TOKEN=XXX
###< knplabs/github-api ###
Expand Down
70 changes: 70 additions & 0 deletions .github/workflows/docs-invalid-use.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: Verify docs

on:
schedule:
- cron: '58 6 * * *'

jobs:
use:
name: Invalid use statements
runs-on: ubuntu-latest
strategy:
fail-fast: false

steps:
- name: Set up PHP
uses: shivammathur/setup-php@2.7.0
with:
php-version: 7.4
coverage: none

- name: Checkout code
uses: actions/checkout@v2

- name: Checkout Symfony repo
run: git clone https://github.com/symfony/symfony .github/workflows/docs-invalid-use/symfony

- name: Checkout Symfony Docs repo
run: git clone https://github.com/symfony/symfony-docs .github/workflows/docs-invalid-use/docs

- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: composer-${{ runner.os }}-7.4-${{ hashFiles('composer.*') }}
restore-keys: |
composer-${{ runner.os }}-7.4-
composer-${{ runner.os }}-
composer-

- name: Download dependencies
run: |
composer install --no-interaction --optimize-autoloader
cd .github/workflows/docs-invalid-use
composer update --prefer-stable --no-interaction --optimize-autoloader
cd symfony
composer update --prefer-stable --no-interaction --optimize-autoloader

- name: Verify docs
run: |
cd .github/workflows/docs-invalid-use
./run.php `pwd`/docs `pwd`/symfony > output.txt
cat output.txt

- name: Open issue
env:
GITHUB_TOKEN: ${{ secrets.CARSONPROD_GITHUB_TOKEN }}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be added as a secret

run: |
if [ ! -s .github/workflows/docs-invalid-use/output.txt ]; then
echo "No issues to report"
exit 0
fi

echo -e "I've found some pages that have invalid class references. This is a list of use statements that I couldn't find the correct class to: \n\n\`\`\`" > issue.txt
cat .github/workflows/docs-invalid-use/output.txt >> issue.txt
echo -e "\n\`\`\`\n\nCould someone please verify these?" >> issue.txt
bin/console app:issue:open symfony/symfony-docs "Invalid use statements" `pwd`/issue.txt
24 changes: 24 additions & 0 deletions .github/workflows/docs-invalid-use/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "tobias/docs-checker",
"type": "project",
"authors": [
{
"name": "Nyholm",
"email": "tobias.nyholm@gmail.com"
}
],
"require": {
"symfony/filesystem": "^5.1",
"symfony/finder": "^5.1",
"symfony/phpunit-bridge": "^5.1",
"phpunit/phpunit": "^9.4",
"defuse/php-encryption": "^2.2",
"sensiolabs/ansi-to-html": "^1.2",
"twig/twig": "^3.0",
"symfony/mercure": "^0.4.0",
"lcobucci/jwt": "^3.3",
"symfony/psr-http-message-bridge": "^2.0",
"ramsey/uuid": "^4.1",
"twig/cssinliner-extra": "^3.0"
}
}
89 changes: 89 additions & 0 deletions .github/workflows/docs-invalid-use/run.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/usr/bin/env php
<?php

if ($argc !== 3) {
echo "./docs-invalid-use.php path-to-docs path-to-symfony\n";
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo

exit(1);
}

// Input
$docs = $argv[1];
$symfony = $argv[2];

$autoload = [
__DIR__.'/vendor/autoload.php',
$symfony.'/vendor/autoload.php',
];

foreach ($autoload as $autoloadPHP) {
if (!file_exists($autoloadPHP)) {
echo "File $autoloadPHP does not exist.\nMake sure to run 'composer update'\n";
exit(1);
}
require $autoloadPHP;
}

use Symfony\Component\Finder\Finder;
$finder = new Finder();
$finder->in($docs)->name('*.rst');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to include rst.inc files too


$blocklist = getBlocklist();
$count = 0;
foreach ($finder as $file) {
$contents = $file->getContents();
$matches = [];
if (preg_match_all('|^ +use (.*\\\.*); *?$|im', $contents, $matches)) {
foreach ($matches[1] as $class) {
if (substr($class, 0, 3) === 'App' || substr($class, 0, 4) === 'Acme') {
continue;
}

if (false !== $pos = strpos($class, ' as ')) {
$class = substr($class, 0, $pos);
}

if (false !== $pos = strpos($class, 'function ')) {
continue;
}

if (in_array($class, $blocklist)) {
continue;
}

$explode = explode('\\', $class);
if (count($explode) === 3 && $explode[0] === 'Symfony' && $explode[1] === 'Component') {
continue;
}

if (!class_exists($class) && !interface_exists($class) && !trait_exists($class)) {
$count++;
echo $file->getRelativePath().'/'.$file->getFilename(). ' - '.$class. PHP_EOL;
}
}
}
}

if ($count === 0) {
error_log("We found nothing\n");
}

exit(0);

function getBlocklist(): array
{
return [
'Doctrine\ORM\Mapping',
'Symfony\Component\Validator\Constraints',
'Simplex\StringResponseListener',
'Calendar\Model\LeapYear',
'Symfony\Component\Security\Core\Validator\Constraints',
'Simplex\Framework',
'Calendar\Controller\LeapYearController',
'ApiPlatform\Core\Annotation\ApiResource',
'Other\Qux',
'Doctrine\Bundle\FixturesBundle\Fixture',
'Vendor\DependencyClass',
'Example\Namespace\YourAwesomeCoolClass',
'Your\Transport\YourTransportFactory',
];
}
64 changes: 64 additions & 0 deletions src/Command/OpenIssueCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace App\Command;

use App\Api\Issue\IssueApi;
use App\Service\RepositoryProvider;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Open or update issues.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class OpenIssueCommand extends Command
{
protected static $defaultName = 'app:issue:open';
private $issueApi;
private $repositoryProvider;

public function __construct(RepositoryProvider $repositoryProvider, IssueApi $issueApi)
{
parent::__construct();
$this->issueApi = $issueApi;
$this->repositoryProvider = $repositoryProvider;
}

protected function configure()
{
$this->addArgument('repository', InputArgument::REQUIRED, 'The full name to the repository, eg symfony/symfony-docs');
$this->addArgument('title', InputArgument::REQUIRED, 'The title of the issue');
$this->addArgument('file', InputArgument::REQUIRED, 'The path to the issue body text file');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
/** @var string $repositoryName */
$repositoryName = $input->getArgument('repository');
$repository = $this->repositoryProvider->getRepository($repositoryName);
if (null === $repository) {
$output->writeln('Repository not configured');

return 1;
}

/** @var string $title */
$title = $input->getArgument('title');
/** @var string $filePath */
$filePath = $input->getArgument('file');

$body = file_get_contents($filePath);
if (false === $body) {
return 1;
}

$this->issueApi->open($repository, $title, $body, ['help wanted']);

return 0;
}
}