Skip to content
Open
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
108 changes: 108 additions & 0 deletions tests/core/test_update_bounty_board.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
"""Tests for tools/update_bounty_board.py: completion, credit and rendering."""

import importlib.util
import tempfile
import textwrap
import unittest
from pathlib import Path

_PATH = Path(__file__).resolve().parents[2] / "tools" / "update_bounty_board.py"
_spec = importlib.util.spec_from_file_location("update_bounty_board", _PATH)
board = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(board)

YAML = textwrap.dedent("""\
bounty:
- id: 1
priority: 1
category: model
title: Old model
description: "Done long ago."
complete: "@alice (alice@illinois.edu), @bob"
- id: 2
priority: 2
category: model
title: Merged model
description: "Merged, credit not recorded yet."
complete: ~
- id: 3
priority: 3
category: dataset
title: Claimed dataset
description: "An open PR."
complete: ~
- id: 4
priority: 4
category: task
title: Free task
description: "Nobody yet."
complete: ~
""")

PRS = [
{"number": 10, "title": "[BTY-2] Merged model", "state": "MERGED", "author": "carol"},
{"number": 12, "title": "[BTY-2] follow-up", "state": "MERGED", "author": "carol"},
{"number": 11, "title": "[Bounty-2] other half", "state": "MERGED", "author": "dan"},
{"number": 13, "title": "[BTY-3] WIP dataset", "state": "OPEN", "author": "erin"},
{"number": 14, "title": "[BTY-99] unknown", "state": "OPEN", "author": "frank"},
]


class TestBountyBoard(unittest.TestCase):
def setUp(self):
with tempfile.NamedTemporaryFile("w", suffix=".yaml", delete=False) as f:
f.write(YAML)
self.addCleanup(Path(f.name).unlink)
self.bounties = board.load_bounties(f.name)
self.claims, self.unmatched = board.link_prs(self.bounties, PRS)

def test_every_bounty_is_loaded_without_emails(self):
self.assertEqual(sorted(self.bounties), [1, 2, 3, 4])
self.assertEqual(self.bounties[1]["complete"], "@alice, @bob")
self.assertIsNone(self.bounties[2]["complete"])

def test_status_and_credit(self):
def status(bid):
return board.status(self.bounties[bid], self.claims.get(bid, []))

self.assertEqual(status(1), ("Completed", "@alice, @bob"))
# a merged PR completes the bounty; credit groups PRs by author
self.assertEqual(status(2), ("Completed", "@carol (#10, #12), @dan (#11)"))
self.assertEqual(status(3), ("Claimed", None))
self.assertEqual(status(4), ("Unclaimed", None))
self.assertEqual([(bid, pr["number"]) for bid, pr in self.unmatched],
[(99, 14)])

def test_uncredited_lists_merged_bounties_missing_from_yaml(self):
self.assertEqual(board.uncredited(self.bounties, self.claims),
{2: "@carol (#10, #12), @dan (#11)"})

def test_board_has_a_completed_section_with_credit(self):
sections = dict(board.build_sections(self.bounties, self.claims))
completed = dict(sections["Completed"])
self.assertEqual(completed["Model"], [
"- **BTY-2** · Merged model · @carol (#10, #12), @dan (#11)",
"- **BTY-1** · Old model · @alice, @bob",
])
self.assertEqual([c for c, _ in sections["Claimed"]], ["Dataset"])
self.assertEqual([c for c, _ in sections["Unclaimed"]], ["Task"])
header = board.render_header("org/repo", 1, 1, 2)
page = board.paginate(header, board.build_sections(self.bounties,
self.claims))[0]
self.assertIn("Completed: 2. Thank you", page)
self.assertIn("## Completed", page)
self.assertNotIn("illinois.edu", page)

def test_invalid_complete_is_rejected(self):
bad = YAML.replace("complete: ~\n - id: 3", "complete: 5\n - id: 3")
self.assertNotEqual(bad, YAML)
with tempfile.NamedTemporaryFile("w", suffix=".yaml", delete=False) as f:
f.write(bad)
self.addCleanup(Path(f.name).unlink)
with self.assertRaises(SystemExit) as ctx:
board.load_bounties(f.name)
self.assertIn("`complete` must be a non-empty string", str(ctx.exception))


if __name__ == "__main__":
unittest.main()
113 changes: 92 additions & 21 deletions tools/update_bounty_board.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@

Each run rebuilds the board from scratch:

1. Load bounty.yaml and drop entries whose `complete` field is set.
1. Load every bounty from bounty.yaml.
2. Link every open or merged PR whose title carries `[BTY-<id>]` or
`[Bounty-<id>]` (case-insensitive) to that bounty. PRs closed without
merging are ignored.
3. Render the board: Unclaimed, then Claimed; within each, one subsection
per category (alphabetical, only if non-empty); within each, id order.
4. Find the board issue (created by the bot, hidden marker in the body),
3. A bounty is complete once a linked PR merges, or when its `complete`
field is set. Its credit is the `complete` text, or else the authors of
its merged PRs; those are reported so the credit is recorded in
bounty.yaml, which also feeds the completed-bounties list on pyhealth.dev.
4. Render the board: Unclaimed, Claimed, then Completed (with credit);
within each, one subsection per category, in id order.
5. Find the board issue (created by the bot, hidden marker in the body),
creating it if missing; keep it open, locked and pinned.
5. Write the board into the issue body and, when it is longer than one
6. Write the board into the issue body and, when it is longer than one
body allows, into follow-up bot comments. A bounty entry is never split
across two pages. Only pages whose text changed are edited.

