<commit_before>#!/usr/bin/env perl
# Copyright © 2016
#     <NAME> <<EMAIL>>
#     <NAME> <<EMAIL>>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

use v5.10;
use strict;
use warnings;
use utf8;

package Perl6IRCBotable;
use base 'Bot::BasicBot';

use Cwd qw(cwd abs_path);
use Encode qw(encode_utf8 decode_utf8);
use File::Temp qw(tempfile tempdir);
use HTTP::Tiny;
use IPC::Open3;
use JSON::XS;
use Net::GitHub;
use Time::HiRes qw(time);

use constant RAKUDO => abs_path('./rakudo');
use constant BUILDS => abs_path('./builds');
use constant CONFIG => abs_path('./config.json');
use constant SOURCE => 'https://github.com/perl6/bisectbot';

$ENV{'RAKUDO_ERROR_COLOR'} = '';

sub timeout {
  return 10;
}

sub get_output {
  my $self = shift;

  my $out = '';
  my $wait = $self->timeout();
  my $s_start = time();
  my $pid = open3(undef, \*RESULT, \*RESULT, @_);
  {
    local $SIG{ALRM} = sub { kill 9, $pid; $out = "«timed out after $wait seconds, output»: " };
    alarm $wait;
    waitpid($pid, 0);
    alarm 0;
  }
  my $s_end = time();

  my $exit_status = $? >> 8;
  my $exit_signal = $? & 127;

  $out .= decode_utf8(do { local $/; <RESULT> }) // ''; # assume UTF-8, that's the best we can do
  chomp $out;

  return ($out, $exit_status, $exit_signal, $s_end - $s_start)
}

sub to_full_commit {
  my ($self, $commit) = @_;

  my $old_dir = cwd();
  chdir RAKUDO;
  return if system('git', 'rev-parse', '--verify', $commit) != 0; # make sure that $commit is valid
  my ($result, $exit_status, $exit_signal, $time) = $self->get_output('git', 'rev-list', '-1', $commit); # use rev-list to handle tags
  chdir $old_dir;

  return if $exit_status != 0;
  return $result
}

sub write_code {
  my $self = shift;

  my ($fh, $filename) = tempfile(UNLINK => 1); # will unlink on program exit
  binmode $fh, ':encoding(UTF-8)';
  print $fh @_;
  close $fh;
  return $filename
}

sub process_message {
  my ($self, $message, $body) = @_;
  # Bot-specific code goes here
  return
}

sub process_url {
  my ($self, $url, $message) = @_;

  my $response = HTTP::Tiny->new->get($url); # $body is actually a url
  if (not $response->{success}) {<commit_msg>Make sure to chdir back in perl 5 base class<commit_after>#!/usr/bin/env perl
# Copyright © 2016
#     <NAME> <<EMAIL>>
#     <NAME> <<EMAIL>>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

use v5.10;
use strict;
use warnings;
use utf8;

package Perl6IRCBotable;
use base 'Bot::BasicBot';

use Cwd qw(cwd abs_path);
use Encode qw(encode_utf8 decode_utf8);
use File::Temp qw(tempfile tempdir);
use HTTP::Tiny;
use IPC::Open3;
use JSON::XS;
use Net::GitHub;
use Time::HiRes qw(time);

use constant RAKUDO => abs_path('./rakudo');
use constant BUILDS => abs_path('./builds');
use constant CONFIG => abs_path('./config.json');
use constant SOURCE => 'https://github.com/perl6/bisectbot';

$ENV{'RAKUDO_ERROR_COLOR'} = '';

sub timeout {
  return 10;
}

sub get_output {
  my $self = shift;

  my $out = '';
  my $wait = $self->timeout();
  my $s_start = time();
  my $pid = open3(undef, \*RESULT, \*RESULT, @_);
  {
    local $SIG{ALRM} = sub { kill 9, $pid; $out = "«timed out after $wait seconds, output»: " };
    alarm $wait;
    waitpid($pid, 0);
    alarm 0;
  }
  my $s_end = time();

  my $exit_status = $? >> 8;
  my $exit_signal = $? & 127;

  $out .= decode_utf8(do { local $/; <RESULT> }) // ''; # assume UTF-8, that's the best we can do
  chomp $out;

  return ($out, $exit_status, $exit_signal, $s_end - $s_start)
}

sub to_full_commit {
  my ($self, $commit) = @_;

  my $old_dir = cwd();
  chdir RAKUDO;
  if (system('git', 'rev-parse', '--verify', $commit) != 0) { # make sure that $commit is valid
    chdir $old_dir;
    return;
  }
  my ($result, $exit_status, $exit_signal, $time) = $self->get_output('git', 'rev-list', '-1', $commit); # use rev-list to handle tags
  chdir $old_dir;

  return if $exit_status != 0;
  return $result
}

sub write_code {
  my $self = shift;

  my ($fh, $filename) = tempfile(UNLINK => 1); # will unlink on program exit
  binmode $fh, ':encoding(UTF-8)';
  print $fh @_;
  close $fh;
  return $filename
}

sub process_message {
  my ($self, $message, $body) = @_;
  # Bot-specific code goes here
  return
}

sub process_url {
  my ($self, $url, $message) = @_;

  my $response = HTTP::Tiny->new->get($url); # $body is actually a url
  if (not $response->{success}) {
{'fraction_non_alphanumeric': 0.15106497225702523, 'fraction_numerical': 0.011276176839090747, 'mean_word_length': 3.7597955706984667, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 43, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '7346311', 'n_tokens_mistral': 2030, 'n_tokens_neox': 1949, 'n_words': 677}