Stackedit/res/helpers/githubHelper.js

265 lines
8.9 KiB
JavaScript
Raw Permalink Normal View History

2013-05-27 19:45:33 +00:00
define([
"jquery",
"core",
"utils",
2013-07-30 08:46:36 +00:00
"eventMgr",
2013-06-16 10:47:35 +00:00
"classes/AsyncTask"
2013-07-30 08:46:36 +00:00
], function($, core, utils, eventMgr, AsyncTask) {
2013-04-10 23:13:31 +00:00
2013-05-29 19:55:23 +00:00
var connected = undefined;
var github = undefined;
2013-04-10 23:13:31 +00:00
2013-05-29 19:55:23 +00:00
var githubHelper = {};
2013-04-10 23:13:31 +00:00
2013-08-04 00:53:46 +00:00
// Listen to offline status changes
var isOffline = false;
eventMgr.addListener("onOfflineChanged", function(isOfflineParam) {
isOffline = isOfflineParam;
});
2013-05-29 19:55:23 +00:00
// Try to connect github by downloading js file
function connect(task) {
task.onRun(function() {
2013-08-04 00:53:46 +00:00
if(isOffline === true) {
2013-05-29 19:55:23 +00:00
connected = false;
task.error(new Error("Operation not available in offline mode.|stopPublish"));
return;
}
if(connected === true) {
task.chain();
return;
}
$.ajax({
url: "lib/github.js",
dataType: "script",
timeout: AJAX_TIMEOUT
}).done(function() {
connected = true;
task.chain();
}).fail(function(jqXHR) {
var error = {
error: jqXHR.status,
message: jqXHR.statusText
};
handleError(error, task);
});
});
}
2013-04-10 23:13:31 +00:00
2013-05-29 19:55:23 +00:00
// Try to authenticate with Oauth
function authenticate(task) {
var authWindow = undefined;
var intervalId = undefined;
task.onRun(function() {
if(github !== undefined) {
task.chain();
return;
}
var token = localStorage["githubToken"];
if(token !== undefined) {
github = new Github({
token: token,
auth: "oauth"
});
task.chain();
return;
}
2013-07-30 08:46:36 +00:00
eventMgr.onMessage("Please make sure the Github authorization popup is not blocked by your browser.");
2013-05-29 19:55:23 +00:00
var errorMsg = "Failed to retrieve a token from GitHub.";
// We add time for user to enter his credentials
task.timeout = ASYNC_TASK_LONG_TIMEOUT;
var code = undefined;
function getCode() {
localStorage.removeItem("githubCode");
authWindow = utils.popupWindow('github-oauth-client.html?client_id=' + GITHUB_CLIENT_ID, 'stackedit-github-oauth', 960, 600);
authWindow.focus();
intervalId = setInterval(function() {
if(authWindow.closed === true) {
clearInterval(intervalId);
authWindow = undefined;
intervalId = undefined;
code = localStorage["githubCode"];
if(code === undefined) {
task.error(new Error(errorMsg));
return;
}
localStorage.removeItem("githubCode");
task.chain(getToken);
}
}, 500);
}
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(new Error(errorMsg));
}
});
}
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-06-27 23:10:04 +00:00
githubHelper.upload = function(reponame, username, branch, path, content, commitMsg, callback) {
2013-06-16 10:47:35 +00:00
var task = new AsyncTask();
2013-05-29 19:55:23 +00:00
connect(task);
authenticate(task);
task.onRun(function() {
2013-06-27 23:10:04 +00:00
function getUsername() {
2013-05-29 19:55:23 +00:00
var user = github.getUser();
user.show(undefined, function(err, result) {
if(err) {
handleError(err, task);
return;
}
2013-06-27 23:10:04 +00:00
username = result.login;
2013-05-29 19:55:23 +00:00
task.chain(write);
});
}
function write() {
2013-06-27 23:10:04 +00:00
var repo = github.getRepo(username, reponame);
2013-05-29 19:55:23 +00:00
repo.write(branch, path, content, commitMsg, function(err) {
if(err) {
handleError(err, task);
return;
}
task.chain();
});
}
2013-06-27 23:10:04 +00:00
if(username) {
task.chain(write);
}
else {
task.chain(getUsername);
}
2013-05-29 19:55:23 +00:00
});
task.onSuccess(function() {
callback();
});
task.onError(function(error) {
callback(error);
});
2013-06-16 10:47:35 +00:00
task.enqueue();
2013-05-29 19:55:23 +00:00
};
2013-04-27 23:16:38 +00:00
2013-05-29 19:55:23 +00:00
githubHelper.uploadGist = function(gistId, filename, isPublic, title, content, callback) {
2013-06-16 10:47:35 +00:00
var task = new AsyncTask();
2013-05-29 19:55:23 +00:00
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);
});
2013-06-16 10:47:35 +00:00
task.enqueue();
2013-05-29 19:55:23 +00:00
};
githubHelper.downloadGist = function(gistId, filename, callback) {
2013-06-16 10:47:35 +00:00
var task = new AsyncTask();
2013-05-29 19:55:23 +00:00
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);
});
2013-06-16 10:47:35 +00:00
task.enqueue();
2013-05-29 19:55:23 +00:00
};
function handleError(error, task) {
var errorMsg = undefined;
if(error) {
logger.error(error);
// 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) {
github = undefined;
localStorage.removeItem("githubToken");
errorMsg = "Access to GitHub account is not authorized.";
task.retry(new Error(errorMsg), 1);
return;
}
else if(error.error <= 0) {
connected = false;
github = undefined;
core.setOffline();
errorMsg = "|stopPublish";
}
}
}
task.error(new Error(errorMsg));
}
return githubHelper;
2013-04-10 23:13:31 +00:00
});