redirect after main file imported

This commit is contained in:
Steven Hugg 2019-05-08 13:42:24 -04:00
parent 4cc9aaeaca
commit 19d145bbd5
4 changed files with 65 additions and 16 deletions

View File

@ -143,7 +143,6 @@ TODO:
- what if files already open in editor
- import twice?
- un-bind from repo?
- login/logout?
WEB WORKER FORMAT

View File

@ -21,15 +21,45 @@ const README_md_template = "$NAME\n=====\n\nCompatible with the [$PLATFORM](http
export class GithubService {
githubCons;
githubToken;
github;
store;
project : CodeProject;
branch : string = "master";
constructor(github, store, project : CodeProject) {
this.github = github;
constructor(githubCons:() => any, githubToken:string, store, project : CodeProject) {
this.githubCons = githubCons;
this.githubToken = githubToken;
this.store = store;
this.project = project;
this.recreateGithub();
}
recreateGithub() {
this.github = new this.githubCons({token:this.githubToken});
}
login() : Promise<void> {
// already logged in? return immediately
if (this.githubToken && this.githubToken.length) {
return new Promise<void>( (yes,no) => {
yes();
});
}
// login via popup
var provider = new firebase.auth.GithubAuthProvider();
provider.addScope('repo');
return firebase.auth().signInWithPopup(provider).then( (result) => {
this.githubToken = result.credential.accessToken;
var user = result.user;
this.recreateGithub();
document.cookie = "__github_key=" + this.githubToken + ";path=/;max-age=31536000";
console.log("Stored GitHub OAUTH key");
}).catch( (error) => {
console.log(error);
alert("Could not login to GitHub: " + error);
});
}
isFileIgnored(s : string) : boolean {
@ -94,6 +124,7 @@ export class GithubService {
return sess.repo.contents('README.md').read();
})
.catch( () => {
console.log('no README.md found')
return ''; // empty README
})
.then( (readme) => {
@ -101,7 +132,7 @@ export class GithubService {
// check README for main file
const re8main = /\(([^)]+)#mainfile\)/;
m = re8main.exec(readme);
if (m) {
if (m && m[1]) {
console.log("main path: '" + m[1] + "'");
sess.mainPath = m[1];
}
@ -112,7 +143,7 @@ export class GithubService {
throw "Platform mismatch: Repository is " + m[1] + ", you have " + this.project.platform_id + " selected.";
}
// get head commit
return this.pull(ghurl);
return sess;
});
}
@ -151,6 +182,12 @@ export class GithubService {
return sess;
});
}
importAndPull(ghurl:string) {
return this.import(ghurl).then((sess) => {
return this.pull(ghurl);
});
}
publish(reponame:string, desc:string, license:string, isprivate:boolean) : Promise<GHSession> {
var repo;

View File

@ -383,19 +383,20 @@ function getCookie(name) {
function getGithubService() {
if (!githubService) {
// get github API key from cookie
// TODO: move to service?
var ghkey = getCookie('__github_key');
var ghopts = {token:ghkey};
githubService = new GithubService(new exports['Octokat'](ghopts), store, current_project);
githubService = new GithubService(exports['Octokat'], ghkey, store, current_project);
console.log("loaded github service");
}
return githubService;
}
function getBoundGithubURL() {
function getBoundGithubURL() : string {
console.log("main path: " + current_project.mainPath);
var ghurl = getGithubService().getBoundURL(current_project.mainPath);
console.log("Github URL: " + ghurl);
return ghurl || alert("This project (" + current_project.mainPath + ") is not bound to a GitHub project.");
if (!ghurl) alert("This project (" + current_project.mainPath + ") is not bound to a GitHub project.")
return ghurl;
}
function _importProjectFromGithub(e) {
@ -404,9 +405,17 @@ function _importProjectFromGithub(e) {
modal.modal('show');
btn.off('click').on('click', () => {
var githuburl = $("#importGithubURL").val()+"";
getGithubService().import(githuburl).then( (sess:GHSession) => {
var sess;
getGithubService().import(githuburl).then( (sess1:GHSession) => {
sess = sess1;
return getGithubService().pull(githuburl);
}).then( (sess2:GHSession) => {
// TODO: only first sessino has mainPath
if (sess.mainPath) {
reloadPresetNamed(sess.prefix + sess.mainPath);
} else {
updateSelector();
alert("Files imported, but no main file was found so you'll have to select this project in the pulldown.");
}
// TODO : redirect to main file
modal.modal('hide');
@ -435,7 +444,9 @@ function _publishProjectToGithub(e) {
var sess;
modal.modal('hide');
setWaitDialog(true);
getGithubService().publish(name, desc, license, priv).then( (_sess) => {
getGithubService().login().then( () => {
return getGithubService().publish(name, desc, license, priv);
}).then( (_sess) => {
sess = _sess;
console.log(sess);
return current_project.migrateToNewFolder(sess.prefix);
@ -487,7 +498,9 @@ function pushChangesToGithub(message:string) {
}
// push files
setWaitDialog(true);
return getGithubService().commitPush(ghurl, message, files).then( (sess) => {
return getGithubService().login().then( () => {
return getGithubService().commitPush(ghurl, message, files);
}).then( (sess) => {
setWaitDialog(false);
alert("Pushed files to " + ghurl);
return sess;

View File

@ -19,7 +19,7 @@ function newGH(store, platform_id) {
var project = new prj.CodeProject({}, platform_id||test_platform_id, null, store);
project.mainPath = 'local/main.asm';
project.updateFileInStore(project.mainPath, '\torg $0 ; test\n');
return new serv.GithubService(new Octokat({token:'ec64fdd81dedab8b7547388eabef09288e9243a9'}), store, project);
return new serv.GithubService(Octokat, 'ec64fdd81dedab8b7547388eabef09288e9243a9', store, project);
}
const t0 = new Date().getTime();
@ -29,7 +29,7 @@ describe('Store', function() {
it('Should import from Github (check README)', function(done) {
var store = mstore.createNewPersistentStore(test_platform_id, function(store) {
var gh = newGH(store);
gh.import('https://github.com/pzpinfo/testrepo1557322631070').then( (sess) => {
gh.importAndPull('https://github.com/pzpinfo/testrepo1557322631070').then( (sess) => {
console.log(sess.paths);
assert.equal(2, sess.paths.length);
// TODO: test for presence in local storage, make sure returns keys
@ -41,7 +41,7 @@ describe('Store', function() {
it('Should import from Github (no README)', function(done) {
var store = mstore.createNewPersistentStore(test_platform_id, function(store) {
var gh = newGH(store);
gh.import('https://github.com/pzpinfo/testrepo3').then( (sess) => {
gh.importAndPull('https://github.com/pzpinfo/testrepo3').then( (sess) => {
console.log(sess.paths);
assert.equal(3, sess.paths.length);
// TODO: test for presence in local storage, make sure returns keys
@ -53,7 +53,7 @@ describe('Store', function() {
it('Should import from Github (wrong platform)', function(done) {
var store = mstore.createNewPersistentStore('_FOO', function(store) {
var gh = newGH(store, '_FOO');
gh.import('https://github.com/pzpinfo/testrepo1557326056720').catch( (e) => {
gh.importAndPull('https://github.com/pzpinfo/testrepo1557326056720').catch( (e) => {
assert.ok(e.startsWith('Platform mismatch'));
done();
});