5 use app\Github\Storage
;
6 use app\Github\Exception
;
15 use Psr\Log\LoggerInterface
;
45 * @var \Psr\Log\LoggerInterface;
55 function __construct(Config
$config, LoggerInterface
$logger, Storage
$tokens = null, Storage
$cache = null) {
56 $this->logger
= $logger;
57 $this->config
= $config;
58 $this->client
= new Client("curl", "github");
59 $this->client
->configure($config->http
->configure
->toArray());
60 $this->client
->attach(new ClientObserver($logger));
61 $this->tokens
= $tokens ?
: new Storage\Session
;
62 $this->cache
= $cache;
66 * Set maximum acceptable age of cache items
69 function setMaxAge($seconds) {
70 $this->maxAge
= $seconds;
74 function getMaxAge() {
78 function getLogger() {
82 function getConfig() {
86 function getClient() {
90 function getTokenStorage() {
94 function getCacheStorage() {
98 function getCacheKey($ident, $page = null) {
99 return sprintf("%s:%s:%s", $this->getToken(), $ident, $page ?
: 1);
102 function hasToken() {
103 if ($this->tokens
->get("access_token", $token)) {
104 $access_token = $token->getValue();
105 if (isset($access_token)) {
113 function setToken($token) {
114 $this->tokens
->set("access_token", new Storage\
Item(
116 $this->config
->storage
->token
->ttl
120 function getToken() {
121 if ($this->tokens
->get("access_token", $token, true)) {
122 return $token->getValue();
125 $this->logger
->notice("Token expired", $token);
126 throw new Exception\
TokenExpired($token->getLTL());
128 throw new Exception\TokenNotSet
;
131 function dropToken() {
132 $this->tokens
->del("access_token");
135 function getAuthUrl($callback_url) {
136 $state = base64_encode(openssl_random_pseudo_bytes(24));
137 $this->tokens
->set("state", new Storage\
Item($state, 5*60));
140 "client_id" => $this->config
->client
->id
,
141 "scope" => $this->config
->client
->scope
,
142 "redirect_uri" => (string) $callback_url,
144 return new Url("https://github.com/login/oauth/authorize", [
145 "query" => new QueryString($param)
149 function fetchToken($code, $state) {
150 if (!$this->tokens
->get("state", $orig_state, true)) {
151 if (isset($orig_state)) {
152 $this->logger
->notice("State expired", compact("state", "orig_state"));
153 throw new Exception\
StateExpired($orig_state->getLTL());
155 throw new Exception\StateNotSet
;
157 if ($state !== $orig_state->getValue()) {
158 $this->logger
->warning("State mismatch", compact("state", "orig_state"));
159 throw new Exception\
StateMismatch($orig_state->getValue(), $state);
162 return $this->queue(new API\Users\
ReadAuthToken($this, [
164 "client_id" => $this->config
->client
->id
,
165 "client_secret" => $this->config
->client
->secret
,
169 function queue(API\Call
$call) {
170 return $this->queue
[] = $call();
174 $queue = $this->queue
;
175 $this->queue
= array();
176 $this->client
->send();
180 function readAuthUser() {
181 return $this->queue(new API\Users\
ReadAuthUser($this));
184 function listRepos($page) {
185 return $this->queue(new API\Repos\
ListRepos($this, compact("page")));
188 function readRepo($repo) {
189 return $this->queue(new API\Repos\
ReadRepo($this, compact("repo")));
193 * Check if the pharext webhook is set and return it
194 * @param array $hooks
195 * @return stdClass hook
197 function checkHook($hooks) {
198 if (!empty($hooks)) {
199 foreach ($hooks as $hook) {
200 if ($hook->name
=== "web" && $hook->config
->url
=== $this->config
->hook
->url
) {
209 * Check if the pharext webhook is set for the repo and return it
210 * @param object $repo
211 * @return stdClass hook
213 function checkRepoHook($repo) {
214 if (!empty($repo->hooks
)) {
215 return $this->checkHook($repo->hooks
);
220 function listHooks($repo) {
221 return $this->queue(new API\Hooks\
ListHooks($this, compact("repo")));
224 function listReleases($repo, $page) {
225 return $this->queue(new API\Releases\
ListReleases($this, compact("repo", "page")));
228 function listTags($repo, $page) {
229 return $this->queue(new API\Tags\
ListTags($this, compact("repo", "page")));
232 function readContents($repo, $path = null) {
233 return $this->queue(new API\Repos\
ReadContents($this, compact("repo", "path")));
236 function createRepoHook($repo, $conf) {
237 return $this->queue(new API\Hooks\
CreateHook($this, compact("repo", "conf")));
240 function updateRepoHook($repo, $id, $conf) {
241 return $this->queue(new API\Hooks\
UpdateHook($this, compact("repo", "id", "conf")));
244 function deleteRepoHook($repo, $id) {
245 return $this->queue(new API\Hooks\
DeleteHook($this, compact("repo", "id")));
248 function createRelease($repo, $tag) {
249 return $this->queue(new API\Releases\
CreateRelease($this, compact("repo", "tag")));
252 function publishRelease($repo, $id, $tag) {
253 return $this->queue(new API\Releases\
PublishRelease($this, compact("repo", "id", "tag")));
256 function createReleaseAsset($url, $asset, $type) {
257 return $this->queue(new API\Releases\
CreateReleaseAsset($this, compact("url", "asset", "type")));
260 function listReleaseAssets($repo, $id) {
261 return $this->queue(new API\Releases\
ListReleaseAssets($this, compact("repo", "id")));
264 function uploadAssetForRelease($repo, $release, $config = null) {
265 return $this->listHooks($repo->full_name
)->then(function($result) use($release, $repo, $config) {
266 list($repo->hooks
) = $result;
267 $phar = new Pharext\
Package(
271 $config ?
: (array) $this->checkRepoHook($repo)->config
273 $name = $phar->build();
274 $url = uri_template($release->upload_url
, compact("name"));
275 $promise = $this->createReleaseAsset($url, $phar, "application/phar");
276 if ($release->draft
) {
277 return $promise->then(function($result) use($release, $repo) {
278 return $this->publishRelease($repo->full_name
, $release->id
, $release->tag_name
);
285 function createReleaseFromTag($repo, $tag_name, $config = null) {
286 return $this->createRelease($repo->full_name
, $tag_name)->then(function($result) use($repo, $config) {
287 list($release) = $result;
288 return $this->uploadAssetForRelease($repo, $release, $config);