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 and return it
188 * @param array $hooks
189 * @return stdClass hook
191 function checkHook($hooks) {
192 if (!empty($hooks)) {
193 foreach ($hooks as $hook) {
194 if ($hook->name
=== "web" && $hook->config
->url
=== $this->config
->hook
->url
) {
203 * Check if the pharext webhook is set for the repo and return it
204 * @param object $repo
205 * @return stdClass hook
207 function checkRepoHook($repo) {
208 if (!empty($repo->hooks
)) {
209 return $this->checkHook($repo->hooks
);
214 function listHooks($repo) {
215 return $this->queue(new API\Hooks\
ListHooks($this, compact("repo")));
218 function listReleases($repo, $page) {
219 return $this->queue(new API\Releases\
ListReleases($this, compact("repo", "page")));
222 function listTags($repo, $page) {
223 return $this->queue(new API\Tags\
ListTags($this, compact("repo", "page")));
226 function readContents($repo, $path = null) {
227 return $this->queue(new API\Repos\
ReadContents($this, compact("repo", "path")));
230 function createRepoHook($repo, $conf) {
231 return $this->queue(new API\Hooks\
CreateHook($this, compact("repo", "conf")));
234 function updateRepoHook($repo, $id, $conf) {
235 return $this->queue(new API\Hooks\
UpdateHook($this, compact("repo", "id", "conf")));
238 function deleteRepoHook($repo, $id) {
239 return $this->queue(new API\Hooks\
DeleteHook($this, compact("repo", "id")));
242 function createRelease($repo, $tag) {
243 return $this->queue(new API\Releases\
CreateRelease($this, compact("repo", "tag")));
246 function publishRelease($repo, $id, $tag) {
247 return $this->queue(new API\Releases\
PublishRelease($this, compact("repo", "id", "tag")));
250 function createReleaseAsset($url, $asset, $type) {
251 return $this->queue(new API\Releases\
CreateReleaseAsset($this, compact("url", "asset", "type")));
254 function listReleaseAssets($repo, $id) {
255 return $this->queue(new API\Releases\
ListReleaseAssets($this, compact("repo", "id")));
258 function uploadAssetForRelease($repo, $release, $config = null) {
259 return $this->listHooks($repo->full_name
)->then(function($result) use($release, $repo, $config) {
260 list($repo->hooks
) = $result;
261 $phar = new Pharext\
Package(
265 $config ?
: (array) $this->checkRepoHook($repo)->config
267 $name = $phar->build();
268 $url = uri_template($release->upload_url
, compact("name"));
269 $promise = $this->createReleaseAsset($url, $phar, "application/phar");
270 if ($release->draft
) {
271 return $promise->then(function($result) use($release, $repo) {
272 return $this->publishRelease($repo->full_name
, $release->id
, $release->tag_name
);
279 function createReleaseFromTag($repo, $tag_name, $config = null) {
280 return $this->createRelease($repo->full_name
, $tag_name)->then(function($result) use($repo, $config) {
281 list($release) = $result;
282 return $this->uploadAssetForRelease($repo, $release, $config);