Skip to content

Commit

Permalink
Merge pull request #1081 from MarkRx/feature/bitbucket-server-fix-pos…
Browse files Browse the repository at this point in the history
…t-inline-comments

Bitbucket Server fix inline comments and reading .pr_agent.toml
  • Loading branch information
mrT23 committed Aug 1, 2024
2 parents d671c78 + 288e9bb commit 9560bc1
Showing 1 changed file with 14 additions and 24 deletions.
38 changes: 14 additions & 24 deletions pr_agent/git_providers/bitbucket_server_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Optional, Tuple
from urllib.parse import quote_plus, urlparse

import requests
from requests.exceptions import HTTPError
from atlassian.bitbucket import Bitbucket
from starlette_context import context

Expand All @@ -18,17 +18,6 @@ class BitbucketServerProvider(GitProvider):
def __init__(
self, pr_url: Optional[str] = None, incremental: Optional[bool] = False
):
s = requests.Session()
try:
bearer = context.get("bitbucket_bearer_token", None)
s.headers["Authorization"] = f"Bearer {bearer}"
except Exception:
s.headers[
"Authorization"
] = f'Bearer {get_settings().get("BITBUCKET_SERVER.BEARER_TOKEN", None)}'

s.headers["Content-Type"] = "application/json"
self.headers = s.headers
self.bitbucket_server_url = None
self.workspace_slug = None
self.repo_slug = None
Expand All @@ -50,14 +39,15 @@ def __init__(

def get_repo_settings(self):
try:
url = (f"{self.bitbucket_server_url}/projects/{self.workspace_slug}/repos/{self.repo_slug}/src/"
f"{self.pr.destination_branch}/.pr_agent.toml")
response = requests.request("GET", url, headers=self.headers)
if response.status_code == 404: # not found
return ""
contents = response.text.encode('utf-8')
return contents
except Exception:
content = self.bitbucket_client.get_content_of_file(self.workspace_slug, self.repo_slug, ".pr_agent.toml", self.get_pr_branch())

return content
except Exception as e:
if isinstance(e, HTTPError):
if e.response.status_code == 404: # not found
return ""

get_logger().error(f"Failed to load .pr_agent.toml file, error: {e}")
return ""

def get_pr_id(self):
Expand Down Expand Up @@ -247,11 +237,11 @@ def publish_inline_comment(self, comment: str, from_line: int, file: str):
}

try:
requests.post(url=self._get_pr_comments_url(), json=payload, headers=self.headers).raise_for_status()
self.bitbucket_client.post(self._get_pr_comments_path(), data=payload)
except Exception as e:
get_logger().error(f"Failed to publish inline comment to '{file}' at line {from_line}, error: {e}")
raise e

def get_line_link(self, relevant_file: str, relevant_line_start: int, relevant_line_end: int = None) -> str:
if relevant_line_start == -1:
link = f"{self.pr_url}/diff#{quote_plus(relevant_file)}"
Expand Down Expand Up @@ -414,5 +404,5 @@ def publish_labels(self, pr_types: list):
def get_pr_labels(self, update=False):
pass

def _get_pr_comments_url(self):
return f"{self.bitbucket_server_url}/rest/api/latest/projects/{self.workspace_slug}/repos/{self.repo_slug}/pull-requests/{self.pr_num}/comments"
def _get_pr_comments_path(self):
return f"rest/api/latest/projects/{self.workspace_slug}/repos/{self.repo_slug}/pull-requests/{self.pr_num}/comments"

0 comments on commit 9560bc1

Please sign in to comment.