<commit_before><?php

/*
 * This file is part of the Gush.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Gush\Command;

use Github\Client;
use Gush\Factory;
use Symfony\Component\Console\Helper\DialogHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Yaml;

/**
 * Configure the settings needed to run the Commands
 *
 * @author <NAME> <<EMAIL>>
 */
class ConfigureCommand extends BaseCommand
{
    /**
     * @var \Gush\Config $config
     */
    private $config;

    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this
            ->setName('configure')
            ->setDescription('Configure the github credentials and the cache folder')
        ;
    }

    /**
     * {@inheritdoc}
     */
    protected function initialize(InputInterface $input, OutputInterface $output)
    {
        $this->config = Factory::createConfig();
    }

    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $filename = $this->config->get('home').'/.gush.yml';

        $yaml = new Yaml();
        $content = ['parameters' => $this->config->raw()];

        @unlink($filename);
        if (!@file_put_contents($filename, $yaml->dump($content), 0644)) {
            $output->writeln('<error>It could not save file.</error>');
        }

        $output->writeln('<info>Configuration saved successfully.</info>');
    }

    /**
     * {@inheritdoc}
     */
    protected function interact(InputInterface $input, OutputInterface $output)
    {
        $isAuthenticated = false;
        $username = null;
        $password = <PASSWORD>;

        /** @var DialogHelper $dialog */
        $dialog = $this->getHelper('dialog');

        $validator = function ($field) {
            if (empty($field)) {
                throw new \InvalidArgumentException('The field cannot be empty.');
            }

            return $field;
        };

        while (!$isAuthenticated) {
            $output->writeln('<comment>Insert your github credentials:</comment>');
            $username = $dialog->askAndValidate(
                $output,
                'username: ',
                $validator
            );
            $password = $dialog->askHiddenResponseAndValidate(
                $output,
                'password: ',
                $validator
            );

            try {
                $isAuthenticated = $this->isGithubCredentialsValid($username, $password);
            } catch (\Exception $e) {
                $output->writeln("<error>{$e->getMessage()}</error>");
            }
        }

        $cacheDir = $dialog->askAndValidate(
            $output,
            "Cache folder [{$this->config->get('cache-dir')}]: ",
            function ($dir) {
                if (!is_dir($dir)) {
                    throw new \InvalidArgumentException('The folder is does not exist.');
                }

                if (!is_writable($dir)) {
                    throw new \InvalidArgumentException('The folder is not writable.');
                }

                return $dir;
            },
            false,
            $this->config->get('cache-dir')
        );

        $this->config->merge(
            array(
                'cache-dir' => $cacheDir,
                'github' => array(
                    'username' => $username,
                    'password' => $password
                )
            )
        );
    }

    /**
     * Validates if the credentials are valid
     *
     * @param string $username
     * @param string $password
     * @return Boolean
     */
    private function isGithubCredentialsValid($username, $password)
    {
        if (!isset($client)) {
            $client = new Client();
        }

        $client->authenticate($username, $password, Client::AUTH_HTTP_PASSWORD);

        return (bool)count($client->api('authorizations')->all());
    }
}
<commit_msg>Fixed bug with the github authentication validation<commit_after><?php

/*
 * This file is part of the Gush.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Gush\Command;

use Github\Client;
use Gush\Factory;
use Symfony\Component\Console\Helper\DialogHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Yaml;

/**
 * Configure the settings needed to run the Commands
 *
 * @author <NAME> <<EMAIL>>
 */
class ConfigureCommand extends BaseCommand
{
    /**
     * @var \Gush\Config $config
     */
    private $config;

    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this
            ->setName('configure')
            ->setDescription('Configure the github credentials and the cache folder')
        ;
    }

    /**
     * {@inheritdoc}
     */
    protected function initialize(InputInterface $input, OutputInterface $output)
    {
        $this->config = Factory::createConfig();
    }

    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $filename = $this->config->get('home').'/.gush.yml';

        $yaml = new Yaml();
        $content = ['parameters' => $this->config->raw()];

        @unlink($filename);
        if (!@file_put_contents($filename, $yaml->dump($content), 0644)) {
            $output->writeln('<error>It could not save file.</error>');
        }

        $output->writeln('<info>Configuration saved successfully.</info>');
    }

    /**
     * {@inheritdoc}
     */
    protected function interact(InputInterface $input, OutputInterface $output)
    {
        $isAuthenticated = false;
        $username = null;
        $password = <PASSWORD>;

        /** @var DialogHelper $dialog */
        $dialog = $this->getHelper('dialog');

        $validator = function ($field) {
            if (empty($field)) {
                throw new \InvalidArgumentException('The field cannot be empty.');
            }

            return $field;
        };

        while (!$isAuthenticated) {
            $output->writeln('<comment>Insert your github credentials:</comment>');
            $username = $dialog->askAndValidate(
                $output,
                'username: ',
                $validator
            );
            $password = $dialog->askHiddenResponseAndValidate(
                $output,
                'password: ',
                $validator
            );

            try {
                $isAuthenticated = $this->isGithubCredentialsValid($username, $password);
            } catch (\Exception $e) {
                $output->writeln("<error>{$e->getMessage()}</error>");
            }
        }

        $cacheDir = $dialog->askAndValidate(
            $output,
            "Cache folder [{$this->config->get('cache-dir')}]: ",
            function ($dir) {
                if (!is_dir($dir)) {
                    throw new \InvalidArgumentException('The folder is does not exist.');
                }

                if (!is_writable($dir)) {
                    throw new \InvalidArgumentException('The folder is not writable.');
                }

                return $dir;
            },
            false,
            $this->config->get('cache-dir')
        );

        $this->config->merge(
            array(
                'cache-dir' => $cacheDir,
                'github' => array(
                    'username' => $username,
                    'password' => $password
                )
            )
        );
    }

    /**
     * Validates if the credentials are valid
     *
     * @param string $username
     * @param string $password
     * @return Boolean
     */
    private function isGithubCredentialsValid($username, $password)
    {
        if (!isset($client)) {
            $client = new Client();
        }

        $client->authenticate($username, $password, Client::AUTH_HTTP_PASSWORD);

        return is_array($client->api('authorizations')->all());
    }
}

{'fraction_non_alphanumeric': 0.13683065393448568, 'fraction_numerical': 0.0009670010878762239, 'mean_word_length': 1.8037953236191122, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 97, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5921115', 'n_tokens_mistral': 2229, 'n_tokens_neox': 2153, 'n_words': 490}