1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-11-29 14:51:17 +00:00

refactored some github stuff

This commit is contained in:
Steven Hugg 2019-05-10 15:26:12 -04:00
parent 532fe0fe48
commit 2d51d3a011
2 changed files with 47 additions and 32 deletions

View File

@ -19,7 +19,10 @@ export interface GHSession extends GHRepoMetadata {
reponame : string; // repo name reponame : string; // repo name
repopath : string; // "user/repo" repopath : string; // "user/repo"
prefix : string; // file prefix, "local/" or "" prefix : string; // file prefix, "local/" or ""
branch : string; // "master" is default
repo : any; // [repo object] repo : any; // [repo object]
tree? : any; // [tree object]
head? : any; // [head ref]
paths? : string[]; paths? : string[];
} }
@ -30,11 +33,12 @@ export function getRepos() : {[key:string]:GHRepoMetadata} {
} }
export function parseGithubURL(ghurl:string) { export function parseGithubURL(ghurl:string) {
var toks = ghurl.split('/'); var toks = ghurl.split('/', 8);
if (toks.length < 5) return null; if (toks.length < 5) return null;
if (toks[0] != 'https:') return null; if (toks[0] != 'https:') return null;
if (toks[2] != 'github.com') return null; if (toks[2] != 'github.com') return null;
return {user:toks[3], repo:toks[4], repopath:toks[3]+'/'+toks[4]}; if (toks[5] && toks[5] != 'tree') return null;
return {user:toks[3], repo:toks[4], repopath:toks[3]+'/'+toks[4], branch:toks[6], treepath:toks[7]};
} }
export class GithubService { export class GithubService {
@ -44,7 +48,6 @@ export class GithubService {
github; github;
store; store;
project : CodeProject; project : CodeProject;
branch : string = "master";
constructor(githubCons:() => any, githubToken:string, store, project : CodeProject) { constructor(githubCons:() => any, githubToken:string, store, project : CodeProject) {
this.githubCons = githubCons; this.githubCons = githubCons;
@ -99,6 +102,7 @@ export class GithubService {
user: urlparse.user, user: urlparse.user,
reponame: urlparse.repo, reponame: urlparse.repo,
repopath: urlparse.repopath, repopath: urlparse.repopath,
branch: urlparse.branch || "master",
prefix: '', //this.getPrefix(urlparse.user, urlparse.repo), prefix: '', //this.getPrefix(urlparse.user, urlparse.repo),
repo: this.github.repos(urlparse.user, urlparse.repo), repo: this.github.repos(urlparse.user, urlparse.repo),
platform_id: this.project ? this.project.platform_id : null platform_id: this.project ? this.project.platform_id : null
@ -107,6 +111,22 @@ export class GithubService {
}); });
} }
getGithubHEADTree(ghurl:string) : Promise<GHSession> {
var sess;
return this.getGithubSession(ghurl).then( (session) => {
sess = session;
return sess.repo.git.refs.heads(sess.branch).fetch();
})
.then( (head) => {
sess.head = head;
return sess.repo.git.trees(head.object.sha).fetch();
})
.then( (tree) => {
sess.tree = tree;
return sess;
});
}
bind(sess:GHSession, dobind:boolean) { bind(sess:GHSession, dobind:boolean) {
var repos = getRepos(); var repos = getRepos();
if (dobind) { if (dobind) {
@ -157,17 +177,11 @@ export class GithubService {
pull(ghurl:string, deststore?) : Promise<GHSession> { pull(ghurl:string, deststore?) : Promise<GHSession> {
var sess : GHSession; var sess : GHSession;
return this.getGithubSession(ghurl).then( (session) => { return this.getGithubHEADTree(ghurl).then( (session) => {
sess = session; sess = session;
return sess.repo.commits(this.branch).fetch();
})
.then( (sha) => {
return sess.repo.git.trees(sha.sha).fetch();
})
.then( (tree) => {
let blobreads = []; let blobreads = [];
sess.paths = []; sess.paths = [];
tree.tree.forEach( (item) => { sess.tree.tree.forEach( (item) => {
console.log(item.path, item.type, item.size); console.log(item.path, item.type, item.size);
sess.paths.push(item.path); sess.paths.push(item.path);
if (item.type == 'blob' && !this.isFileIgnored(item.path)) { if (item.type == 'blob' && !this.isFileIgnored(item.path)) {
@ -238,33 +252,23 @@ export class GithubService {
commitPush( ghurl:string, message:string, files:{path:string,data:FileData}[] ) : Promise<GHSession> { commitPush( ghurl:string, message:string, files:{path:string,data:FileData}[] ) : Promise<GHSession> {
var sess : GHSession; var sess : GHSession;
var repo; return this.getGithubHEADTree(ghurl).then( (session) => {
var head;
var tree;
return this.getGithubSession(ghurl).then( (session) => {
sess = session; sess = session;
repo = sess.repo;
return repo.git.refs.heads(this.branch).fetch();
}).then( (_head) => {
head = _head;
return repo.git.trees(head.object.sha).fetch();
}).then( (_tree) => {
tree = _tree;
return Promise.all(files.map( (file) => { return Promise.all(files.map( (file) => {
if (typeof file.data === 'string') { if (typeof file.data === 'string') {
return repo.git.blobs.create({ return sess.repo.git.blobs.create({
content: file.data, content: file.data,
encoding: 'utf-8' encoding: 'utf-8'
}); });
} else { } else {
return repo.git.blobs.create({ return sess.repo.git.blobs.create({
content: btoa(byteArrayToString(file.data)), content: btoa(byteArrayToString(file.data)),
encoding: 'base64' encoding: 'base64'
}); });
} }
})); }));
}).then( (blobs) => { }).then( (blobs) => {
return repo.git.trees.create({ return sess.repo.git.trees.create({
tree: files.map( (file, index) => { tree: files.map( (file, index) => {
return { return {
path: file.path, path: file.path,
@ -273,18 +277,18 @@ export class GithubService {
sha: blobs[index]['sha'] sha: blobs[index]['sha']
}; };
}), }),
base_tree: tree.sha base_tree: sess.tree.sha
}); });
}).then( (tree) => { }).then( (newtree) => {
return repo.git.commits.create({ return sess.repo.git.commits.create({
message: message, message: message,
tree: tree.sha, tree: newtree.sha,
parents: [ parents: [
head.object.sha sess.head.object.sha
] ]
}); });
}).then( (commit) => { }).then( (commit) => {
return repo.git.refs.heads(this.branch).update({ return sess.head.update({
sha: commit.sha sha: commit.sha
}); });
}).then( (update) => { }).then( (update) => {

View File

@ -65,6 +65,17 @@ describe('Store', function() {
}); });
}); });
it('Should import from Github (subdirectory tree)', function(done) {
var store = mstore.createNewPersistentStore('nes', function(store) {
var gh = newGH(store, 'nes');
gh.importAndPull('https://github.com/brovador/NESnake/tree/master/src').then( (sess) => {
console.log(sess.paths);
assert.equal(5, sess.paths.length);
done();
});
});
});
it('Should publish (fail) on Github', function(done) { it('Should publish (fail) on Github', function(done) {
var store = mstore.createNewPersistentStore(test_platform_id, function(store) { var store = mstore.createNewPersistentStore(test_platform_id, function(store) {
var gh = newGH(store); var gh = newGH(store);
@ -113,7 +124,7 @@ describe('Store', function() {
assert.deepEqual(serv.getRepos(), {'foo/bar':{url:'_',platform_id:'vcs',mainPath:'test.c'}}); assert.deepEqual(serv.getRepos(), {'foo/bar':{url:'_',platform_id:'vcs',mainPath:'test.c'}});
gh.bind(sess, false); gh.bind(sess, false);
assert.deepEqual(serv.getRepos(), {}); assert.deepEqual(serv.getRepos(), {});
gh.getGithubSession('https://github.com/foo/bar/baz').then((sess) => { gh.getGithubSession('https://github.com/foo/bar/tree').then((sess) => {
assert.equal(sess.url, 'https://github.com/foo/bar'); assert.equal(sess.url, 'https://github.com/foo/bar');
assert.equal(sess.repopath, 'foo/bar'); assert.equal(sess.repopath, 'foo/bar');
done(); done();