Skip to content

Commit f06011d

Browse files
authored
Merge pull request #23 from sourcerer-io/develop
Better bot detection and utility updates
2 parents 5080a02 + 4ef1aa9 commit f06011d

File tree

2 files changed

+54
-21
lines changed

2 files changed

+54
-21
lines changed

do.py

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
update: Updates a repo.
88
print: Prints a tracked repo.
99
glorify: Generate the hall of fame.
10+
code: Generate MD or RST integration code.
1011
"""
1112

1213
__copyright__ = '2018 Sourcerer, Inc.'
@@ -19,19 +20,37 @@
1920

2021
import fame.storage
2122
import fame.ssl_hack
23+
from fame.code_gen import make_md_code, make_rst_code
2224
from fame.github_tracker import RepoTracker
2325
from fame.glory import Glory
2426

2527

26-
def is_repo_command(command):
27-
return command in ['add', 'remove', 'update', 'print', 'glorify']
28+
class Command:
29+
ADD = 'add'
30+
REMOVE = 'remove'
31+
UPDATE = 'update'
32+
LIST = 'list'
33+
PRINT = 'print'
34+
GLORIFY = 'glorify'
35+
CODE = 'code'
36+
37+
@staticmethod
38+
def is_repo_command(command):
39+
return command in [
40+
Command.ADD, Command.REMOVE, Command.UPDATE,
41+
Command.PRINT, Command.GLORIFY]
42+
43+
@staticmethod
44+
def get_all():
45+
return [
46+
Command.ADD, Command.REMOVE, Command.UPDATE,
47+
Command.LIST, Command.PRINT, Command.GLORIFY, Command.CODE]
2848

2949

3050
def parse_args():
3151
parser = argparse.ArgumentParser()
3252
parser.add_argument('command', type=str,
33-
choices=['add', 'remove', 'list',
34-
'update', 'print', 'glorify'],
53+
choices=Command.get_all(),
3554
help='Command to execute')
3655
parser.add_argument('--user', type=str,
3756
help='Github user that tracks a repo')
@@ -43,6 +62,8 @@ def parse_args():
4362
help='Working directory to store data')
4463
parser.add_argument('--gcloud_bucket', type=str,
4564
help='Google cloud bucket to store data')
65+
parser.add_argument('--format', type=str, choices=['md', 'rst'],
66+
default='md', help='Code format to generate')
4667
parser.add_argument('--token', type=str, help='Github API token')
4768
parser.add_argument('--sourcerer_origin', type=str,
4869
default='https://sourcerer.io',
@@ -56,15 +77,16 @@ def parse_args():
5677
help='Disable SSL host checks, useful for debugging')
5778
args = parser.parse_args()
5879

59-
if is_repo_command(args.command):
80+
if Command.is_repo_command(args.command) or args.command == Command.CODE:
6081
if not args.user:
6182
parser.error('Must provide repo user')
6283
if not args.owner:
6384
parser.error('Must provide repo owner')
6485
if not args.repo:
6586
parser.error('Must provide repo name')
66-
if not args.sourcerer_origin:
67-
parser.error('Must provide Sourcerer origin')
87+
88+
if Command.is_repo_command(args.command) and not args.sourcerer_origin:
89+
parser.error('Must provide Sourcerer origin')
6890

6991
if args.work_dir and not os.path.isdir(args.work_dir):
7092
parser.error('--work_dir must be an existing directory')
@@ -88,19 +110,19 @@ def main():
88110

89111
try:
90112
tracker = RepoTracker()
91-
if is_repo_command(args.command):
113+
if Command.is_repo_command(args.command):
92114
tracker.configure(args.user, args.owner, args.repo,
93115
args.sourcerer_api_origin,
94116
args.sourcerer_api_secret,
95117
args.token)
96118

97-
if args.command == 'add':
119+
if args.command == Command.ADD:
98120
tracker.add()
99-
elif args.command == 'remove':
121+
elif args.command == Command.REMOVE:
100122
tracker.remove()
101-
elif args.command == 'update':
123+
elif args.command == Command.UPDATE:
102124
tracker.update()
103-
elif args.command == 'list':
125+
elif args.command == Command.LIST:
104126
table = []
105127
for result in RepoTracker.list(args.user):
106128
user, owner, repo, status, modified, error = result
@@ -109,13 +131,19 @@ def main():
109131
'%s:%s/%s' % (user, owner, repo),
110132
status, modified, error])
111133
print(tabulate(table))
112-
elif args.command == 'print':
134+
elif args.command == Command.PRINT:
113135
repo = tracker.load()
114136
print(repo)
115-
elif args.command == 'glorify':
137+
elif args.command == Command.GLORIFY:
116138
repo = tracker.load()
117139
glory = Glory(args.sourcerer_origin, args.sourcerer_api_origin)
118140
glory.make(repo)
141+
elif args.command == Command.CODE:
142+
if args.format == 'md':
143+
print(make_md_code(args.user, args.owner, args.repo))
144+
elif args.format == 'rst':
145+
print(make_rst_code(args.user, args.owner, args.repo))
146+
119147
except Exception as e:
120148
print('e %s' % str(e))
121149

fame/github_tracker.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ def __init__(self, message):
3030

3131

3232
class RepoTracker:
33-
KNOWN_BOTS = ['pyup-bot', 'dependabot-bot', 'renovate-bot']
34-
3533
def configure(self, user, owner, repo,
3634
sourcerer_api_origin=None,
3735
sourcerer_api_secret=None,
@@ -209,16 +207,16 @@ def _update_latest_commits(self, repo, avatars):
209207
break
210208

211209
try:
212-
# Chop 'Z' out of the timestamp.
213-
commit_date = c['commit']['author']['date'][:-1]
214210
author = c['author']['login']
215-
216-
if author in RepoTracker.KNOWN_BOTS:
211+
if self._is_bot(c['author']):
217212
print('i Skipping bot commit %s by %s' % (sha, author))
218213
continue
219214

220215
avatars[author] = c['author']['avatar_url']
221216

217+
# Chop 'Z' out of the timestamp.
218+
commit_date = c['commit']['author']['date'][:-1]
219+
222220
commit = pb.Commit(
223221
sha=sha, timestamp=commit_date, username=author)
224222
commits.append(commit)
@@ -248,7 +246,7 @@ def _update_top_contributors(self, repo, avatars):
248246

249247
for contrib in contributors[:20]:
250248
username = contrib['login']
251-
if username in RepoTracker.KNOWN_BOTS:
249+
if self._is_bot(contrib):
252250
print('i Skipping bot in top contributors: %s' % username)
253251
continue
254252

@@ -299,6 +297,13 @@ def _get_github_commits(self, owner, repo,
299297
for commit in data:
300298
yield commit
301299

300+
def _is_bot(self, github_user_json):
301+
author = github_user_json['login']
302+
author_type = github_user_json['type']
303+
304+
KNOWN_BOTS = ['pyup-bot', 'dependabot-bot', 'renovate-bot']
305+
return author_type == 'Bot' or author in KNOWN_BOTS
306+
302307
def _format_date(self, date):
303308
return date.strftime('%Y-%m-%dT%H:%M:%SZ')
304309

0 commit comments

Comments
 (0)