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

View File

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

View File

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

View File

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

View File

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