Skip to content
Merged
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
20 changes: 19 additions & 1 deletion scripts/assign_reviewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,15 @@
# Accumulated exponentially decayed review load per reviewer.
review_load: dict[str, float] = {}

# Number of review-requested PRs per reviewer.
review_count: dict[str, int] = {}

# Accumulated exponentially decayed authored-PR load per reviewer.
authored_load: dict[str, float] = {}

# Number of authored PRs per reviewer.
authored_count: dict[str, int] = {}

# Most recent review-request timestamp per reviewer, used for tie-breaking.
last_assigned: dict[str, float] = {}

Expand Down Expand Up @@ -155,6 +161,7 @@ def accumulate_pr_activity(pr: dict, reviewer_set: set[str]) -> None:
if author in reviewer_set:
age = max(0.0, now - active_at)
authored_load[author] += w_pr * math.exp(-DECAY_RATE * age)
authored_count[author] += 1

# Track which reviewers have reviewed or approved so we can ignore removed
# unreviewed requests and drop PENDING_REVIEW_MULTIPLIER on approved PRs.
Expand Down Expand Up @@ -194,6 +201,7 @@ def accumulate_pr_activity(pr: dict, reviewer_set: set[str]) -> None:
mult = PENDING_REVIEW_MULTIPLIER if is_pending else 1.0
age = max(0.0, now - assign_ts)
review_load[r] += mult * w_pr * math.exp(-DECAY_RATE * age)
review_count[r] += 1


# Load per-reviewer weight overrides from REVIEWER_WEIGHTS (JSON object mapping
Expand Down Expand Up @@ -275,7 +283,9 @@ def main() -> None:
return

review_load.update(dict.fromkeys(reviewers, 0.0))
review_count.update(dict.fromkeys(reviewers, 0))
authored_load.update(dict.fromkeys(reviewers, 0.0))
authored_count.update(dict.fromkeys(reviewers, 0))
last_assigned.update(dict.fromkeys(reviewers, 0.0))
for pr in repo_data['recentPRs']['nodes']:
if not pr['isDraft'] and pr['number'] != pr_number:
Expand All @@ -286,14 +296,22 @@ def main() -> None:
for r in eligible
}
# Break ties by least-recently-assigned reviewer, then team member order.
chosen = min(
ranked = sorted(
eligible,
key=lambda r: (
round(scores[r], 6),
last_assigned[r],
reviewers.index(r),
),
)
Comment on lines +299 to 306

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Calling reviewers.index(r) inside the sorting key function results in an O(N) lookup for each comparison. Although the list of reviewers is small, it is more efficient and idiomatic to precompute the reviewer indices into a dictionary for O(1) lookups during sorting.

Suggested change
ranked = sorted(
eligible,
key=lambda r: (
round(scores[r], 6),
last_assigned[r],
reviewers.index(r),
),
)
reviewer_index = {r: i for i, r in enumerate(reviewers)}
ranked = sorted(
eligible,
key=lambda r: (
round(scores[r], 6),
last_assigned[r],
reviewer_index[r],
),
)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely not.

print('Candidate reviewers:')
for r in ranked:
print(
f' {r}: score={scores[r]:.2f} '
f'(review_requests={review_count[r]}, review_load={review_load[r]:.2f}, '
f'authored={authored_count[r]}, authored_load={authored_load[r]:.2f})',
)
chosen = ranked[0]
print(f'Selected reviewer for PR #{pr_number}: {chosen}')

gh_api([
Expand Down
Loading