protected function getRelatedHtmlUrl($subject)
 {
     /** @var Response $response */
     $response = GitHub::connection()->getHttpClient()->get($subject['latest_comment_url']);
     $subjectData = ResponseMediator::getContent($response);
     return $subjectData['html_url'];
 }
Пример #2
0
 /**
  * Retrieves a resource information.
  *
  * @param  string $resource The name of the resource. Should be a key in $resources.
  * @return array
  * @throws InvalidArgumentException If the requested resource is not in the list of valid ones.
  */
 public function get($resource)
 {
     if (!isset($this->resources[$resource])) {
         throw new InvalidArgumentException('The requested resource is not in the list');
     }
     return ResponseMediator::getContent($this->client->get($this->resources[$resource]));
 }
Пример #3
0
 /**
  * undocumented function
  *
  * @return void
  * @author 
  **/
 public function takeCare()
 {
     $req = Request::instance();
     $token = $this->client->getAccessToken('authorizationCode', ['code' => $req->get('code')]);
     $github = new \Github\Client();
     $github->authenticate($token->accessToken, 'http_token');
     $response = $github->getHttpClient()->get('user');
     $user = \Github\HttpClient\Message\ResponseMediator::getContent($response);
     $verifiedEmails = $this->getVerifiedEmails($token->accessToken);
     $email = $this->getPrimaryEmail($verifiedEmails);
     $result['uid'] = $user['id'];
     $result['email'] = $email;
     $result['username'] = $user['login'];
     $result['url'] = $user['html_url'];
     $result['location'] = isset($user['location']) ? $user['location'] : null;
     $fresult = $this->findUser($result['email']);
     if ($fresult['found']) {
         $fuser = $fresult['user'];
         $result['first_name'] = $fuser->first_name;
         $result['last_name'] = $fuser->last_name;
     } else {
         if (isset($user['name'])) {
             $name = explode(' ', $user['name']);
             $result['first_name'] = $name[0];
             $result['last_name'] = isset($name[1]) ? $name[1] : $name[0];
         } else {
             $result['first_name'] = $result['last_name'] = $result['username'];
         }
     }
     $result['access_token'] = $token->accessToken;
     return $result;
 }
Пример #4
0
 /**
  * @param     $login
  * @param     $lastEventId
  * @param int $page
  *
  * @return array
  */
 private function findNewActivity($login, $lastEventId, $page = 1)
 {
     /** @var Response $response */
     $response = GitHub::connection()->getHttpClient()->get(sprintf('/users/%s/received_events?page=%s', $login, (int) $page));
     // Get the interval Github allows for polling
     $interval = $response->hasHeader('X-Poll-Interval') ? (string) $response->getHeader('X-Poll-Interval') : 60;
     $activity = ResponseMediator::getContent($response);
     $pagination = ResponseMediator::getPagination($response);
     $isLastPage = empty($pagination['next']);
     $caughtUp = false;
     $newActivity = [];
     foreach ($activity as $event) {
         if ($event['id'] == $lastEventId) {
             $caughtUp = true;
             break;
         }
         $newActivity[] = $event;
     }
     if (!$caughtUp && !$isLastPage) {
         // Try the next page
         list($interval, $activity) = $this->findNewActivity($login, $lastEventId, $page + 1);
         $newActivity += $activity;
     }
     return [$interval, $newActivity];
 }
Пример #5
0
 /**
  * {@inheritDoc}
  */
 public function onRequestError(Event $event)
 {
     /** @var $request \Guzzle\Http\Message\Request */
     $request = $event['request'];
     $response = $request->getResponse();
     if ($response->isClientError() || $response->isServerError()) {
         $remaining = (string) $response->getHeader('X-RateLimit-Remaining');
         $limit = $response->getHeader('X-RateLimit-Limit');
         if (null != $remaining && 1 > $remaining && 'rate_limit' !== substr($request->getResource(), 1, 10)) {
             throw new ApiLimitExceedException($limit);
         }
         if (401 === $response->getStatusCode()) {
             if ($response->hasHeader('X-GitHub-OTP') && 0 === strpos((string) $response->getHeader('X-GitHub-OTP'), 'required;')) {
                 $type = substr((string) $response->getHeader('X-GitHub-OTP'), 9);
                 throw new TwoFactorAuthenticationRequiredException($type);
             }
         }
         $content = ResponseMediator::getContent($response);
         if (is_array($content) && isset($content['message'])) {
             if (400 == $response->getStatusCode()) {
                 throw new ErrorException($content['message'], 400);
             } elseif (422 == $response->getStatusCode() && isset($content['errors'])) {
                 $errors = array();
                 foreach ($content['errors'] as $error) {
                     switch ($error['code']) {
                         case 'missing':
                             $errors[] = sprintf('The %s %s does not exist, for resource "%s"', $error['field'], $error['value'], $error['resource']);
                             break;
                         case 'missing_field':
                             $errors[] = sprintf('Field "%s" is missing, for resource "%s"', $error['field'], $error['resource']);
                             break;
                         case 'invalid':
                             if (isset($error['message'])) {
                                 $errors[] = sprintf('Field "%s" is invalid, for resource "%s": "%s"', $error['field'], $error['resource'], $error['message']);
                             } else {
                                 $errors[] = sprintf('Field "%s" is invalid, for resource "%s"', $error['field'], $error['resource']);
                             }
                             break;
                         case 'already_exists':
                             $errors[] = sprintf('Field "%s" already exists, for resource "%s"', $error['field'], $error['resource']);
                             break;
                         default:
                             $errors[] = $error['message'];
                             break;
                     }
                 }
                 throw new ValidationFailedException('Validation Failed: ' . implode(', ', $errors), 422);
             }
         }
         throw new RuntimeException(isset($content['message']) ? $content['message'] : $content, $response->getStatusCode());
     }
 }
