Permalink
Browse files

Move more towards using Pithub and take the root repo rather than forks.

  • Loading branch information...
1 parent 3b92e73 commit d96a99d974ba7019bd439f3d7285b6e9921a565b @holdenk committed Sep 18, 2011
Showing with 180 additions and 35 deletions.
  1. +1 −0 README
  2. +17 −0 blacklist.pm
  3. +31 −9 errorcheck.pm
  4. +131 −26 fix_pandas.pl
View
@@ -0,0 +1 @@
+A very simple github bot. Use an accout of the same name.
View
@@ -0,0 +1,17 @@
+package blacklist;
+
+sub ok_to_update {
+ my $user = shift @_;
+ my %none = {"lolerskates" => 1};
+ if ($none{$user}) {
+ print "Not updating for $user ... :( \n";
+ return 0;
+ } else {
+ return 1;
+ }
+}
+
+use base 'Exporter';
+our @EXPORT = qw{ok_to_update};
+
+1;
View
@@ -1,6 +1,4 @@
-package errrorcheck;
-
-use strict;
+package errorcheck;
sub check_php {
my $rt = shift @_;
@@ -9,11 +7,20 @@ sub check_php {
}
return 0;
}
+sub fix_php {
+ my $rt = shift @_;
+ return $rt;
+}
+sub fix_shell {
+ my $rt = shift @_;
+ return $rt;
+}
sub check_shell {
my $rt = shift @_;
my @r = split(/\n/,$rt);
#Is it a shell script
if ($r[0] =~ /\#\!\/bin\/(ba|z|)sh/ || $r[0] =~ /\#\!\/bin\/env (ba|z|)sh/) {
+ print "yes!\n";
#Probably!
#Handle with [-e foo -e bar]
if ($rt =~ /\[\s*\-e\s*\w*?\s*\-e\w*?\s*\]/) {
@@ -57,12 +64,27 @@ sub check_py {
}
return 0;
}
+sub fix_py {
+ my $rt = shift @_;
+ return 1;
+}
sub check_go {
my $rt = shift @_;
- #Check for = nil
- if ($rt =~ /\w*\[\w*\]\s*\=\s*nil\s*$/) {
- return 1;
- } else {
- return 0;
- }
+ return 1;
+}
+sub fix_go {
+ my $filename = shift @_;
+ `gofix "$filename"`;
+ return 1;
+}
+sub check_cpp {
}
+sub fix_cpp {
+ my $rt = shift @_;
+ return $rt;
+}
+
+use base 'Exporter';
+our @EXPORT = qw{check_php fix_php check_py fix_py check_go fix_go check_cpp fix_cpp check_shell fix_shell};
+
+1;
View
@@ -5,18 +5,14 @@
use Pithub;
use Data::Dumper;
-use wordlist qw{fix_text check_common};
use strict;
+use wordlist qw{fix_text check_common};
+use errorcheck qw{check_php fix_php check_py fix_py check_go fix_go check_cpp fix_cpp check_shell fix_shell};
+use blacklist qw{ ok_to_update };
-my $token ="";
-my $user = "holdensmagicalunicorn";
my $p = Pithub->new;
-my $consumer_key = "";
-my $consumer_secret = "";
-
-
my $c = 0;
print "using ck $consumer_key / secret $consumer_secret\n";
my $nt = Net::Twitter->new(
@@ -45,7 +41,7 @@
sub handle_url {
my $url = shift @_;
print "looking at $url\n";
- if ($url =~ /http.*\/(.*?)\/(.*?)\/(raw\/|)(master|development)\/(.*)/) {
+ if ($url =~ /http.*\/(.*?)\/(.*?)\/(raw\/|)(master|development|\w+)\/(.*)/) {
my $ruser = $1;
my $repo = $2;
my $file = $4;
@@ -65,15 +61,21 @@ sub handle_url {
$result = $p->repos->get( user => $ruser , repo => $repo);
$traverse++;
}
+ if (!ok_to_update($ruser)) {
+ #Fuck no love
+ return 0;
+ }
#Ok dokey lets try and fork this business
- my $f = Pithub::Repos::Forks->new(user => $user ,token => $token);
+ print "trying to fork!\n";
+ my $f = Pithub::Repos::Forks->new(token => $token);
my $result = $f->create( user => $ruser, repo => $repo);
- print "got back ".Dumper($result->content)."yay?";
my $clone_url = $result->content->{ssh_url};
my $upstream_url = $result->content->{parent}->{ssh_url};
- my $master_branch = $result->content->{parent}->{master_branch};
+ my $master_branch = $result->content->{parent}->{master_branch} || "master";
+ print "using master branch: $master_branch\n";
#Oh hey lets merge the latest business to eh (just in case we have an old fork)
`rm -rf foo && mkdir -p foo && cd foo && git clone "$clone_url" && cd * && git remote add upstream "$upstream_url" && git fetch upstream && git merge upstream/$master_branch && git push`;
+ print "Did the sexy bit!\n";
#Get the files
my @all_files;
open (my $files,"find ./foo/|");
@@ -83,24 +85,127 @@ sub handle_url {
}
close ($files);
#Now we iterate through each of the processors so the git commit messages are grouped logically
-
+ print "handling the files\n";
+ my @changes = handle_files(@all_files);
+ #Did we change anything?
+ if ($#changes > 0) {
+ #Yes!
+ my $pull_msg = generate_pull_msg(@changes);
+ my $twitter_msg = generate_twitter_msg(@changes);
+ #Make pull
+ my $pu = Pithub::PullRequests->new(user => $user ,token => $token);
+ my $result = $pu->create(user => $user,
+ repo => $repo,
+ data => {
+ title => "Pull request to a fix things",
+ base => $master_branch,
+ head => $master_branch});
+ print "Dump".Dumper($result->content);
+ exit();
+ #Post to twitter
+ $twitter_msg =~ s/\[LINK\]$/$link/;
+ }
+ }
+}
+sub generate_pull_msg {
+ my @msgs = @_;
+ my $msg_txt = join(' ',@msgs);
+ my $pull_msg = "Fix ".$msg_txt." these changes are automagically generated by https://github.com/holdenk/holdensmagicalunicorn";
+ return $pull_msg;
+}
+sub generate_twitter_msg {
+ my ($pname,$link,@msgs) = @_;
+ my $msgs_txt = join(' ',@msgs);
+ my $message = "Fixing: ".$msgs_txt." in ".$pname." see pull request [LINK]";
+ if (length($message) > 120) {
+ $message = "Fixing: ".$msgs_txt." in ".$pname." see [LINK]";
+ }
+ if (length($message) > 120) {
+ $message = "Fixing ".$msgs_txt." in ".$pname." see [LINK]";
+ }
+ if (length($message) > 120) {
+ $message = "Update to ".$pname." see pull request [LINK]";
}
+ return $message;
}
-sub fix_readme {
- my $file = shift @_;
- if ($file =~ /\/README(\.txt|\.rtf|\.md|\.m\w+)$/) {
- print "Checking $file for readme changes";
- open (my $in, "<", "$file") or die "Unable to open $file in $repo";
- my $t = do { local $/ = <$in> };
- close($in);
- #Is there a spelling mistake?
- if (check_common($t)) {
- open ($out, ">", "$file") or die "Unable to open $file in $repo";
- print $out fix_text($t);
- close ($out);
+sub handle_files {
+ print "handle_files called\n";
+ my @files = @_;
+ my @handlers = (handle_group("Fixing typos in README",qr/\/README(\.txt|\.rtf|\.md|\.m\w+)$/,\&check_common,\&fix_text),
+ handle_group("Fixing old PHP calls",qr/\.php$/,\&check_php,\&fix_php),
+ handle_group("Updating shell scripts",qr/\/\w(\.sh|\.bash|)$/,\&check_shell,\&fix_shell),
+ handle_group("Fixing deprecated django",qr/\.py$/,\&check_py,\&fix_py),
+ handle_group_cmd("Fixing go formatting",qr/\.go$/,\&check_go,\&fix_go));
+ my @handler_names = ("typos","deprecated php","portable shell","deprecated django","go fix");
+ print "have ".$#files." and ".$#handlers." to use\n";
+ my $i = 0;
+ my $short_msg = "Fix ";
+ my @changes = ();
+ while ($i < $#handlers+1) {
+ print "running $i\n";
+ print "Running handler $i / ".$handler_names[$i]."\n";
+ my $r = $handlers[$i](@files);
+ if ($r) {
+ push @changes, $handler_names[$i];
}
- } elsif ($file =~ /\/README\.pod$/) {
- #Handle pod l8r
+ $i++;
+ }
+ return @changes;
+}
+sub handle_group {
+ my $git_message = shift @_;
+ my $gate_regex = shift @_;
+ my $gate_function = shift @_;
+ my $fix_function = shift @_;
+ return sub {
+ my $changes = 0;
+ my @files = @_;
+ foreach my $file (@files) {
+ if ($file !~ /\/\.git\// && $file =~ $gate_regex) {
+ open (my $in, "<", "$file") or die "Unable to open $file";
+ my $t = do { local $/ = <$in> };
+ close($in);
+ #Is there a spelling mistake?
+ if ($gate_function->($t)) {
+ open (my $out, ">", "$file") or die "Unable to open $file";
+ print $out $fix_function->($t);
+ close ($out);
+ }
+ }
+ }
+ #Determine if we have made any difference
+ `cd foo/*;git diff --exit-code`;
+ if ($? != 0) {
+ #Yup
+ `cd foo/*;git commit -a -m \"$git_message\";git push; sleep 1; git push`;
+ return 1;
+ }
+ #Nope no changes
+ return 0;
}
}
+sub handle_group_cmd {
+ my $git_message = shift @_;
+ my $gate_regex = shift @_;
+ my $gate_function = shift @_;
+ my $fix_function = shift @_;
+ return sub {
+ my $changes = 0;
+ my @files = @_;
+ foreach my $file (@files) {
+ if ($file !~ /\/\.git\// && $file =~ $gate_regex) {
+ $fix_function->($file);
+ }
+ }
+ #Determine if we have made any difference
+ `cd foo/*;git diff --exit-code`;
+ if ($? != 0) {
+ #Yup
+ `cd foo/*;git commit -a -m \"$git_message\";git push; sleep 1; git push`;
+ return 1;
+ }
+ #Nope no changes
+ return 0;
+ }
+}

0 comments on commit d96a99d

Please sign in to comment.