Skip to content

Commit 03bde1d

Browse files
committed
Fix tests
1 parent 9ed77ba commit 03bde1d

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

src/sentry/integrations/github/client.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def _populate_trees(self, repositories: List[Dict[str, str]]) -> Dict[str, RepoT
190190
This function takes API rate limits into consideration to prevent exhaustion.
191191
"""
192192

193-
def process_error(error: Exception, extra: Dict[str, str]):
193+
def process_error(error: ApiError, extra: Dict[str, str]) -> None:
194194
msg = "Continuing exectuion."
195195
txt = error.text
196196
if error.json:
@@ -234,8 +234,11 @@ def process_error(error: Exception, extra: Dict[str, str]):
234234
trees[repo_full_name] = self._populate_tree(
235235
repo_info, only_use_cache, (3600 * 24) + (3600 * (index % 24))
236236
)
237-
except Exception as error:
237+
except ApiError as error:
238238
process_error(error, extra)
239+
except Exception as error:
240+
# Report for investigatagiation but do not stop processing
241+
logger.exception(error, extra=extra)
239242

240243
remaining_requests -= 1
241244

src/sentry/tasks/derive_code_mappings.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
from sentry.models.organization import Organization
1616
from sentry.models.repository import Repository
1717
from sentry.services.hybrid_cloud.integration import APIOrganizationIntegration, integration_service
18+
from sentry.shared_integrations.exceptions.base import ApiError
1819
from sentry.tasks.base import instrumented_task
20+
from sentry.utils.json import JSONData
1921
from sentry.utils.locking import UnableToAcquireLock
2022
from sentry.utils.safe import get_path
2123

@@ -77,17 +79,23 @@ def derive_code_mappings(
7779
with lock.acquire():
7880
# This method is specific to the GithubIntegration
7981
trees = installation.get_trees_for_org() # type: ignore
80-
82+
except ApiError as error:
83+
msg = error.text
84+
if error.json:
85+
json_data: JSONData = error.json
86+
msg = json_data.get("message")
87+
extra["error"] = msg
88+
89+
if msg == "Not Found":
90+
logger.warning("The org has uninstalled the Sentry App.", extra=extra)
91+
return
92+
93+
raise error # Let's report the issue
8194
except UnableToAcquireLock as error:
8295
extra["error"] = error
8396
logger.warning("derive_code_mappings.getting_lock_failed", extra=extra)
8497
# This will cause the auto-retry logic to try again
8598
raise error
86-
except Exception:
87-
# Investigate in order to determine if we should retry the task
88-
# or catch it within get_trees_for_org
89-
logger.exception("Failed to get trees for org. Investigate.", extra=extra)
90-
return
9199

92100
if not trees:
93101
logger.error("The tree is empty. Investigate.")

tests/sentry/integrations/github/test_integration.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -611,11 +611,8 @@ def test_get_trees_for_org_works(self):
611611
installation = self.get_installation_helper()
612612
self.set_rate_limit(MINIMUM_REQUESTS + 50)
613613
expected_trees = {
614-
"Test-Organization/bar": RepoTree(Repo("Test-Organization/bar", "main"), []),
615-
"Test-Organization/baz": RepoTree(Repo("Test-Organization/baz", "master"), []),
616614
"Test-Organization/foo": RepoTree(
617-
Repo("Test-Organization/foo", "master"),
618-
["src/sentry/api/endpoints/auth_login.py"],
615+
Repo("Test-Organization/foo", "master"), ["src/sentry/api/endpoints/auth_login.py"]
619616
),
620617
"Test-Organization/xyz": RepoTree(
621618
Repo("Test-Organization/xyz", "master"), ["src/foo.py"]
@@ -647,6 +644,7 @@ def test_get_trees_for_org_prevent_exhaustion_some_repos(self):
647644
"""Some repos will hit the network but the rest will grab from the cache."""
648645
gh_org = "Test-Organization"
649646
installation = self.get_installation_helper()
647+
650648
expected_trees = {
651649
f"{gh_org}/xyz": RepoTree(Repo(f"{gh_org}/xyz", "master"), ["src/foo.py"]),
652650
# This will have no files because we will hit the minimum remaining requests floor
@@ -668,8 +666,10 @@ def test_get_trees_for_org_prevent_exhaustion_some_repos(self):
668666
# We reset the remaining values
669667
self.set_rate_limit(remaining=20)
670668
trees = installation.get_trees_for_org()
671-
# Now that the rate limit is reset we should get files for foo
672-
expected_trees[f"{gh_org}/foo"] = RepoTree(
673-
Repo(f"{gh_org}/foo", "master"), ["src/sentry/api/endpoints/auth_login.py"]
674-
)
675-
assert trees == expected_trees
669+
assert trees == {
670+
f"{gh_org}/xyz": RepoTree(Repo(f"{gh_org}/xyz", "master"), ["src/foo.py"]),
671+
# Now that the rate limit is reset we should get files for foo
672+
f"{gh_org}/foo": RepoTree(
673+
Repo(f"{gh_org}/foo", "master"), ["src/sentry/api/endpoints/auth_login.py"]
674+
),
675+
}

0 commit comments

Comments
 (0)