Skip to content

Commit

Permalink
working GitHub API call for repo main file listing
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Magliozzi authored and Andrew Magliozzi committed May 22, 2013
1 parent f3d9c13 commit 80d47de
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 149 deletions.
7 changes: 0 additions & 7 deletions GitGenius.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@
<meta charset="utf-8">
<title>GitGenius Repository Anotations</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
var files = [["docs", "#"], ["CNAME", "#"], ["CONTRIBUTING.md", "#"], ["LICENSE", "#"], ["README.md", "#"], ["Rakefile", "#"], ["index.html", "#"], ["package.json", "#"], ["underscore-min.js", "#"], ["underscore.js", "./underscorejs"]]

for (var i = 0; i < files.length; i++) {
Files.insert({name: files[i][0], path: files[i][1]});
}
</script>
<script src="https://test.hypothes.is/app/embed.js"></script>
<meta name="description" content="">
<meta name="author" content="">
Expand Down
266 changes: 124 additions & 142 deletions GitGenius.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
Files = new Meteor.Collection("files");
var cred = 'client_id=eb9827e7a7daab7678ce&client_secret=b596b7ca0af0f554fe396e81cd647ebe4b0ebb4e';


if (Meteor.isClient) {


Meteor.Router.add({
'/': 'home',
'/addarepo': 'addarepo',
Expand Down Expand Up @@ -35,12 +38,6 @@ if (Meteor.isClient) {
'click .home': function() {
Meteor.Router.to('/home');
},

'submit .form-signin': function(){
var url = $('.input-block-level').val()
Meteor.getFilesForRepo(url);
Meteor.Router.to('/')
}
};

Template.home.events = {
Expand All @@ -52,6 +49,12 @@ if (Meteor.isClient) {
},
'click .bootstrapDir': function() {
Meteor.Router.to('/bootstrapDir');
},
'submit .form-signin': function(e){
var url = $('.input-block-level').val();
e.preventDefault();
getFilesForRepo(url);
Meteor.router.to('/')
}
}

Expand All @@ -65,146 +68,125 @@ if (Meteor.isClient) {
Template.underscoreDir.files = function() {
return Files.find({});
};


// we call this method upon github repo URL submission to get the repo tree
var getFilesForRepo = function (url){
var link = url.split('/'),
repo = link[link.length - 1],
user = link[link.length - 2];

Meteor.http.get('https://api.github.com/repos/' + user + '/' + repo + '/contents?' + cred, {
contentType: 'application/json',
dataType: 'jsonp'
}, function (err, res) {
if (err) throw 'failed callback';
for (var i = 0; i < res.data.length; i++) {
Files.insert({
'repoLink' : 'http://github.com/' + user + '/' + repo,
'repositoryOwner' : user,
'fileName' : res.data[i].name,
'sha' : res.data[i].sha,
'fileType' : res.data[i].type,
'fileURL' : res.data[i].git_url,
'filePath': res.data[i].path,
'updates' : new Date()
});
};
}
);
};


// call this function any time a user clicks a link to view one of our files
var getFileContents = function (fileURL){ // make sure the 'fileURL' is stored as data in the file link
var bitContent;
Meteor.http.get(fileURL + "?" + cred, {
contentType: 'application/json',
dataType: 'jsonp'
}, function (err, res) {
if (err) throw 'failed callback';
bitContent = results.data.content;
var plainContent = decode64(bitContent);
console.log('results', results);
return plainContent;
});
};

var _utf8_decode = function (utftext) {
var string = "";
var i = 0;
var c = c1 = c2 = 0;

while ( i < utftext.length ) {

c = utftext.charCodeAt(i);

if (c < 128) {
string += String.fromCharCode(c);
i++;
}
else if((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i+1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
}
else {
c2 = utftext.charCodeAt(i+1);
c3 = utftext.charCodeAt(i+2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}

}

return string;
};

var decode64 = function (input) {
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

while (i < input.length) {

enc1 = _keyStr.indexOf(input.charAt(i++));
enc2 = _keyStr.indexOf(input.charAt(i++));
enc3 = _keyStr.indexOf(input.charAt(i++));
enc4 = _keyStr.indexOf(input.charAt(i++));

chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;

output = output + String.fromCharCode(chr1);

if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}

}

output = _utf8_decode(output);

return output;

};
}


if (Meteor.isServer) {
Meteor.startup(function () {
var cred = 'client_id=eb9827e7a7daab7678ce&client_secret=b596b7ca0af0f554fe396e81cd647ebe4b0ebb4e';

// call this method upon github repo URL submission to get all
Meteor.getFilesForRepo = function (url){
var link = url.split('/'),
repo = link[link.length - 1],
user = link[link.length - 2],
fut = new Future();

Meteor.http.get('https://api.github.com/repos/' + user + '/' + repo + '/contents?recursive=1?' + cred, {
contentType: 'application/json',
dataType: 'jsonp'
}, function (err, res) {
if (err) throw 'failed callback';
console.log(res.data);
});
var result = fut.wait();
if (result.statusCode === 200) {
var somedata = result.data;
console.log('some data', somedata);
for (var i = 0; i < somedata.length; i++) {
var tempURL = somedata[i].git_url;
// console.log('url', tempURL);
Files.findAndModify({
query : {
'url' : tempURL
},
update : {
'repository' : 'http://github.com/' + user + '/' + repo,
'repositoryOwner' : user,
'fileName' : somedata[i].name,
'filePath' : somedata[i].path,
'fileType' : somedata[i].type,
'url' : tempURL,
'sha' : somedata[i].sha,
'dateUploaded' : new Date()
},
upsert: true
});
};
} else {
console.log('trouble getting repo list');
};
};

// call this function any time a user clicks a link to view one of our files
Meteor.getFileContents = function (url){
var bitContent,
fut = new Future();

Meteor.http.get(fileURL + "?" + cred, {
contentType: 'application/json',
dataType: 'jsonp'
}, function (err, res) {
if (err) throw 'failed callback';
});

var result = fut.wait();
if (result.statusCode === 200) {
bitContent = results.data.content;
// console.log('bitContent', bitContent);
var plainContent = decode64(bitContent);
// console.log(plainContent);
console.log('results', results);
return plainContent;
} else {
console.log('error getting file');
}
};

var _utf8_decode = function (utftext) {
var string = "";
var i = 0;
var c = c1 = c2 = 0;

while ( i < utftext.length ) {

c = utftext.charCodeAt(i);

if (c < 128) {
string += String.fromCharCode(c);
i++;
}
else if((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i+1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
}
else {
c2 = utftext.charCodeAt(i+1);
c3 = utftext.charCodeAt(i+2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}

}

return string;
};

var decode64 = function (input) {
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

while (i < input.length) {

enc1 = _keyStr.indexOf(input.charAt(i++));
enc2 = _keyStr.indexOf(input.charAt(i++));
enc3 = _keyStr.indexOf(input.charAt(i++));
enc4 = _keyStr.indexOf(input.charAt(i++));

chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;

output = output + String.fromCharCode(chr1);

if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}

}

output = _utf8_decode(output);

return output;

};



});
}

0 comments on commit 80d47de

Please sign in to comment.