diff --git a/.gitignore b/.gitignore index 5708e16..6a78056 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ composer.lock vendor/* +.idea/* diff --git a/README.md b/README.md index feb4895..c90dccb 100644 --- a/README.md +++ b/README.md @@ -1,32 +1,31 @@ # DiffCS -A tool to perform PSR-2 check into your pull requests on Github. +A tool to perform code sniffer checks of your pull requests on Github. ## How To Install -Through **Composer**: +You can grab a copy of marcelsud/diffcs in either of the following ways: -``` -composer global require "marcelsud/diffcs":"dev-master" -``` +### As a phar (recommended) -We suggest you to use `global` requirement, so you can use for all projects. - -Or cloning this project: +You can simply download a pre-compiled and ready-to-use version as a Phar to any directory. Simply download the latest diffcs.phar file from our [releases page](https://github.com/marcelsud/diffcs/releases): ``` -git clone git@github.com:marcelsud/diffcs.git +curl -LO https://github.com/marcelsud/diffcs/releases/download/v0.2.0/diffcs.phar +php diffcs.phar --help ``` -Once it's done, go to the root folder and execute: +Optionally you can install it globally by adding it to your bin folder: ``` -composer install +chmod +x diffcs.phar +mv diffcs.phar /usr/local/bin/diffcs ``` -Then, run the following command to use this tool through your `bin` folder: +### Via composer: ``` +composer global require "marcelsud/diffcs":"dev-master" sudo ln -nfs ~/.composer/vendor/bin/diffcs /usr/local/bin/diffcs ``` @@ -48,16 +47,27 @@ diffcs symfony/symfony 13342 ### For private repositories: +#### Authenticate with username and password Execute following command: `diffcs / --github-user=`, where: - `` is your Github username. +- the password will be asked afterwards and is only required check private repositories. **Example**: ``` -diffcs symfony/symfony 13342 --github-user=seuusuario +diffcs symfony/symfony 13342 --github-user=yourusername ``` -Once you run this command, provide your Github password. **The password is only required to authenticate your credentials into the private repository.** +#### Authenticate with Github token +Execute following command: `diffcs / --github-token=`, where: + +- you can generate the `` in [your Github account settings](https://github.com/settings/tokens/new?scopes=repo&description=Diffcs%20token)). + +**Example**: + +``` +diffcs symfony/symfony 13342 --github-token=256199c24f9132f84e9bb06271ff65a3176a2f05 +``` -![Image](https://41.media.tumblr.com/df1e6041b827dc067d1c4c16ffb3df05/tumblr_nldnzr0w011sr73oio1_1280.png) +![Image](output.png) diff --git a/composer.json b/composer.json index f68399f..533f9a8 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,8 @@ "require": { "knplabs/github-api": "~1.2", "league/flysystem": "0.5.*", - "symfony/console": "v2.6.3" + "symfony/console": "v2.6.3", + "squizlabs/php_codesniffer": "~3.1" }, "authors": [ { diff --git a/output.png b/output.png new file mode 100644 index 0000000..487441d Binary files /dev/null and b/output.png differ diff --git a/src/.idea/scopes/scope_settings.xml b/src/.idea/scopes/scope_settings.xml deleted file mode 100644 index 922003b..0000000 --- a/src/.idea/scopes/scope_settings.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - \ No newline at end of file diff --git a/src/Melody/Diffcs/Command/DiffcsCommand.php b/src/Melody/Diffcs/Command/DiffcsCommand.php index d3ad416..7c2a83a 100644 --- a/src/Melody/Diffcs/Command/DiffcsCommand.php +++ b/src/Melody/Diffcs/Command/DiffcsCommand.php @@ -25,6 +25,10 @@ class DiffcsCommand extends Command * @var string */ const REPOSITORY_ARGUMENT = "repository"; + /** + * @var string + */ + const CODE_STANDARD_OPTION = "code-standard"; /** * @var string */ @@ -52,6 +56,13 @@ protected function configure() InputArgument::REQUIRED, 'The pull request id' ) + ->addOption( + self::CODE_STANDARD_OPTION, + 'cs', + InputOption::VALUE_OPTIONAL, + 'The github token to access private repositories', + 'PSR2' + ) ->addOption( self::GITHUB_TOKEN_OPTION, null, @@ -80,6 +91,8 @@ protected function execute(InputInterface $input, OutputInterface $output) $githubUser = $input->getOption(self::GITHUB_USER_OPTION); $githubPass = false; + $codeStandard = $input->getOption(self::CODE_STANDARD_OPTION); + if (!empty($githubUser)) { $helper = $this->getHelper('question'); @@ -101,15 +114,42 @@ protected function execute(InputInterface $input, OutputInterface $output) $output, $owner, $repository, + $codeStandard, $githubToken, $githubUser, $githubPass ); + $output->writeln( + ' ' + ); + $output->writeln( + ' PHP DIFF CS (CODE STANDARD) ' + ); + $output->writeln( + ' ' + ); + + $output->writeln(''); + $output->writeln( + 'Project: ' + .$input->getArgument(self::REPOSITORY_ARGUMENT) + .'' + ); + $output->writeln( + 'Pull Request: #'.$pullRequestId.'' + ); + $output->writeln( + 'Code Standard: '.$codeStandard.'' + ); + $output->writeln(''); + try { $results = $executor->execute($pullRequestId); } catch (\Exception $e) { - die("ERROR: " . $e->getMessage() . PHP_EOL); + $output->writeln('ERROR: '.$e->getMessage()); + + die(); } if (count($results)) { diff --git a/src/Melody/Diffcs/DiffcsApplication.php b/src/Melody/Diffcs/DiffcsApplication.php index 8dfee72..2427a0d 100644 --- a/src/Melody/Diffcs/DiffcsApplication.php +++ b/src/Melody/Diffcs/DiffcsApplication.php @@ -8,7 +8,7 @@ /** * Class DiffcsApplication - * + * * @author Marcelo Santos */ class DiffcsApplication extends Application @@ -50,4 +50,4 @@ public function getDefinition() return $inputDefinition; } -} \ No newline at end of file +} diff --git a/src/Melody/Diffcs/Executor.php b/src/Melody/Diffcs/Executor.php index 2545229..1190fca 100644 --- a/src/Melody/Diffcs/Executor.php +++ b/src/Melody/Diffcs/Executor.php @@ -37,11 +37,16 @@ class Executor * @var type */ protected $progress; + /** + * @var string + */ + protected $codeStandard; /** * @param \Symfony\Component\Console\Output\OutputInterface $output * @param string $owner * @param string $repository + * @param string $codeStandard * @param bool $githubToken Optional. * @param bool $githubUser Optional. * @param bool $githubPass Optional. @@ -50,6 +55,7 @@ public function __construct( $output, $owner, $repository, + $codeStandard, $githubToken = false, $githubUser = false, $githubPass = false @@ -60,6 +66,7 @@ public function __construct( $this->githubUser = $githubUser; $this->githubPass = $githubPass; $this->repository = $repository; + $this->codeStandard = $codeStandard; $this->client = new \Github\Client(); $this->filesystem = new Filesystem(new Adapter(sys_get_temp_dir())); } @@ -89,9 +96,9 @@ public function execute($pullRequestId) $this->repository, $pullRequestId ); - - $downloadedFiles = $this->downloadFiles($files, $pullRequest["head"]["sha"]); + $downloadedFiles = $this->downloadFiles($files, $pullRequest["head"]["sha"]); + return $this->runCodeSniffer($downloadedFiles); } @@ -126,16 +133,13 @@ public function authenticateWithPassword() */ public function downloadFiles($files, $commitId) { - $progress = new ProgressBar($this->output, count($files)); - $progress->setProgressCharacter('|'); - $progress->start(); $downloadedFiles = []; - + foreach ($files as $file) { - if (!preg_match('/src\/.*\.php$/', $file['filename'] || $file['status'] === "removed")) { + if (!preg_match('/.*\.php$/', $file['filename']) || $file['status'] === "removed") { continue; } - + $fileContent = $this->client->api('repo')->contents()->download( $this->owner, $this->repository, @@ -147,11 +151,8 @@ public function downloadFiles($files, $commitId) $this->filesystem->put($file, $fileContent); $downloadedFiles[] = $file; - $progress->advance(); } - $progress->finish(); - return $downloadedFiles; } @@ -161,25 +162,35 @@ public function downloadFiles($files, $commitId) */ public function runCodeSniffer($downloadedFiles) { + $progress = new ProgressBar($this->output, count($downloadedFiles)); + $progress->setProgressCharacter('|'); + $progress->start(); + $outputs = []; - + foreach ($downloadedFiles as $file) { - if (!preg_match('/src\/.*\.php$/', $file)) { + if (!preg_match('/.*\.php$/', $file)) { continue; } $command = sprintf( - "phpcs %s/%s --standard=PSR2", + "vendor/bin/phpcs %s/%s --standard=%s", sys_get_temp_dir(), - $file + $file, + $this->codeStandard ); $output = shell_exec($command); + $output = str_replace('/tmp/tmp/', '', $output); if (!empty($output)) { $outputs[] = $output; } + + $progress->advance(); } + + $progress->finish(); return $outputs; }