-
Notifications
You must be signed in to change notification settings - Fork 52
feat: adds linear tools #499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import logging | ||
|
||
import modal | ||
import modal # deptry: ignore | ||
|
||
from codegen.extensions.events.linear import Linear | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .linear_client import LinearClient | ||
|
||
__all__ = ["LinearClient"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
import json | ||
import logging | ||
import os | ||
from typing import Optional | ||
|
||
import requests | ||
from pydantic import BaseModel | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
# --- TYPES | ||
|
||
|
||
class LinearUser(BaseModel): | ||
id: str | ||
name: str | ||
|
||
|
||
class LinearComment(BaseModel): | ||
id: str | ||
body: str | ||
user: LinearUser | None = None | ||
|
||
|
||
class LinearIssue(BaseModel): | ||
id: str | ||
title: str | ||
description: str | None = None | ||
|
||
|
||
class LinearClient: | ||
api_headers: dict | ||
api_endpoint = "https://api.linear.app/graphql" | ||
|
||
def __init__(self, access_token: Optional[str] = None): | ||
if not access_token: | ||
access_token = os.getenv("LINEAR_ACCESS_TOKEN") | ||
if not access_token: | ||
msg = "access_token is required" | ||
raise ValueError(msg) | ||
self.access_token = access_token | ||
self.api_headers = { | ||
"Content-Type": "application/json", | ||
"Authorization": self.access_token, | ||
} | ||
|
||
def get_issue(self, issue_id: str) -> LinearIssue: | ||
query = """ | ||
query getIssue($issueId: String!) { | ||
issue(id: $issueId) { | ||
id | ||
title | ||
description | ||
} | ||
} | ||
""" | ||
variables = {"issueId": issue_id} | ||
response = requests.post(self.api_endpoint, headers=self.api_headers, json={"query": query, "variables": variables}) | ||
data = response.json() | ||
issue_data = data["data"]["issue"] | ||
return LinearIssue(id=issue_data["id"], title=issue_data["title"], description=issue_data["description"]) | ||
|
||
def get_issue_comments(self, issue_id: str) -> list[LinearComment]: | ||
query = """ | ||
query getIssueComments($issueId: String!) { | ||
issue(id: $issueId) { | ||
comments { | ||
nodes { | ||
id | ||
body | ||
user { | ||
id | ||
name | ||
} | ||
} | ||
|
||
} | ||
} | ||
} | ||
""" | ||
variables = {"issueId": issue_id} | ||
response = requests.post(self.api_endpoint, headers=self.api_headers, json={"query": query, "variables": variables}) | ||
data = response.json() | ||
comments = data["data"]["issue"]["comments"]["nodes"] | ||
|
||
# Parse comments into list of LinearComment objects | ||
parsed_comments = [] | ||
for comment in comments: | ||
user = comment.get("user", None) | ||
parsed_comment = LinearComment(id=comment["id"], body=comment["body"], user=LinearUser(id=user.get("id"), name=user.get("name")) if user else None) | ||
parsed_comments.append(parsed_comment) | ||
|
||
# Convert raw comments to LinearComment objects | ||
return parsed_comments | ||
|
||
def comment_on_issue(self, issue_id: str, body: str) -> dict: | ||
"""issue_id is our internal issue ID""" | ||
query = """mutation makeComment($issueId: String!, $body: String!) { | ||
commentCreate(input: {issueId: $issueId, body: $body}) { | ||
comment { | ||
id | ||
body | ||
url | ||
user { | ||
id | ||
name | ||
} | ||
} | ||
} | ||
} | ||
""" | ||
variables = {"issueId": issue_id, "body": body} | ||
response = requests.post( | ||
self.api_endpoint, | ||
headers=self.api_headers, | ||
data=json.dumps({"query": query, "variables": variables}), | ||
) | ||
data = response.json() | ||
try: | ||
comment_data = data["data"]["commentCreate"]["comment"] | ||
|
||
return comment_data | ||
except Exception as e: | ||
msg = f"Error creating comment\n{data}\n{e}" | ||
raise Exception(msg) | ||
|
||
def register_webhook(self, webhook_url: str, team_id: str, secret: str, enabled: bool, resource_types: list[str]): | ||
mutation = """ | ||
mutation createWebhook($input: WebhookCreateInput!) { | ||
webhookCreate(input: $input) { | ||
success | ||
webhook { | ||
id | ||
enabled | ||
} | ||
} | ||
} | ||
""" | ||
|
||
variables = { | ||
"input": { | ||
"url": webhook_url, | ||
"teamId": team_id, | ||
"resourceTypes": resource_types, | ||
"enabled": enabled, | ||
"secret": secret, | ||
} | ||
} | ||
|
||
response = requests.post(self.api_endpoint, headers=self.api_headers, json={"query": mutation, "variables": variables}) | ||
body = response.json() | ||
return body | ||
|
||
def search_issues(self, query: str, limit: int = 10) -> list[LinearIssue]: | ||
"""Search for issues using a query string. | ||
|
||
Args: | ||
query: Search query string | ||
limit: Maximum number of issues to return (default: 10) | ||
|
||
Returns: | ||
List of LinearIssue objects matching the search query | ||
""" | ||
graphql_query = """ | ||
query searchIssues($query: String!, $limit: Int!) { | ||
issueSearch(query: $query, first: $limit) { | ||
nodes { | ||
id | ||
title | ||
description | ||
} | ||
} | ||
} | ||
""" | ||
variables = {"query": query, "limit": limit} | ||
response = requests.post( | ||
self.api_endpoint, | ||
headers=self.api_headers, | ||
json={"query": graphql_query, "variables": variables}, | ||
) | ||
data = response.json() | ||
|
||
try: | ||
issues_data = data["data"]["issueSearch"]["nodes"] | ||
return [ | ||
LinearIssue( | ||
id=issue["id"], | ||
title=issue["title"], | ||
description=issue["description"], | ||
) | ||
for issue in issues_data | ||
] | ||
except Exception as e: | ||
msg = f"Error searching issues\n{data}\n{e}" | ||
raise Exception(msg) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.