<commit_before>/*
 * grunt-willitmerge
 * https://github.com/shama/grunt-github
 *
 * Copyright (c) 2012 <NAME>
 * Licensed under the MIT license.
 */
/*globals require:true, process:true*/

module.exports = function(grunt) {
  'use strict';

  var util = require('util');
  var events = require('events');

  // libs
  var path = require('path');
  var exec = require('child_process').exec;
  var request = require('request');
  var rimraf = require('rimraf');
  var readline = require('readline');

  // TODO: ditch this when grunt v0.4 is released
  grunt.util = grunt.util || grunt.utils;

  // globals
  var _ = grunt.util._;
  var async = grunt.util.async;
  var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
  });

  // WillItMerge
  var WillItMerge = function(opts) {
    events.EventEmitter.call(this);

    this.opts = _.defaults(opts || {}, {
      auth: {},
      ignore: [],
      comment: 'Sorry, your pull request is unmergeable. ' +
        'Could you please rebase, squash and force push it? Thanks!'
    });

    // local vars
    this.tmpPatch = path.join(process.cwd(), '_tmp', process.pid + '.patch');
    this.isAuthed = !_.isEmpty(this.opts.auth);

    // find this user and repo
    this.userRepo = (function() {
      var res = {user:'',repo:''};
      var repo = grunt.file.readJSON(path.join(process.cwd(), 'package.json')).repository;
      if (repo.type === 'git' && _.isString(repo.url) && repo.url.indexOf('github.com') !== -1) {
        var arr = repo.url.split('/').slice(-2);
        res.user = arr[0];
        res.repo = arr[1].replace('.git', '');
      }
      return res;
    }());
  };
  util.inherits(WillItMerge, events.EventEmitter);

  // search github for open repo issues
  WillItMerge.prototype.getRepoIssues = function() {
    var that = this;
    grunt.helper('github', that.opts.auth, function githubHelper(err, github) {
      if (err) {throw err;}
      that.github = github;
      var msg = {
        user: that.userRepo.user,
        repo: that.userRepo.repo,
        state: 'open'
      };
      github.issues.repoIssues(msg, function githubRepoIssues(err, res) {
        if (err) { throw err; }
        that.emit('issues', null, that.extractPulls(res));
      });
    });
  };

  // process an issue
  WillItMerge.prototype.processIssue = function(iss, done) {
    var that = this;
    if (_.indexOf(that.opts.ignore, iss.number) !== -1) {
      grunt.log.writeln('Issue #' + iss.number + '... ' + 'SKIP'.cyan);
      return done();
    }
    grunt.log.write('Issue #' + iss.number + ', ' + iss.html_url + ', will it merge? ');
    request(iss.pull_request.patch_url, function patchRequest(err, res, body) {
      if (!err && res.statusCode == 200) {

        grunt.file.mkdir(path.dirname(that.tmpPatch));
        grunt.file.write(that.tmpPatch, body);
        var cmd = 'git apply --check --stat ' + that.tmpPatch;
        exec(cmd, function execCommand(err, stdout, stderr) {
          if (stderr) {
            that.mergeFailed(stderr, iss, done);
          } else {
            that.mergeSuccess(stdout, iss, done);
          }
        });

      } else {
        // error?
        return done();
      }
    });
  };

  // if merge failed
  WillItMerge.prototype.mergeFailed = function(msg, iss, done) {
    var that = this;<commit_msg>Use streams<commit_after>/*
 * grunt-willitmerge
 * https://github.com/shama/grunt-github
 *
 * Copyright (c) 2012 <NAME>
 * Licensed under the MIT license.
 */
/*globals require:true, process:true*/

module.exports = function(grunt) {
  'use strict';

  var util = require('util');
  var events = require('events');

  // libs
  var path = require('path');
  var fs = require('fs');
  var exec = require('child_process').exec;
  var request = require('request');
  var rimraf = require('rimraf');
  var readline = require('readline');

  // TODO: ditch this when grunt v0.4 is released
  grunt.util = grunt.util || grunt.utils;

  // globals
  var _ = grunt.util._;
  var async = grunt.util.async;
  var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
  });

  // WillItMerge
  var WillItMerge = function(opts) {
    events.EventEmitter.call(this);

    this.opts = _.defaults(opts || {}, {
      auth: {},
      ignore: [],
      comment: 'Sorry, your pull request is unmergeable. ' +
        'Could you please rebase, squash and force push it? Thanks!'
    });

    // local vars
    this.tmpPatch = path.join(process.cwd(), '_tmp', process.pid + '.patch');
    this.isAuthed = !_.isEmpty(this.opts.auth);

    // find this user and repo
    this.userRepo = (function() {
      var res = {user:'',repo:''};
      var repo = grunt.file.readJSON(path.join(process.cwd(), 'package.json')).repository;
      if (repo.type === 'git' && _.isString(repo.url) && repo.url.indexOf('github.com') !== -1) {
        var arr = repo.url.split('/').slice(-2);
        res.user = arr[0];
        res.repo = arr[1].replace('.git', '');
      }
      return res;
    }());
  };
  util.inherits(WillItMerge, events.EventEmitter);

  // search github for open repo issues
  WillItMerge.prototype.getRepoIssues = function() {
    var that = this;
    grunt.helper('github', that.opts.auth, function githubHelper(err, github) {
      if (err) {throw err;}
      that.github = github;
      var msg = {
        user: that.userRepo.user,
        repo: that.userRepo.repo,
        state: 'open'
      };
      github.issues.repoIssues(msg, function githubRepoIssues(err, res) {
        if (err) { throw err; }
        that.emit('issues', null, that.extractPulls(res));
      });
    });
  };

  // process an issue
  WillItMerge.prototype.processIssue = function(iss, done) {
    var that = this;
    if (_.indexOf(that.opts.ignore, iss.number) !== -1) {
      grunt.log.writeln('Issue #' + iss.number + '... ' + 'SKIP'.cyan);
      return done();
    }
    grunt.log.write('Issue #' + iss.number + ', ' + iss.html_url + ', will it merge? ');

    grunt.file.mkdir(path.dirname(that.tmpPatch));
    request(iss.pull_request.patch_url)
      .on('end', function() {// why doesnt pipe work before end?

        var cmd = 'git apply --check --stat ' + that.tmpPatch;
        exec(cmd, function execCommand(err, stdout, stderr) {
          if (stderr) {
            that.mergeFailed(stderr, iss, done);
          } else {
            that.mergeSuccess(stdout, iss, done);
          }
        });

      })
      .pipe(fs.createWriteStream(that.tmpPatch));
  };

  // if merge failed
  WillItMerge.prototype.mergeFailed = function(msg, iss, done) {
    var that = this;
{'fraction_non_alphanumeric': 0.16417233560090702, 'fraction_numerical': 0.003779289493575208, 'mean_word_length': 2.5436529191215853, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4079746', 'n_tokens_mistral': 2187, 'n_tokens_neox': 2136, 'n_words': 484}