Skip to content

Commit d750154

Browse files
committed
ref: CommitContextClient methods accept repository instance
1 parent d286e63 commit d750154

File tree

5 files changed

+23
-26
lines changed

5 files changed

+23
-26
lines changed

src/sentry/integrations/github/client.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def get_commit(self, repo: str, sha: str) -> Any:
227227
"""
228228
return self.get_cached(f"/repos/{repo}/commits/{sha}")
229229

230-
def get_merge_commit_sha_from_commit(self, repo: str, sha: str) -> str | None:
230+
def get_merge_commit_sha_from_commit(self, repo: Repository, sha: str) -> str | None:
231231
"""
232232
Get the merge commit sha from a commit sha.
233233
"""
@@ -246,21 +246,21 @@ def get_merge_commit_sha_from_commit(self, repo: str, sha: str) -> str | None:
246246

247247
return pull_request.get("merge_commit_sha")
248248

249-
def get_pullrequest_from_commit(self, repo: str, sha: str) -> Any:
249+
def get_pullrequest_from_commit(self, repo: Repository, sha: str) -> Any:
250250
"""
251251
https://docs.github.com/en/rest/commits/commits#list-pull-requests-associated-with-a-commit
252252
253253
Returns the merged pull request that introduced the commit to the repository. If the commit is not present in the default branch, will only return open pull requests associated with the commit.
254254
"""
255-
return self.get(f"/repos/{repo}/commits/{sha}/pulls")
255+
return self.get(f"/repos/{repo.name}/commits/{sha}/pulls")
256256

257-
def get_pullrequest_files(self, repo: str, pull_number: str) -> Any:
257+
def get_pullrequest_files(self, repo: Repository, pull_number: str) -> Any:
258258
"""
259259
https://docs.github.com/en/rest/pulls/pulls#list-pull-requests-files
260260
261261
Returns up to 30 files associated with a pull request. Responses are paginated.
262262
"""
263-
return self.get(f"/repos/{repo}/pulls/{pull_number}/files")
263+
return self.get(f"/repos/{repo.name}/pulls/{pull_number}/files")
264264

265265
def get_repo(self, repo: str) -> Any:
266266
"""
@@ -419,17 +419,17 @@ def create_issue(self, repo: str, data: Mapping[str, Any]) -> Any:
419419
endpoint = f"/repos/{repo}/issues"
420420
return self.post(endpoint, data=data)
421421

422-
def create_comment(self, repo: str, issue_id: str, data: Mapping[str, Any]) -> Any:
422+
def create_comment(self, repo: Repository, issue_id: str, data: Mapping[str, Any]) -> Any:
423423
"""
424424
https://docs.github.com/en/rest/issues/comments#create-an-issue-comment
425425
"""
426-
endpoint = f"/repos/{repo}/issues/{issue_id}/comments"
426+
endpoint = f"/repos/{repo.name}/issues/{issue_id}/comments"
427427
return self.post(endpoint, data=data)
428428

429429
def update_comment(
430-
self, repo: str, issue_id: str, comment_id: str, data: Mapping[str, Any]
430+
self, repo: Repository, issue_id: str, comment_id: str, data: Mapping[str, Any]
431431
) -> Any:
432-
endpoint = f"/repos/{repo}/issues/comments/{comment_id}"
432+
endpoint = f"/repos/{repo.name}/issues/comments/{comment_id}"
433433
return self.patch(endpoint, data=data)
434434

435435
def get_comment_reactions(self, repo: str, comment_id: str) -> Any:

src/sentry/integrations/github/tasks/open_pr_comment.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,7 @@ def safe_for_comment(
168168
) -> list[dict[str, str]]:
169169
logger.info("github.open_pr_comment.check_safe_for_comment")
170170
try:
171-
pr_files = gh_client.get_pullrequest_files(
172-
repo=repository.name, pull_number=pull_request.key
173-
)
171+
pr_files = gh_client.get_pullrequest_files(repo=repository, pull_number=pull_request.key)
174172
except ApiError as e:
175173
logger.info("github.open_pr_comment.api_error")
176174
if e.json and RATE_LIMITED_MESSAGE in e.json.get("message", ""):

