|
| 1 | +import dataclasses |
| 2 | + |
| 3 | +import httpx |
| 4 | +from django.conf import settings |
| 5 | + |
| 6 | +GITHUB_API_URL = "https://api.github.com/graphql" |
| 7 | + |
| 8 | +# GraphQL query |
| 9 | +query = """ |
| 10 | +query($itemId: ID!) { |
| 11 | + node(id: $itemId) { |
| 12 | + ... on ProjectV2Item { |
| 13 | + id |
| 14 | + content { |
| 15 | + ... on DraftIssue { |
| 16 | + id |
| 17 | + title |
| 18 | + } |
| 19 | + ... on Issue { |
| 20 | + id |
| 21 | + title |
| 22 | + url |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | +""" |
| 29 | + |
| 30 | + |
| 31 | +def parse_github_webhook(headers: dict, content: dict) -> tuple[str, str]: |
| 32 | + event = headers["X-Github-Event"] |
| 33 | + |
| 34 | + if event == "projects_v2_item": |
| 35 | + parser = GithubProjectV2Item(content) |
| 36 | + formatted = parser.as_str() |
| 37 | + action = parser.action() |
| 38 | + event_action = f"{event}.{action}" |
| 39 | + return formatted, event_action |
| 40 | + |
| 41 | + elif event == "...": |
| 42 | + return "", "" |
| 43 | + |
| 44 | + else: |
| 45 | + raise ValueError(f"Event {event} not supported") |
| 46 | + |
| 47 | + |
| 48 | +@dataclasses.dataclass |
| 49 | +class GithubIssue: |
| 50 | + id: str |
| 51 | + title: str |
| 52 | + url: str |
| 53 | + |
| 54 | + def as_discord_message(self): |
| 55 | + return f"[{self.title}]({self.url})" |
| 56 | + |
| 57 | + |
| 58 | +@dataclasses.dataclass |
| 59 | +class GithubDraftIssue: |
| 60 | + id: str |
| 61 | + title: str |
| 62 | + |
| 63 | + def as_discord_message(self): |
| 64 | + return self.title |
| 65 | + |
| 66 | + |
| 67 | +def fetch_github_item_details(item_id): |
| 68 | + headers = { |
| 69 | + "Authorization": f"Bearer {settings.GITHUB_TOKEN}", |
| 70 | + "Content-Type": "application/json", |
| 71 | + } |
| 72 | + payload = {"query": query, "variables": {"itemId": item_id}} |
| 73 | + response = httpx.post(GITHUB_API_URL, json=payload, headers=headers) |
| 74 | + if response.status_code == 200: |
| 75 | + return response.json()["data"]["node"]["content"] |
| 76 | + else: |
| 77 | + raise Exception(f"GitHub API error: {response.status_code} - {response.text}") |
| 78 | + |
| 79 | + |
| 80 | +CONTENT_TYPE_MAP = { |
| 81 | + "Issue": GithubIssue, |
| 82 | + "DraftIssue": GithubDraftIssue, |
| 83 | +} |
| 84 | + |
| 85 | + |
| 86 | +class GithubProjectV2Item: |
| 87 | + def __init__(self, content: dict): |
| 88 | + self.content = content |
| 89 | + |
| 90 | + def action(self): |
| 91 | + if self.content["action"] == "edited": |
| 92 | + action = "changed" |
| 93 | + elif self.content["action"] == "created": |
| 94 | + action = "created" |
| 95 | + else: |
| 96 | + raise ValueError(f"Action unsupported {self.content['action']}") |
| 97 | + |
| 98 | + return action |
| 99 | + |
| 100 | + def sender(self): |
| 101 | + login = self.content["sender"]["login"] |
| 102 | + url = self.content["sender"]["html_url"] |
| 103 | + |
| 104 | + return f"[@{login}]({url})" |
| 105 | + |
| 106 | + def content_type(self): |
| 107 | + return self.content["projects_v2_item"]["content_type"] |
| 108 | + |
| 109 | + def node_id(self): |
| 110 | + # NOTE(artcz): This is more relevant, because of how the graphql query |
| 111 | + # above is constructed. |
| 112 | + # Using node_id, which is an id of a ProjectV2Item we can get both |
| 113 | + # DraftIssue and Issue from one query. |
| 114 | + # If we use the content_node_id we need to adjust the query as that ID |
| 115 | + # points us directly either an Issue or DraftIssue |
| 116 | + return self.content["projects_v2_item"]["node_id"] |
| 117 | + |
| 118 | + def content_node_id(self): |
| 119 | + return self.content["projects_v2_item"]["content_node_id"] |
| 120 | + |
| 121 | + def changes(self) -> dict: |
| 122 | + if "changes" in self.content: |
| 123 | + fv = self.content["changes"]["field_value"] |
| 124 | + field_name = fv["field_name"] |
| 125 | + field_type = fv["field_type"] |
| 126 | + if field_type == "date": |
| 127 | + changed_from = ( |
| 128 | + fv["from"].split("T")[0] if fv["from"] is not None else "None" |
| 129 | + ) |
| 130 | + changed_to = fv["to"].split("T")[0] if fv["to"] is not None else "None" |
| 131 | + elif field_type == "single_select": |
| 132 | + changed_from = fv["from"]["name"] if fv["from"] is not None else "None" |
| 133 | + changed_to = fv["to"]["name"] if fv["to"] is not None else "None" |
| 134 | + else: |
| 135 | + changed_from = "None" |
| 136 | + changed_to = "None" |
| 137 | + |
| 138 | + return { |
| 139 | + "field": field_name, |
| 140 | + "from": changed_from, |
| 141 | + "to": changed_to, |
| 142 | + } |
| 143 | + |
| 144 | + return {} |
| 145 | + |
| 146 | + def as_discord_message(self, github_object: GithubDraftIssue | GithubIssue) -> str: |
| 147 | + message = "{sender} {action} {details}".format |
| 148 | + |
| 149 | + action = self.action() |
| 150 | + sender = self.sender() |
| 151 | + |
| 152 | + changes = self.changes() |
| 153 | + |
| 154 | + if changes: |
| 155 | + details = "**{field}** of **{obj}** from **{from}** to **{to}**".format |
| 156 | + details = details(**{"obj": github_object.as_discord_message(), **changes}) |
| 157 | + |
| 158 | + else: |
| 159 | + details = github_object.as_discord_message() |
| 160 | + |
| 161 | + return message( |
| 162 | + **{ |
| 163 | + "sender": sender, |
| 164 | + "action": action, |
| 165 | + "details": details, |
| 166 | + } |
| 167 | + ) |
| 168 | + |
| 169 | + def fetch_quoted_github_object(self) -> GithubIssue | GithubDraftIssue: |
| 170 | + obj = fetch_github_item_details(self.node_id()) |
| 171 | + |
| 172 | + obj = CONTENT_TYPE_MAP[self.content_type()](**obj) |
| 173 | + |
| 174 | + return obj |
| 175 | + |
| 176 | + def as_str(self): |
| 177 | + github_obj = self.fetch_quoted_github_object() |
| 178 | + return self.as_discord_message(github_obj) |
0 commit comments