UNPKG

clabot-fix

Version:

A bot to take the pain out of Contributor License Agreements

108 lines (102 loc) 4.32 kB
(function() { 'use strict'; var comment, exports, github, _; _ = require('lodash'); github = require('@octokit/rest'); comment = require('./comment'); exports = module.exports = function(req, res, options, contractors, payload) { var api, argument, body, method, number, poster, regex, repo, sender, user, _ref; body = payload.comment.body; poster = payload.comment.user.login; number = payload.issue.number; repo = payload.repository.name; sender = payload.issue.user.login; user = payload.repository.owner.login; regex = /\[clabot(:([a-zA-Z0-9]+)\=?([a-zA-Z0-9\_\-]+)?)+\]/; _ref = (body.match(regex) || []).slice(2), method = _ref[0], argument = _ref[1]; api = new github({ version: '3.0.0', auth: options.token }); return api.user.get({}, function(err, data) { var commentData, signed; if (!err && method === 'check' && data.login !== poster) { signed = _.contains(contractors, argument || sender); commentData = { user: user, repo: repo, number: number }; commentData.body = comment.getCommentBody(signed, options.templates, _.extend(options.templateData, { sender: argument || sender, payload: payload, check: true })); return comment.send(options.token, commentData, function(err, data) { var href; if (err) { console.log(err); console.log('Fatal Error: GitHub refused to comment'); return res.send(500, 'Fatal Error: GitHub refused to comment'); } else { href = payload.comment.html_url; console.log("Success: Comment created at " + href); return res.send(200, "Success: Comment created at " + href); } }); } else if (!err && method === 'accept' && data.login !== poster) { if (poster === sender) { signed = _.contains(contractors, poster); if (!signed) { if (_.isFunction(options.addContractor)) { return options.addContractor(poster, function(success) { if (!success) { console.log('Fatal Error: Unable to add signee'); return res.send(500, 'Fatal Error: Unable to add signee'); } else { console.log("Success: Added contractor"); commentData = { user: user, repo: repo, number: number }; commentData.body = comment.getCommentBody('confirm', options.templates, _.extend(options.templateData, { sender: argument || sender, payload: payload })); return comment.send(options.token, commentData, function(err, data) { var href; if (err) { console.log(err); console.log; return res.send(200, "Partial Success: Added contractor, but GitHub refused confirmation comment"); } else { href = payload.comment.html_url; console.log("Success: Comment created at " + href); return res.send(200, "Success: Added contractor and sent a confirmation comment at " + href); } }); } }); } else { console.log('Fatal Error: options#addContractor not provided'); return res.send(500, 'Fatal Error: options#addContractor not provided'); } } else { console.log('Contractor has already accepted the CLA'); return res.send(200, 'Contractor has already accepted the CLA'); } } else { console.log('Contractor is not owner of the GitHub Pull Request'); return res.send(200, 'Contractor is not owner of the GitHub Pull Request'); } } else { if (err) { console.log(err); } console.log('Could not find proper clabot command in comment'); return res.send(200, 'Could not find proper clabot command in comment'); } }); }; }).call(this);