Skip to content

Commit 9e66c36

Browse files
authored
fix(codecov): Fix git blame for multi repo projects (#44827)
In some projects where there are more than one repository associated to a project, we get a commit that comes from the release. For instance, in the case of the sentry project, we get the commit from the getsentry repo, thus, we can't use that commit with the sentry repo. Fixes: * Even if a commit is set, run the git blame experiment * As mentioned above, the commit may not even be for the right repo * Report an error if we fail to git blame
1 parent c24c55d commit 9e66c36

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

src/sentry/api/endpoints/project_stacktrace_link.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ def get_latest_commit_sha_from_blame(
206206
git_blame_list.sort(key=lambda blame: blame["commit"]["committedDate"])
207207
commit_sha = git_blame_list[-1]["commit"]["oid"]
208208
if not commit_sha:
209-
logger.warning(
209+
# Report to Sentry so we can investigate
210+
logger.error(
210211
"Failed to get commit from git blame.",
211212
extra={
212213
"git_blame_response": git_blame_list,
@@ -219,7 +220,6 @@ def get_latest_commit_sha_from_blame(
219220

220221
def fetch_codecov_data(
221222
self,
222-
has_error_commit: bool,
223223
ref: Optional[str],
224224
integrations: BaseQuerySet,
225225
org: Organization,
@@ -238,8 +238,9 @@ def fetch_codecov_data(
238238
)
239239
# Get commit sha from Git blame if valid
240240
gh_integrations = integrations.filter(provider="github")
241-
should_get_commit_sha = fetch_commit_sha and gh_integrations and not has_error_commit
241+
should_get_commit_sha = fetch_commit_sha and gh_integrations
242242

243+
ref_source = "from_release"
243244
if should_get_commit_sha:
244245
try:
245246
integration_installation = gh_integrations[0].get_installation(
@@ -252,11 +253,15 @@ def fetch_codecov_data(
252253
repo,
253254
branch,
254255
)
256+
ref_source = "from_git_blame"
255257
except Exception:
256258
logger.exception(
257259
"Failed to get commit sha from git blame, pending investigation. Continuing execution."
258260
)
259261

262+
with configure_scope() as scope:
263+
scope.set_tag("codecov.ref_source", ref_source)
264+
260265
# Call codecov API if codecov-commit-sha-from-git-blame flag is not enabled
261266
# or getting ref from git blame was successful
262267
codecov_data = None
@@ -267,7 +272,6 @@ def fetch_codecov_data(
267272
ref=ref if ref else branch,
268273
ref_type="sha" if ref else "branch",
269274
path=path,
270-
has_error_commit=has_error_commit,
271275
)
272276
if lineCoverage and codecovUrl:
273277
codecov_data = {
@@ -374,8 +378,7 @@ def get(self, request: Request, project: Project) -> Response:
374378
try:
375379
line_no = ctx.get("line_no")
376380
codecov_data = self.fetch_codecov_data(
377-
has_error_commit := bool(ctx.get("commit_id")),
378-
ref=ctx["commit_id"] if has_error_commit else None,
381+
ref=ctx.get("commit_id"),
379382
integrations=integrations,
380383
org=project.organization,
381384
user=request.user,

src/sentry/integrations/utils/codecov.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
def get_codecov_data(
14-
repo: str, service: str, ref: str, ref_type: REF_TYPE, path: str, has_error_commit: bool
14+
repo: str, service: str, ref: str, ref_type: REF_TYPE, path: str
1515
) -> Tuple[Optional[LineCoverage], Optional[str]]:
1616
codecov_token = options.get("codecov.client-secret")
1717
line_coverage = None
@@ -34,7 +34,6 @@ def get_codecov_data(
3434
"codecov.request_path": path,
3535
"codecov.request_ref": ref,
3636
"codecov.http_code": response.status_code,
37-
"codecov.ref_source": "from_release" if has_error_commit else "from_git_blame",
3837
}
3938

4039
response_json = response.json()

tests/sentry/api/endpoints/test_project_stacktrace_link.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,10 +369,10 @@ def test_get_latest_commit_from_blame(self, get_jwt, mock_blame):
369369
@mock.patch("sentry.integrations.mixins.repositories.RepositoryMixin.get_stacktrace_link")
370370
@mock.patch("sentry.integrations.github.client.GitHubClientMixin.get_blame_for_file")
371371
@mock.patch("sentry.integrations.github.client.get_jwt", return_value=b"jwt_token_1")
372-
def test_get_commit_sha_from_blame_logger_warning(
372+
def test_get_commit_sha_from_blame_failed_to_get_commit(
373373
self, get_jwt, mock_blame, mock_get_stacktrace_link
374374
):
375-
self._caplog.set_level(logging.WARNING, logger="sentry")
375+
self._caplog.set_level(logging.ERROR, logger="sentry")
376376
self.organization.flags.codecov_access = True
377377
self.organization.save()
378378
self.integration.provider = "github"
@@ -389,10 +389,11 @@ def test_get_commit_sha_from_blame_logger_warning(
389389
qs_params={"file": self.filepath, "lineNo": 26},
390390
)
391391

392+
# Error logging means an error is sent to Sentry
392393
assert self._caplog.record_tuples == [
393394
(
394395
"sentry.api.endpoints.project_stacktrace_link",
395-
logging.WARNING,
396+
logging.ERROR,
396397
"Failed to get commit from git blame.",
397398
)
398399
]

0 commit comments

Comments
 (0)