Пример #6
0
 /**
  * @param     $login
  * @param     $lastEventId
  * @param int $page
  *
  * @return array
  */
 private function findNewActivity($login, $lastEventId, $page = 1)
 {
     /** @var Response $response */
     $response = GitHub::connection()->getHttpClient()->get(sprintf('/notifications?page=%s', $login, (int) $page));
     // Get the interval Github allows for polling
     $interval = $response->hasHeader('X-Poll-Interval') ? (string) $response->getHeader('X-Poll-Interval') : 60;
     $activity = ResponseMediator::getContent($response);
     $newActivity = [];
     foreach ($activity as $event) {
         if ($event['id'] == $lastEventId) {
             break;
         }
         $newActivity[] = $event;
     }
     return [$interval, $newActivity];
 }
Пример #7
0
 public function showIndex(Request $request, $page = 1)
 {
     $me = GitHub::me()->show();
     /** @var Response $response */
     $response = GitHub::connection()->getHttpClient()->get(sprintf('/users/%s/received_events?page=%s', $me['login'], (int) $page));
     $activity = ResponseMediator::getContent($response);
     $pagination = $this->getPagination($response);
     $pending = Input::get('pending', 0);
     // Save the latest activity ID for live fetching
     if (count($activity)) {
         $request->session()->put('last_event_id', $activity[0]['id']);
         $this->parseActivity($activity, $me, $pending);
     }
     // Get the interval Github allows for polling
     $interval = $response->hasHeader('X-Poll-Interval') ? (string) $response->getHeader('X-Poll-Interval') : 60;
     return view('activity.index', ['me' => $me, 'activity' => $activity, 'pagination' => $pagination, 'page' => $page, 'interval' => (int) $interval * 1000, 'pending' => $pending]);
 }
Пример #8
0
 public function getPagination(Response $response)
 {
     $ghPagination = ResponseMediator::getPagination($response);
     $pagination = ['first' => null, 'prev' => null, 'next' => null, 'last' => null, 'count' => 0];
     if ($ghPagination) {
         foreach ($ghPagination as $key => $url) {
             if (preg_match('/(.*?)\\?page=(.*?)$/', $url, $match)) {
                 $pagination[$key] = (int) $match[2];
             }
         }
         if (null !== $pagination['last']) {
             $pagination['count'] = $pagination['last'];
         } elseif (null !== $pagination['next']) {
             $pagination['count'] = $pagination['next'];
         } elseif (null !== $pagination['prev']) {
             $pagination['count'] = $pagination['prev'] + 1;
         }
     }
     return $pagination;
 }
Пример #9
0
 /**
  * Send a DELETE request with JSON-encoded parameters.
  *
  * @param string $path           Request path.
  * @param array  $parameters     POST parameters to be JSON encoded.
  * @param array  $requestHeaders Request headers.
  */
 protected function delete($path, array $parameters = array(), $requestHeaders = array())
 {
     $response = $this->client->getHttpClient()->delete($path, $this->createJsonBody($parameters), $requestHeaders);
     return ResponseMediator::getContent($response);
 }
Пример #10
0
 private function getRawActivities($limit = 5)
 {
     $response = $this->client->getHttpClient()->get("users/{$this->user}/events");
     $activities = ResponseMediator::getContent($response);
     return array_slice($activities, 0, $limit);
 }
