|
| 1 | +import json |
| 2 | +import logging |
| 3 | + |
| 4 | +import requests |
| 5 | +from pydantic import BaseModel |
| 6 | + |
| 7 | +logger = logging.getLogger(__name__) |
| 8 | + |
| 9 | + |
| 10 | +# --- TYPES |
| 11 | + |
| 12 | + |
| 13 | +class LinearUser(BaseModel): |
| 14 | + id: str |
| 15 | + name: str |
| 16 | + |
| 17 | + |
| 18 | +class LinearComment(BaseModel): |
| 19 | + id: str |
| 20 | + body: str |
| 21 | + user: LinearUser | None = None |
| 22 | + |
| 23 | + |
| 24 | +class LinearIssue(BaseModel): |
| 25 | + id: str |
| 26 | + title: str |
| 27 | + description: str | None = None |
| 28 | + |
| 29 | + |
| 30 | +class LinearClient: |
| 31 | + api_headers: dict |
| 32 | + api_endpoint = "https://api.linear.app/graphql" |
| 33 | + |
| 34 | + def __init__(self, access_token: str): |
| 35 | + self.access_token = access_token |
| 36 | + self.api_headers = { |
| 37 | + "Content-Type": "application/json", |
| 38 | + "Authorization": self.access_token, |
| 39 | + } |
| 40 | + |
| 41 | + def get_issue(self, issue_id: str) -> LinearIssue: |
| 42 | + query = """ |
| 43 | + query getIssue($issueId: String!) { |
| 44 | + issue(id: $issueId) { |
| 45 | + id |
| 46 | + title |
| 47 | + description |
| 48 | + } |
| 49 | + } |
| 50 | + """ |
| 51 | + variables = {"issueId": issue_id} |
| 52 | + response = requests.post(self.api_endpoint, headers=self.api_headers, json={"query": query, "variables": variables}) |
| 53 | + data = response.json() |
| 54 | + issue_data = data["data"]["issue"] |
| 55 | + return LinearIssue(id=issue_data["id"], title=issue_data["title"], description=issue_data["description"]) |
| 56 | + |
| 57 | + def get_issue_comments(self, issue_id: str) -> list[LinearComment]: |
| 58 | + query = """ |
| 59 | + query getIssueComments($issueId: String!) { |
| 60 | + issue(id: $issueId) { |
| 61 | + comments { |
| 62 | + nodes { |
| 63 | + id |
| 64 | + body |
| 65 | + user { |
| 66 | + id |
| 67 | + name |
| 68 | + } |
| 69 | + } |
| 70 | +
|
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + """ |
| 75 | + variables = {"issueId": issue_id} |
| 76 | + response = requests.post(self.api_endpoint, headers=self.api_headers, json={"query": query, "variables": variables}) |
| 77 | + data = response.json() |
| 78 | + comments = data["data"]["issue"]["comments"]["nodes"] |
| 79 | + |
| 80 | + # Parse comments into list of LinearComment objects |
| 81 | + parsed_comments = [] |
| 82 | + for comment in comments: |
| 83 | + user = comment.get("user", None) |
| 84 | + parsed_comment = LinearComment(id=comment["id"], body=comment["body"], user=LinearUser(id=user.get("id"), name=user.get("name")) if user else None) |
| 85 | + parsed_comments.append(parsed_comment) |
| 86 | + |
| 87 | + # Convert raw comments to LinearComment objects |
| 88 | + return parsed_comments |
| 89 | + |
| 90 | + def comment_on_issue(self, issue_id: str, body: str) -> dict: |
| 91 | + """issue_id is our internal issue ID""" |
| 92 | + query = """mutation makeComment($issueId: String!, $body: String!) { |
| 93 | + commentCreate(input: {issueId: $issueId, body: $body}) { |
| 94 | + comment { |
| 95 | + id |
| 96 | + body |
| 97 | + url |
| 98 | + user { |
| 99 | + id |
| 100 | + name |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + """ |
| 106 | + variables = {"issueId": issue_id, "body": body} |
| 107 | + response = requests.post( |
| 108 | + self.api_endpoint, |
| 109 | + headers=self.api_headers, |
| 110 | + data=json.dumps({"query": query, "variables": variables}), |
| 111 | + ) |
| 112 | + data = response.json() |
| 113 | + try: |
| 114 | + comment_data = data["data"]["commentCreate"]["comment"] |
| 115 | + |
| 116 | + return comment_data |
| 117 | + except: |
| 118 | + msg = f"Error creating comment\n{data}" |
| 119 | + raise Exception(msg) |
| 120 | + |
| 121 | + def unregister_webhook(self, webhook_id: str): |
| 122 | + mutation = """ |
| 123 | + mutation deleteWebhook($id: String!) { |
| 124 | + webhookDelete(id: $id) { |
| 125 | + success |
| 126 | + } |
| 127 | + } |
| 128 | + """ |
| 129 | + variables = {"id": webhook_id} |
| 130 | + response = requests.post(self.api_endpoint, headers=self.api_headers, json={"query": mutation, "variables": variables}) |
| 131 | + return response.json() |
| 132 | + |
| 133 | + def register_webhook(self, webhook_url: str, team_id: str, secret: str, enabled: bool, resource_types: list[str]): |
| 134 | + mutation = """ |
| 135 | + mutation createWebhook($input: WebhookCreateInput!) { |
| 136 | + webhookCreate(input: $input) { |
| 137 | + success |
| 138 | + webhook { |
| 139 | + id |
| 140 | + enabled |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + """ |
| 145 | + |
| 146 | + variables = { |
| 147 | + "input": { |
| 148 | + "url": webhook_url, |
| 149 | + "teamId": team_id, |
| 150 | + "resourceTypes": resource_types, |
| 151 | + "enabled": enabled, |
| 152 | + "secret": secret, |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + response = requests.post(self.api_endpoint, headers=self.api_headers, json={"query": mutation, "variables": variables}) |
| 157 | + if response.status_code != 200: |
| 158 | + return None |
| 159 | + |
| 160 | + body = response.json() |
| 161 | + body = body["data"]["webhookCreate"]["webhook"]["id"] |
| 162 | + return body |
0 commit comments