diff options
author | rubenwardy <rw@rubenwardy.com> | 2018-05-11 12:57:16 +0100 |
---|---|---|
committer | rubenwardy <rw@rubenwardy.com> | 2018-05-11 12:57:16 +0100 |
commit | 5e44f3d64c2be1b890e393832efd9fb100adf2c0 (patch) | |
tree | ea4a1fd0ce7fe8ed9c97687f6afd8e18b0a393e4 /app/static/package_create.js | |
parent | a55b4f84ff8fa9774b1aa3a0363d848b9a9bd252 (diff) | |
download | cheatdb-5e44f3d64c2be1b890e393832efd9fb100adf2c0.tar.xz |
Move Github import to backend
Fixes #41
Diffstat (limited to 'app/static/package_create.js')
-rw-r--r-- | app/static/package_create.js | 112 |
1 files changed, 48 insertions, 64 deletions
diff --git a/app/static/package_create.js b/app/static/package_create.js index 0bea7ca..25cdd3e 100644 --- a/app/static/package_create.js +++ b/app/static/package_create.js @@ -1,20 +1,4 @@ $(function() { - function readConfig(text) { - var retval = {} - - const lines = text.split("\n") - for (var i = 0; i < lines.length; i++) { - const idx = lines[i].indexOf("=") - if (idx > 0) { - const name = lines[i].substring(0, idx - 1).trim() - const value = lines[i].substring(idx + 1).trim() - retval[name] = value - } - } - - return retval - } - function finish() { $(".pkg_wiz_1").hide() $(".pkg_wiz_2").hide() @@ -22,66 +6,55 @@ $(function() { $(".pkg_meta").show() } - function repoIsSupported(url) { - try { - return URI(url).hostname() == "github.com" - } catch(e) { - return false - } - } - - function getFile(url) { + function getJSON(url) { return new Promise(function(resolve, reject) { fetch(url).then(function(response) { - response.text().then(resolve).catch(reject) + response.text().then(function(txt) { + resolve(JSON.parse(txt)) + }).catch(reject) }).catch(reject) }) } - function getInfo(baseUrl) { + function performTask(url) { return new Promise(function(resolve, reject) { - getFile(baseUrl + "/mod.conf").then(function(text) { - var config = readConfig(text) - - if (config["name"]) { - $("#name").val(config["name"]) - } - - if (config["description"]) { - const desc = config["description"] - const idx = desc.indexOf(".") - $("#shortDesc").val((idx < 5 || idx > 100) ? desc.substring(0, 100) : desc.substring(0, idx)) - $("#desc").val(desc) + getJSON(url).then(function(startResult) { + console.log(startResult) + if (typeof startResult.poll_url == "string") { + var tries = 0; + function retry() { + tries++; + if (tries > 10) { + reject("timeout") + } else { + console.log("Polling task in " + (tries*100) + "ms") + setTimeout(step, tries*100) + } + } + function step() { + getJSON(startResult.poll_url).then(function(res) { + if (res.status == "SUCCESS") { + console.log("Got result") + resolve(res.result) + } else { + retry() + } + }).catch(retry) + } + retry() + } else { + reject("Start task didn't return string!") } - - resolve() - }).catch(function() { - reject() - }) + }).catch(reject) }) } - function importInfo(urlstr) { - // Convert to HTTPs + function repoIsSupported(url) { try { - var url = URI(urlstr).scheme("https") - .username("") - .password("") + return URI(url).hostname() == "github.com" } catch(e) { - return Promise.reject(e) - } - // Change domain - url = url.hostname("raw.githubusercontent.com") - - // Rewrite path - const re = /^\/([^\/]+)\/([^\/]+)\/?$/ - const results = re.exec(url.path()) - if (results == null || results.length != 3) { - return Promise.reject("Unable to parse URL - please provide a direct URL to the repo") + return false } - url.path("/" + results[1] + "/" + results[2].replace(".git", "") + "/master") - - return getInfo(url.toString()) } $(".pkg_meta").hide() @@ -93,11 +66,22 @@ $(function() { $(".pkg_wiz_2").show() $(".pkg_repo").hide() - importInfo(repoURL).then(finish).catch(function(x) { - alert(x) + performTask("/tasks/getmeta/new/?url=" + encodeURI(repoURL)).then(function(result) { + console.log(result) + $("#name").val(result.name) + const desc = result.description || "" + if (desc.length > 0) { + const idx = desc.indexOf(".") + $("#shortDesc").val((idx < 5 || idx > 100) ? desc.substring(0, Math.min(desc.length, 100)) : desc.substring(0, idx)) + $("#desc").val(desc) + } + finish() + }).catch(function(e) { + alert(e) $(".pkg_wiz_1").show() $(".pkg_wiz_2").hide() $(".pkg_repo").show() + // finish() }) } else { finish() |