-
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 2 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 |
---|---|---|
@@ -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,153 @@ | ||
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 | ||
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,39 @@ | ||
from typing import Any | ||
|
||
from codegen.extensions.linear.linear_client import LinearClient | ||
|
||
|
||
def linear_get_issue_tool(client: LinearClient, issue_id: str) -> dict[str, Any]: | ||
"""Get an issue by its ID.""" | ||
try: | ||
issue = client.get_issue(issue_id) | ||
return {"status": "success", "issue": issue.dict()} | ||
except Exception as e: | ||
return {"error": f"Failed to get issue: {e!s}"} | ||
|
||
|
||
def linear_get_issue_comments_tool(client: LinearClient, issue_id: str) -> dict[str, Any]: | ||
"""Get comments for a specific issue.""" | ||
try: | ||
comments = client.get_issue_comments(issue_id) | ||
return {"status": "success", "comments": [comment.dict() for comment in comments]} | ||
except Exception as e: | ||
return {"error": f"Failed to get issue comments: {e!s}"} | ||
|
||
|
||
def linear_comment_on_issue_tool(client: LinearClient, issue_id: str, body: str) -> dict[str, Any]: | ||
"""Add a comment to an issue.""" | ||
try: | ||
comment = client.comment_on_issue(issue_id, body) | ||
return {"status": "success", "comment": comment} | ||
except Exception as e: | ||
return {"error": f"Failed to comment on issue: {e!s}"} | ||
|
||
|
||
def linear_register_webhook_tool(client: LinearClient, webhook_url: str, team_id: str, secret: str, enabled: bool, resource_types: list[str]) -> dict[str, Any]: | ||
"""Register a webhook with Linear.""" | ||
try: | ||
response = client.register_webhook(webhook_url, team_id, secret, enabled, resource_types) | ||
return {"status": "success", "response": response} | ||
except Exception as e: | ||
return {"error": f"Failed to register webhook: {e!s}"} | ||
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,43 @@ | ||
"""Tests for Linear tools.""" | ||
|
||
import os | ||
|
||
import pytest | ||
|
||
from codegen.extensions.linear.linear_client import LinearClient | ||
from codegen.extensions.tools.linear_tools import ( | ||
linear_comment_on_issue_tool, | ||
linear_get_issue_comments_tool, | ||
linear_get_issue_tool, | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def client() -> LinearClient: | ||
"""Create a Linear client for testing.""" | ||
token = os.getenv("LINEAR_ACCESS_TOKEN") | ||
if not token: | ||
pytest.skip("LINEAR_ACCESS_TOKEN environment variable not set") | ||
jayhack marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return LinearClient(token) | ||
|
||
|
||
def test_linear_get_issue(client: LinearClient) -> None: | ||
"""Test getting an issue from Linear.""" | ||
# Link to issue: https://linear.app/codegen-sh/issue/CG-10775/read-file-and-reveal-symbol-tool-size-limits | ||
issue = linear_get_issue_tool(client, "CG-10775") | ||
assert issue["status"] == "success" | ||
assert issue["issue"]["id"] == "d5a7d6db-e20d-4d67-98f8-acedef6d3536" | ||
|
||
|
||
def test_linear_get_issue_comments(client: LinearClient) -> None: | ||
"""Test getting comments for an issue from Linear.""" | ||
comments = linear_get_issue_comments_tool(client, "CG-10775") | ||
assert comments["status"] == "success" | ||
assert len(comments["comments"]) > 1 | ||
|
||
|
||
def test_linear_comment_on_issue(client: LinearClient) -> None: | ||
"""Test commenting on a Linear issue.""" | ||
test_comment = "Test comment from automated testing" | ||
result = linear_comment_on_issue_tool(client, "CG-10775", test_comment) | ||
assert result["status"] == "success" |
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.