GitHub publishing enhancements

This commit is contained in:
benweet 2013-04-13 00:09:34 +01:00
parent 7283ff7229
commit 0c1c8b274f
5 changed files with 46 additions and 42 deletions

View File

@ -12,12 +12,14 @@
}
var client_id = getParameter("client_id");
var code = getParameter("code");
if (code) {
localStorage["githubCode"] = code;
window.close();
} else if (client_id) {
if (client_id) {
window.location.href = "https://github.com/login/oauth/authorize?client_id="
+ client_id + "&scope=repo";
} else {
if (code) {
localStorage["githubCode"] = code;
}
window.close();
}
</script>
</head>

View File

@ -258,33 +258,25 @@
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"
aria-hidden="true">&times;</button>
<h3>Publish</h3>
<h3 class="modal-publish-github">Publish on GitHub</h3>
</div>
<div class="modal-body">
<div class="form-horizontal">
<div class="control-group control-publish-github">
<label class="control-label" for="input-publish-github-username">GitHub
user</label>
<div class="controls">
<input type="text" id="input-publish-github-username"
placeholder="User name">
</div>
</div>
<div class="control-group control-publish-github">
<div class="control-group modal-publish-github">
<label class="control-label" for="input-publish-github-reponame">Repository</label>
<div class="controls">
<input type="text" id="input-publish-github-reponame"
placeholder="Repository name">
</div>
</div>
<div class="control-group control-publish-github">
<div class="control-group modal-publish-github">
<label class="control-label" for="input-publish-github-branch">Branch</label>
<div class="controls">
<input type="text" id="input-publish-github-branch"
placeholder="Branch name">
</div>
</div>
<div class="control-group control-publish-github">
<div class="control-group modal-publish-github">
<label class="control-label" for="input-publish-github-path">File
path</label>
<div class="controls">
@ -292,7 +284,7 @@
placeholder="path/to/file.md">
</div>
</div>
<div class="control-group control-publish-blogger">
<div class="control-group modal-publish-blogger">
<label class="control-label" for="input-publish-blogger-url">Blog
URL</label>
<div class="controls">
@ -300,7 +292,7 @@
placeholder="http://exemple.blogger.com/">
</div>
</div>
<div class="control-group control-publish-blogger">
<div class="control-group modal-publish-blogger">
<label class="control-label" for="input-publish-blogger-postid">Update
existing post ID (optional)</label>
<div class="controls">
@ -309,13 +301,12 @@
</div>
</div>
<div class="control-group">
<div class="control-label">Publish as</div>
<div class="control-label">Format</div>
<div class="controls">
<label class="radio"> <input type="radio"
name="radio-publish-format" value="markdown"> Markdown
format
</label> <label class="radio"> <input type="radio"
name="radio-publish-format" value="html"> HTML format
name="radio-publish-format" value="html"> HTML
</label>
</div>
</div>

View File

@ -434,7 +434,7 @@ define(["jquery", "google-helper", "dropbox-helper", "github-helper", "synchroni
newPublishProvider = provider;
// Show/hide controls depending on provider
$('div[class*=" control-publish-"]').hide().filter(".control-publish-" + provider).show();
$('div[class*=" modal-publish-"]').hide().filter(".modal-publish-" + provider).show();
// Reset fields
core.resetModalInputs();
@ -457,7 +457,6 @@ define(["jquery", "google-helper", "dropbox-helper", "github-helper", "synchroni
// Create a new publication on GitHub
function newPublishGithub(event) {
var publishObject = {};
publishObject.username = core.getInputValue($("#input-publish-github-username"), event);
publishObject.repository = core.getInputValue($("#input-publish-github-reponame"), event);
publishObject.branch = core.getInputValue($("#input-publish-github-branch"), event);
publishObject.path = core.getInputValue($("#input-publish-github-path"), event);
@ -470,7 +469,7 @@ define(["jquery", "google-helper", "dropbox-helper", "github-helper", "synchroni
var title = localStorage[fileIndex + ".title"];
var content = publisher.getPublishContent(publishObject);
var commitMsg = core.settings.commitMsg;
githubHelper.upload(publishObject.username, publishObject.repository,
githubHelper.upload(publishObject.repository,
publishObject.branch, publishObject.path, content, commitMsg,
function(error) {
if(error === undefined) {

View File

@ -55,6 +55,7 @@ define(["jquery", "async-runner"], function($, asyncTaskRunner) {
var intervalId = undefined;
var authWindow = undefined;
var token = localStorage["githubToken"];
var errorMsg = "Access to GitHub is not authorized.";
var asyncTask = {};
asyncTask.run = function() {
if (github !== undefined) {
@ -71,7 +72,7 @@ define(["jquery", "async-runner"], function($, asyncTaskRunner) {
return;
}
if(immediate === true) {
core.showError("Unable to perform GitHub authenticate.");
core.showError();
asyncTask.error();
return;
}
@ -85,15 +86,18 @@ define(["jquery", "async-runner"], function($, asyncTaskRunner) {
authWindow.focus();
intervalId = setInterval(function() {
var code = localStorage["githubCode"];
if(code !== undefined) {
if(authWindow.closed === true || code !== undefined) {
localStorage.removeItem("githubCode");
if(code === undefined) {
asyncTask.error();
return;
}
$.getJSON(GATEKEEPER_URL + "authenticate/" + code, function(data) {
if(data.token !== undefined) {
localStorage["githubToken"] = data.token;
asyncTask.success();
}
else {
core.showError("Error retrieving GitHub Oauth token.");
asyncTask.error();
}
});
@ -117,50 +121,59 @@ define(["jquery", "async-runner"], function($, asyncTaskRunner) {
if(authWindow !== undefined) {
authWindow.close();
}
core.showError(errorMsg);
callback();
};
asyncTaskRunner.addTask(asyncTask);
});
}
githubHelper.upload = function(username, reponame, branch, path, content, commitMsg, callback) {
githubHelper.upload = function(reponame, branch, path, content, commitMsg, callback) {
callback = callback || core.doNothing;
authenticate(function() {
if (github === undefined) {
callback();
callback("error");
return;
}
var error = undefined;
var asyncTask = {};
asyncTask.run = function() {
var repo = github.getRepo(username, reponame);
repo.write(branch, path, content, commitMsg, function(err) {
if(!err) {
asyncTask.success();
var user = github.getUser();
user.show(undefined, function(err, result) {
if(err) {
error = err.error;
asyncTask.error();
return;
}
error = err.error;
asyncTask.error();
var repo = github.getRepo(result.login, reponame);
repo.write(branch, path, content, commitMsg, function(err) {
if(err) {
error = err.error;
asyncTask.error();
return;
}
asyncTask.success();
});
});
};
asyncTask.onSuccess = function() {
callback(error);
};
asyncTask.onError = function() {
if(error === 401) {
var errorMsg = "Could not publish on GitHub";
if(error === 401 || error === 403) {
github = undefined;
// Token must be renewed
localStorage.removeItem("githubToken");
githubHelper.upload(username, reponame, branch, path, content, commitMsg, callback);
return;
errorMsg = "Access to GitHub is not authorized.";
}
if(error === 0) {
else if(error === 0) {
connected = false;
github = undefined;
core.setOffline();
}
core.showError("Could not publish on GitHub");
core.showError(errorMsg);
callback(error);
};
asyncTaskRunner.addTask(asyncTask);

View File

@ -66,8 +66,7 @@ define(["jquery", "google-helper", "github-helper"], function($, googleHelper, g
// Try to find the provider
if(publishObject.provider == PUBLISH_PROVIDER_GITHUB) {
githubHelper.upload(publishObject.username, publishObject.repository,
publishObject.branch, publishObject.path, content, commitMsg,
githubHelper.upload(publishObject.repository, publishObject.branch, publishObject.path, content, commitMsg,
function(error) {
publishLocation(callback, error);
});