Пример #11
0
                $openCount = count($issues);
                if ($SHOW_LABEL) {
                    if ($openCount === 0) {
                        print $COLOR_GRAY;
                    } else {
                        print $COLOR_RED;
                    }
                    print "    " . $label['name'] . ' ' . $openCount . $NO_COLOR . PHP_EOL;
                }
                $repositories[$repo]['labels'][$label['name']] = ['color' => $label['color'], 'open' => $openCount];
            }
        }
    }
}
$response = $client->getHttpClient()->get("rate_limit");
print "Remaining requests to GitHub this hour: " . \Github\HttpClient\Message\ResponseMediator::getContent($response)['rate']['remaining'] . PHP_EOL;
foreach ($repositories as $name => $repository) {
    foreach ($repository['milestones'] as $milestone => $info) {
        if (array_key_exists($milestone, $config->renameMilestones)) {
            $data = ["title" => $config->renameMilestones->{$milestone}, "state" => $info['state'], "description" => $info['description'], "due_on" => $info['due_on']];
            if (strpos($config->renameMilestones->{$milestone}, '-') === false && $info['open_issues'] === 0) {
                $data['state'] = 'closed';
            }
            $textStyle = $data['state'] === 'open' ? $BOLD : '';
            print $COLOR_RED . $config->org . '/' . $name . ': rename milestone ' . $milestone . ' -> ' . $config->renameMilestones->{$milestone} . ' - state: ' . $textStyle . $data['state'] . $NO_COLOR . PHP_EOL;
            if (array_key_exists($config->renameMilestones->{$milestone}, $config->dueDates)) {
                $newName = $config->renameMilestones->{$milestone};
                $data['due_on'] = $config->dueDates->{$newName} . 'T04:00:00Z';
            }
            continue;
            // comment this to RENAME MILESTONES
Пример #12
0
 /**
  * @param $gistId
  * @return array
  */
 public function getGistComments($gistId)
 {
     $response = $this->github->getHttpClient()->get("gists/{$gistId}/comments");
     return ResponseMediator::getContent($response);
 }
Пример #13
0
 /**
  * Parses markdown via GitHub.
  *
  * @param $markdown
  * @return \Guzzle\Http\EntityBodyInterface|mixed|string
  */
 public function parseMarkdown($markdown)
 {
     $body = json_encode(['text' => $markdown]);
     $response = $this->client->getHttpClient()->post('markdown', $body);
     return ResponseMediator::getContent($response);
 }
Пример #14
0
 private function getAllProjects(Remote $remote)
 {
     $client = $this->getClient($remote);
     $projects = array();
     $page = 1;
     while (true) {
         $response = $client->getHttpClient()->get('/user/repos', array('page' => $page, 'per_page' => 100));
         $projects = array_merge($projects, ResponseMediator::getContent($response));
         $pageInfo = ResponseMediator::getPagination($response);
         if (!isset($pageInfo['next'])) {
             break;
         }
         $page++;
     }
     return $projects;
 }
Пример #15
0
 /**
  * {@inheritdoc}
  */
 protected function get($key)
 {
     if ($this->has($key)) {
         $result = $this->client->getHttpClient()->get($this->pagination[$key]);
         $this->postFetch();
         return ResponseMediator::getContent($result);
     }
 }
Пример #16
0
 /**
  * @test
  */
 public function shouldHandlePagination()
 {
     $path = '/some/path';
     $body = 'a = b';
     $headers = array('c' => 'd');
     $response = new Response(200);
     $response->addHeader('Link', "<page1>; rel=\"page2\", \n<page3>; rel=\"page4\"");
     $client = $this->getBrowserMock();
     $httpClient = new HttpClient(array(), $client);
     $httpClient->request($path, $body, 'HEAD', $headers);
     $this->assertEquals(array('page2' => 'page1', 'page4' => 'page3'), ResponseMediator::getPagination($response));
 }
 /**
  * Get the Api rate limit.
  *
  * @return array
  */
 public function getApiRateLimit()
 {
     return ResponseMediator::getContent($this->github->getHttpClient()->get('rate_limit'));
 }
Пример #18
0
 public function checkRateLimit()
 {
     $response = $this->getClient()->getHttpClient()->get('rate_limit');
     $limit = GitHubResponseMediator::getContent($response);
     return $limit['resources']['core']['remaining'] > 10;
 }
Пример #19
0
 /**
  * @param $gistId
  * @param $comment
  * @return array
  */
 public function postGistComment($gistId, $comment)
 {
     $this->github->authenticate(Auth::user()->token, GitHubClient::AUTH_HTTP_TOKEN);
     $response = $this->github->getHttpClient()->post("gists/{$gistId}/comments", json_encode(['body' => $comment]));
     return ResponseMediator::getContent($response);
 }