src/sentry/integrations/gitlab/client.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,16 @@ def create_issue(self, project, data):
226226
"""
227227
return self.post(GitLabApiClientPath.issues.format(project=project), data=data)
228228

229-
def create_comment(self, repo: str, issue_id: str, data: dict[str, Any]):
229+
def create_comment(self, repo: Repository, issue_id: str, data: dict[str, Any]):
230230
"""Create an issue note/comment
231231
232232
See https://docs.gitlab.com/ee/api/notes.html#create-new-issue-note
233233
"""
234234
return self.post(
235-
GitLabApiClientPath.create_note.format(project=repo, issue_id=issue_id), data=data
235+
GitLabApiClientPath.create_note.format(
236+
project=repo.config["project_id"], issue_id=issue_id
237+
),
238+
data=data,
236239
)
237240

238241
def update_comment(self, repo: str, issue_id: str, comment_id: str, data: dict[str, Any]):
@@ -309,7 +312,7 @@ def get_commit(self, project_id, sha):
309312
"""
310313
return self.get_cached(GitLabApiClientPath.commit.format(project=project_id, sha=sha))
311314

312-
def get_merge_commit_sha_from_commit(self, repo: str, sha: str) -> str | None:
315+
def get_merge_commit_sha_from_commit(self, repo: Repository, sha: str) -> str | None:
313316
raise IntegrationFeatureNotImplementedError
314317

315318
def compare_commits(self, project_id, start_sha, end_sha):

src/sentry/integrations/source_code_management/commit_context.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def queue_comment_task_if_needed(
190190
try:
191191
client = self.get_client()
192192
merge_commit_sha = client.get_merge_commit_sha_from_commit(
193-
repo=repo.name, sha=commit.key
193+
repo=repo, sha=commit.key
194194
)
195195
except Exception as e:
196196
sentry_sdk.capture_exception(e)
@@ -303,7 +303,7 @@ def create_or_update_comment(
303303
).capture():
304304
if pr_comment is None:
305305
resp = client.create_comment(
306-
repo=repo.name,
306+
repo=repo,
307307
issue_id=str(pr_key),
308308
data=(
309309
{
@@ -338,7 +338,7 @@ def create_or_update_comment(
338338
)
339339
else:
340340
resp = client.update_comment(
341-
repo=repo.name,
341+
repo=repo,
342342
issue_id=str(pr_key),
343343
comment_id=pr_comment.external_id,
344344
data=(

tests/sentry/integrations/github/test_client.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ def test_update_comment(self, get_jwt):
306306
"author_association": "COLLABORATOR",
307307
},
308308
)
309-
self.github_client.create_comment(repo=self.repo.name, issue_id="1", data={"body": "hello"})
309+
self.github_client.create_comment(repo=self.repo, issue_id="1", data={"body": "hello"})
310310

311311
responses.add(
312312
method=responses.PATCH,
@@ -325,7 +325,7 @@ def test_update_comment(self, get_jwt):
325325
)
326326

327327
self.github_client.update_comment(
328-
repo=self.repo.name, issue_id="1", comment_id="1", data={"body": "world"}
328+
repo=self.repo, issue_id="1", comment_id="1", data={"body": "world"}
329329
)
330330
assert responses.calls[1].response.status_code == 200
331331
assert responses.calls[1].request.body == b'{"body": "world"}'
@@ -364,9 +364,7 @@ def test_get_merge_commit_sha_from_commit(self, get_jwt):
364364
json=pull_requests,
365365
)
366366

367-
sha = self.github_client.get_merge_commit_sha_from_commit(
368-
repo=self.repo.name, sha=commit_sha
369-
)
367+
sha = self.github_client.get_merge_commit_sha_from_commit(repo=self.repo, sha=commit_sha)
370368
assert sha == merge_commit_sha
371369

372370
@mock.patch("sentry.integrations.github.client.get_jwt", return_value="jwt_token_1")
@@ -381,9 +379,7 @@ def test_get_merge_commit_sha_from_commit_open_pr(self, get_jwt):
381379
json=pull_requests,
382380
)
383381

384-
sha = self.github_client.get_merge_commit_sha_from_commit(
385-
repo=self.repo.name, sha=commit_sha
386-
)
382+
sha = self.github_client.get_merge_commit_sha_from_commit(repo=self.repo, sha=commit_sha)
387383
assert sha is None
388384

389385
@responses.activate

0 commit comments

Comments
 (0)