diff --git a/.idea/scopes/scope_settings.xml b/.idea/scopes/scope_settings.xml
deleted file mode 100644
index 922003b..0000000
--- a/.idea/scopes/scope_settings.xml
+++ /dev/null
@@ -1,5 +0,0 @@
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
deleted file mode 100644
index 3b80637..0000000
--- a/.idea/workspace.xml
+++ /dev/null
@@ -1,285 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- true
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 1420813069554
-
- 1420813069554
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/bin/diffcs b/bin/diffcs
index 0393a08..ca247e0 100755
--- a/bin/diffcs
+++ b/bin/diffcs
@@ -68,9 +68,8 @@ if (!defined('COMPOSER_AUTOLOAD_FILE')) {
require COMPOSER_AUTOLOAD_FILE;
-use Melody\Diffcs\Command\DiffcsCommand;
-use Symfony\Component\Console\Application;
-$application = new Application();
-$application->add(new DiffcsCommand);
+use Melody\Diffcs\DiffcsApplication;
+
+$application = new DiffcsApplication();
$application->run();
\ No newline at end of file
diff --git a/src/Melody/Diffcs/Command/DiffcsCommand.php b/src/Melody/Diffcs/Command/DiffcsCommand.php
index 515271b..b30f540 100644
--- a/src/Melody/Diffcs/Command/DiffcsCommand.php
+++ b/src/Melody/Diffcs/Command/DiffcsCommand.php
@@ -8,54 +8,89 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Question\Question;
class DiffcsCommand extends Command
{
+ const PULL_REQUEST_ARGUMENT = "pull-request";
+ const REPOSITORY_ARGUMENT = "repository";
+ const GITHUB_TOKEN_OPTION = "github-token";
+ const GITHUB_USER_OPTION = "github-user";
+
protected function configure()
{
$this
- ->setName('demo:greet')
- ->setDescription('Greet someone')
+ ->setName('diffcs')
+ ->setDescription('Used to run phpcs on pull requests')
->addArgument(
- 'pr',
+ self::REPOSITORY_ARGUMENT,
InputArgument::REQUIRED,
- 'The pull request id'
+ 'The repository name'
)
->addArgument(
- 'repo',
+ self::PULL_REQUEST_ARGUMENT,
InputArgument::REQUIRED,
- 'The repository name'
+ 'The pull request id'
+ )
+ ->addOption(
+ self::GITHUB_TOKEN_OPTION,
+ null,
+ InputOption::VALUE_REQUIRED,
+ 'The github token to access private repositories'
)
->addOption(
- 'token',
+ self::GITHUB_USER_OPTION,
null,
- InputOption::VALUE_NONE,
- 'The access token'
+ InputOption::VALUE_REQUIRED,
+ 'The github username to access private repositories'
)
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
- $pullRequestId = $input->getArgument('pr');
+ $pullRequestId = $input->getArgument(self::PULL_REQUEST_ARGUMENT);
- list($owner, $repository) = explode("/", $input->getArgument("repo"));
- $accessToken = $input->getOption('token');
+ list($owner, $repository) = explode("/", $input->getArgument(self::REPOSITORY_ARGUMENT));
+ $githubToken = $input->getOption(self::GITHUB_TOKEN_OPTION);
+ $githubUser = $input->getOption(self::GITHUB_USER_OPTION);
+ $githubPass = false;
- $diffcs = new Diffcs($owner, $repository, $accessToken);
+ if (!empty($githubUser)) {
+ $helper = $this->getHelper('question');
+
+ $question = new Question('Password:');
+ $question->setHidden(true);
+ $question->setHiddenFallback(false);
+ $question->setValidator(function ($value) {
+ if (trim($value) == '') {
+ throw new \Exception('The password can not be empty');
+ }
+
+ return $value;
+ });
+
+ $githubPass = $helper->ask($input, $output, $question);
+ }
+
+ $executor = new Executor(
+ $owner,
+ $repository,
+ $githubToken,
+ $githubUser,
+ $githubPass
+ );
try {
- $results = $diffcs->execute($pullRequestId);
+ $results = $executor->execute($pullRequestId);
} catch (\Exception $e) {
die("ERROR: " . $e->getMessage() . PHP_EOL);
}
-
- if (count($results) > 0) {
+ if (count($results)) {
foreach ($results as $result) {
$output->writeln($result);
}
}
}
}
-
diff --git a/src/Melody/Diffcs/DiffcsApplication.php b/src/Melody/Diffcs/DiffcsApplication.php
new file mode 100644
index 0000000..9f17862
--- /dev/null
+++ b/src/Melody/Diffcs/DiffcsApplication.php
@@ -0,0 +1,48 @@
+setArguments();
+
+ return $inputDefinition;
+ }
+}
\ No newline at end of file
diff --git a/src/Melody/Diffcs/Diffcs.php b/src/Melody/Diffcs/Executor.php
similarity index 69%
rename from src/Melody/Diffcs/Diffcs.php
rename to src/Melody/Diffcs/Executor.php
index 0ae05b6..25e64e1 100644
--- a/src/Melody/Diffcs/Diffcs.php
+++ b/src/Melody/Diffcs/Executor.php
@@ -5,7 +5,7 @@
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Local as Adapter;
-class Diffcs
+class Executor
{
protected $owner;
protected $repository;
@@ -13,10 +13,12 @@ class Diffcs
protected $client;
protected $filesystem;
- public function __construct($owner, $repository, $accessToken = false)
+ public function __construct($owner, $repository, $githubToken = false, $githubUser = false, $githubPass = false)
{
$this->owner = $owner;
- $this->accessToken = $accessToken;
+ $this->githubToken = $githubToken;
+ $this->githubUser = $githubUser;
+ $this->githubPass = $githubPass;
$this->repository = $repository;
$this->client = new \Github\Client();
$this->filesystem = new Filesystem(new Adapter(sys_get_temp_dir()));
@@ -24,19 +26,32 @@ public function __construct($owner, $repository, $accessToken = false)
public function execute($pullRequestId)
{
- if ($this->accessToken) {
- $this->authenticate();
+ if ($this->githubToken) {
+ $this->authenticateWithToken();
}
- $pullRequest = $this->client->api('pull_request')->show($this->owner, $this->repository, $pullRequestId);
- $files = $this->client->api('pull_request')->files($this->owner, $this->repository, $pullRequestId);
+ if ($this->githubUser) {
+ $this->authenticateWithPassword();
+ }
+
+ $pullRequest = $this->client->api('pull_request')->show(
+ $this->owner,
+ $this->repository,
+ $pullRequestId
+ );
+
+ $files = $this->client->api('pull_request')->files(
+ $this->owner,
+ $this->repository,
+ $pullRequestId
+ );
$downloadedFiles = $this->downloadFiles($files, $pullRequest["head"]["sha"]);
return $this->runCodeSniffer($downloadedFiles);
}
- public function authenticate()
+ public function authenticateWithToken()
{
$this->client->authenticate(
$this->accessToken,
@@ -45,6 +60,15 @@ public function authenticate()
);
}
+ public function authenticateWithPassword()
+ {
+ $this->client->authenticate(
+ $this->githubUser,
+ $this->githubPass,
+ \Github\Client::AUTH_HTTP_PASSWORD
+ );
+ }
+
public function downloadFiles($files, $commitId)
{
$downloadedFiles = [];