|
| 1 | +def verify_level(username, repo_cfg, state, toml_keys): |
| 2 | + authorized = False |
| 3 | + if repo_cfg.get('auth_collaborators', False): |
| 4 | + authorized = state.get_repo().is_collaborator(username) |
| 5 | + if not authorized: |
| 6 | + authorized = username.lower() == state.delegate.lower() |
| 7 | + for toml_key in toml_keys: |
| 8 | + if not authorized: |
| 9 | + authorized = username in repo_cfg.get(toml_key, []) |
| 10 | + return authorized |
| 11 | + |
| 12 | + |
| 13 | +def verify(username, repo_cfg, state, auth, realtime, my_username): |
| 14 | + # The import is inside the function to prevent circular imports: main.py |
| 15 | + # requires auth.py and auth.py requires main.py |
| 16 | + from .main import AuthState |
| 17 | + |
| 18 | + # In some cases (e.g. non-fully-qualified r+) we recursively talk to |
| 19 | + # ourself via a hidden markdown comment in the message. This is so that |
| 20 | + # when re-synchronizing after shutdown we can parse these comments and |
| 21 | + # still know the SHA for the approval. |
| 22 | + # |
| 23 | + # So comments from self should always be allowed |
| 24 | + if username == my_username: |
| 25 | + return True |
| 26 | + |
| 27 | + authorized = False |
| 28 | + if auth == AuthState.REVIEWER: |
| 29 | + authorized = verify_level(username, repo_cfg, state, ['reviewers']) |
| 30 | + elif auth == AuthState.TRY: |
| 31 | + authorized = verify_level( |
| 32 | + username, repo_cfg, state, ['reviewers', 'try_users'], |
| 33 | + ) |
| 34 | + |
| 35 | + if authorized: |
| 36 | + return True |
| 37 | + else: |
| 38 | + if realtime: |
| 39 | + reply = '@{}: :key: Insufficient privileges: '.format(username) |
| 40 | + if auth == AuthState.REVIEWER: |
| 41 | + if repo_cfg.get('auth_collaborators', False): |
| 42 | + reply += 'Collaborator required' |
| 43 | + else: |
| 44 | + reply += 'Not in reviewers' |
| 45 | + elif auth == AuthState.TRY: |
| 46 | + reply += 'not in try users' |
| 47 | + state.add_comment(reply) |
| 48 | + return False |
0 commit comments