Stackedit/js/github-helper.js

253 lines
6.3 KiB
JavaScript
Raw Normal View History

2013-05-27 19:45:33 +00:00
define([
"jquery",
"core",
"utils",
"extension-manager",
"async-runner"
], function($, core, utils, extensionMgr, 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;
2013-05-19 18:56:15 +00:00
task.error(new Error("Operation not available in offline mode.|stopPublish"));
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-27 23:16:38 +00:00
}).fail(function(jqXHR) {
var error = {
error: jqXHR.status,
message: jqXHR.statusText
};
handleError(error, task);
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;
}
2013-05-27 19:45:33 +00:00
extensionMgr.onMessage("Please make sure the Github authorization popup is not blocked by your browser.");
2013-04-27 23:16:38 +00:00
var errorMsg = "Failed to retrieve a token from GitHub.";
2013-04-20 00:14:20 +00:00
// 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");
2013-05-27 19:45:33 +00:00
authWindow = utils.popupWindow(
2013-04-11 22:38:41 +00:00
'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-27 23:16:38 +00:00
task.error(new Error(errorMsg));
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-28 22:33:17 +00:00
}, 500);
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 {
2013-04-27 23:16:38 +00:00
task.error(new Error(errorMsg));
2013-04-20 00:14:20 +00:00
}
});
}
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-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-27 23:16:38 +00:00
handleError(err, task);
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) {
2013-04-27 23:16:38 +00:00
handleError(err, task);
2013-04-20 00:14:20 +00:00
return;
}
task.chain();
});
}
task.chain(getUserLogin);
});
task.onSuccess(function() {
callback();
});
2013-04-27 23:16:38 +00:00
task.onError(function(error) {
callback(error);
});
asyncRunner.addTask(task);
};
2013-05-04 00:05:58 +00:00
githubHelper.uploadGist = function(gistId, filename, isPublic, title, content, callback) {
2013-04-29 23:19:54 +00:00
var task = asyncRunner.createTask();
connect(task);
authenticate(task);
task.onRun(function() {
var gist = github.getGist(gistId);
var files = {};
files[filename] = {content: content};
githubFunction = gist.update;
if(gistId === undefined) {
githubFunction = gist.create;
}
githubFunction({
description: title,
"public": isPublic,
files: files
}, function(err, gist) {
if(err) {
// Handle error
if(err.error === 404 && gistId !== undefined) {
err = 'Gist ' + gistId + ' not found on GitHub.|removePublish';
}
handleError(err, task);
return;
}
gistId = gist.id;
task.chain();
});
});
task.onSuccess(function() {
callback(undefined, gistId);
});
task.onError(function(error) {
callback(error);
});
asyncRunner.addTask(task);
};
2013-05-04 00:05:58 +00:00
githubHelper.downloadGist = function(gistId, filename, callback) {
var task = asyncRunner.createTask();
connect(task);
// No need for authentication
var title = undefined;
var content = undefined;
task.onRun(function() {
var github = new Github({});
var gist = github.getGist(gistId);
gist.read(function(err, gist) {
if(err) {
// Handle error
task.error(new Error('Error trying to access Gist ' + gistId + '.'));
return;
}
title = gist.description;
var file = gist.files[filename];
if(file === undefined) {
task.error(new Error('Gist ' + gistId + ' does not contain "' + filename + '".'));
return;
}
content = file.content;
task.chain();
});
});
task.onSuccess(function() {
callback(undefined, title, content);
});
task.onError(function(error) {
callback(error);
});
asyncRunner.addTask(task);
};
2013-04-27 23:16:38 +00:00
function handleError(error, task) {
var errorMsg = undefined;
if (error) {
2013-05-28 23:41:09 +00:00
logger.error(error);
2013-04-27 23:16:38 +00:00
// Try to analyze the error
if (typeof error === "string") {
errorMsg = error;
}
else {
errorMsg = "Could not publish on GitHub.";
if (error.error === 401 || error.error === 403) {
2013-04-11 22:38:41 +00:00
github = undefined;
2013-04-28 22:33:17 +00:00
localStorage.removeItem("githubToken");
2013-04-27 23:16:38 +00:00
errorMsg = "Access to GitHub account is not authorized.";
task.retry(new Error(errorMsg), 1);
return;
} else if (error.error <= 0) {
2013-04-11 22:38:41 +00:00
connected = false;
github = undefined;
core.setOffline();
2013-04-27 23:16:38 +00:00
errorMsg = "|stopPublish";
2013-04-11 22:38:41 +00:00
}
2013-04-20 00:14:20 +00:00
}
2013-04-27 23:16:38 +00:00
}
task.error(new Error(errorMsg));
}
2013-04-10 23:13:31 +00:00
return githubHelper;
});