protected function githubPullRequest(Project $project, array $payload)
    {
        // We only want to know about open pull requests:
        if (!in_array($payload['action'], array('opened', 'synchronize', 'reopened'))) {
            return array('status' => 'ok');
        }

        $headers = array();
        $token = \b8\Config::getInstance()->get('phpci.github.token');

        if (!empty($token)) {
            $headers[] = 'Authorization: token ' . $token;
        }

        $url    = $payload['pull_request']['commits_url'];
        $http   = new \b8\HttpClient();
        $http->setHeaders($headers);
        $response = $http->get($url);

        // Check we got a success response:
        if (!$response['success']) {
            throw new Exception('Could not get commits, failed API request.');
        }

        $results = array();
        $status = 'failed';
        foreach ($response['body'] as $commit) {
            // Skip all but the current HEAD commit ID:
            $id = $commit['sha'];
            if ($id != $payload['pull_request']['head']['sha']) {
                $results[$id] = array('status' => 'ignored', 'message' => 'not branch head');
                continue;
            }

            try {
                $branch = str_replace('refs/heads/', '', $payload['pull_request']['base']['ref']);
                $committer = $commit['commit']['author']['email'];
                $message = $commit['commit']['message'];

                $remoteUrlKey = $payload['pull_request']['head']['repo']['private'] ? 'ssh_url' : 'clone_url';

                $extra = array(
                    'build_type' => 'pull_request',
                    'pull_request_id' => $payload['pull_request']['id'],
                    'pull_request_number' => $payload['number'],
                    'remote_branch' => $payload['pull_request']['head']['ref'],
                    'remote_url' => $payload['pull_request']['head']['repo'][$remoteUrlKey],
                );

                $results[$id] = $this->createBuild($project, $id, $branch, $committer, $message, false, $extra);
                $status = 'ok';
            } catch (Exception $ex) {
                $results[$id] = array('status' => 'failed', 'error' => $ex->getMessage());
            }
        }

        return array('status' => $status, 'commits' => $results);
    }
protected function githubPullRequest(Project $project, array $payload)
    {
        // We only want to know about open pull requests:
        if (!in_array($payload['action'], array('opened', 'synchronize', 'reopened'))) {
            return array('status' => 'ok');
        }

        $headers = array();
        $token = \b8\Config::getInstance()->get('phpci.github.token');

        if (!empty($token)) {
            $headers[] = 'Authorization: token ' . $token;
        }

        $url    = $payload['pull_request']['commits_url'];
        $http   = new \b8\HttpClient();
        $http->setHeaders($headers);
        $response = $http->get($url);

        // Check we got a success response:
        if (!$response['success']) {
            throw new Exception('Could not get commits, failed API request.');
        }

        $results = array();
        $status = 'failed';
        foreach ($response['body'] as $commit) {
            // Skip all but the current HEAD commit ID:
            $id = $commit['sha'];
            if ($id != $payload['pull_request']['head']['sha']) {
                $results[$id] = array('status' => 'ignored', 'message' => 'not branch head');
                continue;
            }

            try {
                $branch = str_replace('refs/heads/', '', $payload['pull_request']['base']['ref']);
                $committer = $commit['commit']['author']['email'];
                $message = $commit['commit']['message'];

                $remoteUrlKey = $payload['pull_request']['head']['repo']['private'] ? 'ssh_url' : 'clone_url';

                $extra = array(
                    'build_type' => 'pull_request',
                    'pull_request_id' => $payload['pull_request']['id'],
                    'pull_request_number' => $payload['number'],
                    'remote_branch' => $payload['pull_request']['head']['ref'],
                    'remote_url' => $payload['pull_request']['head']['repo'][$remoteUrlKey],
                );

                $results[$id] = $this->createBuild($project, $id, $branch, $committer, $message, false, $extra);
                $status = 'ok';
            } catch (Exception $ex) {
                $results[$id] = array('status' => 'failed', 'error' => $ex->getMessage());
            }
        }

        return array('status' => $status, 'commits' => $results);
    }
protected function githubPullRequest ( Project $ project , array $ payload ) { // We only want to know about open pull requests: if ( ! in_array ( $ payload [ 'action' ] , array ( 'opened' , 'synchronize' , 'reopened' ) ) ) { return array ( 'status' => 'ok' ) ; } $ headers = array ( ) ; $ token = \ b8 \ Config :: getInstance ( ) -> get ( 'phpci.github.token' ) ; if ( ! empty ( $ token ) ) { $ headers [ ] = 'Authorization: token ' . $ token ; } $ url = $ payload [ 'pull_request' ] [ 'commits_url' ] ; $ http = new \ b8 \ HttpClient ( ) ; $ http -> setHeaders ( $ headers ) ; $ response = $ http -> get ( $ url ) ; // Check we got a success response: if ( ! $ response [ 'success' ] ) { throw new Exception ( 'Could not get commits, failed API request.' ) ; } $ results = array ( ) ; $ status = 'failed' ; foreach ( $ response [ 'body' ] as $ commit ) { // Skip all but the current HEAD commit ID: $ id = $ commit [ 'sha' ] ; if ( $ id != $ payload [ 'pull_request' ] [ 'head' ] [ 'sha' ] ) { $ results [ $ id ] = array ( 'status' => 'ignored' , 'message' => 'not branch head' ) ; continue ; } try { $ branch = str_replace ( 'refs/heads/' , '' , $ payload [ 'pull_request' ] [ 'base' ] [ 'ref' ] ) ; $ committer = $ commit [ 'commit' ] [ 'author' ] [ 'email' ] ; $ message = $ commit [ 'commit' ] [ 'message' ] ; $ remoteUrlKey = $ payload [ 'pull_request' ] [ 'head' ] [ 'repo' ] [ 'private' ] ? 'ssh_url' : 'clone_url' ; $ extra = array ( 'build_type' => 'pull_request' , 'pull_request_id' => $ payload [ 'pull_request' ] [ 'id' ] , 'pull_request_number' => $ payload [ 'number' ] , 'remote_branch' => $ payload [ 'pull_request' ] [ 'head' ] [ 'ref' ] , 'remote_url' => $ payload [ 'pull_request' ] [ 'head' ] [ 'repo' ] [ $ remoteUrlKey ] , ) ; $ results [ $ id ] = $ this -> createBuild ( $ project , $ id , $ branch , $ committer , $ message , false , $ extra ) ; $ status = 'ok' ; } catch ( Exception $ ex ) { $ results [ $ id ] = array ( 'status' => 'failed' , 'error' => $ ex -> getMessage ( ) ) ; } } return array ( 'status' => $ status , 'commits' => $ results ) ; }
Handle the payload when Github sends a Pull Request webhook.

@param Project $project
@param array $payload
Handle the payload when Github sends a Pull Request webhook .