From 5c2125dffa9cc40215461c75e8504b985e51177f Mon Sep 17 00:00:00 2001 From: Maxwell G Date: Tue, 25 Jul 2023 16:15:22 +0000 Subject: [PATCH] labeler: improve logging --- hacking/pr_labeler/label.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/hacking/pr_labeler/label.py b/hacking/pr_labeler/label.py index 55e76badc0..4c7523ad67 100644 --- a/hacking/pr_labeler/label.py +++ b/hacking/pr_labeler/label.py @@ -31,6 +31,12 @@ IssueOrPrCtx = Union["IssueLabelerCtx", "PRLabelerCtx"] IssueOrPr = Union["github.Issue.Issue", "github.PullRequest.PullRequest"] +# TODO: If we end up needing to log more things with more granularity, +# switch to something like `logging` +def log(ctx: IssueOrPrCtx, *args: str) -> None: + print(f"{ctx.member.number}:", *args) + + def get_repo(authed: bool = True) -> tuple[github.Github, github.Repository.Repository]: gclient = github.Github( auth=github.Auth.Token(os.environ["GITHUB_TOKEN"]) if authed else None, @@ -120,7 +126,7 @@ def add_label_if_new(ctx: IssueOrPrCtx, labels: Collection[str] | str) -> None: labels = set(labels) - previously_labeled if not labels: return - print(f"Adding labels to {ctx.member.number}:", *map(repr, labels)) + log(ctx, "Adding labels", *map(repr, labels)) if not ctx.dry_run: ctx.member.add_to_labels(*labels) @@ -133,11 +139,13 @@ def new_contributor_welcome(ctx: IssueOrPrCtx) -> None: # This contributor has already been welcomed! if "new_contributor" in previously_labeled: return + log(ctx, "author_association is", ctx.member.raw_data["author_association"]) if ctx.member.raw_data["author_association"] not in { "FIRST_TIMER", "FIRST_TIME_CONTRIBUTOR", }: return + log(ctx, "Welcoming new contributor") add_label_if_new(ctx, "new_contributor") create_comment(ctx, get_data_file("docs_team_info.md")) @@ -156,10 +164,10 @@ def cb(): def process_pr(pr_number: int, dry_run: bool = False) -> None: gclient, repo = get_repo(authed=not dry_run) pr = repo.get_pull(pr_number) - if pr.state != "open": - print("Refusing to process closed ticket") - return ctx = PRLabelerCtx(client=gclient, repo=repo, pr=pr, dry_run=dry_run) + if pr.state != "open": + log(ctx, "Refusing to process closed ticket") + return handle_codeowner_labels(ctx) add_label_if_new(ctx, "needs_triage") @@ -170,10 +178,10 @@ def process_pr(pr_number: int, dry_run: bool = False) -> None: def process_issue(issue_number: int, dry_run: bool = False) -> None: gclient, repo = get_repo(authed=not dry_run) issue = repo.get_issue(issue_number) - if issue.state != "open": - print("Refusing to process closed ticket") - return ctx = IssueLabelerCtx(client=gclient, repo=repo, issue=issue, dry_run=dry_run) + if issue.state != "open": + log(ctx, "Refusing to process closed ticket") + return add_label_if_new(ctx, "needs_triage") new_contributor_welcome(ctx)