Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 212 lines (204 sloc) 7.587 kB
c5f98b5 @holdenk Initial collection of shaaady perl scripts to do the spelling correct…
authored
1 #!/usr/bin/perl -s
2 $| = 1;
3 use LWP::UserAgent;
4 use Net::Twitter;
3b92e73 @holdenk Migrate towards using Pithub & support for grabbing from the root repo.
authored
5 use Pithub;
6 use Data::Dumper;
7
8 use strict;
d96a99d @holdenk Move more towards using Pithub and take the root repo rather than forks.
authored
9 use wordlist qw{fix_text check_common};
10 use errorcheck qw{check_php fix_php check_py fix_py check_go fix_go check_cpp fix_cpp check_shell fix_shell};
11 use blacklist qw{ ok_to_update };
c5f98b5 @holdenk Initial collection of shaaady perl scripts to do the spelling correct…
authored
12
3b92e73 @holdenk Migrate towards using Pithub & support for grabbing from the root repo.
authored
13
14 my $p = Pithub->new;
15
16 my $c = 0;
17 print "using ck $consumer_key / secret $consumer_secret\n";
c5f98b5 @holdenk Initial collection of shaaady perl scripts to do the spelling correct…
authored
18 my $nt = Net::Twitter->new(
19 traits => [qw/OAuth API::REST/],
20 consumer_key => $consumer_key,
21 consumer_secret => $consumer_secret,
22 );
23 $nt->access_token();
24 $nt->access_token_secret();
25
26 my $ua = new LWP::UserAgent;
27 print "Hello!\n";
28 print "Connecting to github!\n";
29 print "Reading input\n";
30 while (my $l = <>) {
3b92e73 @holdenk Migrate towards using Pithub & support for grabbing from the root repo.
authored
31 if ($l =~ /github\.com\/(.*?)\s*$/) {
32 print "Checking $1\n";
33 my $url = "https://www.github.com/".$1;
34 $url =~ s/raw\/.*?\//raw\/master\//;
35 handle_url($url);
36 } else {
37 print "fuck $l\n";
c5f98b5 @holdenk Initial collection of shaaady perl scripts to do the spelling correct…
authored
38 }
39 }
40
41 sub handle_url {
42 my $url = shift @_;
3b92e73 @holdenk Migrate towards using Pithub & support for grabbing from the root repo.
authored
43 print "looking at $url\n";
d96a99d @holdenk Move more towards using Pithub and take the root repo rather than forks.
authored
44 if ($url =~ /http.*\/(.*?)\/(.*?)\/(raw\/|)(master|development|\w+)\/(.*)/) {
3b92e73 @holdenk Migrate towards using Pithub & support for grabbing from the root repo.
authored
45 my $ruser = $1;
46 my $repo = $2;
47 my $file = $4;
48 print "u:".$ruser."\n";
49 print "r:".$repo."\n";
50 print "f:".$file."\n";
51 my $result = $p->repos->get( user => $ruser , repo => $repo);
52 my $traverse = 0;
53 #Do we need to go up a level?
54 while ($traverse < 10 && $result->content->{source}) {
55 my $above = $result->content->{source}->{url};
56 print "Yup, source exists was pulled from $above\n";
57 if ($above =~ /repos\/(.*?)\/(.*)$/) {
58 $ruser = $1;
59 $repo = $2;
60 }
61 $result = $p->repos->get( user => $ruser , repo => $repo);
62 $traverse++;
63 }
d96a99d @holdenk Move more towards using Pithub and take the root repo rather than forks.
authored
64 if (!ok_to_update($ruser)) {
65 #Fuck no love
66 return 0;
67 }
3b92e73 @holdenk Migrate towards using Pithub & support for grabbing from the root repo.
authored
68 #Ok dokey lets try and fork this business
d96a99d @holdenk Move more towards using Pithub and take the root repo rather than forks.
authored
69 print "trying to fork!\n";
70 my $f = Pithub::Repos::Forks->new(token => $token);
3b92e73 @holdenk Migrate towards using Pithub & support for grabbing from the root repo.
authored
71 my $result = $f->create( user => $ruser, repo => $repo);
72 my $clone_url = $result->content->{ssh_url};
73 my $upstream_url = $result->content->{parent}->{ssh_url};
d96a99d @holdenk Move more towards using Pithub and take the root repo rather than forks.
authored
74 my $master_branch = $result->content->{parent}->{master_branch} || "master";
75 print "using master branch: $master_branch\n";
3b92e73 @holdenk Migrate towards using Pithub & support for grabbing from the root repo.
authored
76 #Oh hey lets merge the latest business to eh (just in case we have an old fork)
77 `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`;
d96a99d @holdenk Move more towards using Pithub and take the root repo rather than forks.
authored
78 print "Did the sexy bit!\n";
3b92e73 @holdenk Migrate towards using Pithub & support for grabbing from the root repo.
authored
79 #Get the files
80 my @all_files;
81 open (my $files,"find ./foo/|");
82 while (my $file = <$files>) {
83 chomp ($file);
84 push @all_files, $file;
85 }
86 close ($files);
87 #Now we iterate through each of the processors so the git commit messages are grouped logically
d96a99d @holdenk Move more towards using Pithub and take the root repo rather than forks.
authored
88 print "handling the files\n";
89 my @changes = handle_files(@all_files);
90 #Did we change anything?
91 if ($#changes > 0) {
92 #Yes!
93 my $pull_msg = generate_pull_msg(@changes);
94 my $twitter_msg = generate_twitter_msg(@changes);
95 #Make pull
96 my $pu = Pithub::PullRequests->new(user => $user ,token => $token);
97 my $result = $pu->create(user => $user,
98 repo => $repo,
99 data => {
100 title => "Pull request to a fix things",
101 base => $master_branch,
102 head => $master_branch});
103 print "Dump".Dumper($result->content);
104 exit();
105 #Post to twitter
106 $twitter_msg =~ s/\[LINK\]$/$link/;
107 }
108 }
109 }
110 sub generate_pull_msg {
111 my @msgs = @_;
112 my $msg_txt = join(' ',@msgs);
113 my $pull_msg = "Fix ".$msg_txt." these changes are automagically generated by https://github.com/holdenk/holdensmagicalunicorn";
114 return $pull_msg;
115 }
116 sub generate_twitter_msg {
117 my ($pname,$link,@msgs) = @_;
118 my $msgs_txt = join(' ',@msgs);
119 my $message = "Fixing: ".$msgs_txt." in ".$pname." see pull request [LINK]";
120 if (length($message) > 120) {
121 $message = "Fixing: ".$msgs_txt." in ".$pname." see [LINK]";
122 }
123 if (length($message) > 120) {
124 $message = "Fixing ".$msgs_txt." in ".$pname." see [LINK]";
125 }
126 if (length($message) > 120) {
127 $message = "Update to ".$pname." see pull request [LINK]";
3b92e73 @holdenk Migrate towards using Pithub & support for grabbing from the root repo.
authored
128 }
d96a99d @holdenk Move more towards using Pithub and take the root repo rather than forks.
authored
129 return $message;
3b92e73 @holdenk Migrate towards using Pithub & support for grabbing from the root repo.
authored
130 }
d96a99d @holdenk Move more towards using Pithub and take the root repo rather than forks.
authored
131 sub handle_files {
132 print "handle_files called\n";
133 my @files = @_;
134 my @handlers = (handle_group("Fixing typos in README",qr/\/README(\.txt|\.rtf|\.md|\.m\w+)$/,\&check_common,\&fix_text),
135 handle_group("Fixing old PHP calls",qr/\.php$/,\&check_php,\&fix_php),
136 handle_group("Updating shell scripts",qr/\/\w(\.sh|\.bash|)$/,\&check_shell,\&fix_shell),
137 handle_group("Fixing deprecated django",qr/\.py$/,\&check_py,\&fix_py),
138 handle_group_cmd("Fixing go formatting",qr/\.go$/,\&check_go,\&fix_go));
139 my @handler_names = ("typos","deprecated php","portable shell","deprecated django","go fix");
140 print "have ".$#files." and ".$#handlers." to use\n";
141 my $i = 0;
142 my $short_msg = "Fix ";
143 my @changes = ();
144 while ($i < $#handlers+1) {
145 print "running $i\n";
146 print "Running handler $i / ".$handler_names[$i]."\n";
147 my $r = $handlers[$i](@files);
148 if ($r) {
149 push @changes, $handler_names[$i];
3b92e73 @holdenk Migrate towards using Pithub & support for grabbing from the root repo.
authored
150 }
d96a99d @holdenk Move more towards using Pithub and take the root repo rather than forks.
authored
151 $i++;
152 }
153 return @changes;
154 }
155 sub handle_group {
156 my $git_message = shift @_;
157 my $gate_regex = shift @_;
158 my $gate_function = shift @_;
159 my $fix_function = shift @_;
160 return sub {
161 my $changes = 0;
162 my @files = @_;
163 foreach my $file (@files) {
164 if ($file !~ /\/\.git\// && $file =~ $gate_regex) {
165 open (my $in, "<", "$file") or die "Unable to open $file";
166 my $t = do { local $/ = <$in> };
167 close($in);
168 #Is there a spelling mistake?
169 if ($gate_function->($t)) {
170 open (my $out, ">", "$file") or die "Unable to open $file";
171 print $out $fix_function->($t);
172 close ($out);
173 }
174 }
175 }
176 #Determine if we have made any difference
177 `cd foo/*;git diff --exit-code`;
178 if ($? != 0) {
179 #Yup
180 `cd foo/*;git commit -a -m \"$git_message\";git push; sleep 1; git push`;
181 return 1;
182 }
183 #Nope no changes
184 return 0;
c5f98b5 @holdenk Initial collection of shaaady perl scripts to do the spelling correct…
authored
185 }
186 }
3b92e73 @holdenk Migrate towards using Pithub & support for grabbing from the root repo.
authored
187
d96a99d @holdenk Move more towards using Pithub and take the root repo rather than forks.
authored
188 sub handle_group_cmd {
189 my $git_message = shift @_;
190 my $gate_regex = shift @_;
191 my $gate_function = shift @_;
192 my $fix_function = shift @_;
193 return sub {
194 my $changes = 0;
195 my @files = @_;
196 foreach my $file (@files) {
197 if ($file !~ /\/\.git\// && $file =~ $gate_regex) {
198 $fix_function->($file);
199 }
200 }
201 #Determine if we have made any difference
202 `cd foo/*;git diff --exit-code`;
203 if ($? != 0) {
204 #Yup
205 `cd foo/*;git commit -a -m \"$git_message\";git push; sleep 1; git push`;
206 return 1;
207 }
208 #Nope no changes
209 return 0;
210 }
211 }
Something went wrong with that request. Please try again.