Skip to content

Commit 3f9dc48

Browse files
committed
ref: BitbucketServerAPIPath
1 parent 013eb49 commit 3f9dc48

File tree

3 files changed

+47
-26
lines changed

3 files changed

+47
-26
lines changed

src/sentry/integrations/bitbucket_server/client.py

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from requests_oauthlib import OAuth1
77

88
from sentry.identity.services.identity.model import RpcIdentity
9+
from sentry.integrations.bitbucket_server.utils import BitbucketServerAPIPath
910
from sentry.integrations.client import ApiClient
1011
from sentry.integrations.models.integration import Integration
1112
from sentry.integrations.services.integration.model import RpcIntegration
@@ -16,23 +17,6 @@
1617
logger = logging.getLogger("sentry.integrations.bitbucket_server")
1718

1819

19-
class BitbucketServerAPIPath:
20-
"""
21-
project is the short key of the project
22-
repo is the fully qualified slug
23-
"""
24-
25-
repository = "/rest/api/1.0/projects/{project}/repos/{repo}"
26-
repositories = "/rest/api/1.0/repos"
27-
repository_hook = "/rest/api/1.0/projects/{project}/repos/{repo}/webhooks/{id}"
28-
repository_hooks = "/rest/api/1.0/projects/{project}/repos/{repo}/webhooks"
29-
repository_commits = "/rest/api/1.0/projects/{project}/repos/{repo}/commits"
30-
commit_changes = "/rest/api/1.0/projects/{project}/repos/{repo}/commits/{commit}/changes"
31-
32-
raw = "/projects/{project}/repos/{repo}/raw/{path}?at={sha}"
33-
source = "/rest/api/1.0/projects/{project}/repos/{repo}/browse/{path}?at={sha}"
34-
35-
3620
class BitbucketServerSetupClient(ApiClient):
3721
"""
3822
Client for making requests to Bitbucket Server to follow OAuth1 flow.
@@ -259,7 +243,7 @@ def _get_values(self, uri, params, max_pages=1000000):
259243

260244
def check_file(self, repo: Repository, path: str, version: str | None) -> object | None:
261245
return self.head_cached(
262-
path=BitbucketServerAPIPath.source.format(
246+
path=BitbucketServerAPIPath.build_source(
263247
project=repo.config["project"],
264248
repo=repo.config["repo"],
265249
path=path,
@@ -271,7 +255,7 @@ def get_file(
271255
self, repo: Repository, path: str, ref: str | None, codeowners: bool = False
272256
) -> str:
273257
response = self.get_cached(
274-
path=BitbucketServerAPIPath.raw.format(
258+
path=BitbucketServerAPIPath.build_raw(
275259
project=repo.config["project"],
276260
repo=repo.config["repo"],
277261
path=path,
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from urllib.parse import quote, urlencode
2+
3+
4+
class BitbucketServerAPIPath:
5+
"""
6+
project is the short key of the project
7+
repo is the fully qualified slug
8+
"""
9+
10+
repository = "/rest/api/1.0/projects/{project}/repos/{repo}"
11+
repositories = "/rest/api/1.0/repos"
12+
repository_hook = "/rest/api/1.0/projects/{project}/repos/{repo}/webhooks/{id}"
13+
repository_hooks = "/rest/api/1.0/projects/{project}/repos/{repo}/webhooks"
14+
repository_commits = "/rest/api/1.0/projects/{project}/repos/{repo}/commits"
15+
commit_changes = "/rest/api/1.0/projects/{project}/repos/{repo}/commits/{commit}/changes"
16+
17+
@staticmethod
18+
def build_raw(project: str, repo: str, path: str, sha: str) -> str:
19+
project = quote(project)
20+
repo = quote(repo)
21+
22+
params = {}
23+
if sha:
24+
params["at"] = sha
25+
26+
return f"/projects/{project}/repos/{repo}/raw/{path}?{urlencode(params)}"
27+
28+
@staticmethod
29+
def build_source(project: str, repo: str, path: str, sha: str) -> str:
30+
project = quote(project)
31+
repo = quote(repo)
32+
33+
params = {}
34+
if sha:
35+
params["at"] = sha
36+
37+
return f"/rest/api/1.0/projects/{project}/repos/{repo}/browse/{path}?{urlencode(params)}"

tests/sentry/integrations/bitbucket_server/test_client.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from requests import Request
66

77
from fixtures.bitbucket_server import REPO
8-
from sentry.integrations.bitbucket_server.client import BitbucketServerAPIPath
98
from sentry.integrations.bitbucket_server.integration import BitbucketServerIntegration
9+
from sentry.integrations.bitbucket_server.utils import BitbucketServerAPIPath
1010
from sentry.models.repository import Repository
1111
from sentry.shared_integrations.exceptions import ApiError
1212
from sentry.shared_integrations.response.base import BaseApiResponse
@@ -111,7 +111,7 @@ def test_get_repo_authentication(self):
111111
def test_check_file(self):
112112
path = "src/sentry/integrations/bitbucket_server/client.py"
113113
version = "master"
114-
url = self.bb_server_client.base_url + BitbucketServerAPIPath.source.format(
114+
url = self.bb_server_client.base_url + BitbucketServerAPIPath.build_source(
115115
project=self.repo.config["project"],
116116
repo=self.repo.config["repo"],
117117
path=path,
@@ -132,7 +132,7 @@ def test_check_file(self):
132132
def test_check_no_file(self):
133133
path = "src/santry/integrations/bitbucket_server/client.py"
134134
version = "master"
135-
url = self.bb_server_client.base_url + BitbucketServerAPIPath.source.format(
135+
url = self.bb_server_client.base_url + BitbucketServerAPIPath.build_source(
136136
project=self.repo.config["project"],
137137
repo=self.repo.config["repo"],
138138
path=path,
@@ -152,7 +152,7 @@ def test_check_no_file(self):
152152
def test_get_file(self):
153153
path = "src/sentry/integrations/bitbucket_server/client.py"
154154
version = "master"
155-
url = self.bb_server_client.base_url + BitbucketServerAPIPath.raw.format(
155+
url = self.bb_server_client.base_url + BitbucketServerAPIPath.build_raw(
156156
project=self.repo.config["project"],
157157
repo=self.repo.config["repo"],
158158
path=path,
@@ -173,7 +173,7 @@ def test_get_file(self):
173173
def test_get_stacktrace_link(self):
174174
path = "src/sentry/integrations/bitbucket/client.py"
175175
version = "master"
176-
url = self.bb_server_client.base_url + BitbucketServerAPIPath.source.format(
176+
url = self.bb_server_client.base_url + BitbucketServerAPIPath.build_source(
177177
project=self.repo.config["project"],
178178
repo=self.repo.config["repo"],
179179
path=path,
@@ -200,13 +200,13 @@ def test_get_codeowner_file(self):
200200
)
201201

202202
path = ".bitbucket/CODEOWNERS"
203-
source_url = self.bb_server_client.base_url + BitbucketServerAPIPath.source.format(
203+
source_url = self.bb_server_client.base_url + BitbucketServerAPIPath.build_source(
204204
project=self.repo.config["project"],
205205
repo=self.repo.config["repo"],
206206
path=path,
207207
sha=self.config.default_branch,
208208
)
209-
raw_url = self.bb_server_client.base_url + BitbucketServerAPIPath.raw.format(
209+
raw_url = self.bb_server_client.base_url + BitbucketServerAPIPath.build_raw(
210210
project=self.repo.config["project"],
211211
repo=self.repo.config["repo"],
212212
path=path,

0 commit comments

Comments
 (0)