url = $url; $this->oauth_id = $oauth_id; $this->oauth_secret = $oauth_secret; $this->apitokenpath = $apitokenpath; if ($this->apitokenpath !== null && file_exists($this->apitokenpath)) $this->apitoken = file_get_contents($this->apitokenpath); else $this->apitoken = null; } /** * @throws Exception */ public function queryAPIToken() { $url = Utils::sharpFormat(self::URL_OAUTH_TOKEN, ['id'=>$this->oauth_id, 'secret'=>$this->oauth_secret, 'code'=>'egg']); $fullresult = $result = file_get_contents($url); if (Utils::startsWith($fullresult, 'error=')) throw new EGGException('GitHub Auth failed: ' . $fullresult); $result = str_replace('access_token=', '', $result); $result = str_replace('&scope=&token_type=bearer', '', $result); $this->logger->proclog("Updated Github API token"); if ($result!=='' && $result !== null && $this->apitokenpath !== null) file_put_contents($this->apitokenpath, $result); $this->apitoken = $result; } /** @inheritDoc */ protected function preUpdate() { if ($this->apitoken === null) $this->queryAPIToken(); } /** @inheritDoc */ protected function postUpdate() { // } /** @inheritDoc */ protected function queryOrganizations($page) { throw new EGGException("Github does not support organization queries."); } /** @inheritDoc */ protected function queryRepositories($user, $page) { $url = Utils::sharpFormat(self::API_REPOSITORIESLIST, ['user'=>$user, 'page'=>$page]); return Utils::getJSONWithTokenAuth($this->logger, $url, $this->apitoken); } /** @inheritDoc */ protected function queryBranches($reponame, $page) { $url = Utils::sharpFormat(self::API_BRANCHLIST, ['repo'=>$reponame, 'page'=>$page]); return Utils::getJSONWithTokenAuth($this->logger, $url, $this->apitoken); } /** @inheritDoc */ protected function queryCommits($reponame, $branchname, $startsha) { $url = Utils::sharpFormat(self::API_COMMITSLIST, [ 'repo'=>$reponame, 'sha'=>$startsha ]); return Utils::getJSONWithTokenAuth($this->logger, $url, $this->apitoken); } /** @inheritDoc */ protected function readOrganization($data) { return [ 'id' => null, // no orgs in github 'full_name' => null, // no orgs in github 'key' => null, // no orgs in github ]; } /** @inheritDoc */ protected function readRepository($data) { return [ 'full_name' => $data->{'full_name'}, 'html_url' => $data->{'html_url'}, ]; } /** @inheritDoc */ protected function readBranch($data) { if (isset($data->{'block'})) return null; return [ 'name' => $data->{'name'}, 'sha' => $data->{'commit'}->{'sha'}, ]; } /** @inheritDoc */ protected function readCommit($data) { return [ 'sha' => $data->{'sha'}, 'author_name' => $data->{'commit'}->{'author'}->{'name'}, 'author_email' => $data->{'commit'}->{'author'}->{'email'}, 'committer_name' => $data->{'commit'}->{'committer'}->{'name'}, 'committer_email' => $data->{'commit'}->{'committer'}->{'email'}, 'message' => $data->{'commit'}->{'message'}, 'date' => (new DateTime($data->{'commit'}->{'author'}->{'date'}))->format("Y-m-d H:i:s"), 'parents' => array_map(function ($v){ return $v->{'sha'}; }, $data->{'parents'}), ]; } /** @inheritDoc */ public function toString() { return "[Github|".$this->filter."]"; } }