Skip to content

Commit 23edaab

Browse files
committed
integrate with the rust team repo
1 parent 37e2328 commit 23edaab

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

cfg.sample.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ sync_on_start = true
6060
owner = ""
6161
name = ""
6262

63+
# If this repo should be integrated with the permissions defined in
64+
# https://github.com/rust-lang/team uncomment the following line.
65+
# Note that the other ACLs will *also* apply.
66+
#rust_team = true
67+
6368
# Who can approve PRs (r+ rights)? You can put GitHub usernames here.
6469
reviewers = []
6570
# Alternatively, set this allow any github collaborator;

homu/auth.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
1-
def verify_level(username, repo_cfg, state, toml_keys):
1+
import requests
2+
3+
4+
RUST_TEAM_BASE = "https://team-api.infra.rust-lang.org/v1/"
5+
6+
7+
def fetch_rust_team(repo_label, level):
8+
repo = repo_label.replace('-', '_')
9+
url = RUST_TEAM_BASE + "permissions/bors." + repo + "." + level + ".json"
10+
try:
11+
resp = requests.get(url)
12+
resp.raise_for_status()
13+
return resp.json()["github_users"]
14+
except requests.exceptions.RequestException as e:
15+
print("error while fetching " + url + ": " + str(e))
16+
return []
17+
18+
19+
def verify_level(username, repo_label, repo_cfg, state, toml_keys,
20+
rust_team_level):
221
authorized = False
322
if repo_cfg.get('auth_collaborators', False):
423
authorized = state.get_repo().is_collaborator(username)
24+
if repo_cfg.get('rust_team', False):
25+
authorized = username in fetch_rust_team(repo_label, rust_team_level)
526
if not authorized:
627
authorized = username.lower() == state.delegate.lower()
728
for toml_key in toml_keys:
@@ -10,7 +31,7 @@ def verify_level(username, repo_cfg, state, toml_keys):
1031
return authorized
1132

1233

13-
def verify(username, repo_cfg, state, auth, realtime, my_username):
34+
def verify(username, repo_label, repo_cfg, state, auth, realtime, my_username):
1435
# The import is inside the function to prevent circular imports: main.py
1536
# requires auth.py and auth.py requires main.py
1637
from .main import AuthState
@@ -26,10 +47,13 @@ def verify(username, repo_cfg, state, auth, realtime, my_username):
2647

2748
authorized = False
2849
if auth == AuthState.REVIEWER:
29-
authorized = verify_level(username, repo_cfg, state, ['reviewers'])
50+
authorized = verify_level(
51+
username, repo_label, repo_cfg, state, ['reviewers'], 'review',
52+
)
3053
elif auth == AuthState.TRY:
3154
authorized = verify_level(
32-
username, repo_cfg, state, ['reviewers', 'try_users'],
55+
username, repo_label, repo_cfg, state, ['reviewers', 'try_users'],
56+
'try',
3357
)
3458

3559
if authorized:

homu/main.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -413,14 +413,15 @@ class LabelEvent(Enum):
413413
PORTAL_TURRET_IMAGE = "https://cloud.githubusercontent.com/assets/1617736/22222924/c07b2a1c-e16d-11e6-91b3-ac659550585c.png" # noqa
414414

415415

416-
def parse_commands(body, username, repo_cfg, state, my_username, db, states,
417-
*, realtime=False, sha=''):
416+
def parse_commands(body, username, repo_label, repo_cfg, state, my_username,
417+
db, states, *, realtime=False, sha=''):
418418
global global_cfg
419419
state_changed = False
420420

421421
_reviewer_auth_verified = functools.partial(
422422
verify_auth,
423423
username,
424+
repo_label,
424425
repo_cfg,
425426
state,
426427
AuthState.REVIEWER,
@@ -430,6 +431,7 @@ def parse_commands(body, username, repo_cfg, state, my_username, db, states,
430431
_try_auth_verified = functools.partial(
431432
verify_auth,
432433
username,
434+
repo_label,
433435
repo_cfg,
434436
state,
435437
AuthState.TRY,
@@ -551,8 +553,8 @@ def parse_commands(body, username, repo_cfg, state, my_username, db, states,
551553
state.change_labels(LabelEvent.APPROVED)
552554

553555
elif word == 'r-':
554-
if not verify_auth(username, repo_cfg, state, AuthState.REVIEWER,
555-
realtime, my_username):
556+
if not verify_auth(username, repo_label, repo_cfg, state,
557+
AuthState.REVIEWER, realtime, my_username):
556558
continue
557559

558560
state.approved_by = ''
@@ -561,8 +563,8 @@ def parse_commands(body, username, repo_cfg, state, my_username, db, states,
561563
state.change_labels(LabelEvent.REJECTED)
562564

563565
elif word.startswith('p='):
564-
if not verify_auth(username, repo_cfg, state, AuthState.TRY,
565-
realtime, my_username):
566+
if not verify_auth(username, repo_label, repo_cfg, state,
567+
AuthState.TRY, realtime, my_username):
566568
continue
567569
try:
568570
pvalue = int(word[len('p='):])
@@ -580,8 +582,8 @@ def parse_commands(body, username, repo_cfg, state, my_username, db, states,
580582
state.save()
581583

582584
elif word.startswith('delegate='):
583-
if not verify_auth(username, repo_cfg, state, AuthState.REVIEWER,
584-
realtime, my_username):
585+
if not verify_auth(username, repo_label, repo_cfg, state,
586+
AuthState.REVIEWER, realtime, my_username):
585587
continue
586588

587589
state.delegate = word[len('delegate='):]
@@ -1483,6 +1485,7 @@ def synchronize(repo_label, repo_cfg, logger, gh, states, repos, db, mergeable_q
14831485
parse_commands(
14841486
comment.body,
14851487
comment.user.login,
1488+
repo_label,
14861489
repo_cfg,
14871490
state,
14881491
my_username,
@@ -1495,6 +1498,7 @@ def synchronize(repo_label, repo_cfg, logger, gh, states, repos, db, mergeable_q
14951498
parse_commands(
14961499
comment.body,
14971500
comment.user.login,
1501+
repo_label,
14981502
repo_cfg,
14991503
state,
15001504
my_username,

homu/server.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ def github():
343343
if parse_commands(
344344
body,
345345
username,
346+
repo_label,
346347
repo_cfg,
347348
state,
348349
g.my_username,
@@ -389,6 +390,7 @@ def github():
389390
found = parse_commands(
390391
c.body,
391392
c.user.login,
393+
repo_label,
392394
repo_cfg,
393395
state,
394396
g.my_username,
@@ -507,6 +509,7 @@ def fail(err):
507509
if parse_commands(
508510
body,
509511
username,
512+
repo_label,
510513
repo_cfg,
511514
state,
512515
g.my_username,

0 commit comments

Comments
 (0)