Skip to content

Instantly share code, notes, and snippets.

@jmcarp
Last active November 22, 2015 22:54
Show Gist options
  • Select an option

  • Save jmcarp/f108cd32f5aa1d1a7733 to your computer and use it in GitHub Desktop.

Select an option

Save jmcarp/f108cd32f5aa1d1a7733 to your computer and use it in GitHub Desktop.
Store all travis builds for a given repo in mongo
/* global console */
var urijs = require('urijs');
var Promise = require('bluebird');
var request = Promise.promisifyAll(require('request'));
var mongodb = Promise.promisifyAll(require('mongodb'));
var baseUrl = 'https://api.travis-ci.org';
var client = mongodb.MongoClient.connect('mongodb://localhost:27017/dashboard');
function update(user, repo, opts) {
return client.then(function(db) {
var collection = db.collection('travis');
var uri = urijs(baseUrl).path(['repos', user, repo, 'builds'].join('/'));
return fetch(uri, collection, opts);
});
}
function fetch(uri, collection, opts) {
console.log('fetching', uri.toString());
return request.getAsync(uri.toString()).then(function(response) {
var builds = JSON.parse(response.body);
if (!builds.length) { return; }
var bulk = collection.initializeUnorderedBulkOp();
builds.forEach(function(build) {
bulk
.find({id: build.id, repository_id: build.repository_id})
.upsert().updateOne(build);
});
return bulk.execute().then(function(result) {
if (result.nMatched && !opts.complete) { return; }
var build = builds[builds.length - 1];
var nextUri = uri.query({after_number: build.number});
return fetch(nextUri, collection, opts);
});
});
}
module.exports = {update: update};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment