Skip to content
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

Fix type checking errors in resolver directory #6738

Merged
merged 5 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions openhands/resolver/interfaces/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,39 @@ def __init__(self, owner: str, repo: str, token: str, username: str | None = Non
self.clone_url = self.get_clone_url()
self.headers = self.get_headers()

def set_owner(self, owner: str):
def set_owner(self, owner: str) -> None:
self.owner = owner

def get_headers(self):
def get_headers(self) -> dict[str, str]:
return {
'Authorization': f'token {self.token}',
'Accept': 'application/vnd.github.v3+json',
}

def get_base_url(self):
def get_base_url(self) -> str:
return f'https://api.github.com/repos/{self.owner}/{self.repo}'

def get_authorize_url(self):
def get_authorize_url(self) -> str:
return f'https://{self.username}:{self.token}@github.com/'

def get_branch_url(self, branch_name: str):
def get_branch_url(self, branch_name: str) -> str:
return self.get_base_url() + f'/branches/{branch_name}'

def get_download_url(self):
def get_download_url(self) -> str:
return f'{self.base_url}/issues'

def get_clone_url(self):
def get_clone_url(self) -> str:
username_and_token = (
f'{self.username}:{self.token}'
if self.username
else f'x-auth-token:{self.token}'
)
return f'https://{username_and_token}@github.com/{self.owner}/{self.repo}.git'

def get_graphql_url(self):
def get_graphql_url(self) -> str:
return 'https://api.github.com/graphql'

def get_compare_url(self, branch_name: str):
def get_compare_url(self, branch_name: str) -> str:
return f'https://github.com/{self.owner}/{self.repo}/compare/{branch_name}?expand=1'

def get_converted_issues(
Expand Down Expand Up @@ -186,15 +186,15 @@ def branch_exists(self, branch_name: str) -> bool:
print(f'Branch {branch_name} exists: {exists}')
return exists

def get_branch_name(self, base_branch_name: str):
def get_branch_name(self, base_branch_name: str) -> str:
branch_name = base_branch_name
attempt = 1
while self.branch_exists(branch_name):
attempt += 1
branch_name = f'{base_branch_name}-try{attempt}'
return branch_name

def reply_to_comment(self, pr_number: int, comment_id: str, reply: str):
def reply_to_comment(self, pr_number: int, comment_id: str, reply: str) -> None:
# Opting for graphql as REST API doesn't allow reply to replies in comment threads
query = """
mutation($body: String!, $pullRequestReviewThreadId: ID!) {
Expand All @@ -221,15 +221,18 @@ def reply_to_comment(self, pr_number: int, comment_id: str, reply: str):
)
response.raise_for_status()

def get_pull_url(self, pr_number: int):
def get_pull_url(self, pr_number: int) -> str:
return f'https://github.com/{self.owner}/{self.repo}/pull/{pr_number}'

def get_default_branch_name(self) -> str:
response = requests.get(f'{self.base_url}', headers=self.headers)
response.raise_for_status()
return response.json()['default_branch']
data = response.json()
return str(data['default_branch'])

def create_pull_request(self, data=dict) -> dict:
def create_pull_request(self, data: dict[str, Any] | None = None) -> dict[str, Any]:
if data is None:
data = {}
response = requests.post(
f'{self.base_url}/pulls', headers=self.headers, json=data
)
Expand All @@ -240,9 +243,9 @@ def create_pull_request(self, data=dict) -> dict:
)
response.raise_for_status()
pr_data = response.json()
return pr_data
return dict(pr_data)

def request_reviewers(self, reviewer: str, pr_number: int):
def request_reviewers(self, reviewer: str, pr_number: int) -> None:
review_data = {'reviewers': [reviewer]}
review_response = requests.post(
f'{self.base_url}/pulls/{pr_number}/requested_reviewers',
Expand All @@ -254,7 +257,7 @@ def request_reviewers(self, reviewer: str, pr_number: int):
f'Warning: Failed to request review from {reviewer}: {review_response.text}'
)

def send_comment_msg(self, issue_number: int, msg: str):
def send_comment_msg(self, issue_number: int, msg: str) -> None:
"""Send a comment message to a GitHub issue or pull request.

Args:
Expand Down Expand Up @@ -282,8 +285,8 @@ def get_context_from_external_issues_references(
review_comments: list[str] | None,
review_threads: list[ReviewThread],
thread_comments: list[str] | None,
):
pass
) -> list[str]:
return []


class GithubPRHandler(GithubIssueHandler):
Expand Down Expand Up @@ -487,7 +490,7 @@ def get_context_from_external_issues_references(
review_comments: list[str] | None,
review_threads: list[ReviewThread],
thread_comments: list[str] | None,
):
) -> list[str]:
new_issue_references = []

