|
| 1 | +import typing |
| 2 | + |
| 3 | +import httpx |
| 4 | + |
| 5 | +from .config import GITHUB_TOKEN |
| 6 | +from .release import ReleaseInfo |
| 7 | + |
| 8 | +SIGNATURE = "<!-- action-check: release-file -->" |
| 9 | +API_BASE = "https://api.github.com" |
| 10 | + |
| 11 | + |
| 12 | +def is_release_check_comment(comment: dict) -> bool: |
| 13 | + return ( |
| 14 | + comment["user"]["login"] in ["github-actions[bot]", "botberry"] |
| 15 | + and SIGNATURE in comment["body"] |
| 16 | + ) |
| 17 | + |
| 18 | + |
| 19 | +def get_comments_link(pr_number: int) -> str: |
| 20 | + url = f"/repos/strawberry-graphql/strawberry/issues/{pr_number}/comments" |
| 21 | + |
| 22 | + return API_BASE + url |
| 23 | + |
| 24 | + |
| 25 | +def get_labels_link(pr_number: int) -> str: |
| 26 | + url = f"/repos/strawberry-graphql/strawberry/issues/{pr_number}/labels" |
| 27 | + |
| 28 | + return API_BASE + url |
| 29 | + |
| 30 | + |
| 31 | +def get_comments(pr_number: int) -> typing.List[dict]: |
| 32 | + comments_link = get_comments_link(pr_number) |
| 33 | + |
| 34 | + response = httpx.get(comments_link) |
| 35 | + |
| 36 | + return response.json() |
| 37 | + |
| 38 | + |
| 39 | +def get_labels(pr_number) -> typing.List[dict]: |
| 40 | + labels_link = get_labels_link(pr_number) |
| 41 | + |
| 42 | + response = httpx.get(labels_link) |
| 43 | + |
| 44 | + return response.json() |
| 45 | + |
| 46 | + |
| 47 | +def add_or_edit_comment(pr_number: int, comment: str): |
| 48 | + current_comments = get_comments(pr_number) |
| 49 | + |
| 50 | + previous_comment = next( |
| 51 | + (comment for comment in current_comments if is_release_check_comment(comment)), |
| 52 | + None, |
| 53 | + ) |
| 54 | + |
| 55 | + method = httpx.patch if previous_comment else httpx.post |
| 56 | + url = previous_comment["url"] if previous_comment else get_comments_link(pr_number) |
| 57 | + |
| 58 | + response = method( |
| 59 | + url, |
| 60 | + headers={"Authorization": f"token {GITHUB_TOKEN}"}, |
| 61 | + json={"body": comment + SIGNATURE}, |
| 62 | + ) |
| 63 | + |
| 64 | + response.raise_for_status() |
| 65 | + |
| 66 | + |
| 67 | +def update_labels(pr_number: int, release_info: typing.Optional[ReleaseInfo]): |
| 68 | + labels_to_add = {"bot:has-release-file"} |
| 69 | + labels_to_remove: typing.Set[str] = set() |
| 70 | + |
| 71 | + new_release_label = None |
| 72 | + |
| 73 | + if release_info is None or release_info.change_type is None: |
| 74 | + labels_to_remove = labels_to_add |
| 75 | + labels_to_add = set() |
| 76 | + else: |
| 77 | + new_release_label = f"bot:release-type-{release_info.change_type.value}" |
| 78 | + labels_to_add.add(new_release_label) |
| 79 | + |
| 80 | + labels_url = get_labels_link(pr_number) |
| 81 | + current_labels = get_labels(pr_number) |
| 82 | + |
| 83 | + current_labels_url_by_name = { |
| 84 | + label["name"]: label["url"] for label in current_labels |
| 85 | + } |
| 86 | + |
| 87 | + current_labels = set(current_labels_url_by_name.keys()) |
| 88 | + |
| 89 | + release_labels_to_remove = [ |
| 90 | + label |
| 91 | + for label in current_labels |
| 92 | + if label.startswith("bot:release-type-") and label != new_release_label |
| 93 | + ] |
| 94 | + labels_to_remove.update(release_labels_to_remove) |
| 95 | + |
| 96 | + if not current_labels.issuperset(labels_to_add): |
| 97 | + response = httpx.post( |
| 98 | + labels_url, |
| 99 | + headers={"Authorization": f"token {GITHUB_TOKEN}"}, |
| 100 | + json={"labels": list(labels_to_add)}, |
| 101 | + ) |
| 102 | + |
| 103 | + response.raise_for_status() |
| 104 | + |
| 105 | + if current_labels.issuperset(labels_to_remove): |
| 106 | + for label in labels_to_remove: |
| 107 | + response = httpx.delete( |
| 108 | + current_labels_url_by_name[label], |
| 109 | + headers={"Authorization": f"token {GITHUB_TOKEN}"}, |
| 110 | + ) |
| 111 | + |
| 112 | + response.raise_for_status() |
0 commit comments