def should_allow(pr): bonuses = pr.get_new_bonuses_or_raise() if len(bonuses) > 1: raise Exception('Only one new player can be added in a PR') elif len(bonuses) < 1: raise Exception('Empty diff?') (points_user, points_name, points_change) = bonuses[0] if points_user in util.users(): raise Exception('Cannot create an existing user') if points_name != 'initial': raise Exception('New player bonus value must be called "initial"') if points_change < 0: raise Exception('Points cannot be negative') if points_change > max_start_bonus: raise Exception( '%s initial points exceeds maximum starting value of %s points' % (points_change, max_start_bonus)) return True
def start(): travis_pull_request = os.environ['TRAVIS_PULL_REQUEST'] if travis_pull_request == 'false': determine_if_winner() else: target_commit = os.environ['TRAVIS_PULL_REQUEST_SHA'] repo_slug = os.environ['TRAVIS_REPO_SLUG'] determine_if_mergeable( pull_request.PullRequest(repo=repo_slug, pr_number=travis_pull_request, target_commit=target_commit, users=util.users()))
def should_block(pr): required_approvals = math.ceil(len(util.users()) * 2 / 3) # Allow three days to go by with no commits, but if longer happens then start # lowering the threshold for allowing a commit. approvals_to_skip = util.days_since_last_commit() - 3 if approvals_to_skip > 0: print("Skipping up to %s approvals, because it's been %s days" " since the last commit." % (approvals_to_skip, util.days_since_last_commit())) required_approvals -= approvals_to_skip if len(pr.approvals) < required_approvals: raise Exception( 'Insufficient approval: got %s out of %s required approvals' % (len(pr.approvals), required_approvals))
def should_allow(pr): # If a PR only moves points around by the creation of new bonus files, has # been approved by every player losing points, reduces the total number of # points, and does not create any new users, allow it. # # Returns to indicate yes, raises an exception to indicate no. # # Having a PR merged gives you a point (#33), so a PR like: # # - me: -2 points # - you: +1 point # # is effectively: # # - me: -1 point # - you: +1 point bonuses = pr.get_new_bonuses_or_raise() total_points_change = 0 for points_user, points_name, points_change in bonuses: if points_user not in util.users(): raise Exception( 'Points transfer PRs should not add users: got %s' % points_user) if points_change < 0: if points_user not in pr.approvals: raise Exception( 'Taking %s points from %s requires their approval.' % (abs(points_change), points_user)) total_points_change += points_change if total_points_change >= 0: raise Exception('points change PRs must on net remove points') return True
def print_users(): users = util.users() print('Users:') for user in users: print(' %s' % user)
def should_allow(pr): if not all(user in pr.approvals for user in util.users()): raise Exception('PR does not have unanimous approval') return True
def should_allow(pr): # If a PR only moves points around by the creation of new bonus files, has # been approved by every player losing points, reduces the total number of # points, and does not create any new users, allow it. # # Returns to indicate yes, raises an exception to indicate no. # # Having a PR merged gives you a point (#33), so a PR like: # # - me: -2 points # - you: +1 point # # is effectively: # # - me: -1 point # - you: +1 point diff = pr.diff() if diff.modified_files or diff.removed_files: raise Exception('All file changes must be additions') total_points_change = 0 for added_file in diff.added_files: s_players, points_user, s_bonuses, bonus_name = added_file.path.split( '/') if s_players != 'players' or s_bonuses != 'bonuses': raise Exception('Added file %s is not a bonus file' % added_file) if points_user not in util.users(): raise Exception( 'Points transfer PRs should not add users: got %s' % points_user) (diff_invocation_line, file_mode_line, _, removed_file_line, added_file_line, patch_location_line, file_delta_line, empty_line) = str(added_file).split('\n') if diff_invocation_line != 'diff --git a/%s b/%s' % (added_file.path, added_file.path): raise Exception('Unexpected diff invocation: %s' % diff_invocation_line) if file_mode_line != 'new file mode 100644': raise Exception('File added with incorrect mode: %s' % file_mode_line) if removed_file_line != '--- /dev/null': raise Exception( 'Diff format makes no sense: added files should say they are from /dev/null' ) if added_file_line != '+++ b/%s' % added_file.path: raise Exception('Something wrong with file adding line: file is ' '%s but got %s' % (added_file.path, added_file_line)) if patch_location_line != '@@ -0,0 +1,1 @@': raise Exception('Patch location makes no sense: %s' % patch_location_line) if empty_line: raise Exception('Last line should be empty') if file_delta_line.startswith('+'): actual_file_delta = file_delta_line[1:] else: raise Exception('File delta missing initial + for addition: %s' % file_delta_line) # If this isn't an int, then it raises and the PR isn't mergeable points_change = int(actual_file_delta) if points_change < 0: if points_user not in pr.approvals: raise Exception( 'Taking %s points from %s requires their approval.' % (abs(points_change), points_user)) total_points_change += points_change if total_points_change >= 0: raise Exception('points change PRs must on net remove points') return True
def should_allow(pr): return all(user in pr.approvals for user in util.users())
def should_block(pr): # Don't allow PRs to be merged the day they're created unless they pass unanimously if len(pr.approvals) < len(util.users()) and (pr.days_since_created() < 1): raise Exception( 'PR created within last 24 hours does not have unanimous approval.' )