6 use app\Github\Storage
;
7 use app\Github\Exception
;
16 use Psr\Log\LoggerInterface
;
46 * @var \Psr\Log\LoggerInterface;
56 function __construct(Config
$config, LoggerInterface
$logger, Storage
$tokens = null, Storage
$cache = null) {
57 $this->logger
= $logger;
58 $this->config
= $config;
59 $this->client
= new Client("curl", "github");
60 $this->client
->configure($config->http
->configure
->toArray());
61 $this->client
->attach(new ClientObserver($logger));
62 $this->tokens
= $tokens ?
: new Storage\Session
;
63 $this->cache
= $cache;
67 * Set maximum acceptable age of cache items
70 function setMaxAge($seconds) {
71 $this->maxAge
= $seconds;
75 function getMaxAge() {
79 function getLogger() {
83 function getConfig() {
87 function getClient() {
91 function getTokenStorage() {
95 function getCacheStorage() {
99 function getCacheKey($ident, $page = null) {
100 return sprintf("%s:%s:%s", $this->getToken(), $ident, $page ?
: 1);
103 function hasToken() {
104 return $this->tokens
->get("access_token");
107 function setToken($token) {
108 $this->tokens
->set("access_token", new Storage\
Item(
110 $this->config
->storage
->token
->ttl
114 function getToken() {
115 if ($this->tokens
->get("access_token", $token, true)) {
116 return $token->getValue();
119 $this->logger
->notice("Token expired", $token);
120 throw new Exception\
TokenExpired($token->getLTL());
122 throw new Exception\TokenNotSet
;
125 function dropToken() {
126 $this->tokens
->del("access_token");
129 function getAuthUrl($callback_url) {
130 $state = base64_encode(openssl_random_pseudo_bytes(24));
131 $this->tokens
->set("state", new Storage\
Item($state, 5*60));
134 "client_id" => $this->config
->client
->id
,
135 "scope" => $this->config
->client
->scope
,
136 "redirect_uri" => $callback_url,
138 return new Url("https://github.com/login/oauth/authorize", [
139 "query" => new QueryString($param)
143 function fetchToken($code, $state) {
144 if (!$this->tokens
->get("state", $orig_state, true)) {
145 if (isset($orig_state)) {
146 $this->logger
->notice("State expired", $orig_state);
147 throw new Exception\
StateExpired($orig_state->getLTL());
149 throw new Exception\StateNotSet
;
151 if ($state !== $orig_state->getValue()) {
152 $this->logger
->warning("State mismatch", compact("state", "orig_state"));
153 throw new Exception\
StateMismatch($orig_state->getValue(), $state);
156 return $this->queue(new API\Users\
ReadAuthToken($this, [
158 "client_id" => $this->config
->client
->id
,
159 "client_secret" => $this->config
->client
->secret
,
163 function queue(API\Call
$call) {
164 return $this->queue
[] = $call();
168 $queue = $this->queue
;
169 $this->queue
= array();
170 $this->client
->send();
174 function readAuthUser() {
175 return $this->queue(new API\Users\
ReadAuthUser($this));
178 function listRepos($page) {
179 return $this->queue(new API\Repos\
ListRepos($this, compact("page")));
182 function readRepo($repo) {
183 return $this->queue(new API\Repos\
ReadRepo($this, compact("repo")));
187 * Check if the pharext webhook is set for the repo and return it
188 * @param object $repo
189 * @return stdClass hook
191 function checkRepoHook($repo) {
192 if (!empty($repo->hooks
)) {
193 foreach ($repo->hooks
as $hook) {
194 if ($hook->name
=== "web" && $hook->config
->url
=== $this->config
->hook
->url
) {
202 function listHooks($repo) {
203 return $this->queue(new API\Hooks\
ListHooks($this, compact("repo")));
206 function listReleases($repo, $page) {
207 return $this->queue(new API\Releases\
ListReleases($this, compact("repo", "page")));
210 function listTags($repo, $page) {
211 return $this->queue(new API\Tags\
ListTags($this, compact("repo", "page")));
214 function readContents($repo, $path = null) {
215 return $this->queue(new API\Repos\
ReadContents($this, compact("repo", "path")));
218 function createRepoHook($repo, $conf) {
219 return $this->queue(new API\Hooks\
CreateHook($this, compact("repo", "conf")));
222 function updateRepoHook($repo, $id, $conf) {
223 return $this->queue(new API\Hooks\
UpdateHook($this, compact("repo", "id", "conf")));
226 function deleteRepoHook($repo, $id) {
227 return $this->queue(new API\Hooks\
DeleteHook($this, compact("repo", "id")));
230 function createRelease($repo, $tag) {
231 return $this->queue(new API\Releases\
CreateRelease($this, compact("repo", "tag")));
234 function publishRelease($repo, $id, $tag) {
235 return $this->queue(new API\Releases\
PublishRelease($this, compact("repo", "id", "tag")));
238 function createReleaseAsset($url, $asset, $type) {
239 return $this->queue(new API\Releases\
CreateReleaseAsset($this, compact("url", "asset", "type")));
242 function listReleaseAssets($repo, $id) {
243 return $this->queue(new API\Releases\
ListReleaseAssets($this, compact("repo", "id")));
246 function uploadAssetForRelease($repo, $release, $config = null) {
247 return $this->listHooks($repo->full_name
)->then(function($result) use($release, $repo, $config) {
248 list($repo->hooks
) = $result;
249 $phar = new Pharext\
Package(
253 $config ?
: (array) $this->checkRepoHook($repo)->config
255 $name = sprintf("%s-%s.ext.phar", $repo->name
, $release->tag_name
);
256 $url = uri_template($release->upload_url
, compact("name"));
257 $promise = $this->createReleaseAsset($url, $phar, "application/phar");
258 if ($release->draft
) {
259 return $promise->then(function($result) use($release, $repo) {
260 return $this->publishRelease($repo->full_name
, $release->id
, $release->tag_name
);
267 function createReleaseFromTag($repo, $tag_name, $config = null) {
268 return $this->createRelease($repo->full_name
, $tag_name)->then(function($result) use($repo, $config) {
269 list($release) = $result;
270 return $this->uploadAssetForRelease($repo, $release, $config);