Skip to content

Commit

Permalink
Added the phpcs as a dependency and removed the tmp folder from the o…
Browse files Browse the repository at this point in the history
…utput
  • Loading branch information
marcelsud committed Sep 30, 2017
1 parent 14f9641 commit 1e82300
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
composer.lock
vendor/*
.idea/*
40 changes: 25 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```

Expand All @@ -48,16 +47,27 @@ diffcs symfony/symfony 13342

### For private repositories:

#### Authenticate with username and password
Execute following command: `diffcs <source>/<project> <pull request id> --github-user=<github username>`, where:

- `<github username>` 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 <source>/<project> <pull request id> --github-token=<github token>`, where:

- you can generate the `<github token>` 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)
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand Down
Binary file added output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 0 additions & 5 deletions src/.idea/scopes/scope_settings.xml

This file was deleted.

42 changes: 41 additions & 1 deletion src/Melody/Diffcs/Command/DiffcsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class DiffcsCommand extends Command
* @var string
*/
const REPOSITORY_ARGUMENT = "repository";
/**
* @var string
*/
const CODE_STANDARD_OPTION = "code-standard";
/**
* @var string
*/
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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');

Expand All @@ -101,15 +114,42 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output,
$owner,
$repository,
$codeStandard,
$githubToken,
$githubUser,
$githubPass
);

$output->writeln(
'<fg=white;bg=cyan;options=bold> </fg=white;bg=cyan;options=bold>'
);
$output->writeln(
'<fg=white;bg=cyan;options=bold> PHP DIFF CS (CODE STANDARD) </fg=white;bg=cyan;options=bold>'
);
$output->writeln(
'<fg=white;bg=cyan;options=bold> </fg=white;bg=cyan;options=bold>'
);

$output->writeln('');
$output->writeln(
'<fg=cyan>Project: <options=bold>'
.$input->getArgument(self::REPOSITORY_ARGUMENT)
.'</options=bold></fg=cyan>'
);
$output->writeln(
'<fg=cyan>Pull Request: <options=bold>#'.$pullRequestId.'</options=bold></fg=cyan>'
);
$output->writeln(
'<fg=cyan>Code Standard: <options=bold>'.$codeStandard.'</options=bold></fg=cyan>'
);
$output->writeln('');

try {
$results = $executor->execute($pullRequestId);
} catch (\Exception $e) {
die("ERROR: " . $e->getMessage() . PHP_EOL);
$output->writeln('<error>ERROR:</error> '.$e->getMessage());

die();
}

if (count($results)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Melody/Diffcs/DiffcsApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

/**
* Class DiffcsApplication
*
*
* @author Marcelo Santos <marcelsud@gmail.com>
*/
class DiffcsApplication extends Application
Expand Down Expand Up @@ -50,4 +50,4 @@ public function getDefinition()

return $inputDefinition;
}
}
}
41 changes: 26 additions & 15 deletions src/Melody/Diffcs/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -50,6 +55,7 @@ public function __construct(
$output,
$owner,
$repository,
$codeStandard,
$githubToken = false,
$githubUser = false,
$githubPass = false
Expand All @@ -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()));
}
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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,
Expand All @@ -147,11 +151,8 @@ public function downloadFiles($files, $commitId)
$this->filesystem->put($file, $fileContent);

$downloadedFiles[] = $file;
$progress->advance();
}

$progress->finish();

return $downloadedFiles;
}

Expand All @@ -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;
}
Expand Down

0 comments on commit 1e82300

Please sign in to comment.