From 19d145bbd57c2821bef09cb9caacaf00399df38d Mon Sep 17 00:00:00 2001 From: Steven Hugg Date: Wed, 8 May 2019 13:42:24 -0400 Subject: [PATCH] redirect after main file imported --- doc/notes.txt | 1 - src/services.ts | 45 ++++++++++++++++++++++++++++++++++++++---- src/ui.ts | 27 ++++++++++++++++++------- test/cli/testgithub.js | 8 ++++---- 4 files changed, 65 insertions(+), 16 deletions(-) diff --git a/doc/notes.txt b/doc/notes.txt index bc6db787..e9f5cc80 100644 --- a/doc/notes.txt +++ b/doc/notes.txt @@ -143,7 +143,6 @@ TODO: - what if files already open in editor - import twice? - un-bind from repo? - - login/logout? WEB WORKER FORMAT diff --git a/src/services.ts b/src/services.ts index 49042b8e..0f5065d1 100644 --- a/src/services.ts +++ b/src/services.ts @@ -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 { + // already logged in? return immediately + if (this.githubToken && this.githubToken.length) { + return new Promise( (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 { var repo; diff --git a/src/ui.ts b/src/ui.ts index 251104f5..2cc971a3 100644 --- a/src/ui.ts +++ b/src/ui.ts @@ -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; diff --git a/test/cli/testgithub.js b/test/cli/testgithub.js index 0a67c66e..d790d264 100644 --- a/test/cli/testgithub.js +++ b/test/cli/testgithub.js @@ -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(); });