<commit_before><?php
namespace alanmanderson\clever_deploy\Connectors;

use alanmanderson\clever_deploy\Result;

class BitbucketConnector implements RepositoryConnectorInterface{
    private $localRoot;
    private $branch;
    private $payload;
    private $event;
    private $acceptedEvents;
    
    public function __construct($localRoot, $branch) {
        $this->localRoot = $localRoot;
        $this->branch = $branch;
        $this->payload = file_get_contents('php://input');
        $headers = getallheaders();
        $headers = array_change_key_case($headers);
        $this->event = $headers['x-event-key'];
        $this->acceptedEvents = ['repo:push'];
    }
    
    public function deploy(array $cmds = []) {
        header('Content-Type: application/json');
        $result = new Result();
        if (!$this->verify()) {
            $result->catchException(new \Exception('Could not verify request', 403));
            return $result;
        }
        
        if (!in_array($this->event, $this->acceptedEvents)) {
            $result->catchException(new \Exception('Invalid event', 400));
            return $result;
        }
        
        $data = json_decode($this->payload);
        if (empty($data)) {
            $result->catchException(new \Exception('Invalid request', 400));
            return $result;
        }
        
        $relevantChanges = false;
        foreach($data->data->push->changes as $change){
            if (isset($change->new->name) && $change->new->name == $this->branch){
                $relevantChanges = true;
                break;
            }
        }
        if ($relevantChanges) {
            $output = array();
            if (file_exists($this->localRoot)) {
                array_unshift(
                    $cmds,
                    "cd {$this->localRoot}",
                    "git fetch origin {$this->branch}",
                    "git merge origin/{$this->branch} --no-edit"
                );
                foreach ($cmds as $cmd) {
                    $output[] = $cmd . " " . shell_exec($cmd);
                }
                $result->setOutput($output);
                $result->setWasDeployed(true);
            }
        }
        return $result;
    }
    
    public function verify(){
        return in_array($_SERVER['REMOTE_ADDR'], [
            '192.168.3.11',
            '172.16.31.10',
            '192.168.3.11',
            '172.16.17.32',
            '172.16.31.10'
        ]);
        return false;
    }
}<commit_msg>Make a more robust verification method<commit_after><?php
namespace alanmanderson\clever_deploy\Connectors;

use alanmanderson\clever_deploy\Result;

class BitbucketConnector implements RepositoryConnectorInterface{
    private $localRoot;
    private $branch;
    private $repoSecret;
    private $hookUuid;
    private $payload;
    private $event;
    private $acceptedEvents;
    
    public function __construct($localRoot, $branch, $repoSecret) {
        $this->localRoot = $localRoot;
        $this->branch = $branch;
        $this->repoSecret = $repoSecret;
        $this->payload = file_get_contents('php://input');
        $headers = getallheaders();
        $headers = array_change_key_case($headers);
        $this->hookUuid = $headers['x-hook-uuid'];
        $this->event = $headers['x-event-key'];
        $this->acceptedEvents = ['repo:push'];
    }
    
    public function deploy(array $cmds = []) {
        header('Content-Type: application/json');
        $result = new Result();
        if (!$this->verify()) {
            $result->catchException(new \Exception('Could not verify request', 403));
            return $result;
        }
        
        if (!in_array($this->event, $this->acceptedEvents)) {
            $result->catchException(new \Exception('Invalid event', 400));
            return $result;
        }
        
        $data = json_decode($this->payload);
        if (empty($data)) {
            $result->catchException(new \Exception('Invalid request', 400));
            return $result;
        }
        
        $relevantChanges = false;
        foreach($data->data->push->changes as $change){
            if (isset($change->new->name) && $change->new->name == $this->branch){
                $relevantChanges = true;
                break;
            }
        }
        if ($relevantChanges) {
            $output = array();
            if (file_exists($this->localRoot)) {
                array_unshift(
                    $cmds,
                    "cd {$this->localRoot}",
                    "git fetch origin {$this->branch}",
                    "git merge origin/{$this->branch} --no-edit"
                );
                foreach ($cmds as $cmd) {
                    $output[] = $cmd . " " . shell_exec($cmd);
                }
                $result->setOutput($output);
                $result->setWasDeployed(true);
            }
        }
        return $result;
    }
    
    public function verify(){
        if ($this->repoSecret == null) return true;
        return $this->repoSecret == $this->hookUuid;
    }
}
{'fraction_non_alphanumeric': 0.14170755642787047, 'fraction_numerical': 0.012365063788027477, 'mean_word_length': 1.766558089033659, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 5, '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': '1543215', 'n_tokens_mistral': 1444, 'n_tokens_neox': 1371, 'n_words': 269}