report: Use ZULIP_VERSION rather than shelling out to git every time.

This commit is contained in:
Alex Vandiver 2023-03-03 21:36:06 +00:00 committed by Tim Abbott
parent 90b1e0b8b9
commit 73631950a5
2 changed files with 6 additions and 16 deletions

View File

@ -1,4 +1,3 @@
import subprocess
from typing import Callable, ContextManager, Dict, List, Tuple from typing import Callable, ContextManager, Dict, List, Tuple
from unittest import mock from unittest import mock
@ -151,11 +150,8 @@ class TestReport(ZulipTestCase):
) )
) )
subprocess_mock = mock.patch( version_mock = mock.patch("zerver.views.report.ZULIP_VERSION", spec="1.2.3")
"zerver.views.report.subprocess.check_output", with mock_queue_publish("zerver.views.report.queue_json_publish") as m, version_mock:
side_effect=subprocess.CalledProcessError(1, []),
)
with mock_queue_publish("zerver.views.report.queue_json_publish") as m, subprocess_mock:
result = self.client_post("/json/report/error", params) result = self.client_post("/json/report/error", params)
self.assert_json_success(result) self.assert_json_success(result)
@ -168,7 +164,7 @@ class TestReport(ZulipTestCase):
# Test with no more_info # Test with no more_info
del params["more_info"] del params["more_info"]
with mock_queue_publish("zerver.views.report.queue_json_publish") as m, subprocess_mock: with mock_queue_publish("zerver.views.report.queue_json_publish") as m, version_mock:
result = self.client_post("/json/report/error", params) result = self.client_post("/json/report/error", params)
self.assert_json_success(result) self.assert_json_success(result)
@ -181,7 +177,7 @@ class TestReport(ZulipTestCase):
# js_source_map actually gets instantiated. # js_source_map actually gets instantiated.
with self.settings(DEVELOPMENT=False, TEST_SUITE=False), mock.patch( with self.settings(DEVELOPMENT=False, TEST_SUITE=False), mock.patch(
"zerver.lib.unminify.SourceMap.annotate_stacktrace" "zerver.lib.unminify.SourceMap.annotate_stacktrace"
) as annotate, self.assertLogs(level="INFO") as info_logs: ) as annotate, self.assertLogs(level="INFO") as info_logs, version_mock:
result = self.client_post("/json/report/error", params) result = self.client_post("/json/report/error", params)
self.assert_json_success(result) self.assert_json_success(result)
# fix_params (see above) adds quotes when JSON encoding. # fix_params (see above) adds quotes when JSON encoding.

View File

@ -1,6 +1,5 @@
# System documented in https://zulip.readthedocs.io/en/latest/subsystems/logging.html # System documented in https://zulip.readthedocs.io/en/latest/subsystems/logging.html
import logging import logging
import subprocess
from typing import Any, Mapping, Optional, Union from typing import Any, Mapping, Optional, Union
from urllib.parse import SplitResult from urllib.parse import SplitResult
@ -10,6 +9,7 @@ from django.http import HttpRequest, HttpResponse
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST from django.views.decorators.http import require_POST
from version import ZULIP_VERSION
from zerver.context_processors import get_valid_realm_from_request from zerver.context_processors import get_valid_realm_from_request
from zerver.decorator import human_users_only from zerver.decorator import human_users_only
from zerver.lib.markdown import privacy_clean_markdown from zerver.lib.markdown import privacy_clean_markdown
@ -139,13 +139,7 @@ def report_error(
if js_source_map: if js_source_map:
stacktrace = js_source_map.annotate_stacktrace(stacktrace) stacktrace = js_source_map.annotate_stacktrace(stacktrace)
try: version = str(ZULIP_VERSION)
version: Optional[str] = subprocess.check_output(
["git", "show", "-s", "--oneline"],
text=True,
)
except (FileNotFoundError, subprocess.CalledProcessError):
version = None
# Get the IP address of the request # Get the IP address of the request
remote_ip = request.META["REMOTE_ADDR"] remote_ip = request.META["REMOTE_ADDR"]