Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to improve synchronization and testability #45

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ install:
- pip install flake8
script:
- flake8 homu
- pip install -e .
- pip install -e .[test]
- python setup.py test
49 changes: 32 additions & 17 deletions homu/auth.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
import requests
from enum import IntEnum


RUST_TEAM_BASE = "https://team-api.infra.rust-lang.org/v1/"


class AuthorizationException(Exception):
"""
The exception thrown when a user is not authorized to perform an action
"""

comment = None

def __init__(self, message, comment):
super().__init__(message)
self.comment = comment


class AuthState(IntEnum):
# Higher is more privileged
REVIEWER = 3
TRY = 2
NONE = 1


def fetch_rust_team(repo_label, level):
repo = repo_label.replace('-', '_')
url = RUST_TEAM_BASE + "permissions/bors." + repo + "." + level + ".json"
Expand Down Expand Up @@ -31,18 +51,14 @@ def verify_level(username, repo_label, repo_cfg, state, toml_keys,
return authorized


def verify(username, repo_label, repo_cfg, state, auth, realtime, my_username):
# The import is inside the function to prevent circular imports: main.py
# requires auth.py and auth.py requires main.py
from .main import AuthState

def assert_authorized(username, repo_label, repo_cfg, state, auth, botname):
# In some cases (e.g. non-fully-qualified r+) we recursively talk to
# ourself via a hidden markdown comment in the message. This is so that
# when re-synchronizing after shutdown we can parse these comments and
# still know the SHA for the approval.
#
# So comments from self should always be allowed
if username == my_username:
if username == botname:
return True

authorized = False
Expand All @@ -59,14 +75,13 @@ def verify(username, repo_label, repo_cfg, state, auth, realtime, my_username):
if authorized:
return True
else:
if realtime:
reply = '@{}: :key: Insufficient privileges: '.format(username)
if auth == AuthState.REVIEWER:
if repo_cfg.get('auth_collaborators', False):
reply += 'Collaborator required'
else:
reply += 'Not in reviewers'
elif auth == AuthState.TRY:
reply += 'not in try users'
state.add_comment(reply)
return False
reply = '@{}: :key: Insufficient privileges: '.format(username)
if auth == AuthState.REVIEWER:
if repo_cfg.get('auth_collaborators', False):
reply += 'Collaborator required'
else:
reply += 'Not in reviewers'
elif auth == AuthState.TRY:
reply += 'not in try users'
raise AuthorizationException(
'Authorization failed for user {}'.format(username), reply)
38 changes: 38 additions & 0 deletions homu/consts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import re
from enum import Enum

STATUS_TO_PRIORITY = {
'success': 0,
'pending': 1,
'approved': 2,
'': 3,
'error': 4,
'failure': 5,
}

INTERRUPTED_BY_HOMU_FMT = 'Interrupted by Homu ({})'
INTERRUPTED_BY_HOMU_RE = re.compile(r'Interrupted by Homu \((.+?)\)')
DEFAULT_TEST_TIMEOUT = 3600 * 10

WORDS_TO_ROLLUP = {
'rollup-': 0,
'rollup': 1,
'rollup=maybe': 0,
'rollup=never': -1,
'rollup=always': 1,
}


class LabelEvent(Enum):
APPROVED = 'approved'
REJECTED = 'rejected'
CONFLICT = 'conflict'
SUCCEED = 'succeed'
FAILED = 'failed'
TRY = 'try'
TRY_SUCCEED = 'try_succeed'
TRY_FAILED = 'try_failed'
EXEMPTED = 'exempted'
TIMED_OUT = 'timed_out'
INTERRUPTED = 'interrupted'
PUSHED = 'pushed'
Loading