<commit_before><?php  declare(strict_types=1);
namespace Room11\Jeeves\Plugins;

use Amp\Artax\HttpClient;
use Amp\Artax\Response as HttpResponse;
use Room11\Jeeves\Chat\Client\ChatClient;
use Room11\Jeeves\Chat\Message\Command;
use Room11\Jeeves\Storage\KeyValue as KeyValueStore;
use Room11\Jeeves\System\PluginCommandEndpoint;
use Room11\Jeeves\Chat\Room\Room;
use function Amp\repeat;

class Github extends BasePlugin
{
    private $chatClient;
    private $httpClient;
    private $pluginData;
    private $lastKnownStatus = null;

    public function __construct(ChatClient $chatClient, HttpClient $httpClient, KeyValueStore $pluginData)
    {
        $this->chatClient = $chatClient;
        $this->httpClient = $httpClient;
        $this->pluginData = $pluginData;
    }

    public function enableForRoom(Room $room, bool $persist = true)
    {
        repeat(
            function() use ($room) { return $this->checkStatusChange($room); }, 150000
        );
    }

    public function github(Command $command): \Generator {
        $obj = $command->getParameter(0) ?? 'status';

        if ($obj === 'status') {
            return yield from $this->status($command->getRoom());
        } elseif (strpos($obj, '/') === false) {
            return yield from $this->profile($command->getRoom(), $obj);
        } elseif (strpos($obj, '/') === strrpos($obj, '/')) {
            return yield from $this->repo($command->getRoom(), $obj);
        }

        return $this->chatClient->postMessage($command->getRoom(),
            "Usage: !!github [status | <project> | <profile> | <profile>/<repo> ]");
    }

    /**
     * Example:
     *   !!github
     *   !!github status
     *
     * [tag:github-status] good: Everything operating normally. as of 2016-05-25T18:44:58Z
     *
     * @param Command $command
     * @return \Generator
     */
    protected function status(Room $room)
    {
        return $this->postResponse($room, yield from $this->getGithubStatus());
    }

    private function checkStatusChange(Room $room)
    {
        $githubResponse = yield from $this->getGithubStatus();

        if (!$githubResponse) {
            return;
        }

        if (is_null($this->lastKnownStatus)) {
            $this->lastKnownStatus = $githubResponse->status;
            return;
        }

        if ($this->lastKnownStatus !== $githubResponse->status) {
            $this->lastKnownStatus = $githubResponse->status;
            return $this->postResponse($room, $githubResponse);
        }
    }

    private function postResponse(Room $room, $response)
    {
        if (!$response) {
            return $this->chatClient->postMessage($room, "Failed fetching status");
        }

        return $this->chatClient->postMessage(
            $room,
            sprintf(
                "[tag:github-status] **%s**: %s *as of %s*",
                $response->status,
                rtrim($response->body, '.!?'),
                $response->created_on
            )
        );
    }

    private function getGithubStatus()
    {
        /** @var HttpResponse $response */
        $response = yield $this->httpClient->request('https://status.github.com/api/last-message.json');

        if ($response->getStatus() !== 200) {
            return false;
        }

        return json_decode($response->getBody());
    }

    /**
     * Example:
     *   !!github Room-11
     *
     * [tag:github-profile] Organization [Room-11](https://github.com/Room-11): 15 public repos
     *<commit_msg>Everything's OK!<commit_after><?php  declare(strict_types=1);
namespace Room11\Jeeves\Plugins;

use Amp\Artax\HttpClient;
use Amp\Artax\Response as HttpResponse;
use Room11\Jeeves\Chat\Client\ChatClient;
use Room11\Jeeves\Chat\Message\Command;
use Room11\Jeeves\Storage\KeyValue as KeyValueStore;
use Room11\Jeeves\System\PluginCommandEndpoint;
use Room11\Jeeves\Chat\Room\Room;
use function Amp\repeat;

class Github extends BasePlugin
{
    private $chatClient;
    private $httpClient;
    private $pluginData;
    private $lastKnownStatus = null;

    public function __construct(ChatClient $chatClient, HttpClient $httpClient, KeyValueStore $pluginData)
    {
        $this->chatClient = $chatClient;
        $this->httpClient = $httpClient;
        $this->pluginData = $pluginData;
    }

    public function enableForRoom(Room $room, bool $persist = true)
    {
        repeat(
            function() use ($room) { return $this->checkStatusChange($room); }, 150000
        );
    }

    public function github(Command $command): \Generator {
        $obj = $command->getParameter(0) ?? 'status';

        if ($obj === 'status') {
            return yield from $this->status($command->getRoom());
        } elseif (strpos($obj, '/') === false) {
            return yield from $this->profile($command->getRoom(), $obj);
        } elseif (strpos($obj, '/') === strrpos($obj, '/')) {
            return yield from $this->repo($command->getRoom(), $obj);
        }

        return $this->chatClient->postMessage($command->getRoom(),
            "Usage: !!github [status | <project> | <profile> | <profile>/<repo> ]");
    }

    /**
     * Example:
     *   !!github
     *   !!github status
     *
     * [tag:github-status] good: Everything operating normally. as of 2016-05-25T18:44:58Z
     *
     * @param Command $command
     * @return \Generator
     */
    protected function status(Room $room)
    {
        return $this->postResponse($room, yield from $this->getGithubStatus());
    }

    private function checkStatusChange(Room $room)
    {
        $githubResponse = yield from $this->getGithubStatus();

        if (!$githubResponse) {
            return;
        }

        if (is_null($this->lastKnownStatus)) {
            $this->lastKnownStatus = $githubResponse->status;
            return;
        }

        if ($this->lastKnownStatus !== $githubResponse->status) {
            $this->lastKnownStatus = $githubResponse->status;
            return $this->postResponse($room, $githubResponse);
        }
    }

    private function postResponse(Room $room, $response)
    {
        if (!$response) {
            return $this->chatClient->postMessage($room, "Failed fetching status");
        }

        $messageTemplate = '%s *as of %s*';
        
        if ($response->status === 'good') {
            $messageTemplate = '[%s *as of %s*](https://www.youtube.com/watch?v=aJFyWBLeM7Q)';
        }
        
        return $this->chatClient->postMessage(
            $room,
            sprintf(
                "[tag:github-status] **%s**: " . $messageTemplate,
                $response->status,
                rtrim($response->body, '.!?'),
                $response->created_on
            )
        );
    }

    private function getGithubStatus()
    {
        /** @var HttpResponse $response */
        $response = yield $this->httpClient->request('https://status.github.com/api/last-message.json');

        if ($response->getStatus() !== 200) {
            return false;
        }

        return json_decode($response->getBody());
    }

    /**
     * Example:
     *   !!github Room-11
     *
     * [tag:github-profile] Organization [Room-11](https://github.com/Room-11): 15 public repos
     *
{'fraction_non_alphanumeric': 0.1474004424778761, 'fraction_numerical': 0.012582964601769912, 'mean_word_length': 2.339335180055402, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 84, 'https://': 5, 'lorem ipsum': 0, 'www.': 1, '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': '4702286', 'n_tokens_mistral': 2171, 'n_tokens_neox': 2069, 'n_words': 421}