forked from Miserlou/WhitespaceBot
-
Notifications
You must be signed in to change notification settings - Fork 3
/
robot.py
273 lines (242 loc) · 9.17 KB
/
robot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import requests
import settings
import simplejson
import subprocess
import sys
import argparse
import time
from random import choice
import os
from os.path import join
import shutil
import urllib2
import json
#pseudo
# take name from list
# scan names for most names most popular repo
# fork it - POST /repos/:user/:repo/forks
# clone it
# switch branch
# fix it
# commit it!
# push it
# submit pull req
# remove name from list
def main():
parser = argparse.ArgumentParser(description='Whitespace annihilating GitHub robot.\nBy Rich Jones - Gun.io - [email protected]')
parser.add_argument('-u', '--users', help='A text file with usernames.', default='users.txt')
parser.add_argument('-o', '--old-users', help='A text file with usernames.', default='old_users.txt')
parser.add_argument('-c', '--count', help='The maximum number of total requests to make.', default=999999)
parser.add_argument('-v', '--verbose', help='Make this sucker loud? (True/False)', default=True)
args = parser.parse_args()
auth = (settings.username, settings.password)
old_users_file = args.old_users
old_users = load_user_list(old_users_file)
users = args.users
new_users = load_user_list(users)
user = get_user(users)
#XXX: Potential deal breaker in here!
count = 0
while user in old_users:
print "We've already done that user!"
user = get_user(users)
count = count + 1
if count > len(new_users):
return
repos = 'https://api.github.com/users/' + user + '/repos'
r = requests.get (repos, auth = auth)
if (r.status_code == 200):
resp = simplejson.loads (r.content)
topwatch = 0
top_repo = ''
for repo in resp:
if repo['watchers'] > topwatch:
top_repo = repo['name']
topwatch = repo['watchers']
print dir(repo)
print user + "'s most watched repo is " + top_repo + " with " + str(topwatch) + " watchers. Forking."
repo = top_repo
print "GitHub Forking.."
clone_url = fork_repo(user, repo)
print "Waiting.."
time.sleep(30)
print "Cloning.."
cloned = clone_repo(clone_url)
if not cloned:
return
print "Changing branch.."
branched = change_branch(repo)
print "Fixing repo.."
fixed = fix_repo(repo)
print "Comitting.."
commited = commit_repo(repo)
print "Pushing.."
pushed = push_commit(repo)
print "Submitting pull request.."
submitted = submit_pull_request(user, repo)
print "Delting local repo.."
deleted = delete_local_repo(repo)
print "Olding user.."
old = save_user(old_users_file, user)
def save_user(old_users_file, user):
with open(old_users_file, "a") as id_file:
id_file.write(user + '\n')
return True
def load_user_list(old_users):
text_file = open(old_users, "r")
old = text_file.readlines()
text_file.close()
x=0
for hid in old:
old[x] = hid.rstrip()
x=x+1
return old
def get_user(users):
text_file = open(users, "r")
u = text_file.readlines()
text_file.close()
return choice(u).rstrip()
def fork_repo(user, repo):
url = 'https://api.github.com/repos/' + user + '/' + repo + '/forks'
auth = (settings.username, settings.password)
r = requests.post (url, auth=auth)
if (r.status_code == 201):
resp = simplejson.loads(r.content)
return resp['ssh_url']
else:
return None
def clone_repo(clone_url):
try:
args =['/usr/bin/git', 'clone', clone_url]
p = subprocess.Popen(args)
p.wait()
return True
except Exception, e:
return False
def change_branch(repo):
#XXX fuck this
gitdir = os.path.join(settings.pwd, repo, ".git")
repo = os.path.join(settings.pwd, repo)
try:
args =['/usr/bin/git', '--git-dir', gitdir, '--work-tree', repo, 'branch', 'clean']
p = subprocess.Popen(args)
p.wait()
args =['/usr/bin/git', '--git-dir', gitdir, '--work-tree', repo, 'checkout', 'clean']
p = subprocess.Popen(args)
p.wait()
return True
except Exception, e:
return False
def fix_repo (repo):
gitdir = os.path.join(settings.pwd, repo, ".git")
repo = os.path.join(settings.pwd, repo)
for root, dirs, files in os.walk(repo):
for f in files:
path = os.path.join(root, f)
if '.git' in path:
continue
p = subprocess.Popen(['file','-bi',path],stdout=subprocess.PIPE)
while True:
o = p.stdout.readline()
if o == '':
break
#XXX: Motherfucking OSX is a super shitty and not real operating system
#XXX: and doesn't do file -bi properly
if 'text' in o:
q = subprocess.Popen(['sed','-i','s/[ \\t]*$//', path])
q.wait()
args =['/usr/bin/git', '--git-dir', gitdir, '--work-tree', repo, 'add', path]
pee = subprocess.Popen(args)
pee.wait()
if o == '' and p.poll() != None: break
git_ignore = os.path.join(repo, '.gitignore')
if not os.path.exists(git_ignore):
ignorefile = open(git_ignore, 'w')
ignore = '# Compiled source #\n' + \
'###################\n' + \
'*.com\n' + \
'*.class\n'+ \
'*.dll\n'+\
'*.exe\n'+\
'*.o\n'+\
'*.so\n'+\
'*.pyc\n\n'+\
'# Logs and databases #\n'+\
'######################\n'+\
'*.log\n\n'+\
'# OS generated files #\n'+\
'######################\n'+\
'.DS_Store*\n'+\
'ehthumbs.db\n'+\
'Icon?\n'+\
'Thumbs.db\n'+\
'ignorefile.close()'
ignorefile.write(ignore)
ignorefile.close()
try:
args =['/usr/bin/git', '--git-dir', gitdir, '--work-tree', repo, 'add', git_ignore]
p = subprocess.Popen(args)
p.wait ()
return True
except Exception, e:
return False
return True
def commit_repo(repo):
gitdir = os.path.join(settings.pwd, repo, ".git")
repo = os.path.join(settings.pwd, repo)
try:
message = "Remove whitespace [Gun.io WhitespaceBot]"
args =['/usr/bin/git', '--git-dir', gitdir, '--work-tree', repo, 'commit', '-m', message]
p = subprocess.Popen(args)
p.wait()
return True
except Exception, e:
print e
return False
def push_commit(repo):
gitdir = os.path.join(settings.pwd, repo, ".git")
repo = os.path.join(settings.pwd, repo)
try:
args =['/usr/bin/git', '--git-dir', gitdir, '--work-tree', repo, 'push', 'origin', 'clean']
p = subprocess.Popen(args)
p.wait ()
return True
except Exception, e:
print e
return False
def basic_authorization(user, password):
s = user + ":" + password
return "Basic " + s.encode("base64").rstrip()
def submit_pull_request(user, repo):
auth = (settings.username, settings.password)
url = 'https://api.github.com/repos/' + user + '/' + repo + '/pulls'
params = {'title': 'Hi! We cleaned up your code for you!', 'body': 'Hi'
+ ' there!\n\nThis is WhitespaceBot from [Gun.io](http://gun.io). I\'m an open-source robot that'
+ ' removes trailing white space in your code, and gives you a gitignore file if you didn\'t have one! '+
'I\'ve only cleaned your most popular project, and I\'ve added you to a list of users not to contact ' +
'again, so you won\'t get any more pull requests from me unless you ask. If I\'m misbehaving, please email my ' +
'owner and tell him to turn me off!\n\n== About Gun.io ==\n[Gun.io](http://Gun.io) is a place for hackers to hire ' +
'each other for small tasks. We offer no-hassle, winner-take-all freelance gigs, by hackers, for hackers. Got ' +
'a bug you can\'t fix or a feature you want for your project? Post a gig and have somebody else sort it out for you. Oh, and it\'s free for open ' +
'source! Sign up and get notified about new gigs you can work on!\n\n== About WhitespaceBot==\nWhitespaceBot ' +
'is a simple open source robot which uses GitHub\'s API as a way of cleaning up open source projects! We\'ve ' +
'put up a [paid bounty](http://gun.io/open/12/add-security-flaw-fixing-features-to-whitespacebot) for whoever can add ' +
'security fixing features to it.', 'base': 'master', 'head': 'GunioRobot:clean'}
req = urllib2.Request(url,
headers = {
"Authorization": basic_authorization(settings.username, settings.password),
"Content-Type": "application/json",
"Accept": "*/*",
"User-Agent": "WhitespaceRobot/Gunio",
}, data = json.dumps(params))
f = urllib2.urlopen(req)
return True
def delete_local_repo(repo):
repo = os.path.join(settings.pwd, repo)
try:
shutil.rmtree(repo)
return True
except Exception, e:
return False
if __name__ == '__main__':
sys.exit(main())