Expand Down Expand Up @@ -46,9 +50,12 @@
GITHUB_MAX_CHARS = 65536
PAGE_BUDGET = 60000

# Never publish an email address from `complete`, even if one is added there.
EMAIL_RE = re.compile(r"\s*\(?[\w.%+-]+@[\w-]+(?:\.[\w-]+)+\)?")

TITLE_TAG_RE = re.compile(r"\[\s*(?:bounty|bty)\s*-\s*(\d+)\s*\]", re.IGNORECASE)

SECTIONS = ("Unclaimed", "Claimed")
SECTIONS = ("Unclaimed", "Claimed", "Completed")

# Subsection order; a category outside this list is rejected.
CATEGORIES = (
Expand All @@ -67,7 +74,10 @@
# bounty.yaml
# --------------------------------------------------------------------------- #
def load_bounties(path):
"""Return the open (not complete) bounties from `path`, keyed by id."""
"""Return every bounty from `path`, keyed by id.

`complete` is the credit text when the field is set, else None.
"""
data = yaml.safe_load(Path(path).read_text(encoding="utf-8")) or {}
entries = data.get("bounty") or []
errors = []
Expand Down Expand Up @@ -102,14 +112,18 @@ def load_bounties(path):
errors.append(
f"{where}: `priority` must be an integer from 1 (highest) to 5"
)
if entry.get("complete") is not None:
continue
complete = entry.get("complete")
if complete is not None and (not isinstance(complete, str)
or not complete.strip()):
errors.append(f"{where}: `complete` must be a non-empty string or ~")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[low] Previously accepted complete values now abort the whole run

Before this change any non-null complete marked a bounty complete. Now "", whitespace or non-string values such as true raise SystemExit for the entire board update. The current bounty.yaml has only valid strings or ~, so this only affects future edits. Consider mentioning the stricter rule in the bounty.yaml header or the contribution docs.

complete = None
bounties[bid] = {
"id": bid,
"priority": priority,
"title": str(entry.get("title", "")).strip(),
"category": category,
"description": str(entry.get("description", "")).strip(),
"complete": EMAIL_RE.sub("", complete).strip() if complete else None,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[low] Email-only complete reopens the bounty

EMAIL_RE.sub("", complete).strip() can produce an empty string when complete holds only an email address. The validation accepts that value because it is a non-empty string before stripping. The stored value is then "", which status() treats as not complete, so the bounty returns to Claimed or Unclaimed and uncredited() skips it. Validate the value after stripping, or reject a result that is empty.

}
if errors:
raise SystemExit("bounty.yaml is invalid:\n " + "\n ".join(errors))
Expand Down Expand Up @@ -215,7 +229,7 @@ def fetch_prs(gh):
# Linking and rendering
# --------------------------------------------------------------------------- #
def link_prs(bounties, prs):
"""Map bounty id -> linked PRs, plus PRs that name an unknown/complete id."""
"""Map bounty id -> linked PRs, plus PRs that name an unknown id."""
claims = defaultdict(list)
unmatched = []
for pr in prs:
Expand All @@ -232,6 +246,38 @@ def link_prs(bounties, prs):
return claims, unmatched


def merged_credit(claim_prs):
"""`@author (#1, #2), @other (#3)` for the merged PRs, or None."""
by_author = defaultdict(list) # insertion order = first PR number
for p in claim_prs:
if p["state"] == "MERGED":
by_author[p["author"]].append(f"#{p['number']}")
if not by_author:
return None
return ", ".join(f"@{a} ({', '.join(prs)})" for a, prs in by_author.items())


def status(bounty, claim_prs):
"""`(section, credit)`: credit is set for completed bounties only."""
credit = bounty["complete"] or merged_credit(claim_prs)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[low] The first merged PR completes a bounty, even for multi-PR bounties

status() marks a bounty Completed as soon as any linked PR merges, which removes it from the open list. Bounties that need several PRs, such as the BTY-2 example in the tests, lose their remaining work from the board after the first merge. This follows the stated rule "Merged means complete", so it may be intended. If it is, consider saying so in the header or the PR description.

if credit:
return "Completed", credit
return ("Claimed" if claim_prs else "Unclaimed"), None


def uncredited(bounties, claims):
"""Bounties completed by a merged PR whose credit isn't in bounty.yaml."""
return {
bid: merged_credit(claims.get(bid, []))
for bid, b in sorted(bounties.items())
if b["complete"] is None and merged_credit(claims.get(bid, []))
}


def render_completed(bounty, credit):
return f"- **BTY-{bounty['id']}** · {bounty['title']} · {credit}"


def render_entry(bounty, claim_prs):
icon = PRIORITY_ICONS[bounty["priority"]]
heading = f"#### {icon} · BTY-{bounty['id']} · {bounty['title']}"
Expand All @@ -254,10 +300,19 @@ def build_sections(bounties, claims):
grouped = {s: defaultdict(list) for s in SECTIONS}
for bounty in sorted(bounties.values(), key=lambda b: (b["priority"], b["id"])):
bid = bounty["id"]
section = "Claimed" if claims.get(bid) else "Unclaimed"
section, credit = status(bounty, claims.get(bid, []))
if section == "Completed":
continue
grouped[section][bounty["category"]].append(
render_entry(bounty, claims.get(bid, []))
)
# Completed: one line each, newest bounty first, credit included.
for bounty in sorted(bounties.values(), key=lambda b: -b["id"]):
section, credit = status(bounty, claims.get(bounty["id"], []))
if section == "Completed":
grouped[section][bounty["category"]].append(
render_completed(bounty, credit)
)
return [
(
section,
Expand Down Expand Up @@ -293,8 +348,9 @@ def build_sections(bounties, claims):
- Claim a bounty by opening a pull request (a draft is fine) with its ID in the \
title, like `[BTY-<id>] <short description>`. `[Bounty-<id>]` works too. This \
board usually updates within a few minutes.
- Your claim holds while the PR is open and after it merges. Closing it without \
merging releases the bounty.
- Your claim holds while the PR is open. When it merges, the bounty moves to \
*Completed* with your name, here and on [pyhealth.dev](https://pyhealth.dev). \
Closing it without merging releases the bounty.
- Build from the paper's official code, use datasets PyHealth already loads \
where possible, and aim to reproduce the paper's results.
- Every bounty needs an implementation that follows the \
Expand All @@ -309,10 +365,11 @@ def build_sections(bounties, claims):
*This board is generated from [`bounty.yaml`]({yaml_url}) and updated when a tagged PR changes. \
Edits made here are overwritten.*

Open bounties: {total} ({claimed} claimed, {unclaimed} unclaimed)."""
Open bounties: {total} ({claimed} claimed, {unclaimed} unclaimed). \
Completed: {completed}. Thank you to everyone who built them!"""


def render_header(repo, n_claimed, n_unclaimed):
def render_header(repo, n_claimed, n_unclaimed, n_completed=0):
legend = ", ".join(
f"{PRIORITY_ICONS[p]} {PRIORITY_NAMES[p]}" for p in PRIORITY_ICONS
)
Expand All @@ -325,6 +382,7 @@ def render_header(repo, n_claimed, n_unclaimed):
total=n_claimed + n_unclaimed,
claimed=n_claimed,
unclaimed=n_unclaimed,
completed=n_completed,
)


Expand Down Expand Up @@ -522,15 +580,24 @@ def main():

claims, unmatched = link_prs(bounties, prs)
sections = build_sections(bounties, claims)
n_claimed = sum(1 for bid in bounties if claims.get(bid))
header = render_header(args.repo, n_claimed, len(bounties) - n_claimed)
counts = defaultdict(int)
for bid, bounty in bounties.items():
counts[status(bounty, claims.get(bid, []))[0]] += 1
n_claimed, n_open = counts["Claimed"], counts["Claimed"] + counts["Unclaimed"]
header = render_header(args.repo, n_claimed, counts["Unclaimed"],
counts["Completed"])
body, comments = finalize_pages(paginate(header, sections, args.page_budget))
to_record = uncredited(bounties, claims)

print(f"{len(bounties)} open bounties ({n_claimed} claimed), "
print(f"{n_open} open bounties ({n_claimed} claimed), "
f"{counts['Completed']} completed, "
f"{len(prs)} PRs scanned, {1 + len(comments)} page(s).")
for bid, pr in unmatched:
print(f"::notice::PR #{pr['number']} names BTY-{bid}, "
"which is not an open bounty.")
"which is not in bounty.yaml.")
for bid, credit in to_record.items():
print(f"::notice::BTY-{bid} is complete; record "
f'`complete: "{credit}"` in bounty.yaml.')

if args.dry_run:
for i, text in enumerate([body, *comments], 1):
Expand All @@ -543,11 +610,15 @@ def main():
summary = [
"## Bounty board",
f"Issue: {issue['html_url']}",
f"- Open bounties: {len(bounties)} ({n_claimed} claimed)",
f"- Open bounties: {n_open} ({n_claimed} claimed)",
f"- Completed bounties: {counts['Completed']}",
f"- Pages: {1 + len(comments)}",
]
if to_record:
summary.append("- Complete, credit not yet in bounty.yaml: " + ", ".join(
f"BTY-{bid} ({credit})" for bid, credit in to_record.items()))
if unmatched:
summary.append("- PRs naming a bounty that is not open: " + ", ".join(
summary.append("- PRs naming a bounty not in bounty.yaml: " + ", ".join(
f"#{pr['number']} (BTY-{bid})" for bid, pr in unmatched))
write_summary(summary)

Expand Down
Loading