forked from CellProfiling/cyto-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpr_validation.py
92 lines (80 loc) · 2.7 KB
/
pr_validation.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
"""Validate PR only changes allowed files."""
import argparse
import os
import sys
import sh
BRANCH_SLUG = 'branch_slug'
# TRAVIS_COMMIT_RANGE or FETCH_HEAD $(git merge-base FETCH_HEAD master)
COMMIT_RANGE = 'commit_range'
REPO_PATH = 'repo_path'
ALLOWED_CHALLENGES = 'allowed_challenges'
CHALLENGES = ['2', '3', '4', 'bonus', 'test']
SUBMISSIONS = 'submissions'
USERNAME_CHALLENGE = '{}_{}.csv.gpg'
def parse_command_line():
"""Parse the provided command line."""
parser = argparse.ArgumentParser(
description='Validate that a PR only changes allowed files.')
parser.add_argument(
'-p', '--repo-path', dest=REPO_PATH, type=str,
help='the name of the git project')
parser.add_argument(
'-g', '--commit-range', dest=COMMIT_RANGE, type=str,
default='master..HEAD',
help='the commit range to scan, ie "master..HEAD"')
parser.add_argument(
'-b', '--branch', dest=BRANCH_SLUG, type=str, help='the branch slug')
parser.add_argument(
'-a', '--allowed', dest=ALLOWED_CHALLENGES, type=list,
help='the branch slug')
args = parser.parse_args()
return args
def validate(commit_range, branch_slug, allowed_challenges=None, repo=None):
"""Validate pull request."""
git = sh.git.bake('--no-pager')
cwd = os.getcwd()
if repo is not None:
os.chdir(repo)
changes = git.diff(commit_range, '--name-only')
if not changes:
print('No changes found')
stop(cwd, 1)
change_list = changes.strip().split('\n')
username = branch_slug.split('/')[0]
if allowed_challenges is None:
allowed_challenges = CHALLENGES
allowed = [
os.path.join(
SUBMISSIONS, str(challenge),
USERNAME_CHALLENGE.format(username, challenge))
for challenge in allowed_challenges]
unallowed = [fil for fil in change_list if fil not in allowed]
if not unallowed:
stop(cwd)
return
print(
'All files changed are not equal to '
'[GitHub username]_[challenge number].csv.gpg')
print('Your GitHub username is:', username)
print('Allowed challenges to test are:', allowed_challenges)
print('Allowed changed files would be:')
for fil in allowed:
print(fil)
print('Unallowed changes in:')
for fil in unallowed:
print(fil)
stop(cwd, 1)
def stop(path, code=0):
"""Change dir to path, exit and return code."""
os.chdir(path)
if code == 0:
return
sys.exit(code)
def main():
"""Run main."""
cmd_args = parse_command_line()
validate(
cmd_args.commit_range, cmd_args.branch_slug,
cmd_args.allowed_challenges, cmd_args.repo_path)
if __name__ == '__main__':
main()