Skip to content

Instantly share code, notes, and snippets.

@Python1320
Created March 19, 2015 21:19
Show Gist options
  • Select an option

  • Save Python1320/d800c3803c15b6f18508 to your computer and use it in GitHub Desktop.

Select an option

Save Python1320/d800c3803c15b6f18508 to your computer and use it in GitHub Desktop.
An attempt at getting new github commits for any repo.
// GitHub please let us subscribe to others repos with pubsubhubbub or something. your RSS and emails are annoying.
var fs = require("fs");
var data = require("./test_storage.json");
console.log("Config: ",JSON.stringify(data, null, 2));
function data_flush() {
fs.writeFile( "test_storage.json", JSON.stringify( data ), "utf8", function(err){
if (err) throw err;
console.log("<Saved: "+JSON.stringify( data )+">");
} );
}
var GitHubApi = require("github-cache");
var github = new GitHubApi({
// required
version: "3.0.0",
// optional
debug: false,
protocol: "https",
timeout: 5000,
headers: {
"user-agent": "Python1320@github metastruct repowatcher"
},
cachedb: './cachedb',
validateCache: true
});
var lastsince = data.lastsince;
var lastsha = data.lastsince;
var params = {
// optional:
// headers: {
// "cookie": "blahblah"
// },
user: "python1320",
repo: "test"
}
if (lastsince){
params.since = lastsince;
} else {
lastsince = '1970-01-01T00:00:00.000Z';
}
console.log("Getting entries since: ",lastsince);
github.repos.getCommits(params, function(err, commits) {
//if (err) return;
if (err) throw err;
//console.log(JSON.stringify(commits, null, 2));
var count = commits.length;
for(var i = count -1; i >= 0 ; i--){
var v = commits[i];
if((v.commit.author.date==lastsince) || (v.commit.committer.date==lastsince)){
commits.splice(i, 1);
}
}
var newcount = commits.length;
console.log("> Received "+newcount+" / "+count+" commits");
count=newcount;
if (count==0) {
return;
}
var biggest_since = lastsince;
var biggest_since_ts = Date.parse(biggest_since);
console.log("biggest_since_ts",biggest_since_ts);
for(var k = 0, size = commits.length; k < size ; k++) {
var v = commits[k];
var commit = v.commit;
console.log("new commit: ",commit.message);
var dat = commit.author.date;
var ts = Date.parse(dat);
//console.log("auth",dat,ts);
var changed =false;
if (ts>biggest_since_ts) {
biggest_since = dat;
biggest_since_ts = ts;
}
var dat = commit.committer.date;
var ts = Date.parse(dat);
//console.log("comm",dat,ts);
if (ts>biggest_since_ts) {
biggest_since = dat;
biggest_since_ts = ts;
}
if (changed) {
data.lastsha = v.sha;
}
//console.log("new commit (",v.sha,"): ",JSON.stringify(v.commit, null, 2));
}
console.log("biggest_since",biggest_since);
data.lastsince = biggest_since;
data_flush();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment