Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Github api integration #137

Merged
merged 3 commits into from
Apr 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
"cjs": "*",
"ace": "~1.2.2",
"browserfs": "0.5.8",
"ace": "~1.2.2",
"bootstrap-contextmenu": "^0.3.4",
"bootbox.js": "^4.4.0"
"bootbox.js": "^4.4.0",
"github-api": "~0.10.7"
},
"devDependencies": {},
"resolutions": {
Expand Down
7 changes: 7 additions & 0 deletions src/app/github-int/github-base64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class GithubBase64 {
constructor() {
this.Base64 = { encode: window.btoa };
}
}

export default (new GithubBase64());
218 changes: 218 additions & 0 deletions src/app/github-int/github-int.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
/* global Buffer */
import SysFileSystem from 'app/sys-filesystem';
import { notify } from 'app/notifications';
import * as Github from 'github-api';


class GithubInt {

// notification options

constructor(username, password) {
//TODO work out initialization conditions and grabbing github authkey

if(username == undefined)
{
this.hub = new Github({});
this.authenticated = false;
}
else
{
this.hub = new Github({username:username, password:password, auth:'basic'});
this.authenticated = true;
this.user = this.hub.getUser();
this.username = username;
}
}

/*
* Clones specified repo in to a new directory within the local file system.
*
* Note: unauthenticated users are limited to 60 github api calls per hour
* and each file is an api call.
*/
cloneRepo(repoUrl) {

var tokens = repoUrl.split('/', 2);
if(tokens.length<2)
return;

var username = tokens[0];
var reponame = tokens[1];

var repo = this.hub.getRepo(username, reponame);
var fs = SysFileSystem;

notify('Cloning ' + repoUrl + '...', 'yellow');

repo.getTree('master?recursive=true', function(err, tree) {

if(err)
{
if(err.request.response=='')
notify('Something happened... Try again.', 'red');
else
notify(JSON.parse(err.request.response)['message'], 'red');

return;
}

fs.makeDirectory(reponame);

var loaded = 0;
var showErrors = true;

for(var i = 0; i<tree.length; i++)
{
if(tree[i].type == 'blob')
{
repo.read('master', tree[i].path, function(err, data) {

if(err)
{
if(showErrors)
{
if(err.request.response=='')
notify('Something happened... Try again.', 'red');
else
notify(JSON.parse(err.request.response)['message'], 'red');

showErrors = false;
}
return;
}

fs.writeFile(reponame+'/'+tree[this].path, new Buffer(data,'binary'));

loaded += 1;

if(loaded == tree.length)
notify('Successfully cloned ' + repoUrl + '!', 'green');

}.bind(i));
}
if(tree[i].type == 'tree'){

loaded += 1;
fs.makeDirectory(reponame+'/'+tree[i].path);
}
}
}.bind(this));
}

/*
* Pushes all local files (i.e those in /home/user) to a public repo named 'saved-jor1k-workspace'
* If the repo already exists, it deletes it and creates a new repo.
* If needed this can be extended to modify current existing repo rather than deleting, but this is non-trivial.
*/
saveAll() {
var fs = SysFileSystem;

if(!this.authenticated)
{
notify('Must be authenticated...', 'red');
}
var repo = this.hub.getRepo(this.username, 'saved-jor1k-workspace');
repo.show(function(err, repo_info){
if(err){
if(err.error==404)
this.createSaveRepo();
else
{
if(err.request.response=='')
notify('Something happened... Try again.', 'red');
else
notify(JSON.parse(err.request.response)['message'], 'red');

return;
}
}
else{
repo.deleteRepo(this.createSaveRepo.bind(this));
}

}.bind(this));
}

/*
* Helper for saveAll.
*/
createSaveRepo(err, res) {
this.user.createRepo({'name': 'saved-jor1k-workspace'}, function(err, res) {

if(err)
{
if(err.request.response=='')
notify('Something happened... Try again.', 'red');
else
notify(JSON.parse(err.request.response)['message'], 'red');

return;
}

var repo = this.hub.getRepo(this.username, 'saved-jor1k-workspace');
this.pushToRepo(repo);
}.bind(this));
}

/*
* Helper for saveAll.
*
* This is very hacky... the api is very limited and doesn't allow parallel writes
*/
pushToRepo(repo) {
var fs = SysFileSystem;
var tree = fs.getDirectoryTree();

var readFile = function(err){

if(err)
{
if(err.request.response=='')
notify('Something happened... Try again.', 'red');
else
notify(JSON.parse(err.request.response)['message'], 'red');

return;
}

var i = 0;
if(typeof this == 'number')
i = this;

while(i<tree.length && tree[i].isDirectory)
i++;

if(i>=tree.length)
{
notify('Successfully pushed!', 'green');
return;
}

fs.readFile(tree[i].path, writeFile.bind(i));
};

var writeFile = function(err, buf){

if(err)
{
if(err.request.response=='')
notify('Something happened... Try again.', 'red');
else
notify(JSON.parse(err.request.response)['message'], 'red');

return;
}

var i = this;
tree[this].path = tree[this].path.substring(1,tree[this].path.length);
repo.write('master', tree[i].path, buf.toString('binary'), 'save', readFile.bind(i+1));
};

readFile();

}

}

export default (GithubInt);
7 changes: 7 additions & 0 deletions src/app/github-int/github-xmlhttpreq.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class GithubDeps {
constructor() {
this.XMLHttpRequest = window.XMLHttpRequest;
}
}

export default (new GithubDeps());
7 changes: 5 additions & 2 deletions src/app/require.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ require.config({
"browserfs": "bower_modules/browserfs/dist/browserfs.min",
"bootstrap-contextmenu":"bower_modules/bootstrap-contextmenu/bootstrap-contextmenu",
"bootbox": "bower_modules/bootbox.js/bootbox",
"github-api": "bower_modules/github-api/github",


// Application-specific modules
"app/config": "app/config/config.dev" // overridden to 'config.dist' in build config
"app/config": "app/config/config.dev", // overridden to 'config.dist' in build config
"js-base64": "app/github-int/github-base64",
"xmlhttprequest": "app/github-int/github-xmlhttpreq"
},
shim: {
"github-api": {},
"bootstrap": { deps: ["jquery"] },
"jquery-ui": { deps: ["jquery"] },
"jquery-ui-layout": { deps: ["jquery", "jquery-ui"] },
Expand Down
5 changes: 5 additions & 0 deletions src/app/sys-global-observables.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export var compileStatus = ko.observable('');
export var focusTerm = ko.observable((tty) => {});
export var runCode = ko.observable((gccOptions) => {});

export var githubUsername = ko.observable('');
export var githubPassword = ko.observable('');
export var githubRepo = ko.observable('');

export var buildCmd = ko.observable('');
export var execCmd = ko.observable('');

Expand All @@ -17,6 +21,7 @@ export var editorAnnotations = ko.observableArray([]);

export var currentFileName = ko.observable('untitled');
export var currentFilePath = ko.observable('');
export var compileBtnEnable = ko.observable('');

export var projectLicense = ko.observable('');

Expand Down
3 changes: 3 additions & 0 deletions src/components/compiler-controls/compiler-controls.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ko from 'knockout';
import templateMarkup from 'text!./compiler-controls.html';
import { notify } from 'app/notifications';
import * as SysGlobalObservables from 'app/sys-global-observables';

class CompilerControls {
constructor(params) {
Expand All @@ -20,6 +21,8 @@ class CompilerControls {
return ready;
});

SysGlobalObservables.compileBtnEnable(this.compileBtnEnable);

const $compileBtn = $('#compile-btn');
$compileBtn.click(() => {
params.compileCallback();
Expand Down
39 changes: 39 additions & 0 deletions src/components/file-browser/file-browser.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,43 @@
</ul>
</div>
<div id="file-browser-body"><span class="loading">Loading...</span></div>
<div id="github-opts-container">
<div class="row">
<div class="col-sm-9">
<div class="row">
<label class="col-sm-2 control-label"><small>username</small></label>
<div class="col-sm-4">
<input class="form-control input-sm" type="text" maxlen=256
title="Github username" data-bind="textInput: githubUsername">
</div>
<label class="col-sm-2 control-label"><small>password</small></label>
<div class="col-sm-4">
<input id="githubPassword" class="form-control input-sm" type="password" maxlen=256
title="Github password" data-bind="textInput: githubPassword">
</div>
</div>
<div class="row">
<label class="col-sm-2 control-label"><small>repo&nbsp;url</small></label>
<div class="col-sm-10">
<input id="githubRepo" class="form-control input-sm" type="text" maxlen=256 title="URL of repo to clone into workspace" placeholder="username/saved-jor1k-workspace"
data-bind="textInput: githubRepo">
</div>
</div>
</div>
<div>
<button id="save-workspace-btn"
data-bind="enable: compileBtnEnable"
title="push workspace to 'saved-jor1k-workspace' repo"
type="button" class="btn btn-xs pull-right col-sm-3 btn-default">
Save Workspace<br>
</button>
<button id="clone-repo-btn"
data-bind="enable: compileBtnEnable"
title="clone repo into workspace"
type="button" class="btn btn-xs pull-right col-sm-3 btn-default">
Clone Repo<br>
</button>
</div>
</div>
</div>
</div>
Loading