Move Github import to backend

Fixes #41
This commit is contained in:
rubenwardy
2018-05-11 12:57:16 +01:00
parent a55b4f84ff
commit 5e44f3d64c
9 changed files with 209 additions and 129 deletions

View File

@@ -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,6 +6,49 @@ $(function() {
$(".pkg_meta").show()
}
function getJSON(url) {
return new Promise(function(resolve, reject) {
fetch(url).then(function(response) {
response.text().then(function(txt) {
resolve(JSON.parse(txt))
}).catch(reject)
}).catch(reject)
})
}
function performTask(url) {
return new Promise(function(resolve, reject) {
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!")
}
}).catch(reject)
})
}
function repoIsSupported(url) {
try {
return URI(url).hostname() == "github.com"
@@ -30,60 +57,6 @@ $(function() {
}
}
function getFile(url) {
return new Promise(function(resolve, reject) {
fetch(url).then(function(response) {
response.text().then(resolve).catch(reject)
}).catch(reject)
})
}
function getInfo(baseUrl) {
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)
}
resolve()
}).catch(function() {
reject()
})
})
}
function importInfo(urlstr) {
// Convert to HTTPs
try {
var url = URI(urlstr).scheme("https")
.username("")
.password("")
} 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")
}
url.path("/" + results[1] + "/" + results[2].replace(".git", "") + "/master")
return getInfo(url.toString())
}
$(".pkg_meta").hide()
$(".pkg_wiz_1").show()
$("#pkg_wiz_1_next").click(function() {
@@ -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()