Skip to content

Commit

Permalink
Use argument to specify challenge to refresh #23
Browse files Browse the repository at this point in the history
  • Loading branch information
mariuscoto committed Jan 31, 2015
1 parent ee47a6f commit 24a59dd
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 51 deletions.
113 changes: 65 additions & 48 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,65 +272,81 @@ exports.eval_formulae = function(formulae, pull) {
}

/*
Refresh all repos from all challeneges that are active ('live').
Refresh specified challenge.
If no argument is provided, all challenges are refreshed, as long as
they are active ('live').
*/
exports.refresh_challenges = function() {

Challenges.find({'status': 'live'}).exec(gotChallenges);
exports.refresh_challenges = function(challenge) {
if (typeof challenge !== 'undefined') {
gotChallenges('', [challenge])
} else {
Challenges.find({'status': 'live'}).exec(gotChallenges)
}

function gotChallenges(err, all) {

// For each challenge in pool
for (var c in all) {

var ch = all[c];

all.forEach(function(ch) {
// Update last refresh date
var update = {$set: {'refresh': Date.now()}};
Challenges.update({'link': ch.link}, update).exec();

//New request for each repo of challenge
for (var r=0; r<ch.repos.length; r++) {

var options = {
host: "api.github.com",
path: "/repos/" + ch.repos[r] + "/pulls?state=all",
method: "GET",
headers: { "User-Agent": "github-connect" }
};

var request = https.request(options, function(response){
var body = '';
var update = {$set: {'refresh': Date.now()}}
Challenges.update({'link': ch.link}, update).exec()

// Request repo info
ch.repos.forEach(function(repo) {
get_repo_info(ch, repo)
})
})
}

response.on("data", function(chunk){
body+=chunk.toString("utf8");
});
function get_repo_info(ch, repo, token) {
token_query = ''
if (typeof token !== 'undefined')
token_query = '&access_token=' + token

var options = {
host: 'api.github.com',
path: '/repos/' + repo + '/pulls?state=all' + token_query,
method: 'GET',
headers: {
'User-Agent': 'github-connect'
}
}

response.on("end", function(){
var pulls = JSON.parse(body);
var request = https.request(options, function(response) {
var body = ''

// Log errors
if (pulls.message)
console.log("[ERR] " + pulls.message + " - " + options.path
+ " (" + pulls.documentation_url + ")");
response.on("data", function(chunk) {
body+=chunk.toString("utf8")
})

for (var p in pulls) {
response.on("end", function() {
var pulls = JSON.parse(body)

// Log errors
if (pulls.message) {
if (pulls.message.match('API rate limit exceeded')) {
console.log('[ERR] Rate limited. Retrying ...')
//arguments.callee(ch, repo, new_token)
return

} else {
console.log("[ERR] " + pulls.message + " - " + options.path
+ " (" + pulls.documentation_url + ")")
}
}

// Accept only pulls created after challenge start date, before end
// date and only from registered users
if (new Date(pulls[p].created_at).getTime() > ch.start.getTime() &&
new Date(pulls[p].created_at).getTime() < ch.end.getTime() &&
ch.users.indexOf(pulls[p].user.login) > -1) {
pulls.forEach(function(pull) {
// Accept only pulls created after challenge start date, before end
// date and only from registered users
if (new Date(pull.created_at).getTime() > ch.start.getTime() &&
new Date(pull.created_at).getTime() < ch.end.getTime() &&
ch.users.indexOf(pull.user.login) > -1) {

create_patch_request(ch, pulls[p]);
}
}
});
});
request.end();
create_patch_request(ch, pull)
}
})
})
})

}
}
request.end()
}
}

Expand All @@ -355,6 +371,7 @@ exports.add_user = function(userid, username, callback) {
var update = {
'user_id': u.id,
'user_name': u.login,
'token': '32131231231231321',
'user_fullname': 'Development user',
'user_email': '[email protected]',
'avatar_url': 'https://avatars.githubusercontent.com/u/0',
Expand Down
6 changes: 3 additions & 3 deletions routes/challenge.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,11 @@ Manually refresh challenges.
*/
exports.refresh = function(req, res) {

Challenges.findOne({'link': req.params.ch}).exec(gotChallenge);
Challenges.findOne({'link': req.params.ch}).exec(gotChallenge)

function gotChallenge(err, ch) {
core.refresh_challenges();
res.redirect('/challenges/' + req.params.ch);
core.refresh_challenges(ch)
res.redirect('/challenges/' + req.params.ch)
}
};

Expand Down

0 comments on commit 24a59dd

Please sign in to comment.