|
1 | 1 | from datetime import datetime, timedelta
|
2 | 2 | from unittest.mock import patch
|
| 3 | +from urllib.parse import urlencode |
3 | 4 |
|
4 | 5 | import responses
|
5 | 6 | from django.urls import reverse
|
6 | 7 |
|
| 8 | +from sentry.integrations.github.integration import build_repository_query |
7 | 9 | from sentry.integrations.models.organization_integration import OrganizationIntegration
|
8 | 10 | from sentry.integrations.source_code_management.metrics import SourceCodeSearchEndpointHaltReason
|
9 | 11 | from sentry.integrations.types import EventLifecycleOutcome
|
@@ -38,6 +40,17 @@ def _create_integration(self):
|
38 | 40 | },
|
39 | 41 | )
|
40 | 42 |
|
| 43 | + def _build_repo_query_path(self, *, query: str) -> str: |
| 44 | + return f"{self.base_url}/search/repositories?" + urlencode( |
| 45 | + { |
| 46 | + "q": build_repository_query( |
| 47 | + metadata=self.integration.metadata, |
| 48 | + name=self.integration.name, |
| 49 | + query=query, |
| 50 | + ).decode("utf-8") |
| 51 | + } |
| 52 | + ) |
| 53 | + |
41 | 54 | def setUp(self):
|
42 | 55 | super().setUp()
|
43 | 56 | self.integration = self._create_integration()
|
@@ -112,7 +125,7 @@ def test_finds_external_issue_results_with_id(self):
|
112 | 125 | def test_finds_repo_results(self, mock_record):
|
113 | 126 | responses.add(
|
114 | 127 | responses.GET,
|
115 |
| - self.base_url + "/search/repositories?q=org:test%20ex", |
| 128 | + self._build_repo_query_path(query="ex"), |
116 | 129 | json={
|
117 | 130 | "items": [
|
118 | 131 | {"name": "example", "full_name": "test/example"},
|
@@ -144,7 +157,7 @@ def test_finds_repo_results(self, mock_record):
|
144 | 157 | def test_repo_search_validation_error(self, mock_record):
|
145 | 158 | responses.add(
|
146 | 159 | responses.GET,
|
147 |
| - self.base_url + "/search/repositories?q=org:test%20nope", |
| 160 | + self._build_repo_query_path(query="nope"), |
148 | 161 | json={
|
149 | 162 | "message": "Validation Error",
|
150 | 163 | "errors": [{"message": "Cannot search for that org"}],
|
@@ -185,9 +198,7 @@ def test_finds_no_external_issues_results(self):
|
185 | 198 |
|
186 | 199 | @responses.activate
|
187 | 200 | def test_finds_no_project_results(self):
|
188 |
| - responses.add( |
189 |
| - responses.GET, self.base_url + "/search/repositories?q=org:test%20nope", json={} |
190 |
| - ) |
| 201 | + responses.add(responses.GET, self._build_repo_query_path(query="nope"), json={}) |
191 | 202 | resp = self.client.get(self.url, data={"field": "repo", "query": "nope"})
|
192 | 203 |
|
193 | 204 | assert resp.status_code == 200
|
@@ -227,7 +238,7 @@ def test_search_issues_rate_limit(self, mock_record):
|
227 | 238 | def test_search_project_rate_limit(self, mock_record):
|
228 | 239 | responses.add(
|
229 | 240 | responses.GET,
|
230 |
| - self.base_url + "/search/repositories?q=org:test%20ex", |
| 241 | + self._build_repo_query_path(query="ex"), |
231 | 242 | status=403,
|
232 | 243 | json={
|
233 | 244 | "message": "API rate limit exceeded",
|
|
0 commit comments