Stackedit/js/github-helper.js

162 lines
4.0 KiB
JavaScript
Raw Normal View History

2013-04-21 00:07:27 +00:00
define(["jquery", "core", "async-runner"], function($, core, asyncRunner) {
2013-04-10 23:13:31 +00:00
var connected = undefined;
2013-04-11 22:38:41 +00:00
var github = undefined;
2013-04-10 23:13:31 +00:00
var githubHelper = {};
// Try to connect github by downloading js file
2013-04-20 00:14:20 +00:00
function connect(task) {
task.onRun(function() {
2013-04-10 23:13:31 +00:00
if(core.isOffline === true) {
connected = false;
core.showMessage("Operation not available in offline mode.");
2013-04-20 00:14:20 +00:00
task.error();
2013-04-10 23:13:31 +00:00
return;
}
if (connected === true) {
2013-04-20 00:14:20 +00:00
task.chain();
2013-04-10 23:13:31 +00:00
return;
}
$.ajax({
url : "lib/github.js",
dataType : "script", timeout : AJAX_TIMEOUT
}).done(function() {
connected = true;
2013-04-20 00:14:20 +00:00
task.chain();
2013-04-10 23:13:31 +00:00
}).fail(function() {
2013-04-20 00:14:20 +00:00
core.setOffline();
task.error(new Error("Network timeout"));
2013-04-10 23:13:31 +00:00
});
2013-04-20 00:14:20 +00:00
});
2013-04-10 23:13:31 +00:00
}
// Try to authenticate with Oauth
2013-04-20 00:14:20 +00:00
function authenticate(task) {
var authWindow = undefined;
var intervalId = undefined;
task.onRun(function() {
if (github !== undefined) {
task.chain();
2013-04-10 23:13:31 +00:00
return;
}
2013-04-11 22:38:41 +00:00
var token = localStorage["githubToken"];
2013-04-20 00:14:20 +00:00
if(token !== undefined) {
github = new Github({
token: token,
auth: "oauth"
});
task.chain();
return;
}
core.showMessage("Please make sure the Github authorization popup is not blocked by your browser.");
// We add time for user to enter his credentials
task.timeout = ASYNC_TASK_LONG_TIMEOUT;
var code = undefined;
function getCode() {
2013-04-11 22:38:41 +00:00
localStorage.removeItem("githubCode");
authWindow = core.popupWindow(
'github-oauth-client.html?client_id=' + GITHUB_CLIENT_ID,
'stackedit-github-oauth', 960, 600);
authWindow.focus();
intervalId = setInterval(function() {
2013-04-20 00:14:20 +00:00
if(authWindow.closed === true) {
clearInterval(intervalId);
authWindow = undefined;
intervalId = undefined;
code = localStorage["githubCode"];
2013-04-12 23:09:34 +00:00
if(code === undefined) {
2013-04-20 00:14:20 +00:00
task.error();
2013-04-12 23:09:34 +00:00
return;
}
2013-04-20 00:14:20 +00:00
localStorage.removeItem("githubCode");
task.chain(getToken);
2013-04-11 22:38:41 +00:00
}
2013-04-20 00:14:20 +00:00
});
}
function getToken() {
$.getJSON(GATEKEEPER_URL + "authenticate/" + code, function(data) {
if(data.token !== undefined) {
token = data.token;
localStorage["githubToken"] = token;
github = new Github({
token: token,
auth: "oauth"
});
task.chain();
}
else {
task.error();
}
});
}
task.chain(getCode);
});
task.onError(function() {
if(intervalId !== undefined) {
clearInterval(intervalId);
}
if(authWindow !== undefined) {
authWindow.close();
}
2013-04-10 23:13:31 +00:00
});
}
2013-04-12 23:09:34 +00:00
githubHelper.upload = function(reponame, branch, path, content, commitMsg, callback) {
2013-04-10 23:13:31 +00:00
callback = callback || core.doNothing;
2013-04-20 00:14:20 +00:00
var task = asyncRunner.createTask();
connect(task);
authenticate(task);
task.onRun(function() {
var userLogin = undefined;
function getUserLogin() {
2013-04-12 23:09:34 +00:00
var user = github.getUser();
user.show(undefined, function(err, result) {
if(err) {
2013-04-20 00:14:20 +00:00
task.error(err);
2013-04-10 23:13:31 +00:00
return;
}
2013-04-20 00:14:20 +00:00
userLogin = result.login;
task.chain(write);
2013-04-10 23:13:31 +00:00
});
2013-04-20 00:14:20 +00:00
}
function write() {
var repo = github.getRepo(userLogin, reponame);
repo.write(branch, path, content, commitMsg, function(err) {
if(err) {
task.error(err);
return;
}
task.chain();
});
}
task.chain(getUserLogin);
});
task.onSuccess(function() {
callback();
});
task.onError(function(err) {
var errorMsg = "Could not publish on GitHub.";
if(err !== undefined) {
console.error(err);
if(err.error === 401 || err.error === 403) {
2013-04-11 22:38:41 +00:00
github = undefined;
// Token must be renewed
localStorage.removeItem("githubToken");
2013-04-12 23:09:34 +00:00
errorMsg = "Access to GitHub is not authorized.";
2013-04-11 22:38:41 +00:00
}
2013-04-20 00:14:20 +00:00
else if(err.error === 0) {
2013-04-11 22:38:41 +00:00
connected = false;
github = undefined;
core.setOffline();
}
2013-04-20 00:14:20 +00:00
}
core.showError(errorMsg);
callback(errorMsg);
2013-04-10 23:13:31 +00:00
});
2013-04-20 00:14:20 +00:00
asyncRunner.addTask(task);
2013-04-10 23:13:31 +00:00
};
return githubHelper;
});