var request = require('request')

// When a new invite email is recieved this is
// called and all of the pending invites are accepted
module.exports = function acceptInvites (callback) {
  var options = {
      url: 'https://api.github.com/user/repository_invitations',
      json: true,
      headers: { 'User-Agent': 'request',
                 'Authorization': 'token ' + process.env['REPOROBOT_TOKEN']
      }
  }

  acceptBatch()

  function acceptBatch () {
    request(options, function gotInvites (err, response, body) {
      var inviteCount = 0
      var acceptedCount = 0

      if (err) return callback(err)
      if (!err && response.statusCode == 200) {
        // Return if there are no pending invites
        inviteCount = body.lenth
        if (inviteCount === 0) return callback()
        console.log(new Date(), 'Accepting', body.length, 'invites')
        body.forEach(function getID (invite) {
          // Accept each invite
          options.url = 'https://api.github.com/user/repository_invitations/' + body.id
          request.patch(options, function approved (err, response, body) {
            if (err) return callback(err)
            console.log(new Date(), 'Invite', body.repository.owner.login, body.id, response.statusCode)
            acceptedCount++
            // Once you've accepted 30, the batch max, try again
            // to see if there are more, else you're done!
            if (acceptedCount === 30) return acceptBatch()
            if (acceptedCount === inviteCount) return callback()
          })
        })
      }
    })
  }
}

@@ -906,24 +906,105 @@
  (invite) %7B%0A
+          var username = invite.inviter.login%0A          var inviteID = invite.id%0A
           //
@@ -1103,23 +1103,24 @@
 ons/' + 
-body.id
+inviteID
 %0A       
@@ -1279,44 +1279,26 @@
 e', 
-body.repository.owner.login, body.id
+username, inviteID
 , re

Update username and inviteID