if issue_body:
Expand Down
45 changes: 24 additions & 21 deletions openhands/resolver/interfaces/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,38 @@ def __init__(self, owner: str, repo: str, token: str, username: str | None = Non
self.clone_url = self.get_clone_url()
self.headers = self.get_headers()

def set_owner(self, owner: str):
def set_owner(self, owner: str) -> None:
self.owner = owner

def get_headers(self):
def get_headers(self) -> dict[str, str]:
return {
'Authorization': f'Bearer {self.token}',
'Accept': 'application/json',
}

def get_base_url(self):
project_path = quote(f'{self.owner}/{self.repo}', safe="")
def get_base_url(self) -> str:
project_path = quote(f'{self.owner}/{self.repo}', safe='')
return f'https://gitlab.com/api/v4/projects/{project_path}'

def get_authorize_url(self):
def get_authorize_url(self) -> str:
return f'https://{self.username}:{self.token}@gitlab.com/'

def get_branch_url(self, branch_name: str):
def get_branch_url(self, branch_name: str) -> str:
return self.get_base_url() + f'/repository/branches/{branch_name}'

def get_download_url(self):
def get_download_url(self) -> str:
return f'{self.base_url}/issues'

def get_clone_url(self):
def get_clone_url(self) -> str:
username_and_token = self.token
if self.username:
username_and_token = f'{self.username}:{self.token}'
return f'https://{username_and_token}@gitlab.com/{self.owner}/{self.repo}.git'

def get_graphql_url(self):
def get_graphql_url(self) -> str:
return 'https://gitlab.com/api/graphql'

def get_compare_url(self, branch_name: str):
def get_compare_url(self, branch_name: str) -> str:
return f'https://gitlab.com/{self.owner}/{self.repo}/-/compare/{self.get_default_branch_name()}...{branch_name}'

def get_converted_issues(
Expand Down Expand Up @@ -189,15 +189,15 @@ def branch_exists(self, branch_name: str) -> bool:
print(f'Branch {branch_name} exists: {exists}')
return exists

def get_branch_name(self, base_branch_name: str):
def get_branch_name(self, base_branch_name: str) -> str:
branch_name = base_branch_name
attempt = 1
while self.branch_exists(branch_name):
attempt += 1
branch_name = f'{base_branch_name}-try{attempt}'
return branch_name

def reply_to_comment(self, pr_number: int, comment_id: str, reply: str):
def reply_to_comment(self, pr_number: int, comment_id: str, reply: str) -> None:
response = requests.get(
f'{self.base_url}/merge_requests/{pr_number}/discussions/{comment_id.split('/')[-1]}',
headers=self.headers,
Expand All @@ -216,17 +216,20 @@ def reply_to_comment(self, pr_number: int, comment_id: str, reply: str):
)
response.raise_for_status()

def get_pull_url(self, pr_number: int):
def get_pull_url(self, pr_number: int) -> str:
return (
f'https://gitlab.com/{self.owner}/{self.repo}/-/merge_requests/{pr_number}'
)

def get_default_branch_name(self) -> str:
response = requests.get(f'{self.base_url}', headers=self.headers)
response.raise_for_status()
return response.json()['default_branch']
data = response.json()
return str(data['default_branch'])

def create_pull_request(self, data=dict) -> dict:
def create_pull_request(self, data: dict[str, Any] | None = None) -> dict[str, Any]:
if data is None:
data = {}
response = requests.post(
f'{self.base_url}/merge_requests', headers=self.headers, json=data
)
Expand All @@ -243,9 +246,9 @@ def create_pull_request(self, data=dict) -> dict:
if 'iid' in pr_data:
pr_data['number'] = pr_data['iid']

return pr_data
return dict(pr_data)

def request_reviewers(self, reviewer: str, pr_number: int):
def request_reviewers(self, reviewer: str, pr_number: int) -> None:
response = requests.get(
f'https://gitlab.com/api/v4/users?username={reviewer}',
headers=self.headers,
Expand All @@ -264,7 +267,7 @@ def request_reviewers(self, reviewer: str, pr_number: int):
f'Warning: Failed to request review from {reviewer}: {review_response.text}'
)

def send_comment_msg(self, issue_number: int, msg: str):
def send_comment_msg(self, issue_number: int, msg: str) -> None:
"""Send a comment message to a GitHub issue or pull request.

Args:
Expand Down Expand Up @@ -292,8 +295,8 @@ def get_context_from_external_issues_references(
review_comments: list[str] | None,
review_threads: list[ReviewThread],
thread_comments: list[str] | None,
):
pass
) -> list[str]:
return []


class GitlabPRHandler(GitlabIssueHandler):
Expand Down Expand Up @@ -479,7 +482,7 @@ def get_context_from_external_issues_references(
review_comments: list[str] | None,
review_threads: list[ReviewThread],
thread_comments: list[str] | None,
):
) -> list[str]:
new_issue_references = []

if issue_body:
Expand Down
38 changes: 20 additions & 18 deletions openhands/resolver/interfaces/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Issue(BaseModel):

class IssueHandlerInterface(ABC):
@abstractmethod
def set_owner(self, owner: str):
def set_owner(self, owner: str) -> None:
pass

@abstractmethod
Expand All @@ -40,67 +40,69 @@ def get_issue_comments(
pass

@abstractmethod
def get_base_url(self):
def get_base_url(self) -> str:
pass

@abstractmethod
def get_branch_url(self, branch_name):
def get_branch_url(self, branch_name: str) -> str:
pass

@abstractmethod
def get_download_url(self):
def get_download_url(self) -> str:
pass

@abstractmethod
def get_clone_url(self):
def get_clone_url(self) -> str:
pass

@abstractmethod
def get_pull_url(self, pr_number: int):
def get_pull_url(self, pr_number: int) -> str:
pass

@abstractmethod
def get_graphql_url(self):
def get_graphql_url(self) -> str:
pass

@abstractmethod
def get_headers(self):
def get_headers(self) -> dict[str, str]:
pass

@abstractmethod
def get_compare_url(self, branch_name):
def get_compare_url(self, branch_name: str) -> str:
pass

@abstractmethod
def get_branch_name(self, base_branch_name: str):
def get_branch_name(self, base_branch_name: str) -> str:
pass

@abstractmethod
def get_default_branch_name(self):
def get_default_branch_name(self) -> str:
pass

@abstractmethod
def branch_exists(self, branch_name: str) -> bool:
pass

@abstractmethod
def reply_to_comment(self, pr_number: int, comment_id: str, reply: str):
def reply_to_comment(self, pr_number: int, comment_id: str, reply: str) -> None:
pass

@abstractmethod
def send_comment_msg(self, issue_number: int, msg: str):
def send_comment_msg(self, issue_number: int, msg: str) -> None:
pass

@abstractmethod
def get_authorize_url(self):
def get_authorize_url(self) -> str:
pass

@abstractmethod
def create_pull_request(self, data=dict) -> dict:
pass
def create_pull_request(self, data: dict[str, Any] | None = None) -> dict[str, Any]:
if data is None:
data = {}
raise NotImplementedError

@abstractmethod
def request_reviewers(self, reviewer: str, pr_number: int):
def request_reviewers(self, reviewer: str, pr_number: int) -> None:
pass

@abstractmethod
Expand All @@ -112,7 +114,7 @@ def get_context_from_external_issues_references(
review_comments: list[str] | None,
review_threads: list[ReviewThread],
thread_comments: list[str] | None,
):
) -> list[str]:
pass

@abstractmethod
Expand Down
Loading
Loading