use app\Github\API;
use app\Github\Storage;
use app\Github\Exception;
+use app\Pharext;
use merry\Config;
function __construct(Config $config, LoggerInterface $logger, Storage $tokens = null, Storage $cache = null) {
$this->logger = $logger;
$this->config = $config;
- $this->client = new Client;
+ $this->client = new Client("curl", "github");
+ $this->client->configure($config->http->configure->toArray());
$this->client->attach(new ClientObserver($logger));
$this->tokens = $tokens ?: new Storage\Session;
$this->cache = $cache;
], 0);
}
- function fetchToken($code, $state, callable $callback) {
+ function fetchToken($code, $state) {
if (!$this->tokens->get("state", $orig_state, true)) {
if (isset($orig_state)) {
$this->logger->notice("State expired", $orig_state);
"client_id" => $this->config->client->id,
"client_secret" => $this->config->client->secret,
]);
- return $call($callback);
+ return $call();
}
- function readAuthUser(callable $callback) {
+ function readAuthUser() {
$call = new API\Users\ReadAuthUser($this);
- return $call($callback);
+ return $call();
}
function listRepos($page, callable $callback) {
return $call($callback);
}
+ /**
+ * Check if the pharext webhook is set for the repo and return it
+ * @param object $repo
+ * @return stdClass hook
+ */
+ function checkRepoHook($repo) {
+ if ($repo->hooks) {
+ foreach ($repo->hooks as $hook) {
+ if ($hook->name === "web" && $hook->config->url === $this->config->hook->url) {
+ return $hook;
+ }
+ }
+ }
+ return null;
+ }
+
function listHooks($repo, callable $callback) {
$call = new API\Hooks\ListHooks($this, compact("repo"));
return $call($callback);
$call = new API\Releases\CreateRelease($this, compact("repo", "tag"));
return $call($callback);
}
-
+
+ function publishRelease($repo, $id, $tag, callable $callback) {
+ $call = new API\Releases\PublishRelease($this, compact("repo", "id", "tag"));
+ return $call($callback);
+ }
+
function createReleaseAsset($url, $asset, $type, callable $callback) {
$call = new API\Releases\CreateReleaseAsset($this, compact("url", "asset", "type"));
return $call($callback);
}
+
+ function listReleaseAssets($repo, $id, callable $callback) {
+ $call = new API\Releases\ListReleaseAssets($this, compact("repo", "id"));
+ return $call($callback);
+ }
+
+ function uploadAssetForRelease($repo, $release, $config, callable $callback) {
+ return $this->listHooks($repo->full_name, function($hooks) use($release, $repo, $config, $callback) {
+ $repo->hooks = $hooks;
+ $hook = $this->checkRepoHook($repo);
+ $phar = new Pharext\Package($repo->clone_url, $release->tag_name, $repo->name, $config ?: $hook->config);
+ $name = sprintf("%s-%s.ext.phar", $repo->name, $release->tag_name);
+ $url = uri_template($release->upload_url, compact("name"));
+ $this->createReleaseAsset($url, $phar, "application/phar", function($json) use($release, $repo, $callback) {
+ if ($release->draft) {
+ $this->publishRelease($repo->full_name, $release->id, $release->tag_name, function($json) use($callback) {
+ $callback($json);
+ });
+ } else {
+ $callback($json);
+ }
+ });
+ });
+ }
+
+ function createReleaseFromTag($repo, $tag_name, $config, callable $callback) {
+ return $this->createRelease($repo->full_name, $tag_name, function($json) use($repo, $callback) {
+ $this->uploadAssetForRelease($repo, $json, $config, $callback);
+ });
+ }
+
}