require('dotenv').load();

var restify = require('restify');
var server = restify.createServer();
server.use(restify.CORS());

var google = require('googleapis');
var jwtClient = new google.auth.JWT(
  process.env.GOOGLE_CLIENT_ID,
  'key.pem',
  null,
  ['https://www.googleapis.com/auth/bigquery']);

var client = restify.createJsonClient('https://api.github.com');
var auth = '?client_id='+ process.env.GITHUB_CLIENT_ID +'&client_secret='+ process.env.GITHUB_CLIENT_SECRET;

var respond = function (data, res, next) {
	res.contentType = 'json';
    res.send(data);
    next();
};

server.get('/get/:owner/:repo', function (req, res, next) {
	jwtClient.authorize(function (err, tokens) {
		if (err) {
		    respond(err, res, next);
		    return;
		}

		var bigquery = google.bigquery({ version: 'v2', auth: jwtClient });

		bigquery.jobs.query({
			projectId: process.env.GOOGLE_PROJECT_ID,
			resource: {
				query: 'SELECT u.actor, d.stars '+
					'FROM [githubarchive:github.timeline] u '+
					'JOIN EACH ( '+
					  'SELECT repository_owner, COUNT(*) AS stars '+
					  'FROM [githubarchive:github.timeline] '+
					  'WHERE type="WatchEvent" '+
					  'GROUP BY repository_owner '+
					') d ON d.repository_owner = u.actor '+
					'WHERE '+
					  'u.type="WatchEvent" '+
					  'AND u.repository_owner = "'+ req.params.owner +'" '+
					  'AND u.repository_name = "'+ req.params.repo +'" '+
					'ORDER BY d.stars DESC '+
					'LIMIT 10'
			}
		}, function (err, response) {
			if (err) {
				respond(err, res, next);
				return;
			}

			var results = {};
			var count = 0;
			for (var i = 0; i < response.rows.length; i++) {
				var row = response.rows[i];
				var name = row.f[0].v;

				results[name] = {
					name: name,
					stars: row.f[1].v
				};

				client.get('/users/'+ name + auth, function (err, req2, res2, user) {
					if (err) {
						respond(err, res, next);
						return;
					}

					results[user.login].avatar = user.avatar_url;

					count++;

					if (count === response.rows.length) {
						respond(results, res, next);
					}
				});
			}
		});
	});
});

server.listen(3000, function() {
    console.log('%s listening at %s', server.name, server.url);
});
@@ -1539,16 +1539,198 @@
 ;%0A%09%09%09%7D%0A%0A
+%09%09%09if (!response.rows) %7B%0A%09%09%09%09respond(%7B%0A%09%09%09%09%09code: 'error',%0A%09%09%09%09%09message: 'This repo was not found, or it has no stars. Please, try another one.'%0A%09%09%09%09%7D, res, next);%0A%09%09%09%09return;%0A%09%09%09%7D%0A%0A
 %09%09%09var r

add error when a repo was not found